OLD | NEW |
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/message_loop/message_loop_proxy.h" | 7 #include "base/message_loop/message_loop_proxy.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 | 9 |
10 namespace chromeos { | 10 namespace chromeos { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 const FakeEntry* FakeProvidedFileSystem::GetEntry( | 73 const FakeEntry* FakeProvidedFileSystem::GetEntry( |
74 const base::FilePath& entry_path) const { | 74 const base::FilePath& entry_path) const { |
75 const Entries::const_iterator entry_it = entries_.find(entry_path); | 75 const Entries::const_iterator entry_it = entries_.find(entry_path); |
76 if (entry_it == entries_.end()) | 76 if (entry_it == entries_.end()) |
77 return NULL; | 77 return NULL; |
78 | 78 |
79 return entry_it->second.get(); | 79 return entry_it->second.get(); |
80 } | 80 } |
81 | 81 |
82 ProvidedFileSystemInterface::AbortCallback | 82 AbortCallback FakeProvidedFileSystem::RequestUnmount( |
83 FakeProvidedFileSystem::RequestUnmount( | |
84 const storage::AsyncFileUtil::StatusCallback& callback) { | 83 const storage::AsyncFileUtil::StatusCallback& callback) { |
85 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 84 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
86 } | 85 } |
87 | 86 |
88 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::GetMetadata( | 87 AbortCallback FakeProvidedFileSystem::GetMetadata( |
89 const base::FilePath& entry_path, | 88 const base::FilePath& entry_path, |
90 ProvidedFileSystemInterface::MetadataFieldMask fields, | 89 ProvidedFileSystemInterface::MetadataFieldMask fields, |
91 const ProvidedFileSystemInterface::GetMetadataCallback& callback) { | 90 const ProvidedFileSystemInterface::GetMetadataCallback& callback) { |
92 const Entries::const_iterator entry_it = entries_.find(entry_path); | 91 const Entries::const_iterator entry_it = entries_.find(entry_path); |
93 | 92 |
94 if (entry_it == entries_.end()) { | 93 if (entry_it == entries_.end()) { |
95 return PostAbortableTask( | 94 return PostAbortableTask( |
96 base::Bind(callback, | 95 base::Bind(callback, |
97 base::Passed(make_scoped_ptr<EntryMetadata>(NULL)), | 96 base::Passed(make_scoped_ptr<EntryMetadata>(NULL)), |
98 base::File::FILE_ERROR_NOT_FOUND)); | 97 base::File::FILE_ERROR_NOT_FOUND)); |
99 } | 98 } |
100 | 99 |
101 scoped_ptr<EntryMetadata> metadata(new EntryMetadata); | 100 scoped_ptr<EntryMetadata> metadata(new EntryMetadata); |
102 metadata->is_directory = entry_it->second->metadata->is_directory; | 101 metadata->is_directory = entry_it->second->metadata->is_directory; |
103 metadata->name = entry_it->second->metadata->name; | 102 metadata->name = entry_it->second->metadata->name; |
104 metadata->size = entry_it->second->metadata->size; | 103 metadata->size = entry_it->second->metadata->size; |
105 metadata->modification_time = entry_it->second->metadata->modification_time; | 104 metadata->modification_time = entry_it->second->metadata->modification_time; |
106 metadata->mime_type = entry_it->second->metadata->mime_type; | 105 metadata->mime_type = entry_it->second->metadata->mime_type; |
107 metadata->thumbnail = entry_it->second->metadata->thumbnail; | 106 metadata->thumbnail = entry_it->second->metadata->thumbnail; |
108 | 107 |
109 return PostAbortableTask( | 108 return PostAbortableTask( |
110 base::Bind(callback, base::Passed(&metadata), base::File::FILE_OK)); | 109 base::Bind(callback, base::Passed(&metadata), base::File::FILE_OK)); |
111 } | 110 } |
112 | 111 |
113 ProvidedFileSystemInterface::AbortCallback | 112 AbortCallback FakeProvidedFileSystem::ReadDirectory( |
114 FakeProvidedFileSystem::ReadDirectory( | |
115 const base::FilePath& directory_path, | 113 const base::FilePath& directory_path, |
116 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { | 114 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { |
117 storage::AsyncFileUtil::EntryList entry_list; | 115 storage::AsyncFileUtil::EntryList entry_list; |
118 | 116 |
119 for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); | 117 for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); |
120 ++it) { | 118 ++it) { |
121 const base::FilePath file_path = it->first; | 119 const base::FilePath file_path = it->first; |
122 if (file_path == directory_path || directory_path.IsParent(file_path)) { | 120 if (file_path == directory_path || directory_path.IsParent(file_path)) { |
123 const EntryMetadata* const metadata = it->second->metadata.get(); | 121 const EntryMetadata* const metadata = it->second->metadata.get(); |
124 entry_list.push_back(storage::DirectoryEntry( | 122 entry_list.push_back(storage::DirectoryEntry( |
125 metadata->name, | 123 metadata->name, |
126 metadata->is_directory ? storage::DirectoryEntry::DIRECTORY | 124 metadata->is_directory ? storage::DirectoryEntry::DIRECTORY |
127 : storage::DirectoryEntry::FILE, | 125 : storage::DirectoryEntry::FILE, |
128 metadata->size, | 126 metadata->size, |
129 metadata->modification_time)); | 127 metadata->modification_time)); |
130 } | 128 } |
131 } | 129 } |
132 | 130 |
133 return PostAbortableTask(base::Bind( | 131 return PostAbortableTask(base::Bind( |
134 callback, base::File::FILE_OK, entry_list, false /* has_more */)); | 132 callback, base::File::FILE_OK, entry_list, false /* has_more */)); |
135 } | 133 } |
136 | 134 |
137 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::OpenFile( | 135 AbortCallback FakeProvidedFileSystem::OpenFile( |
138 const base::FilePath& entry_path, | 136 const base::FilePath& entry_path, |
139 OpenFileMode mode, | 137 OpenFileMode mode, |
140 const OpenFileCallback& callback) { | 138 const OpenFileCallback& callback) { |
141 const Entries::const_iterator entry_it = entries_.find(entry_path); | 139 const Entries::const_iterator entry_it = entries_.find(entry_path); |
142 | 140 |
143 if (entry_it == entries_.end()) { | 141 if (entry_it == entries_.end()) { |
144 return PostAbortableTask(base::Bind( | 142 return PostAbortableTask(base::Bind( |
145 callback, 0 /* file_handle */, base::File::FILE_ERROR_NOT_FOUND)); | 143 callback, 0 /* file_handle */, base::File::FILE_ERROR_NOT_FOUND)); |
146 } | 144 } |
147 | 145 |
148 const int file_handle = ++last_file_handle_; | 146 const int file_handle = ++last_file_handle_; |
149 opened_files_[file_handle] = entry_path; | 147 opened_files_[file_handle] = entry_path; |
150 return PostAbortableTask( | 148 return PostAbortableTask( |
151 base::Bind(callback, file_handle, base::File::FILE_OK)); | 149 base::Bind(callback, file_handle, base::File::FILE_OK)); |
152 } | 150 } |
153 | 151 |
154 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CloseFile( | 152 AbortCallback FakeProvidedFileSystem::CloseFile( |
155 int file_handle, | 153 int file_handle, |
156 const storage::AsyncFileUtil::StatusCallback& callback) { | 154 const storage::AsyncFileUtil::StatusCallback& callback) { |
157 const OpenedFilesMap::iterator opened_file_it = | 155 const OpenedFilesMap::iterator opened_file_it = |
158 opened_files_.find(file_handle); | 156 opened_files_.find(file_handle); |
159 | 157 |
160 if (opened_file_it == opened_files_.end()) { | 158 if (opened_file_it == opened_files_.end()) { |
161 return PostAbortableTask( | 159 return PostAbortableTask( |
162 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | 160 base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
163 } | 161 } |
164 | 162 |
165 opened_files_.erase(opened_file_it); | 163 opened_files_.erase(opened_file_it); |
166 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 164 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
167 } | 165 } |
168 | 166 |
169 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::ReadFile( | 167 AbortCallback FakeProvidedFileSystem::ReadFile( |
170 int file_handle, | 168 int file_handle, |
171 net::IOBuffer* buffer, | 169 net::IOBuffer* buffer, |
172 int64 offset, | 170 int64 offset, |
173 int length, | 171 int length, |
174 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { | 172 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { |
175 const OpenedFilesMap::iterator opened_file_it = | 173 const OpenedFilesMap::iterator opened_file_it = |
176 opened_files_.find(file_handle); | 174 opened_files_.find(file_handle); |
177 | 175 |
178 if (opened_file_it == opened_files_.end() || | 176 if (opened_file_it == opened_files_.end() || |
179 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { | 177 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 task_ids.push_back(task_id); | 218 task_ids.push_back(task_id); |
221 current_offset++; | 219 current_offset++; |
222 current_length--; | 220 current_length--; |
223 } | 221 } |
224 | 222 |
225 return base::Bind(&FakeProvidedFileSystem::AbortMany, | 223 return base::Bind(&FakeProvidedFileSystem::AbortMany, |
226 weak_ptr_factory_.GetWeakPtr(), | 224 weak_ptr_factory_.GetWeakPtr(), |
227 task_ids); | 225 task_ids); |
228 } | 226 } |
229 | 227 |
230 ProvidedFileSystemInterface::AbortCallback | 228 AbortCallback FakeProvidedFileSystem::CreateDirectory( |
231 FakeProvidedFileSystem::CreateDirectory( | |
232 const base::FilePath& directory_path, | 229 const base::FilePath& directory_path, |
233 bool recursive, | 230 bool recursive, |
234 const storage::AsyncFileUtil::StatusCallback& callback) { | 231 const storage::AsyncFileUtil::StatusCallback& callback) { |
235 // TODO(mtomasz): Implement it once needed. | 232 // TODO(mtomasz): Implement it once needed. |
236 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 233 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
237 } | 234 } |
238 | 235 |
239 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::DeleteEntry( | 236 AbortCallback FakeProvidedFileSystem::DeleteEntry( |
240 const base::FilePath& entry_path, | 237 const base::FilePath& entry_path, |
241 bool recursive, | 238 bool recursive, |
242 const storage::AsyncFileUtil::StatusCallback& callback) { | 239 const storage::AsyncFileUtil::StatusCallback& callback) { |
243 // TODO(mtomasz): Implement it once needed. | 240 // TODO(mtomasz): Implement it once needed. |
244 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 241 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
245 } | 242 } |
246 | 243 |
247 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CreateFile( | 244 AbortCallback FakeProvidedFileSystem::CreateFile( |
248 const base::FilePath& file_path, | 245 const base::FilePath& file_path, |
249 const storage::AsyncFileUtil::StatusCallback& callback) { | 246 const storage::AsyncFileUtil::StatusCallback& callback) { |
250 const base::File::Error result = file_path.AsUTF8Unsafe() != kFakeFilePath | 247 const base::File::Error result = file_path.AsUTF8Unsafe() != kFakeFilePath |
251 ? base::File::FILE_ERROR_EXISTS | 248 ? base::File::FILE_ERROR_EXISTS |
252 : base::File::FILE_OK; | 249 : base::File::FILE_OK; |
253 | 250 |
254 return PostAbortableTask(base::Bind(callback, result)); | 251 return PostAbortableTask(base::Bind(callback, result)); |
255 } | 252 } |
256 | 253 |
257 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::CopyEntry( | 254 AbortCallback FakeProvidedFileSystem::CopyEntry( |
258 const base::FilePath& source_path, | 255 const base::FilePath& source_path, |
259 const base::FilePath& target_path, | 256 const base::FilePath& target_path, |
260 const storage::AsyncFileUtil::StatusCallback& callback) { | 257 const storage::AsyncFileUtil::StatusCallback& callback) { |
261 // TODO(mtomasz): Implement it once needed. | 258 // TODO(mtomasz): Implement it once needed. |
262 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 259 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
263 } | 260 } |
264 | 261 |
265 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::MoveEntry( | 262 AbortCallback FakeProvidedFileSystem::MoveEntry( |
266 const base::FilePath& source_path, | 263 const base::FilePath& source_path, |
267 const base::FilePath& target_path, | 264 const base::FilePath& target_path, |
268 const storage::AsyncFileUtil::StatusCallback& callback) { | 265 const storage::AsyncFileUtil::StatusCallback& callback) { |
269 // TODO(mtomasz): Implement it once needed. | 266 // TODO(mtomasz): Implement it once needed. |
270 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 267 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
271 } | 268 } |
272 | 269 |
273 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::Truncate( | 270 AbortCallback FakeProvidedFileSystem::Truncate( |
274 const base::FilePath& file_path, | 271 const base::FilePath& file_path, |
275 int64 length, | 272 int64 length, |
276 const storage::AsyncFileUtil::StatusCallback& callback) { | 273 const storage::AsyncFileUtil::StatusCallback& callback) { |
277 // TODO(mtomasz): Implement it once needed. | 274 // TODO(mtomasz): Implement it once needed. |
278 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 275 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
279 } | 276 } |
280 | 277 |
281 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::WriteFile( | 278 AbortCallback FakeProvidedFileSystem::WriteFile( |
282 int file_handle, | 279 int file_handle, |
283 net::IOBuffer* buffer, | 280 net::IOBuffer* buffer, |
284 int64 offset, | 281 int64 offset, |
285 int length, | 282 int length, |
286 const storage::AsyncFileUtil::StatusCallback& callback) { | 283 const storage::AsyncFileUtil::StatusCallback& callback) { |
287 const OpenedFilesMap::iterator opened_file_it = | 284 const OpenedFilesMap::iterator opened_file_it = |
288 opened_files_.find(file_handle); | 285 opened_files_.find(file_handle); |
289 | 286 |
290 if (opened_file_it == opened_files_.end() || | 287 if (opened_file_it == opened_files_.end() || |
291 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { | 288 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { |
(...skipping 17 matching lines...) Expand all Loading... |
309 if (offset + length > entry->metadata->size) { | 306 if (offset + length > entry->metadata->size) { |
310 entry->metadata->size = offset + length; | 307 entry->metadata->size = offset + length; |
311 entry->contents.resize(entry->metadata->size); | 308 entry->contents.resize(entry->metadata->size); |
312 } | 309 } |
313 | 310 |
314 entry->contents.replace(offset, length, buffer->data(), length); | 311 entry->contents.replace(offset, length, buffer->data(), length); |
315 | 312 |
316 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 313 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
317 } | 314 } |
318 | 315 |
319 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::AddWatcher( | 316 AbortCallback FakeProvidedFileSystem::AddWatcher( |
320 const GURL& origin, | 317 const GURL& origin, |
321 const base::FilePath& entry_watcher, | 318 const base::FilePath& entry_watcher, |
322 bool recursive, | 319 bool recursive, |
323 bool persistent, | 320 bool persistent, |
324 const storage::AsyncFileUtil::StatusCallback& callback, | 321 const storage::AsyncFileUtil::StatusCallback& callback, |
325 const storage::WatcherManager::NotificationCallback& | 322 const storage::WatcherManager::NotificationCallback& |
326 notification_callback) { | 323 notification_callback) { |
327 // TODO(mtomasz): Implement it once needed. | 324 // TODO(mtomasz): Implement it once needed. |
328 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 325 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
329 } | 326 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 Profile* profile, | 374 Profile* profile, |
378 const ProvidedFileSystemInfo& file_system_info) { | 375 const ProvidedFileSystemInfo& file_system_info) { |
379 return new FakeProvidedFileSystem(file_system_info); | 376 return new FakeProvidedFileSystem(file_system_info); |
380 } | 377 } |
381 | 378 |
382 base::WeakPtr<ProvidedFileSystemInterface> | 379 base::WeakPtr<ProvidedFileSystemInterface> |
383 FakeProvidedFileSystem::GetWeakPtr() { | 380 FakeProvidedFileSystem::GetWeakPtr() { |
384 return weak_ptr_factory_.GetWeakPtr(); | 381 return weak_ptr_factory_.GetWeakPtr(); |
385 } | 382 } |
386 | 383 |
387 ProvidedFileSystemInterface::AbortCallback | 384 AbortCallback FakeProvidedFileSystem::PostAbortableTask( |
388 FakeProvidedFileSystem::PostAbortableTask(const base::Closure& callback) { | 385 const base::Closure& callback) { |
389 const int task_id = tracker_.PostTask( | 386 const int task_id = tracker_.PostTask( |
390 base::MessageLoopProxy::current().get(), FROM_HERE, callback); | 387 base::MessageLoopProxy::current().get(), FROM_HERE, callback); |
391 return base::Bind( | 388 return base::Bind( |
392 &FakeProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), task_id); | 389 &FakeProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), task_id); |
393 } | 390 } |
394 | 391 |
395 void FakeProvidedFileSystem::Abort( | 392 void FakeProvidedFileSystem::Abort( |
396 int task_id, | 393 int task_id, |
397 const storage::AsyncFileUtil::StatusCallback& callback) { | 394 const storage::AsyncFileUtil::StatusCallback& callback) { |
398 tracker_.TryCancel(task_id); | 395 tracker_.TryCancel(task_id); |
399 callback.Run(base::File::FILE_OK); | 396 callback.Run(base::File::FILE_OK); |
400 } | 397 } |
401 | 398 |
402 void FakeProvidedFileSystem::AbortMany( | 399 void FakeProvidedFileSystem::AbortMany( |
403 const std::vector<int>& task_ids, | 400 const std::vector<int>& task_ids, |
404 const storage::AsyncFileUtil::StatusCallback& callback) { | 401 const storage::AsyncFileUtil::StatusCallback& callback) { |
405 for (size_t i = 0; i < task_ids.size(); ++i) { | 402 for (size_t i = 0; i < task_ids.size(); ++i) { |
406 tracker_.TryCancel(task_ids[i]); | 403 tracker_.TryCancel(task_ids[i]); |
407 } | 404 } |
408 callback.Run(base::File::FILE_OK); | 405 callback.Run(base::File::FILE_OK); |
409 } | 406 } |
410 | 407 |
411 } // namespace file_system_provider | 408 } // namespace file_system_provider |
412 } // namespace chromeos | 409 } // namespace chromeos |
OLD | NEW |