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

Side by Side Diff: webkit/fileapi/file_system_operation.cc

Issue 6286038: Add initial code to do filename munging in the FileSystem.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "webkit/fileapi/file_system_operation.h" 5 #include "webkit/fileapi/file_system_operation.h"
6 6
7 #include "base/file_util_proxy.h"
7 #include "base/time.h" 8 #include "base/time.h"
8 #include "net/url_request/url_request_context.h" 9 #include "net/url_request/url_request_context.h"
9 #include "webkit/fileapi/file_system_callback_dispatcher.h" 10 #include "webkit/fileapi/file_system_callback_dispatcher.h"
10 #include "webkit/fileapi/file_writer_delegate.h" 11 #include "webkit/fileapi/file_writer_delegate.h"
11 12
12 namespace fileapi { 13 namespace fileapi {
13 14
14 FileSystemOperation::FileSystemOperation( 15 FileSystemOperation::FileSystemOperation(
15 FileSystemCallbackDispatcher* dispatcher, 16 FileSystemCallbackDispatcher* dispatcher,
16 scoped_refptr<base::MessageLoopProxy> proxy) 17 scoped_refptr<base::MessageLoopProxy> proxy)
17 : proxy_(proxy), 18 : proxy_(proxy),
18 dispatcher_(dispatcher), 19 dispatcher_(dispatcher),
19 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 20 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
20 DCHECK(dispatcher); 21 DCHECK(dispatcher);
21 #ifndef NDEBUG 22 #ifndef NDEBUG
22 pending_operation_ = kOperationNone; 23 pending_operation_ = kOperationNone;
23 #endif 24 #endif
24 } 25 }
25 26
26 FileSystemOperation::~FileSystemOperation() { 27 FileSystemOperation::~FileSystemOperation() {
27 if (file_writer_delegate_.get()) 28 if (file_writer_delegate_.get())
28 base::FileUtilProxy::Close(proxy_, file_writer_delegate_->file(), NULL); 29 file_util_proxy()->Close(
30 proxy_, file_writer_delegate_->file(), NULL);
29 } 31 }
30 32
31 void FileSystemOperation::CreateFile(const FilePath& path, 33 void FileSystemOperation::CreateFile(const FilePath& path,
32 bool exclusive) { 34 bool exclusive) {
33 #ifndef NDEBUG 35 #ifndef NDEBUG
34 DCHECK(kOperationNone == pending_operation_); 36 DCHECK(kOperationNone == pending_operation_);
35 pending_operation_ = kOperationCreateFile; 37 pending_operation_ = kOperationCreateFile;
36 #endif 38 #endif
37 39
38 base::FileUtilProxy::EnsureFileExists( 40 file_util_proxy()->EnsureFileExists(
39 proxy_, path, callback_factory_.NewCallback( 41 proxy_, path, callback_factory_.NewCallback(
40 exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive 42 exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive
41 : &FileSystemOperation::DidEnsureFileExistsNonExclusive)); 43 : &FileSystemOperation::DidEnsureFileExistsNonExclusive));
42 } 44 }
43 45
44 void FileSystemOperation::CreateDirectory(const FilePath& path, 46 void FileSystemOperation::CreateDirectory(const FilePath& path,
45 bool exclusive, 47 bool exclusive,
46 bool recursive) { 48 bool recursive) {
47 #ifndef NDEBUG 49 #ifndef NDEBUG
48 DCHECK(kOperationNone == pending_operation_); 50 DCHECK(kOperationNone == pending_operation_);
49 pending_operation_ = kOperationCreateDirectory; 51 pending_operation_ = kOperationCreateDirectory;
50 #endif 52 #endif
51 53
52 base::FileUtilProxy::CreateDirectory( 54 file_util_proxy()->CreateDirectory(
53 proxy_, path, exclusive, recursive, callback_factory_.NewCallback( 55 proxy_, path, exclusive, recursive, callback_factory_.NewCallback(
54 &FileSystemOperation::DidFinishFileOperation)); 56 &FileSystemOperation::DidFinishFileOperation));
55 } 57 }
56 58
57 void FileSystemOperation::Copy(const FilePath& src_path, 59 void FileSystemOperation::Copy(const FilePath& src_path,
58 const FilePath& dest_path) { 60 const FilePath& dest_path) {
59 #ifndef NDEBUG 61 #ifndef NDEBUG
60 DCHECK(kOperationNone == pending_operation_); 62 DCHECK(kOperationNone == pending_operation_);
61 pending_operation_ = kOperationCopy; 63 pending_operation_ = kOperationCopy;
62 #endif 64 #endif
63 65
64 base::FileUtilProxy::Copy(proxy_, src_path, dest_path, 66 file_util_proxy()->Copy(
65 callback_factory_.NewCallback( 67 proxy_, src_path, dest_path, callback_factory_.NewCallback(
66 &FileSystemOperation::DidFinishFileOperation)); 68 &FileSystemOperation::DidFinishFileOperation));
67 } 69 }
68 70
69 void FileSystemOperation::Move(const FilePath& src_path, 71 void FileSystemOperation::Move(const FilePath& src_path,
70 const FilePath& dest_path) { 72 const FilePath& dest_path) {
71 #ifndef NDEBUG 73 #ifndef NDEBUG
72 DCHECK(kOperationNone == pending_operation_); 74 DCHECK(kOperationNone == pending_operation_);
73 pending_operation_ = kOperationMove; 75 pending_operation_ = kOperationMove;
74 #endif 76 #endif
75 77
76 base::FileUtilProxy::Move(proxy_, src_path, dest_path, 78 file_util_proxy()->Move(
77 callback_factory_.NewCallback( 79 proxy_, src_path, dest_path, callback_factory_.NewCallback(
78 &FileSystemOperation::DidFinishFileOperation)); 80 &FileSystemOperation::DidFinishFileOperation));
79 } 81 }
80 82
81 void FileSystemOperation::DirectoryExists(const FilePath& path) { 83 void FileSystemOperation::DirectoryExists(const FilePath& path) {
82 #ifndef NDEBUG 84 #ifndef NDEBUG
83 DCHECK(kOperationNone == pending_operation_); 85 DCHECK(kOperationNone == pending_operation_);
84 pending_operation_ = kOperationDirectoryExists; 86 pending_operation_ = kOperationDirectoryExists;
85 #endif 87 #endif
86 88
87 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( 89 file_util_proxy()->GetFileInfo(
88 &FileSystemOperation::DidDirectoryExists)); 90 proxy_, path, callback_factory_.NewCallback(
91 &FileSystemOperation::DidDirectoryExists));
89 } 92 }
90 93
91 void FileSystemOperation::FileExists(const FilePath& path) { 94 void FileSystemOperation::FileExists(const FilePath& path) {
92 #ifndef NDEBUG 95 #ifndef NDEBUG
93 DCHECK(kOperationNone == pending_operation_); 96 DCHECK(kOperationNone == pending_operation_);
94 pending_operation_ = kOperationFileExists; 97 pending_operation_ = kOperationFileExists;
95 #endif 98 #endif
96 99
97 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( 100 file_util_proxy()->GetFileInfo(
98 &FileSystemOperation::DidFileExists)); 101 proxy_, path, callback_factory_.NewCallback(
102 &FileSystemOperation::DidFileExists));
99 } 103 }
100 104
101 void FileSystemOperation::GetMetadata(const FilePath& path) { 105 void FileSystemOperation::GetMetadata(const FilePath& path) {
102 #ifndef NDEBUG 106 #ifndef NDEBUG
103 DCHECK(kOperationNone == pending_operation_); 107 DCHECK(kOperationNone == pending_operation_);
104 pending_operation_ = kOperationGetMetadata; 108 pending_operation_ = kOperationGetMetadata;
105 #endif 109 #endif
106 110
107 base::FileUtilProxy::GetFileInfo(proxy_, path, callback_factory_.NewCallback( 111 file_util_proxy()->GetFileInfo(
108 &FileSystemOperation::DidGetMetadata)); 112 proxy_, path, callback_factory_.NewCallback(
113 &FileSystemOperation::DidGetMetadata));
109 } 114 }
110 115
111 void FileSystemOperation::ReadDirectory(const FilePath& path) { 116 void FileSystemOperation::ReadDirectory(const FilePath& path) {
112 #ifndef NDEBUG 117 #ifndef NDEBUG
113 DCHECK(kOperationNone == pending_operation_); 118 DCHECK(kOperationNone == pending_operation_);
114 pending_operation_ = kOperationReadDirectory; 119 pending_operation_ = kOperationReadDirectory;
115 #endif 120 #endif
116 121
117 base::FileUtilProxy::ReadDirectory(proxy_, path, 122 file_util_proxy()->ReadDirectory(
118 callback_factory_.NewCallback( 123 proxy_, path, callback_factory_.NewCallback(
119 &FileSystemOperation::DidReadDirectory)); 124 &FileSystemOperation::DidReadDirectory));
120 } 125 }
121 126
122 void FileSystemOperation::Remove(const FilePath& path, bool recursive) { 127 void FileSystemOperation::Remove(const FilePath& path, bool recursive) {
123 #ifndef NDEBUG 128 #ifndef NDEBUG
124 DCHECK(kOperationNone == pending_operation_); 129 DCHECK(kOperationNone == pending_operation_);
125 pending_operation_ = kOperationRemove; 130 pending_operation_ = kOperationRemove;
126 #endif 131 #endif
127 132
128 base::FileUtilProxy::Delete(proxy_, path, recursive, 133 file_util_proxy()->Delete(
129 callback_factory_.NewCallback( 134 proxy_, path, recursive, callback_factory_.NewCallback(
130 &FileSystemOperation::DidFinishFileOperation)); 135 &FileSystemOperation::DidFinishFileOperation));
131 } 136 }
132 137
133 void FileSystemOperation::Write( 138 void FileSystemOperation::Write(
134 scoped_refptr<net::URLRequestContext> url_request_context, 139 scoped_refptr<net::URLRequestContext> url_request_context,
135 const FilePath& path, 140 const FilePath& path,
136 const GURL& blob_url, 141 const GURL& blob_url,
137 int64 offset) { 142 int64 offset) {
138 #ifndef NDEBUG 143 #ifndef NDEBUG
139 DCHECK(kOperationNone == pending_operation_); 144 DCHECK(kOperationNone == pending_operation_);
140 pending_operation_ = kOperationWrite; 145 pending_operation_ = kOperationWrite;
141 #endif 146 #endif
142 DCHECK(blob_url.is_valid()); 147 DCHECK(blob_url.is_valid());
143 file_writer_delegate_.reset(new FileWriterDelegate(this, offset)); 148 file_writer_delegate_.reset(new FileWriterDelegate(this, offset));
144 blob_request_.reset( 149 blob_request_.reset(
145 new net::URLRequest(blob_url, file_writer_delegate_.get())); 150 new net::URLRequest(blob_url, file_writer_delegate_.get()));
146 blob_request_->set_context(url_request_context); 151 blob_request_->set_context(url_request_context);
147 base::FileUtilProxy::CreateOrOpen( 152 file_util_proxy()->CreateOrOpen(
148 proxy_, 153 proxy_,
149 path, 154 path,
150 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | 155 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
151 base::PLATFORM_FILE_ASYNC, 156 base::PLATFORM_FILE_ASYNC,
152 callback_factory_.NewCallback( 157 callback_factory_.NewCallback(
153 &FileSystemOperation::OnFileOpenedForWrite)); 158 &FileSystemOperation::OnFileOpenedForWrite));
154 } 159 }
155 160
156 void FileSystemOperation::OnFileOpenedForWrite( 161 void FileSystemOperation::OnFileOpenedForWrite(
157 base::PlatformFileError rv, 162 base::PlatformFileError rv,
158 base::PassPlatformFile file, 163 base::PassPlatformFile file,
159 bool created) { 164 bool created) {
160 if (base::PLATFORM_FILE_OK != rv) { 165 if (base::PLATFORM_FILE_OK != rv) {
161 dispatcher_->DidFail(rv); 166 dispatcher_->DidFail(rv);
162 delete this; 167 delete this;
163 return; 168 return;
164 } 169 }
165 file_writer_delegate_->Start(file.ReleaseValue(), blob_request_.get()); 170 file_writer_delegate_->Start(file.ReleaseValue(), blob_request_.get());
166 } 171 }
167 172
168 void FileSystemOperation::Truncate(const FilePath& path, int64 length) { 173 void FileSystemOperation::Truncate(const FilePath& path, int64 length) {
169 #ifndef NDEBUG 174 #ifndef NDEBUG
170 DCHECK(kOperationNone == pending_operation_); 175 DCHECK(kOperationNone == pending_operation_);
171 pending_operation_ = kOperationTruncate; 176 pending_operation_ = kOperationTruncate;
172 #endif 177 #endif
173 base::FileUtilProxy::Truncate(proxy_, path, length, 178 file_util_proxy()->Truncate(
174 callback_factory_.NewCallback( 179 proxy_, path, length, callback_factory_.NewCallback(
175 &FileSystemOperation::DidFinishFileOperation)); 180 &FileSystemOperation::DidFinishFileOperation));
176 } 181 }
177 182
178 void FileSystemOperation::TouchFile(const FilePath& path, 183 void FileSystemOperation::TouchFile(const FilePath& path,
179 const base::Time& last_access_time, 184 const base::Time& last_access_time,
180 const base::Time& last_modified_time) { 185 const base::Time& last_modified_time) {
181 #ifndef NDEBUG 186 #ifndef NDEBUG
182 DCHECK(kOperationNone == pending_operation_); 187 DCHECK(kOperationNone == pending_operation_);
183 pending_operation_ = kOperationTouchFile; 188 pending_operation_ = kOperationTouchFile;
184 #endif 189 #endif
185 190
186 base::FileUtilProxy::Touch( 191 file_util_proxy()->Touch(
187 proxy_, path, last_access_time, last_modified_time, 192 proxy_, path, last_access_time, last_modified_time,
188 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile)); 193 callback_factory_.NewCallback(&FileSystemOperation::DidTouchFile));
189 } 194 }
190 195
191 // We can only get here on a write or truncate that's not yet completed. 196 // We can only get here on a write or truncate that's not yet completed.
192 // We don't support cancelling any other operation at this time. 197 // We don't support cancelling any other operation at this time.
193 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation_ptr) { 198 void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation_ptr) {
194 scoped_ptr<FileSystemOperation> cancel_operation(cancel_operation_ptr); 199 scoped_ptr<FileSystemOperation> cancel_operation(cancel_operation_ptr);
195 if (file_writer_delegate_.get()) { 200 if (file_writer_delegate_.get()) {
196 #ifndef NDEBUG 201 #ifndef NDEBUG
(...skipping 17 matching lines...) Expand all
214 #endif 219 #endif
215 // We're cancelling a truncate operation, but we can't actually stop it 220 // We're cancelling a truncate operation, but we can't actually stop it
216 // since it's been proxied to another thread. We need to save the 221 // since it's been proxied to another thread. We need to save the
217 // cancel_operation so that when the truncate returns, it can see that it's 222 // cancel_operation so that when the truncate returns, it can see that it's
218 // been cancelled, report it, and report that the cancel has succeeded. 223 // been cancelled, report it, and report that the cancel has succeeded.
219 DCHECK(!cancel_operation_.get()); 224 DCHECK(!cancel_operation_.get());
220 cancel_operation_.swap(cancel_operation); 225 cancel_operation_.swap(cancel_operation);
221 } 226 }
222 } 227 }
223 228
229 base::FileUtilProxyBase* FileSystemOperation::file_util_proxy() const {
230 return base::FileUtilProxy::GetInstance();
231 }
232
224 void FileSystemOperation::DidEnsureFileExistsExclusive( 233 void FileSystemOperation::DidEnsureFileExistsExclusive(
225 base::PlatformFileError rv, bool created) { 234 base::PlatformFileError rv, bool created) {
226 if (rv == base::PLATFORM_FILE_OK && !created) { 235 if (rv == base::PLATFORM_FILE_OK && !created) {
227 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_EXISTS); 236 dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_EXISTS);
228 delete this; 237 delete this;
229 } else 238 } else
230 DidFinishFileOperation(rv); 239 DidFinishFileOperation(rv);
231 } 240 }
232 241
233 void FileSystemOperation::DidEnsureFileExistsNonExclusive( 242 void FileSystemOperation::DidEnsureFileExistsNonExclusive(
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 const base::PlatformFileInfo& file_info) { 294 const base::PlatformFileInfo& file_info) {
286 if (rv == base::PLATFORM_FILE_OK) 295 if (rv == base::PLATFORM_FILE_OK)
287 dispatcher_->DidReadMetadata(file_info); 296 dispatcher_->DidReadMetadata(file_info);
288 else 297 else
289 dispatcher_->DidFail(rv); 298 dispatcher_->DidFail(rv);
290 delete this; 299 delete this;
291 } 300 }
292 301
293 void FileSystemOperation::DidReadDirectory( 302 void FileSystemOperation::DidReadDirectory(
294 base::PlatformFileError rv, 303 base::PlatformFileError rv,
295 const std::vector<base::FileUtilProxy::Entry>& entries) { 304 const std::vector<base::FileUtilProxyBase::Entry>& entries) {
296 if (rv == base::PLATFORM_FILE_OK) 305 if (rv == base::PLATFORM_FILE_OK)
297 dispatcher_->DidReadDirectory(entries, false /* has_more */); 306 dispatcher_->DidReadDirectory(entries, false /* has_more */);
298 else 307 else
299 dispatcher_->DidFail(rv); 308 dispatcher_->DidFail(rv);
300 delete this; 309 delete this;
301 } 310 }
302 311
303 void FileSystemOperation::DidWrite( 312 void FileSystemOperation::DidWrite(
304 base::PlatformFileError rv, 313 base::PlatformFileError rv,
305 int64 bytes, 314 int64 bytes,
306 bool complete) { 315 bool complete) {
307 if (rv == base::PLATFORM_FILE_OK) 316 if (rv == base::PLATFORM_FILE_OK)
308 dispatcher_->DidWrite(bytes, complete); 317 dispatcher_->DidWrite(bytes, complete);
309 else 318 else
310 dispatcher_->DidFail(rv); 319 dispatcher_->DidFail(rv);
311 if (complete || rv != base::PLATFORM_FILE_OK) 320 if (complete || rv != base::PLATFORM_FILE_OK)
312 delete this; 321 delete this;
313 } 322 }
314 323
315 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) { 324 void FileSystemOperation::DidTouchFile(base::PlatformFileError rv) {
316 if (rv == base::PLATFORM_FILE_OK) 325 if (rv == base::PLATFORM_FILE_OK)
317 dispatcher_->DidSucceed(); 326 dispatcher_->DidSucceed();
318 else 327 else
319 dispatcher_->DidFail(rv); 328 dispatcher_->DidFail(rv);
320 delete this; 329 delete this;
321 } 330 }
322 331
323 } // namespace fileapi 332 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698