blob: 3c834f673eac90570c6be71095768ca8f90fdebf [file] [log] [blame]
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +09001// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_FILES_FILE_PROXY_H_
6#define BASE_FILES_FILE_PROXY_H_
7
8#include "base/base_export.h"
9#include "base/callback_forward.h"
10#include "base/files/file.h"
11#include "base/files/file_path.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/weak_ptr.h"
14
15namespace tracked_objects {
16class Location;
17};
18
19namespace base {
20
21class TaskRunner;
22class Time;
23
24// This class provides asynchronous access to a File. All methods follow the
25// same rules of the equivalent File method, as they are implemented by bouncing
26// the operation to File using a TaskRunner.
27//
rvargas@chromium.orge60d9672014-05-06 09:55:35 +090028// This class performs automatic proxying to close the underlying file at
29// destruction.
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090030//
31// The TaskRunner is in charge of any sequencing of the operations, but a single
32// operation can be proxied at a time, regardless of the use of a callback.
33// In other words, having a sequence like
34//
35// proxy.Write(...);
36// delete proxy;
37//
38// will keep the file valid during the Write operation but will cause the file
39// to be closed in the current thread, when the operation finishes. If Close is
40// called right away after Write, the second call will fail because there is an
41// operation in progress.
42class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
43 public:
44 // This callback is used by methods that report only an error code. It is
45 // valid to pass a null callback to some functions that takes a
46 // StatusCallback, in which case the operation will complete silently.
47 typedef Callback<void(File::Error)> StatusCallback;
48
49 typedef Callback<void(File::Error,
50 const FilePath&)> CreateTemporaryCallback;
51 typedef Callback<void(File::Error,
52 const File::Info&)> GetFileInfoCallback;
53 typedef Callback<void(File::Error,
54 const char* data,
55 int bytes_read)> ReadCallback;
56 typedef Callback<void(File::Error,
57 int bytes_written)> WriteCallback;
58
59 FileProxy();
60 explicit FileProxy(TaskRunner* task_runner);
61 ~FileProxy();
62
63 // Creates or opens a file with the given flags. It is invalid to pass a null
64 // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to
65 // create a new file at the given |file_path| and fails if the file already
66 // exists.
67 //
68 // This returns false if task posting to |task_runner| has failed.
69 bool CreateOrOpen(const FilePath& file_path,
70 uint32 file_flags,
71 const StatusCallback& callback);
72
73 // Creates a temporary file for writing. The path and an open file are
74 // returned. It is invalid to pass a null callback. The additional file flags
75 // will be added on top of the default file flags which are:
76 // File::FLAG_CREATE_ALWAYS
77 // File::FLAG_WRITE
78 // File::FLAG_TEMPORARY.
79 //
80 // This returns false if task posting to |task_runner| has failed.
81 bool CreateTemporary(uint32 additional_file_flags,
82 const CreateTemporaryCallback& callback);
83
84 // Returns true if the underlying |file_| is valid.
85 bool IsValid() const;
86
87 // Returns true if a new file was created (or an old one truncated to zero
88 // length to simulate a new file), and false otherwise.
89 bool created() const { return file_.created(); }
90
91 File TakeFile();
92
93 // Proxies File::Close. The callback can be null.
94 // This returns false if task posting to |task_runner| has failed.
95 bool Close(const StatusCallback& callback);
96
97 // Proxies File::GetInfo. The callback can't be null.
98 // This returns false if task posting to |task_runner| has failed.
99 bool GetInfo(const GetFileInfoCallback& callback);
100
101 // Proxies File::Read. The callback can't be null.
102 // This returns false if |bytes_to_read| is less than zero, or
103 // if task posting to |task_runner| has failed.
104 bool Read(int64 offset, int bytes_to_read, const ReadCallback& callback);
105
106 // Proxies File::Write. The callback can be null.
107 // This returns false if |bytes_to_write| is less than or equal to zero,
108 // if |buffer| is NULL, or if task posting to |task_runner| has failed.
109 bool Write(int64 offset,
110 const char* buffer,
111 int bytes_to_write,
112 const WriteCallback& callback);
113
114 // Proxies File::SetTimes. The callback can be null.
115 // This returns false if task posting to |task_runner| has failed.
116 bool SetTimes(Time last_access_time,
117 Time last_modified_time,
118 const StatusCallback& callback);
119
120 // Proxies File::SetLength. The callback can be null.
121 // This returns false if task posting to |task_runner| has failed.
122 bool SetLength(int64 length, const StatusCallback& callback);
123
124 // Proxies File::Flush. The callback can be null.
125 // This returns false if task posting to |task_runner| has failed.
126 bool Flush(const StatusCallback& callback);
127
128 private:
129 friend class FileHelper;
130 void SetFile(File file);
rvargas@chromium.orge60d9672014-05-06 09:55:35 +0900131 TaskRunner* task_runner() { return task_runner_.get(); }
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900132
133 scoped_refptr<TaskRunner> task_runner_;
134 File file_;
135 DISALLOW_COPY_AND_ASSIGN(FileProxy);
136};
137
138} // namespace base
139
140#endif // BASE_FILES_FILE_PROXY_H_