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

Side by Side Diff: chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc

Issue 440653003: [fsp] Add support for aborting running operations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed DCHECKs. Created 6 years, 4 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/fake_provided_file_system .h" 5 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system .h"
6 6
7 #include "base/files/file.h" 7 #include "base/files/file.h"
8 #include "base/message_loop/message_loop_proxy.h" 8 #include "base/message_loop/message_loop_proxy.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 bool FakeProvidedFileSystem::GetEntry(const base::FilePath& entry_path, 66 bool FakeProvidedFileSystem::GetEntry(const base::FilePath& entry_path,
67 FakeEntry* fake_entry) const { 67 FakeEntry* fake_entry) const {
68 const Entries::const_iterator entry_it = entries_.find(entry_path); 68 const Entries::const_iterator entry_it = entries_.find(entry_path);
69 if (entry_it == entries_.end()) 69 if (entry_it == entries_.end())
70 return false; 70 return false;
71 71
72 *fake_entry = entry_it->second; 72 *fake_entry = entry_it->second;
73 return true; 73 return true;
74 } 74 }
75 75
76 void FakeProvidedFileSystem::RequestUnmount( 76 ProvidedFileSystemInterface::AbortCallback
77 FakeProvidedFileSystem::RequestUnmount(
77 const fileapi::AsyncFileUtil::StatusCallback& callback) { 78 const fileapi::AsyncFileUtil::StatusCallback& callback) {
78 base::MessageLoopProxy::current()->PostTask( 79 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
79 FROM_HERE, base::Bind(callback, base::File::FILE_OK));
80 } 80 }
81 81
82 void FakeProvidedFileSystem::GetMetadata( 82 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::GetMetadata(
83 const base::FilePath& entry_path, 83 const base::FilePath& entry_path,
84 const ProvidedFileSystemInterface::GetMetadataCallback& callback) { 84 const ProvidedFileSystemInterface::GetMetadataCallback& callback) {
85 const Entries::const_iterator entry_it = entries_.find(entry_path); 85 const Entries::const_iterator entry_it = entries_.find(entry_path);
86 86
87 if (entry_it == entries_.end()) { 87 if (entry_it == entries_.end()) {
88 base::MessageLoopProxy::current()->PostTask( 88 return PostAbortableTask(base::Bind(
89 FROM_HERE, 89 callback, EntryMetadata(), base::File::FILE_ERROR_NOT_FOUND));
90 base::Bind(
91 callback, EntryMetadata(), base::File::FILE_ERROR_NOT_FOUND));
92 return;
93 } 90 }
94 91
95 base::MessageLoopProxy::current()->PostTask( 92 return PostAbortableTask(
96 FROM_HERE,
97 base::Bind(callback, entry_it->second.metadata, base::File::FILE_OK)); 93 base::Bind(callback, entry_it->second.metadata, base::File::FILE_OK));
98 } 94 }
99 95
100 void FakeProvidedFileSystem::ReadDirectory( 96 ProvidedFileSystemInterface::AbortCallback
97 FakeProvidedFileSystem::ReadDirectory(
101 const base::FilePath& directory_path, 98 const base::FilePath& directory_path,
102 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) { 99 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) {
103 fileapi::AsyncFileUtil::EntryList entry_list; 100 fileapi::AsyncFileUtil::EntryList entry_list;
104 101
105 for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); 102 for (Entries::const_iterator it = entries_.begin(); it != entries_.end();
106 ++it) { 103 ++it) {
107 const base::FilePath file_path = it->first; 104 const base::FilePath file_path = it->first;
108 if (file_path == directory_path || directory_path.IsParent(file_path)) { 105 if (file_path == directory_path || directory_path.IsParent(file_path)) {
109 const EntryMetadata& metadata = it->second.metadata; 106 const EntryMetadata& metadata = it->second.metadata;
110 entry_list.push_back(fileapi::DirectoryEntry( 107 entry_list.push_back(fileapi::DirectoryEntry(
111 metadata.name, 108 metadata.name,
112 metadata.is_directory ? fileapi::DirectoryEntry::DIRECTORY 109 metadata.is_directory ? fileapi::DirectoryEntry::DIRECTORY
113 : fileapi::DirectoryEntry::FILE, 110 : fileapi::DirectoryEntry::FILE,
114 metadata.size, 111 metadata.size,
115 metadata.modification_time)); 112 metadata.modification_time));
116 } 113 }
117 } 114 }
118 115
119 base::MessageLoopProxy::current()->PostTask( 116 return PostAbortableTask(base::Bind(
120 FROM_HERE, 117 callback, base::File::FILE_OK, entry_list, false /* has_more */));
121 base::Bind(
122 callback, base::File::FILE_OK, entry_list, false /* has_more */));
123 } 118 }
124 119
125 void FakeProvidedFileSystem::OpenFile(const base::FilePath& entry_path, 120 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::OpenFile(
126 OpenFileMode mode, 121 const base::FilePath& entry_path,
127 const OpenFileCallback& callback) { 122 OpenFileMode mode,
123 const OpenFileCallback& callback) {
128 const Entries::const_iterator entry_it = entries_.find(entry_path); 124 const Entries::const_iterator entry_it = entries_.find(entry_path);
129 125
130 if (entry_it == entries_.end()) { 126 if (entry_it == entries_.end()) {
131 base::MessageLoopProxy::current()->PostTask( 127 return PostAbortableTask(base::Bind(
132 FROM_HERE, 128 callback, 0 /* file_handle */, base::File::FILE_ERROR_NOT_FOUND));
133 base::Bind(
134 callback, 0 /* file_handle */, base::File::FILE_ERROR_NOT_FOUND));
135 return;
136 } 129 }
137 130
138 const int file_handle = ++last_file_handle_; 131 const int file_handle = ++last_file_handle_;
139 opened_files_[file_handle] = entry_path; 132 opened_files_[file_handle] = entry_path;
140 base::MessageLoopProxy::current()->PostTask( 133 return PostAbortableTask(
141 FROM_HERE, base::Bind(callback, file_handle, base::File::FILE_OK)); 134 base::Bind(callback, file_handle, base::File::FILE_OK));
142 } 135 }
143 136
144 void FakeProvidedFileSystem::CloseFile( 137 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CloseFile(
145 int file_handle, 138 int file_handle,
146 const fileapi::AsyncFileUtil::StatusCallback& callback) { 139 const fileapi::AsyncFileUtil::StatusCallback& callback) {
147 const OpenedFilesMap::iterator opened_file_it = 140 const OpenedFilesMap::iterator opened_file_it =
148 opened_files_.find(file_handle); 141 opened_files_.find(file_handle);
149 142
150 if (opened_file_it == opened_files_.end()) { 143 if (opened_file_it == opened_files_.end()) {
151 base::MessageLoopProxy::current()->PostTask( 144 return PostAbortableTask(
152 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); 145 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND));
153 return;
154 } 146 }
155 147
156 opened_files_.erase(opened_file_it); 148 opened_files_.erase(opened_file_it);
157 base::MessageLoopProxy::current()->PostTask( 149 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
158 FROM_HERE, base::Bind(callback, base::File::FILE_OK));
159 } 150 }
160 151
161 void FakeProvidedFileSystem::ReadFile( 152 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::ReadFile(
162 int file_handle, 153 int file_handle,
163 net::IOBuffer* buffer, 154 net::IOBuffer* buffer,
164 int64 offset, 155 int64 offset,
165 int length, 156 int length,
166 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { 157 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) {
167 const OpenedFilesMap::iterator opened_file_it = 158 const OpenedFilesMap::iterator opened_file_it =
168 opened_files_.find(file_handle); 159 opened_files_.find(file_handle);
169 160
170 if (opened_file_it == opened_files_.end() || 161 if (opened_file_it == opened_files_.end() ||
171 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { 162 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) {
172 base::MessageLoopProxy::current()->PostTask( 163 return PostAbortableTask(
173 FROM_HERE,
174 base::Bind(callback, 164 base::Bind(callback,
175 0 /* chunk_length */, 165 0 /* chunk_length */,
176 false /* has_more */, 166 false /* has_more */,
177 base::File::FILE_ERROR_INVALID_OPERATION)); 167 base::File::FILE_ERROR_INVALID_OPERATION));
178 return;
179 } 168 }
180 169
181 const Entries::const_iterator entry_it = 170 const Entries::const_iterator entry_it =
182 entries_.find(opened_file_it->second); 171 entries_.find(opened_file_it->second);
183 if (entry_it == entries_.end()) { 172 if (entry_it == entries_.end()) {
184 base::MessageLoopProxy::current()->PostTask( 173 return PostAbortableTask(
185 FROM_HERE,
186 base::Bind(callback, 174 base::Bind(callback,
187 0 /* chunk_length */, 175 0 /* chunk_length */,
188 false /* has_more */, 176 false /* has_more */,
189 base::File::FILE_ERROR_INVALID_OPERATION)); 177 base::File::FILE_ERROR_INVALID_OPERATION));
190 return;
191 } 178 }
192 179
193 // Send the response byte by byte. 180 // Send the response byte by byte.
194 int64 current_offset = offset; 181 int64 current_offset = offset;
195 int current_length = length; 182 int current_length = length;
196 183
197 // Reading behind EOF is fine, it will just return 0 bytes. 184 // Reading behind EOF is fine, it will just return 0 bytes.
198 if (current_offset >= entry_it->second.metadata.size || !current_length) { 185 if (current_offset >= entry_it->second.metadata.size || !current_length) {
199 base::MessageLoopProxy::current()->PostTask( 186 return PostAbortableTask(base::Bind(callback,
200 FROM_HERE, 187 0 /* chunk_length */,
201 base::Bind(callback, 188 false /* has_more */,
202 0 /* chunk_length */, 189 base::File::FILE_OK));
203 false /* has_more */,
204 base::File::FILE_OK));
205 } 190 }
206 191
207 const FakeEntry& entry = entry_it->second; 192 const FakeEntry& entry = entry_it->second;
193 std::vector<int> task_ids;
208 while (current_offset < entry.metadata.size && current_length) { 194 while (current_offset < entry.metadata.size && current_length) {
209 buffer->data()[current_offset - offset] = entry.contents[current_offset]; 195 buffer->data()[current_offset - offset] = entry.contents[current_offset];
210 const bool has_more = 196 const bool has_more =
211 (current_offset + 1 < entry.metadata.size) && (current_length - 1); 197 (current_offset + 1 < entry.metadata.size) && (current_length - 1);
212 base::MessageLoopProxy::current()->PostTask( 198 const int task_id = tracker_.PostTask(
199 base::MessageLoopProxy::current(),
213 FROM_HERE, 200 FROM_HERE,
214 base::Bind( 201 base::Bind(
215 callback, 1 /* chunk_length */, has_more, base::File::FILE_OK)); 202 callback, 1 /* chunk_length */, has_more, base::File::FILE_OK));
203 task_ids.push_back(task_id);
216 current_offset++; 204 current_offset++;
217 current_length--; 205 current_length--;
218 } 206 }
207
208 return base::Bind(&FakeProvidedFileSystem::AbortMany,
209 weak_ptr_factory_.GetWeakPtr(),
210 task_ids);
219 } 211 }
220 212
221 void FakeProvidedFileSystem::CreateDirectory( 213 ProvidedFileSystemInterface::AbortCallback
214 FakeProvidedFileSystem::CreateDirectory(
222 const base::FilePath& directory_path, 215 const base::FilePath& directory_path,
223 bool exclusive, 216 bool exclusive,
224 bool recursive, 217 bool recursive,
225 const fileapi::AsyncFileUtil::StatusCallback& callback) { 218 const fileapi::AsyncFileUtil::StatusCallback& callback) {
226 // TODO(mtomasz): Implement it once needed. 219 // TODO(mtomasz): Implement it once needed.
227 base::MessageLoopProxy::current()->PostTask( 220 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
228 FROM_HERE, base::Bind(callback, base::File::FILE_OK));
229 } 221 }
230 222
231 void FakeProvidedFileSystem::DeleteEntry( 223 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::DeleteEntry(
232 const base::FilePath& entry_path, 224 const base::FilePath& entry_path,
233 bool recursive, 225 bool recursive,
234 const fileapi::AsyncFileUtil::StatusCallback& callback) { 226 const fileapi::AsyncFileUtil::StatusCallback& callback) {
235 // TODO(mtomasz): Implement it once needed. 227 // TODO(mtomasz): Implement it once needed.
236 base::MessageLoopProxy::current()->PostTask( 228 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
237 FROM_HERE, base::Bind(callback, base::File::FILE_OK));
238 } 229 }
239 230
240 void FakeProvidedFileSystem::CreateFile( 231 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CreateFile(
241 const base::FilePath& file_path, 232 const base::FilePath& file_path,
242 const fileapi::AsyncFileUtil::StatusCallback& callback) { 233 const fileapi::AsyncFileUtil::StatusCallback& callback) {
243 const base::File::Error result = file_path.AsUTF8Unsafe() != kFakeFilePath 234 const base::File::Error result = file_path.AsUTF8Unsafe() != kFakeFilePath
244 ? base::File::FILE_ERROR_EXISTS 235 ? base::File::FILE_ERROR_EXISTS
245 : base::File::FILE_OK; 236 : base::File::FILE_OK;
246 237
247 base::MessageLoopProxy::current()->PostTask(FROM_HERE, 238 return PostAbortableTask(base::Bind(callback, result));
248 base::Bind(callback, result));
249 } 239 }
250 240
251 void FakeProvidedFileSystem::CopyEntry( 241 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CopyEntry(
252 const base::FilePath& source_path, 242 const base::FilePath& source_path,
253 const base::FilePath& target_path, 243 const base::FilePath& target_path,
254 const fileapi::AsyncFileUtil::StatusCallback& callback) { 244 const fileapi::AsyncFileUtil::StatusCallback& callback) {
255 base::MessageLoopProxy::current()->PostTask( 245 // TODO(mtomasz): Implement it once needed.
256 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); 246 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
257 } 247 }
258 248
259 void FakeProvidedFileSystem::MoveEntry( 249 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::MoveEntry(
260 const base::FilePath& source_path, 250 const base::FilePath& source_path,
261 const base::FilePath& target_path, 251 const base::FilePath& target_path,
262 const fileapi::AsyncFileUtil::StatusCallback& callback) { 252 const fileapi::AsyncFileUtil::StatusCallback& callback) {
263 base::MessageLoopProxy::current()->PostTask( 253 // TODO(mtomasz): Implement it once needed.
264 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); 254 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
265 } 255 }
266 256
267 void FakeProvidedFileSystem::Truncate( 257 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::Truncate(
268 const base::FilePath& file_path, 258 const base::FilePath& file_path,
269 int64 length, 259 int64 length,
270 const fileapi::AsyncFileUtil::StatusCallback& callback) { 260 const fileapi::AsyncFileUtil::StatusCallback& callback) {
271 base::MessageLoopProxy::current()->PostTask( 261 // TODO(mtomasz): Implement it once needed.
272 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); 262 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
273 } 263 }
274 264
275 void FakeProvidedFileSystem::WriteFile( 265 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::WriteFile(
276 int file_handle, 266 int file_handle,
277 net::IOBuffer* buffer, 267 net::IOBuffer* buffer,
278 int64 offset, 268 int64 offset,
279 int length, 269 int length,
280 const fileapi::AsyncFileUtil::StatusCallback& callback) { 270 const fileapi::AsyncFileUtil::StatusCallback& callback) {
281 const OpenedFilesMap::iterator opened_file_it = 271 const OpenedFilesMap::iterator opened_file_it =
282 opened_files_.find(file_handle); 272 opened_files_.find(file_handle);
283 273
284 if (opened_file_it == opened_files_.end() || 274 if (opened_file_it == opened_files_.end() ||
285 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { 275 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) {
286 base::MessageLoopProxy::current()->PostTask( 276 return PostAbortableTask(
287 FROM_HERE,
288 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); 277 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION));
289 return;
290 } 278 }
291 279
292 const Entries::iterator entry_it = entries_.find(opened_file_it->second); 280 const Entries::iterator entry_it = entries_.find(opened_file_it->second);
293 if (entry_it == entries_.end()) { 281 if (entry_it == entries_.end()) {
294 base::MessageLoopProxy::current()->PostTask( 282 return PostAbortableTask(
295 FROM_HERE,
296 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); 283 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION));
297 return;
298 } 284 }
299 285
300 FakeEntry* const entry = &entry_it->second; 286 FakeEntry* const entry = &entry_it->second;
301 if (offset > entry->metadata.size) { 287 if (offset > entry->metadata.size) {
302 base::MessageLoopProxy::current()->PostTask( 288 return PostAbortableTask(
303 FROM_HERE,
304 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); 289 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION));
305 return;
306 } 290 }
307 291
308 // Allocate the string size in advance. 292 // Allocate the string size in advance.
309 if (offset + length > entry->metadata.size) { 293 if (offset + length > entry->metadata.size) {
310 entry->metadata.size = offset + length; 294 entry->metadata.size = offset + length;
311 entry->contents.resize(entry->metadata.size); 295 entry->contents.resize(entry->metadata.size);
312 } 296 }
313 297
314 entry->contents.replace(offset, length, buffer->data(), length); 298 entry->contents.replace(offset, length, buffer->data(), length);
315 299
316 base::MessageLoopProxy::current()->PostTask( 300 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK));
317 FROM_HERE, base::Bind(callback, base::File::FILE_OK));
318 } 301 }
319 302
320 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo() 303 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo()
321 const { 304 const {
322 return file_system_info_; 305 return file_system_info_;
323 } 306 }
324 307
325 RequestManager* FakeProvidedFileSystem::GetRequestManager() { 308 RequestManager* FakeProvidedFileSystem::GetRequestManager() {
326 NOTREACHED(); 309 NOTREACHED();
327 return NULL; 310 return NULL;
328 } 311 }
329 312
330 ProvidedFileSystemInterface* FakeProvidedFileSystem::Create( 313 ProvidedFileSystemInterface* FakeProvidedFileSystem::Create(
331 Profile* profile, 314 Profile* profile,
332 const ProvidedFileSystemInfo& file_system_info) { 315 const ProvidedFileSystemInfo& file_system_info) {
333 return new FakeProvidedFileSystem(file_system_info); 316 return new FakeProvidedFileSystem(file_system_info);
334 } 317 }
335 318
336 base::WeakPtr<ProvidedFileSystemInterface> 319 base::WeakPtr<ProvidedFileSystemInterface>
337 FakeProvidedFileSystem::GetWeakPtr() { 320 FakeProvidedFileSystem::GetWeakPtr() {
338 return weak_ptr_factory_.GetWeakPtr(); 321 return weak_ptr_factory_.GetWeakPtr();
339 } 322 }
340 323
324 ProvidedFileSystemInterface::AbortCallback
325 FakeProvidedFileSystem::PostAbortableTask(const base::Closure& callback) {
326 const int task_id =
327 tracker_.PostTask(base::MessageLoopProxy::current(), FROM_HERE, callback);
328 return base::Bind(
329 &FakeProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), task_id);
330 }
331
332 void FakeProvidedFileSystem::Abort(
333 int task_id,
334 const fileapi::AsyncFileUtil::StatusCallback& callback) {
335 tracker_.TryCancel(task_id);
336 callback.Run(base::File::FILE_OK);
337 }
338
339 void FakeProvidedFileSystem::AbortMany(
340 const std::vector<int>& task_ids,
341 const fileapi::AsyncFileUtil::StatusCallback& callback) {
342 for (size_t i = 0; i < task_ids.size(); ++i) {
343 tracker_.TryCancel(task_ids[i]);
344 }
345 callback.Run(base::File::FILE_OK);
346 }
347
341 } // namespace file_system_provider 348 } // namespace file_system_provider
342 } // namespace chromeos 349 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698