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

Side by Side Diff: content/browser/file_system/file_system_dispatcher_host.cc

Issue 9372044: Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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
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 "content/browser/file_system/file_system_dispatcher_host.h" 5 #include "content/browser/file_system/file_system_dispatcher_host.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h"
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/platform_file.h" 13 #include "base/platform_file.h"
13 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "content/common/file_system_messages.h" 16 #include "content/common/file_system_messages.h"
16 #include "content/public/browser/user_metrics.h" 17 #include "content/public/browser/user_metrics.h"
17 #include "googleurl/src/gurl.h" 18 #include "googleurl/src/gurl.h"
18 #include "ipc/ipc_platform_file.h" 19 #include "ipc/ipc_platform_file.h"
19 #include "net/url_request/url_request_context.h" 20 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h" 21 #include "net/url_request/url_request_context_getter.h"
21 #include "webkit/fileapi/file_system_callback_dispatcher.h"
22 #include "webkit/fileapi/file_system_context.h" 22 #include "webkit/fileapi/file_system_context.h"
23 #include "webkit/fileapi/file_system_operation.h" 23 #include "webkit/fileapi/file_system_operation.h"
24 #include "webkit/fileapi/file_system_quota_util.h" 24 #include "webkit/fileapi/file_system_quota_util.h"
25 #include "webkit/fileapi/file_system_util.h" 25 #include "webkit/fileapi/file_system_util.h"
26 #include "webkit/fileapi/sandbox_mount_point_provider.h" 26 #include "webkit/fileapi/sandbox_mount_point_provider.h"
27 27
28 using content::BrowserMessageFilter; 28 using content::BrowserMessageFilter;
29 using content::BrowserThread; 29 using content::BrowserThread;
30 using content::UserMetricsAction; 30 using content::UserMetricsAction;
31 using fileapi::FileSystemCallbackDispatcher;
32 using fileapi::FileSystemFileUtil; 31 using fileapi::FileSystemFileUtil;
33 using fileapi::FileSystemOperation; 32 using fileapi::FileSystemOperation;
34 using fileapi::FileSystemOperationInterface; 33 using fileapi::FileSystemOperationInterface;
35 34
36 class BrowserFileSystemCallbackDispatcher
37 : public FileSystemCallbackDispatcher {
38 public:
39 // An instance of this class must be created by Create()
40 // (so that we do not leak ownerships).
41 static scoped_ptr<FileSystemCallbackDispatcher> Create(
42 FileSystemDispatcherHost* dispatcher_host, int request_id) {
43 return scoped_ptr<FileSystemCallbackDispatcher>(
44 new BrowserFileSystemCallbackDispatcher(dispatcher_host, request_id));
45 }
46
47 virtual ~BrowserFileSystemCallbackDispatcher() {
48 dispatcher_host_->UnregisterOperation(request_id_);
49 }
50
51 virtual void DidSucceed() {
52 dispatcher_host_->Send(new FileSystemMsg_DidSucceed(request_id_));
53 }
54
55 virtual void DidReadMetadata(
56 const base::PlatformFileInfo& info,
57 const FilePath& platform_path) {
58 dispatcher_host_->Send(new FileSystemMsg_DidReadMetadata(
59 request_id_, info, platform_path));
60 }
61
62 virtual void DidReadDirectory(
63 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) {
64 dispatcher_host_->Send(new FileSystemMsg_DidReadDirectory(
65 request_id_, entries, has_more));
66 }
67
68 virtual void DidOpenFileSystem(const std::string& name,
69 const GURL& root) {
70 DCHECK(root.is_valid());
71 dispatcher_host_->Send(
72 new FileSystemMsg_DidOpenFileSystem(request_id_, name, root));
73 }
74
75 virtual void DidFail(base::PlatformFileError error_code) {
76 dispatcher_host_->Send(new FileSystemMsg_DidFail(request_id_, error_code));
77 }
78
79 virtual void DidWrite(int64 bytes, bool complete) {
80 dispatcher_host_->Send(new FileSystemMsg_DidWrite(
81 request_id_, bytes, complete));
82 }
83
84 virtual void DidOpenFile(
85 base::PlatformFile file,
86 base::ProcessHandle peer_handle) {
87 IPC::PlatformFileForTransit file_for_transit =
88 file != base::kInvalidPlatformFileValue ?
89 IPC::GetFileHandleForProcess(file, peer_handle, true) :
90 IPC::InvalidPlatformFileForTransit();
91
92 dispatcher_host_->Send(new FileSystemMsg_DidOpenFile(
93 request_id_, file_for_transit));
94 }
95
96 private:
97 BrowserFileSystemCallbackDispatcher(
98 FileSystemDispatcherHost* dispatcher_host, int request_id)
99 : dispatcher_host_(dispatcher_host),
100 request_id_(request_id) {
101 DCHECK(dispatcher_host_);
102 }
103
104 scoped_refptr<FileSystemDispatcherHost> dispatcher_host_;
105 int request_id_;
106 };
107
108 FileSystemDispatcherHost::FileSystemDispatcherHost( 35 FileSystemDispatcherHost::FileSystemDispatcherHost(
109 net::URLRequestContextGetter* request_context_getter, 36 net::URLRequestContextGetter* request_context_getter,
110 fileapi::FileSystemContext* file_system_context) 37 fileapi::FileSystemContext* file_system_context)
111 : context_(file_system_context), 38 : context_(file_system_context),
112 request_context_getter_(request_context_getter), 39 request_context_getter_(request_context_getter),
113 request_context_(NULL) { 40 request_context_(NULL) {
114 DCHECK(context_); 41 DCHECK(context_);
115 DCHECK(request_context_getter_); 42 DCHECK(request_context_getter_);
116 } 43 }
117 44
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 100 }
174 101
175 void FileSystemDispatcherHost::OnOpen( 102 void FileSystemDispatcherHost::OnOpen(
176 int request_id, const GURL& origin_url, fileapi::FileSystemType type, 103 int request_id, const GURL& origin_url, fileapi::FileSystemType type,
177 int64 requested_size, bool create) { 104 int64 requested_size, bool create) {
178 if (type == fileapi::kFileSystemTypeTemporary) { 105 if (type == fileapi::kFileSystemTypeTemporary) {
179 content::RecordAction(UserMetricsAction("OpenFileSystemTemporary")); 106 content::RecordAction(UserMetricsAction("OpenFileSystemTemporary"));
180 } else if (type == fileapi::kFileSystemTypePersistent) { 107 } else if (type == fileapi::kFileSystemTypePersistent) {
181 content::RecordAction(UserMetricsAction("OpenFileSystemPersistent")); 108 content::RecordAction(UserMetricsAction("OpenFileSystemPersistent"));
182 } 109 }
183 context_->OpenFileSystem(origin_url, type, create, 110 context_->OpenFileSystem(origin_url, type, create, base::Bind(
184 BrowserFileSystemCallbackDispatcher::Create( 111 &FileSystemDispatcherHost::DidOpenFileSystem, this, request_id));
185 this, request_id));
186 } 112 }
187 113
188 void FileSystemDispatcherHost::OnMove( 114 void FileSystemDispatcherHost::OnMove(
189 int request_id, const GURL& src_path, const GURL& dest_path) { 115 int request_id, const GURL& src_path, const GURL& dest_path) {
190 GetNewOperation(src_path, request_id)->Move(src_path, dest_path); 116 GetNewOperation(src_path, request_id)->Move(
117 src_path, dest_path,
118 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
191 } 119 }
192 120
193 void FileSystemDispatcherHost::OnCopy( 121 void FileSystemDispatcherHost::OnCopy(
194 int request_id, const GURL& src_path, const GURL& dest_path) { 122 int request_id, const GURL& src_path, const GURL& dest_path) {
195 GetNewOperation(src_path, request_id)->Copy(src_path, dest_path); 123 GetNewOperation(src_path, request_id)->Copy(
124 src_path, dest_path,
125 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
196 } 126 }
197 127
198 void FileSystemDispatcherHost::OnRemove( 128 void FileSystemDispatcherHost::OnRemove(
199 int request_id, const GURL& path, bool recursive) { 129 int request_id, const GURL& path, bool recursive) {
200 GetNewOperation(path, request_id)->Remove(path, recursive); 130 GetNewOperation(path, request_id)->Remove(
131 path, recursive,
132 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
201 } 133 }
202 134
203 void FileSystemDispatcherHost::OnReadMetadata( 135 void FileSystemDispatcherHost::OnReadMetadata(
204 int request_id, const GURL& path) { 136 int request_id, const GURL& path) {
205 GetNewOperation(path, request_id)->GetMetadata(path); 137 GetNewOperation(path, request_id)->GetMetadata(
138 path,
139 base::Bind(&FileSystemDispatcherHost::DidGetMetadata, this, request_id));
206 } 140 }
207 141
208 void FileSystemDispatcherHost::OnCreate( 142 void FileSystemDispatcherHost::OnCreate(
209 int request_id, const GURL& path, bool exclusive, 143 int request_id, const GURL& path, bool exclusive,
210 bool is_directory, bool recursive) { 144 bool is_directory, bool recursive) {
211 if (is_directory) 145 if (is_directory) {
212 GetNewOperation(path, request_id)->CreateDirectory( 146 GetNewOperation(path, request_id)->CreateDirectory(
213 path, exclusive, recursive); 147 path, exclusive, recursive,
214 else 148 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
215 GetNewOperation(path, request_id)->CreateFile(path, exclusive); 149 } else {
150 GetNewOperation(path, request_id)->CreateFile(
151 path, exclusive,
152 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
153 }
216 } 154 }
217 155
218 void FileSystemDispatcherHost::OnExists( 156 void FileSystemDispatcherHost::OnExists(
219 int request_id, const GURL& path, bool is_directory) { 157 int request_id, const GURL& path, bool is_directory) {
220 if (is_directory) 158 if (is_directory) {
221 GetNewOperation(path, request_id)->DirectoryExists(path); 159 GetNewOperation(path, request_id)->DirectoryExists(
222 else 160 path,
223 GetNewOperation(path, request_id)->FileExists(path); 161 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
162 } else {
163 GetNewOperation(path, request_id)->FileExists(
164 path,
165 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
166 }
224 } 167 }
225 168
226 void FileSystemDispatcherHost::OnReadDirectory( 169 void FileSystemDispatcherHost::OnReadDirectory(
227 int request_id, const GURL& path) { 170 int request_id, const GURL& path) {
228 GetNewOperation(path, request_id)->ReadDirectory(path); 171 GetNewOperation(path, request_id)->ReadDirectory(
172 path, base::Bind(&FileSystemDispatcherHost::DidReadDirectory,
173 this, request_id));
229 } 174 }
230 175
231 void FileSystemDispatcherHost::OnWrite( 176 void FileSystemDispatcherHost::OnWrite(
232 int request_id, 177 int request_id,
233 const GURL& path, 178 const GURL& path,
234 const GURL& blob_url, 179 const GURL& blob_url,
235 int64 offset) { 180 int64 offset) {
236 if (!request_context_) { 181 if (!request_context_) {
237 // We can't write w/o a request context, trying to do so will crash. 182 // We can't write w/o a request context, trying to do so will crash.
238 NOTREACHED(); 183 NOTREACHED();
239 return; 184 return;
240 } 185 }
241 GetNewOperation(path, request_id)->Write( 186 GetNewOperation(path, request_id)->Write(
242 request_context_, path, blob_url, offset); 187 request_context_, path, blob_url, offset,
188 base::Bind(&FileSystemDispatcherHost::DidWrite, this, request_id));
243 } 189 }
244 190
245 void FileSystemDispatcherHost::OnTruncate( 191 void FileSystemDispatcherHost::OnTruncate(
246 int request_id, 192 int request_id,
247 const GURL& path, 193 const GURL& path,
248 int64 length) { 194 int64 length) {
249 GetNewOperation(path, request_id)->Truncate(path, length); 195 GetNewOperation(path, request_id)->Truncate(
196 path, length,
197 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
250 } 198 }
251 199
252 void FileSystemDispatcherHost::OnTouchFile( 200 void FileSystemDispatcherHost::OnTouchFile(
253 int request_id, 201 int request_id,
254 const GURL& path, 202 const GURL& path,
255 const base::Time& last_access_time, 203 const base::Time& last_access_time,
256 const base::Time& last_modified_time) { 204 const base::Time& last_modified_time) {
257 GetNewOperation(path, request_id)->TouchFile( 205 GetNewOperation(path, request_id)->TouchFile(
258 path, last_access_time, last_modified_time); 206 path, last_access_time, last_modified_time,
207 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
259 } 208 }
260 209
261 void FileSystemDispatcherHost::OnCancel( 210 void FileSystemDispatcherHost::OnCancel(
262 int request_id, 211 int request_id,
263 int request_id_to_cancel) { 212 int request_id_to_cancel) {
264 FileSystemOperationInterface* write = operations_.Lookup( 213 FileSystemOperationInterface* write = operations_.Lookup(
265 request_id_to_cancel); 214 request_id_to_cancel);
266 if (write) { 215 if (write) {
267 // The cancel will eventually send both the write failure and the cancel 216 // The cancel will eventually send both the write failure and the cancel
268 // success. 217 // success.
269 write->Cancel( 218 write->Cancel(
270 BrowserFileSystemCallbackDispatcher::Create(this, request_id)); 219 base::Bind(&FileSystemDispatcherHost::DidFinish, this, request_id));
271 } else { 220 } else {
272 // The write already finished; report that we failed to stop it. 221 // The write already finished; report that we failed to stop it.
273 Send(new FileSystemMsg_DidFail( 222 Send(new FileSystemMsg_DidFail(
274 request_id, base::PLATFORM_FILE_ERROR_INVALID_OPERATION)); 223 request_id, base::PLATFORM_FILE_ERROR_INVALID_OPERATION));
275 } 224 }
276 } 225 }
277 226
278 void FileSystemDispatcherHost::OnOpenFile( 227 void FileSystemDispatcherHost::OnOpenFile(
279 int request_id, const GURL& path, int file_flags) { 228 int request_id, const GURL& path, int file_flags) {
280 GetNewOperation(path, request_id)->OpenFile(path, file_flags, peer_handle()); 229 GetNewOperation(path, request_id)->OpenFile(
230 path, file_flags, peer_handle(),
231 base::Bind(&FileSystemDispatcherHost::DidOpenFile, this, request_id));
281 } 232 }
282 233
283 void FileSystemDispatcherHost::OnWillUpdate(const GURL& path) { 234 void FileSystemDispatcherHost::OnWillUpdate(const GURL& path) {
284 GURL origin_url; 235 GURL origin_url;
285 fileapi::FileSystemType type; 236 fileapi::FileSystemType type;
286 if (!CrackFileSystemURL(path, &origin_url, &type, NULL)) 237 if (!CrackFileSystemURL(path, &origin_url, &type, NULL))
287 return; 238 return;
288 fileapi::FileSystemQuotaUtil* quota_util = context_->GetQuotaUtil(type); 239 fileapi::FileSystemQuotaUtil* quota_util = context_->GetQuotaUtil(type);
289 if (!quota_util) 240 if (!quota_util)
290 return; 241 return;
(...skipping 27 matching lines...) Expand all
318 return; 269 return;
319 } 270 }
320 271
321 // This is called only by pepper plugin as of writing to get the 272 // This is called only by pepper plugin as of writing to get the
322 // underlying platform path to upload a file in the sandboxed filesystem 273 // underlying platform path to upload a file in the sandboxed filesystem
323 // (e.g. TEMPORARY or PERSISTENT). 274 // (e.g. TEMPORARY or PERSISTENT).
324 // TODO(kinuko): this hack should go away once appropriate upload-stream 275 // TODO(kinuko): this hack should go away once appropriate upload-stream
325 // handling based on element types is supported. 276 // handling based on element types is supported.
326 FileSystemOperation* operation = 277 FileSystemOperation* operation =
327 context_->CreateFileSystemOperation( 278 context_->CreateFileSystemOperation(
328 path, scoped_ptr<FileSystemCallbackDispatcher>(NULL), 279 path,
329 BrowserThread::GetMessageLoopProxyForThread( 280 BrowserThread::GetMessageLoopProxyForThread(
330 BrowserThread::FILE))->AsFileSystemOperation(); 281 BrowserThread::FILE))->AsFileSystemOperation();
331 DCHECK(operation); 282 DCHECK(operation);
332 operation->SyncGetPlatformPath(path, platform_path); 283 operation->SyncGetPlatformPath(path, platform_path);
333 } 284 }
334 285
286 void FileSystemDispatcherHost::DidFinish(int request_id,
287 base::PlatformFileError result) {
288 if (result == base::PLATFORM_FILE_OK)
289 Send(new FileSystemMsg_DidSucceed(request_id));
290 else
291 Send(new FileSystemMsg_DidFail(request_id, result));
292 UnregisterOperation(request_id);
293 }
294
295 void FileSystemDispatcherHost::DidGetMetadata(
296 int request_id,
297 base::PlatformFileError result,
298 const base::PlatformFileInfo& info,
299 const FilePath& platform_path) {
300 if (result == base::PLATFORM_FILE_OK)
301 Send(new FileSystemMsg_DidReadMetadata(request_id, info, platform_path));
302 else
303 Send(new FileSystemMsg_DidFail(request_id, result));
304 UnregisterOperation(request_id);
305 }
306
307 void FileSystemDispatcherHost::DidReadDirectory(
308 int request_id,
309 base::PlatformFileError result,
310 const std::vector<base::FileUtilProxy::Entry>& entries,
311 bool has_more) {
312 if (result == base::PLATFORM_FILE_OK)
313 Send(new FileSystemMsg_DidReadDirectory(request_id, entries, has_more));
314 else
315 Send(new FileSystemMsg_DidFail(request_id, result));
316 UnregisterOperation(request_id);
317 }
318
319 void FileSystemDispatcherHost::DidOpenFile(int request_id,
320 base::PlatformFileError result,
321 base::PlatformFile file,
322 base::ProcessHandle peer_handle) {
323 if (result == base::PLATFORM_FILE_OK) {
324 IPC::PlatformFileForTransit file_for_transit =
325 file != base::kInvalidPlatformFileValue ?
326 IPC::GetFileHandleForProcess(file, peer_handle, true) :
327 IPC::InvalidPlatformFileForTransit();
328 Send(new FileSystemMsg_DidOpenFile(request_id, file_for_transit));
329 } else {
330 Send(new FileSystemMsg_DidFail(request_id, result));
331 }
332 UnregisterOperation(request_id);
333 }
334
335 void FileSystemDispatcherHost::DidWrite(int request_id,
336 base::PlatformFileError result,
337 int64 bytes,
338 bool complete) {
339 if (result == base::PLATFORM_FILE_OK) {
340 Send(new FileSystemMsg_DidWrite(request_id, bytes, complete));
341 if (complete)
342 UnregisterOperation(request_id);
343 } else {
344 Send(new FileSystemMsg_DidFail(request_id, result));
345 UnregisterOperation(request_id);
346 }
347 }
348
349 void FileSystemDispatcherHost::DidOpenFileSystem(int request_id,
350 base::PlatformFileError result,
351 const std::string& name,
352 const GURL& root) {
353 if (result == base::PLATFORM_FILE_OK) {
354 DCHECK(root.is_valid());
355 Send(new FileSystemMsg_DidOpenFileSystem(request_id, name, root));
356 } else {
357 Send(new FileSystemMsg_DidFail(request_id, result));
358 }
359 // For OpenFileSystem we do not create an operation.
360 }
361
335 FileSystemOperationInterface* FileSystemDispatcherHost::GetNewOperation( 362 FileSystemOperationInterface* FileSystemDispatcherHost::GetNewOperation(
336 const GURL& target_path, 363 const GURL& target_path,
337 int request_id) { 364 int request_id) {
338 FileSystemOperationInterface* operation = 365 FileSystemOperationInterface* operation =
339 context_->CreateFileSystemOperation( 366 context_->CreateFileSystemOperation(
340 target_path, 367 target_path,
341 BrowserFileSystemCallbackDispatcher::Create(this, request_id),
342 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); 368 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
343 DCHECK(operation); 369 DCHECK(operation);
344 operations_.AddWithID(operation, request_id); 370 operations_.AddWithID(operation, request_id);
345 return operation; 371 return operation;
346 } 372 }
347 373
348 void FileSystemDispatcherHost::UnregisterOperation(int request_id) { 374 void FileSystemDispatcherHost::UnregisterOperation(int request_id) {
349 // For Cancel and OpenFileSystem we do not create an operation. 375 // For Cancel and OpenFileSystem we do not create an operation.
kinuko 2012/02/10 05:34:09 Now that we will not call this in DidOpenFileSyste
kinaba 2012/02/10 08:22:58 Callback from Cancel still called this, so it migh
kinuko 2012/02/10 08:49:14 Thanks!
350 if (operations_.Lookup(request_id)) 376 if (operations_.Lookup(request_id))
351 operations_.Remove(request_id); 377 operations_.Remove(request_id);
352 } 378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698