Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: base/files/file_util_proxy.cc

Issue 145303002: Convert Media Galleries to use base::File (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/files/file_util_proxy.h ('k') | base/files/file_util_proxy_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util_proxy.h" 5 #include "base/files/file_util_proxy.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/task_runner.h" 13 #include "base/task_runner.h"
14 #include "base/task_runner_util.h" 14 #include "base/task_runner_util.h"
15 15
16 namespace base { 16 namespace base {
17 17
18 namespace { 18 namespace {
19 19
20 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, 20 void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback,
21 bool value) { 21 bool value) {
22 DCHECK(!callback.is_null()); 22 DCHECK(!callback.is_null());
23 callback.Run(value ? PLATFORM_FILE_OK : PLATFORM_FILE_ERROR_FAILED); 23 callback.Run(value ? File::FILE_OK : File::FILE_ERROR_FAILED);
24 } 24 }
25 25
26 // Helper classes or routines for individual methods. 26 // Helper classes or routines for individual methods.
27 class CreateOrOpenHelper { 27 class CreateOrOpenHelper {
28 public: 28 public:
29 CreateOrOpenHelper(TaskRunner* task_runner, 29 CreateOrOpenHelper(TaskRunner* task_runner,
30 const FileUtilProxy::CloseTask& close_task) 30 const FileUtilProxy::CloseTask& close_task)
31 : task_runner_(task_runner), 31 : task_runner_(task_runner),
32 close_task_(close_task), 32 close_task_(close_task),
33 file_handle_(kInvalidPlatformFileValue), 33 file_handle_(kInvalidPlatformFileValue),
34 created_(false), 34 created_(false),
35 error_(PLATFORM_FILE_OK) {} 35 error_(File::FILE_OK) {}
36 36
37 ~CreateOrOpenHelper() { 37 ~CreateOrOpenHelper() {
38 if (file_handle_ != kInvalidPlatformFileValue) { 38 if (file_handle_ != kInvalidPlatformFileValue) {
39 task_runner_->PostTask( 39 task_runner_->PostTask(
40 FROM_HERE, 40 FROM_HERE,
41 base::Bind(base::IgnoreResult(close_task_), file_handle_)); 41 base::Bind(base::IgnoreResult(close_task_), file_handle_));
42 } 42 }
43 } 43 }
44 44
45 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) { 45 void RunWork(const FileUtilProxy::CreateOrOpenTask& task) {
46 error_ = task.Run(&file_handle_, &created_); 46 error_ = task.Run(&file_handle_, &created_);
47 } 47 }
48 48
49 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) { 49 void Reply(const FileUtilProxy::CreateOrOpenCallback& callback) {
50 DCHECK(!callback.is_null()); 50 DCHECK(!callback.is_null());
51 callback.Run(error_, PassPlatformFile(&file_handle_), created_); 51 callback.Run(error_, PassPlatformFile(&file_handle_), created_);
52 } 52 }
53 53
54 private: 54 private:
55 scoped_refptr<TaskRunner> task_runner_; 55 scoped_refptr<TaskRunner> task_runner_;
56 FileUtilProxy::CloseTask close_task_; 56 FileUtilProxy::CloseTask close_task_;
57 PlatformFile file_handle_; 57 PlatformFile file_handle_;
58 bool created_; 58 bool created_;
59 PlatformFileError error_; 59 File::Error error_;
60 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper); 60 DISALLOW_COPY_AND_ASSIGN(CreateOrOpenHelper);
61 }; 61 };
62 62
63 class CreateTemporaryHelper { 63 class CreateTemporaryHelper {
64 public: 64 public:
65 explicit CreateTemporaryHelper(TaskRunner* task_runner) 65 explicit CreateTemporaryHelper(TaskRunner* task_runner)
66 : task_runner_(task_runner), 66 : task_runner_(task_runner),
67 file_handle_(kInvalidPlatformFileValue), 67 file_handle_(kInvalidPlatformFileValue),
68 error_(PLATFORM_FILE_OK) {} 68 error_(File::FILE_OK) {}
69 69
70 ~CreateTemporaryHelper() { 70 ~CreateTemporaryHelper() {
71 if (file_handle_ != kInvalidPlatformFileValue) { 71 if (file_handle_ != kInvalidPlatformFileValue) {
72 FileUtilProxy::Close( 72 FileUtilProxy::Close(
73 task_runner_.get(), file_handle_, FileUtilProxy::StatusCallback()); 73 task_runner_.get(), file_handle_, FileUtilProxy::StatusCallback());
74 } 74 }
75 } 75 }
76 76
77 void RunWork(int additional_file_flags) { 77 void RunWork(int additional_file_flags) {
78 // TODO(darin): file_util should have a variant of CreateTemporaryFile 78 // TODO(darin): file_util should have a variant of CreateTemporaryFile
79 // that returns a FilePath and a PlatformFile. 79 // that returns a FilePath and a PlatformFile.
80 base::CreateTemporaryFile(&file_path_); 80 base::CreateTemporaryFile(&file_path_);
81 81
82 int file_flags = 82 int file_flags =
83 PLATFORM_FILE_WRITE | 83 PLATFORM_FILE_WRITE |
84 PLATFORM_FILE_TEMPORARY | 84 PLATFORM_FILE_TEMPORARY |
85 PLATFORM_FILE_CREATE_ALWAYS | 85 PLATFORM_FILE_CREATE_ALWAYS |
86 additional_file_flags; 86 additional_file_flags;
87 87
88 error_ = PLATFORM_FILE_OK; 88 error_ = File::FILE_OK;
89 file_handle_ = CreatePlatformFile(file_path_, file_flags, NULL, &error_); 89 // TODO(rvargas): Convert this code to use File.
90 file_handle_ =
91 CreatePlatformFile(file_path_, file_flags, NULL,
92 reinterpret_cast<PlatformFileError*>(&error_));
90 } 93 }
91 94
92 void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) { 95 void Reply(const FileUtilProxy::CreateTemporaryCallback& callback) {
93 DCHECK(!callback.is_null()); 96 DCHECK(!callback.is_null());
94 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_); 97 callback.Run(error_, PassPlatformFile(&file_handle_), file_path_);
95 } 98 }
96 99
97 private: 100 private:
98 scoped_refptr<TaskRunner> task_runner_; 101 scoped_refptr<TaskRunner> task_runner_;
99 PlatformFile file_handle_; 102 PlatformFile file_handle_;
100 FilePath file_path_; 103 FilePath file_path_;
101 PlatformFileError error_; 104 File::Error error_;
102 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper); 105 DISALLOW_COPY_AND_ASSIGN(CreateTemporaryHelper);
103 }; 106 };
104 107
105 class GetFileInfoHelper { 108 class GetFileInfoHelper {
106 public: 109 public:
107 GetFileInfoHelper() 110 GetFileInfoHelper()
108 : error_(PLATFORM_FILE_OK) {} 111 : error_(File::FILE_OK) {}
109 112
110 void RunWorkForFilePath(const FilePath& file_path) { 113 void RunWorkForFilePath(const FilePath& file_path) {
111 if (!PathExists(file_path)) { 114 if (!PathExists(file_path)) {
112 error_ = PLATFORM_FILE_ERROR_NOT_FOUND; 115 error_ = File::FILE_ERROR_NOT_FOUND;
113 return; 116 return;
114 } 117 }
115 // TODO(rvargas): switch this file to base::File. 118 // TODO(rvargas): switch this file to base::File.
116 if (!GetFileInfo(file_path, reinterpret_cast<File::Info*>(&file_info_))) 119 if (!GetFileInfo(file_path, reinterpret_cast<File::Info*>(&file_info_)))
117 error_ = PLATFORM_FILE_ERROR_FAILED; 120 error_ = File::FILE_ERROR_FAILED;
118 } 121 }
119 122
120 void RunWorkForPlatformFile(PlatformFile file) { 123 void RunWorkForPlatformFile(PlatformFile file) {
121 if (!GetPlatformFileInfo(file, &file_info_)) 124 if (!GetPlatformFileInfo(
122 error_ = PLATFORM_FILE_ERROR_FAILED; 125 file, reinterpret_cast<PlatformFileInfo*>(&file_info_))) {
126 error_ = File::FILE_ERROR_FAILED;
127 }
123 } 128 }
124 129
125 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { 130 void Reply(const FileUtilProxy::GetFileInfoCallback& callback) {
126 if (!callback.is_null()) { 131 if (!callback.is_null()) {
127 callback.Run(error_, file_info_); 132 callback.Run(error_, file_info_);
128 } 133 }
129 } 134 }
130 135
131 private: 136 private:
132 PlatformFileError error_; 137 File::Error error_;
133 PlatformFileInfo file_info_; 138 File::Info file_info_;
134 DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper); 139 DISALLOW_COPY_AND_ASSIGN(GetFileInfoHelper);
135 }; 140 };
136 141
137 class ReadHelper { 142 class ReadHelper {
138 public: 143 public:
139 explicit ReadHelper(int bytes_to_read) 144 explicit ReadHelper(int bytes_to_read)
140 : buffer_(new char[bytes_to_read]), 145 : buffer_(new char[bytes_to_read]),
141 bytes_to_read_(bytes_to_read), 146 bytes_to_read_(bytes_to_read),
142 bytes_read_(0) {} 147 bytes_read_(0) {}
143 148
144 void RunWork(PlatformFile file, int64 offset) { 149 void RunWork(PlatformFile file, int64 offset) {
145 bytes_read_ = ReadPlatformFile(file, offset, buffer_.get(), bytes_to_read_); 150 bytes_read_ = ReadPlatformFile(file, offset, buffer_.get(), bytes_to_read_);
146 } 151 }
147 152
148 void Reply(const FileUtilProxy::ReadCallback& callback) { 153 void Reply(const FileUtilProxy::ReadCallback& callback) {
149 if (!callback.is_null()) { 154 if (!callback.is_null()) {
150 PlatformFileError error = 155 File::Error error =
151 (bytes_read_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK; 156 (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
152 callback.Run(error, buffer_.get(), bytes_read_); 157 callback.Run(error, buffer_.get(), bytes_read_);
153 } 158 }
154 } 159 }
155 160
156 private: 161 private:
157 scoped_ptr<char[]> buffer_; 162 scoped_ptr<char[]> buffer_;
158 int bytes_to_read_; 163 int bytes_to_read_;
159 int bytes_read_; 164 int bytes_read_;
160 DISALLOW_COPY_AND_ASSIGN(ReadHelper); 165 DISALLOW_COPY_AND_ASSIGN(ReadHelper);
161 }; 166 };
162 167
163 class WriteHelper { 168 class WriteHelper {
164 public: 169 public:
165 WriteHelper(const char* buffer, int bytes_to_write) 170 WriteHelper(const char* buffer, int bytes_to_write)
166 : buffer_(new char[bytes_to_write]), 171 : buffer_(new char[bytes_to_write]),
167 bytes_to_write_(bytes_to_write), 172 bytes_to_write_(bytes_to_write),
168 bytes_written_(0) { 173 bytes_written_(0) {
169 memcpy(buffer_.get(), buffer, bytes_to_write); 174 memcpy(buffer_.get(), buffer, bytes_to_write);
170 } 175 }
171 176
172 void RunWork(PlatformFile file, int64 offset) { 177 void RunWork(PlatformFile file, int64 offset) {
173 bytes_written_ = WritePlatformFile(file, offset, buffer_.get(), 178 bytes_written_ = WritePlatformFile(file, offset, buffer_.get(),
174 bytes_to_write_); 179 bytes_to_write_);
175 } 180 }
176 181
177 void Reply(const FileUtilProxy::WriteCallback& callback) { 182 void Reply(const FileUtilProxy::WriteCallback& callback) {
178 if (!callback.is_null()) { 183 if (!callback.is_null()) {
179 PlatformFileError error = 184 File::Error error =
180 (bytes_written_ < 0) ? PLATFORM_FILE_ERROR_FAILED : PLATFORM_FILE_OK; 185 (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
181 callback.Run(error, bytes_written_); 186 callback.Run(error, bytes_written_);
182 } 187 }
183 } 188 }
184 189
185 private: 190 private:
186 scoped_ptr<char[]> buffer_; 191 scoped_ptr<char[]> buffer_;
187 int bytes_to_write_; 192 int bytes_to_write_;
188 int bytes_written_; 193 int bytes_written_;
189 DISALLOW_COPY_AND_ASSIGN(WriteHelper); 194 DISALLOW_COPY_AND_ASSIGN(WriteHelper);
190 }; 195 };
191 196
192 PlatformFileError CreateOrOpenAdapter( 197 File::Error CreateOrOpenAdapter(
193 const FilePath& file_path, int file_flags, 198 const FilePath& file_path, int file_flags,
194 PlatformFile* file_handle, bool* created) { 199 PlatformFile* file_handle, bool* created) {
195 DCHECK(file_handle); 200 DCHECK(file_handle);
196 DCHECK(created); 201 DCHECK(created);
197 if (!DirectoryExists(file_path.DirName())) { 202 if (!DirectoryExists(file_path.DirName())) {
198 // If its parent does not exist, should return NOT_FOUND error. 203 // If its parent does not exist, should return NOT_FOUND error.
199 return PLATFORM_FILE_ERROR_NOT_FOUND; 204 return File::FILE_ERROR_NOT_FOUND;
200 } 205 }
201 PlatformFileError error = PLATFORM_FILE_OK; 206 File::Error error = File::FILE_OK;
202 *file_handle = CreatePlatformFile(file_path, file_flags, created, &error); 207 *file_handle =
208 CreatePlatformFile(file_path, file_flags, created,
209 reinterpret_cast<PlatformFileError*>(&error));
203 return error; 210 return error;
204 } 211 }
205 212
206 PlatformFileError CloseAdapter(PlatformFile file_handle) { 213 File::Error CloseAdapter(PlatformFile file_handle) {
207 if (!ClosePlatformFile(file_handle)) { 214 if (!ClosePlatformFile(file_handle)) {
208 return PLATFORM_FILE_ERROR_FAILED; 215 return File::FILE_ERROR_FAILED;
209 } 216 }
210 return PLATFORM_FILE_OK; 217 return File::FILE_OK;
211 } 218 }
212 219
213 PlatformFileError DeleteAdapter(const FilePath& file_path, bool recursive) { 220 File::Error DeleteAdapter(const FilePath& file_path, bool recursive) {
214 if (!PathExists(file_path)) { 221 if (!PathExists(file_path)) {
215 return PLATFORM_FILE_ERROR_NOT_FOUND; 222 return File::FILE_ERROR_NOT_FOUND;
216 } 223 }
217 if (!base::DeleteFile(file_path, recursive)) { 224 if (!base::DeleteFile(file_path, recursive)) {
218 if (!recursive && !base::IsDirectoryEmpty(file_path)) { 225 if (!recursive && !base::IsDirectoryEmpty(file_path)) {
219 return PLATFORM_FILE_ERROR_NOT_EMPTY; 226 return File::FILE_ERROR_NOT_EMPTY;
220 } 227 }
221 return PLATFORM_FILE_ERROR_FAILED; 228 return File::FILE_ERROR_FAILED;
222 } 229 }
223 return PLATFORM_FILE_OK; 230 return File::FILE_OK;
224 } 231 }
225 232
226 } // namespace 233 } // namespace
227 234
228 // static 235 // static
229 bool FileUtilProxy::CreateOrOpen( 236 bool FileUtilProxy::CreateOrOpen(
230 TaskRunner* task_runner, 237 TaskRunner* task_runner,
231 const FilePath& file_path, int file_flags, 238 const FilePath& file_path, int file_flags,
232 const CreateOrOpenCallback& callback) { 239 const CreateOrOpenCallback& callback) {
233 return RelayCreateOrOpen( 240 return RelayCreateOrOpen(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 bool FileUtilProxy::RelayClose( 413 bool FileUtilProxy::RelayClose(
407 TaskRunner* task_runner, 414 TaskRunner* task_runner,
408 const CloseTask& close_task, 415 const CloseTask& close_task,
409 PlatformFile file_handle, 416 PlatformFile file_handle,
410 const StatusCallback& callback) { 417 const StatusCallback& callback) {
411 return base::PostTaskAndReplyWithResult( 418 return base::PostTaskAndReplyWithResult(
412 task_runner, FROM_HERE, Bind(close_task, file_handle), callback); 419 task_runner, FROM_HERE, Bind(close_task, file_handle), callback);
413 } 420 }
414 421
415 } // namespace base 422 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_proxy.h ('k') | base/files/file_util_proxy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698