OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/drive/fake_drive_file_system.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/callback.h" | |
10 #include "base/file_util.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/logging.h" | |
13 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
14 #include "chrome/browser/chromeos/drive/file_errors.h" | |
15 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
16 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" | |
17 #include "chrome/browser/google_apis/drive_api_parser.h" | |
18 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 namespace drive { | |
22 namespace test_util { | |
23 | |
24 using content::BrowserThread; | |
25 | |
26 FakeDriveFileSystem::FakeDriveFileSystem( | |
27 google_apis::DriveServiceInterface* drive_service) | |
28 : drive_service_(drive_service), | |
29 weak_ptr_factory_(this) { | |
30 } | |
31 | |
32 FakeDriveFileSystem::~FakeDriveFileSystem() { | |
33 } | |
34 | |
35 bool FakeDriveFileSystem::InitializeForTesting() { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
37 return cache_dir_.CreateUniqueTempDir(); | |
38 } | |
39 | |
40 void FakeDriveFileSystem::Initialize() { | |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
42 InitializeForTesting(); | |
43 } | |
44 | |
45 void FakeDriveFileSystem::AddObserver(FileSystemObserver* observer) { | |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
47 } | |
48 | |
49 void FakeDriveFileSystem::RemoveObserver(FileSystemObserver* observer) { | |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
51 } | |
52 | |
53 void FakeDriveFileSystem::CheckForUpdates() { | |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
55 } | |
56 | |
57 void FakeDriveFileSystem::GetEntryInfoByResourceId( | |
58 const std::string& resource_id, | |
59 const GetEntryInfoWithFilePathCallback& callback) { | |
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
61 | |
62 drive_service_->GetResourceEntry( | |
63 resource_id, | |
64 base::Bind( | |
65 &FakeDriveFileSystem::GetEntryInfoByResourceIdAfterGetResourceEntry, | |
66 weak_ptr_factory_.GetWeakPtr(), callback)); | |
67 } | |
68 | |
69 void FakeDriveFileSystem::TransferFileFromRemoteToLocal( | |
70 const base::FilePath& remote_src_file_path, | |
71 const base::FilePath& local_dest_file_path, | |
72 const FileOperationCallback& callback) { | |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
74 } | |
75 | |
76 void FakeDriveFileSystem::TransferFileFromLocalToRemote( | |
77 const base::FilePath& local_src_file_path, | |
78 const base::FilePath& remote_dest_file_path, | |
79 const FileOperationCallback& callback) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
81 } | |
82 | |
83 void FakeDriveFileSystem::OpenFile(const base::FilePath& file_path, | |
84 const OpenFileCallback& callback) { | |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
86 } | |
87 | |
88 void FakeDriveFileSystem::CloseFile(const base::FilePath& file_path, | |
89 const FileOperationCallback& callback) { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
91 } | |
92 | |
93 void FakeDriveFileSystem::Copy(const base::FilePath& src_file_path, | |
94 const base::FilePath& dest_file_path, | |
95 const FileOperationCallback& callback) { | |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
97 } | |
98 | |
99 void FakeDriveFileSystem::Move(const base::FilePath& src_file_path, | |
100 const base::FilePath& dest_file_path, | |
101 const FileOperationCallback& callback) { | |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
103 } | |
104 | |
105 void FakeDriveFileSystem::Remove(const base::FilePath& file_path, | |
106 bool is_recursive, | |
107 const FileOperationCallback& callback) { | |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
109 } | |
110 | |
111 void FakeDriveFileSystem::CreateDirectory( | |
112 const base::FilePath& directory_path, | |
113 bool is_exclusive, | |
114 bool is_recursive, | |
115 const FileOperationCallback& callback) { | |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
117 } | |
118 | |
119 void FakeDriveFileSystem::CreateFile(const base::FilePath& file_path, | |
120 bool is_exclusive, | |
121 const FileOperationCallback& callback) { | |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
123 } | |
124 | |
125 void FakeDriveFileSystem::Pin(const base::FilePath& file_path, | |
126 const FileOperationCallback& callback) { | |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
128 } | |
129 | |
130 void FakeDriveFileSystem::Unpin(const base::FilePath& file_path, | |
131 const FileOperationCallback& callback) { | |
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
133 } | |
134 | |
135 void FakeDriveFileSystem::GetFileByPath(const base::FilePath& file_path, | |
136 const GetFileCallback& callback) { | |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
138 } | |
139 | |
140 void FakeDriveFileSystem::GetFileByResourceId( | |
141 const std::string& resource_id, | |
142 const DriveClientContext& context, | |
143 const GetFileCallback& get_file_callback, | |
144 const google_apis::GetContentCallback& get_content_callback) { | |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
146 } | |
147 | |
148 void FakeDriveFileSystem::GetFileContentByPath( | |
149 const base::FilePath& file_path, | |
150 const GetFileContentInitializedCallback& initialized_callback, | |
151 const google_apis::GetContentCallback& get_content_callback, | |
152 const FileOperationCallback& completion_callback) { | |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
154 | |
155 GetEntryInfoByPath( | |
156 file_path, | |
157 base::Bind(&FakeDriveFileSystem::GetFileContentByPathAfterGetEntryInfo, | |
158 weak_ptr_factory_.GetWeakPtr(), | |
159 file_path, initialized_callback, get_content_callback, | |
160 completion_callback)); | |
161 } | |
162 | |
163 void FakeDriveFileSystem::UpdateFileByResourceId( | |
164 const std::string& resource_id, | |
165 const DriveClientContext& context, | |
166 const FileOperationCallback& callback) { | |
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
168 } | |
169 | |
170 void FakeDriveFileSystem::GetEntryInfoByPath( | |
171 const base::FilePath& file_path, | |
172 const GetEntryInfoCallback& callback) { | |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
174 | |
175 // Now, we only support files under my drive. | |
176 DCHECK(!util::IsUnderDriveMountPoint(file_path)); | |
177 | |
178 if (file_path == util::GetDriveMyDriveRootPath()) { | |
179 // Specialized for the root entry. | |
180 drive_service_->GetAboutResource( | |
181 base::Bind( | |
182 &FakeDriveFileSystem::GetEntryInfoByPathAfterGetAboutResource, | |
183 weak_ptr_factory_.GetWeakPtr(), callback)); | |
184 return; | |
185 } | |
186 | |
187 GetEntryInfoByPath( | |
188 file_path.DirName(), | |
189 base::Bind( | |
190 &FakeDriveFileSystem::GetEntryInfoByPathAfterGetParentEntryInfo, | |
191 weak_ptr_factory_.GetWeakPtr(), file_path.BaseName(), callback)); | |
192 } | |
193 | |
194 void FakeDriveFileSystem::ReadDirectoryByPath( | |
195 const base::FilePath& file_path, | |
196 const ReadDirectoryWithSettingCallback& callback) { | |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
198 } | |
199 | |
200 void FakeDriveFileSystem::RefreshDirectory( | |
201 const base::FilePath& file_path, | |
202 const FileOperationCallback& callback) { | |
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
204 } | |
205 | |
206 void FakeDriveFileSystem::Search(const std::string& search_query, | |
207 const GURL& next_feed, | |
208 const SearchCallback& callback) { | |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
210 } | |
211 | |
212 void FakeDriveFileSystem::SearchMetadata( | |
213 const std::string& query, | |
214 int options, | |
215 int at_most_num_matches, | |
216 const SearchMetadataCallback& callback) { | |
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
218 } | |
219 | |
220 void FakeDriveFileSystem::GetAvailableSpace( | |
221 const GetAvailableSpaceCallback& callback) { | |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
223 } | |
224 | |
225 void FakeDriveFileSystem::AddUploadedFile( | |
226 scoped_ptr<google_apis::ResourceEntry> doc_entry, | |
227 const base::FilePath& file_content_path, | |
228 const FileOperationCallback& callback) { | |
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
230 } | |
231 | |
232 void FakeDriveFileSystem::GetMetadata( | |
233 const GetFilesystemMetadataCallback& callback) { | |
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
235 } | |
236 | |
237 void FakeDriveFileSystem::MarkCacheFileAsMounted( | |
238 const base::FilePath& drive_file_path, | |
239 const OpenFileCallback& callback) { | |
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
241 } | |
242 | |
243 void FakeDriveFileSystem::MarkCacheFileAsUnmounted( | |
244 const base::FilePath& cache_file_path, | |
245 const FileOperationCallback& callback) { | |
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
247 } | |
248 | |
249 void FakeDriveFileSystem::GetCacheEntryByResourceId( | |
250 const std::string& resource_id, | |
251 const std::string& md5, | |
252 const GetCacheEntryCallback& callback) { | |
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
254 } | |
255 | |
256 void FakeDriveFileSystem::IterateCache( | |
257 const CacheIterateCallback& iteration_callback, | |
258 const base::Closure& completion_callback) { | |
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
260 } | |
261 | |
262 void FakeDriveFileSystem::Reload() { | |
263 } | |
264 | |
265 // Implementation of GetFilePath. | |
266 void FakeDriveFileSystem::GetFilePath(const std::string& resource_id, | |
267 const GetFilePathCallback& callback) { | |
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
269 | |
270 drive_service_->GetAboutResource( | |
271 base::Bind( | |
272 &FakeDriveFileSystem::GetFilePathAfterGetAboutResource, | |
273 weak_ptr_factory_.GetWeakPtr(), resource_id, callback)); | |
274 } | |
275 | |
276 void FakeDriveFileSystem::GetFilePathAfterGetAboutResource( | |
277 const std::string& resource_id, | |
278 const GetFilePathCallback& callback, | |
279 google_apis::GDataErrorCode error, | |
280 scoped_ptr<google_apis::AboutResource> about_resource) { | |
281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
282 | |
283 // We assume the call always success for test. | |
284 DCHECK_EQ(util::GDataToFileError(error), FILE_ERROR_OK); | |
285 DCHECK(about_resource); | |
286 | |
287 GetFilePathInternal(about_resource->root_folder_id(), resource_id, | |
288 base::FilePath(), callback); | |
289 } | |
290 | |
291 void FakeDriveFileSystem::GetFilePathInternal( | |
292 const std::string& root_resource_id, | |
293 const std::string& resource_id, | |
294 const base::FilePath& file_path, | |
295 const GetFilePathCallback& callback) { | |
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
297 | |
298 if (resource_id == root_resource_id) { | |
299 // Reached to the root. Append the drive root path, and run |callback|. | |
300 callback.Run(util::GetDriveMyDriveRootPath().Append(file_path)); | |
301 return; | |
302 } | |
303 | |
304 drive_service_->GetResourceEntry( | |
305 resource_id, | |
306 base::Bind( | |
307 &FakeDriveFileSystem::GetFilePathAfterGetResourceEntry, | |
308 weak_ptr_factory_.GetWeakPtr(), | |
309 root_resource_id, file_path, callback)); | |
310 } | |
311 | |
312 void FakeDriveFileSystem::GetFilePathAfterGetResourceEntry( | |
313 const std::string& root_resource_id, | |
314 const base::FilePath& remaining_file_path, | |
315 const GetFilePathCallback& callback, | |
316 google_apis::GDataErrorCode error_in, | |
317 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | |
318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
319 | |
320 // We assume the call always success for test. | |
321 DCHECK_EQ(util::GDataToFileError(error_in), FILE_ERROR_OK); | |
322 DCHECK(resource_entry); | |
323 | |
324 DriveEntryProto entry_proto = | |
325 ConvertResourceEntryToDriveEntryProto(*resource_entry); | |
326 base::FilePath file_path = | |
327 base::FilePath::FromUTF8Unsafe(entry_proto.base_name()).Append( | |
328 remaining_file_path); | |
329 | |
330 GetFilePathInternal(root_resource_id, entry_proto.parent_resource_id(), | |
331 file_path, callback); | |
332 } | |
333 | |
334 // Implementation of GetEntryInfoByResourceId. | |
335 void FakeDriveFileSystem::GetEntryInfoByResourceIdAfterGetResourceEntry( | |
336 const GetEntryInfoWithFilePathCallback& callback, | |
337 google_apis::GDataErrorCode error_in, | |
338 scoped_ptr<google_apis::ResourceEntry> resource_entry) { | |
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
340 | |
341 FileError error = util::GDataToFileError(error_in); | |
342 if (error != FILE_ERROR_OK) { | |
343 callback.Run(error, base::FilePath(), scoped_ptr<DriveEntryProto>()); | |
344 return; | |
345 } | |
346 | |
347 DCHECK(resource_entry); | |
348 scoped_ptr<DriveEntryProto> entry_proto(new DriveEntryProto( | |
349 ConvertResourceEntryToDriveEntryProto(*resource_entry))); | |
350 | |
351 const std::string parent_resource_id = entry_proto->parent_resource_id(); | |
352 GetFilePath( | |
353 parent_resource_id, | |
354 base::Bind( | |
355 &FakeDriveFileSystem::GetEntryInfoByResourceIdAfterGetFilePath, | |
356 weak_ptr_factory_.GetWeakPtr(), | |
357 callback, error, base::Passed(&entry_proto))); | |
358 } | |
359 | |
360 void FakeDriveFileSystem::GetEntryInfoByResourceIdAfterGetFilePath( | |
361 const GetEntryInfoWithFilePathCallback& callback, | |
362 FileError error, | |
363 scoped_ptr<DriveEntryProto> entry_proto, | |
364 const base::FilePath& parent_file_path) { | |
365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
366 base::FilePath file_path = parent_file_path.Append( | |
367 base::FilePath::FromUTF8Unsafe(entry_proto->base_name())); | |
368 callback.Run(error, file_path, entry_proto.Pass()); | |
369 } | |
370 | |
371 // Implementation of GetFileContentByPath. | |
372 void FakeDriveFileSystem::GetFileContentByPathAfterGetEntryInfo( | |
373 const base::FilePath& file_path, | |
374 const GetFileContentInitializedCallback& initialized_callback, | |
375 const google_apis::GetContentCallback& get_content_callback, | |
376 const FileOperationCallback& completion_callback, | |
377 FileError error, | |
378 scoped_ptr<DriveEntryProto> entry_proto) { | |
379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
380 | |
381 if (error != FILE_ERROR_OK) { | |
382 completion_callback.Run(error); | |
383 return; | |
384 } | |
385 DCHECK(entry_proto); | |
386 | |
387 // We're only interested in a file. | |
388 if (entry_proto->file_info().is_directory()) { | |
389 completion_callback.Run(FILE_ERROR_NOT_A_FILE); | |
390 return; | |
391 } | |
392 | |
393 base::FilePath cache_path = | |
394 cache_dir_.path().AppendASCII(entry_proto->resource_id()); | |
395 if (file_util::PathExists(cache_path)) { | |
396 // Cache file is found. | |
397 initialized_callback.Run(FILE_ERROR_OK, entry_proto.Pass(), cache_path, | |
398 base::Closure()); | |
399 completion_callback.Run(FILE_ERROR_OK); | |
400 return; | |
401 } | |
402 | |
403 // Copy the URL here before passing |entry_proto| to the callback. | |
404 const GURL download_url(entry_proto->download_url()); | |
405 initialized_callback.Run(FILE_ERROR_OK, entry_proto.Pass(), base::FilePath(), | |
406 base::Bind(&base::DoNothing)); | |
407 drive_service_->DownloadFile( | |
408 file_path, | |
409 cache_path, | |
410 download_url, | |
411 base::Bind(&FakeDriveFileSystem::GetFileContentByPathAfterDownloadFile, | |
412 weak_ptr_factory_.GetWeakPtr(), | |
413 completion_callback), | |
414 get_content_callback, | |
415 google_apis::ProgressCallback()); | |
416 } | |
417 | |
418 void FakeDriveFileSystem::GetFileContentByPathAfterDownloadFile( | |
419 const FileOperationCallback& completion_callback, | |
420 google_apis::GDataErrorCode gdata_error, | |
421 const base::FilePath& temp_file) { | |
422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
423 completion_callback.Run(util::GDataToFileError(gdata_error)); | |
424 } | |
425 | |
426 // Implementation of GetEntryInfoByPath. | |
427 void FakeDriveFileSystem::GetEntryInfoByPathAfterGetAboutResource( | |
428 const GetEntryInfoCallback& callback, | |
429 google_apis::GDataErrorCode gdata_error, | |
430 scoped_ptr<google_apis::AboutResource> about_resource) { | |
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
432 | |
433 FileError error = util::GDataToFileError(gdata_error); | |
434 if (error != FILE_ERROR_OK) { | |
435 callback.Run(error, scoped_ptr<DriveEntryProto>()); | |
436 return; | |
437 } | |
438 | |
439 DCHECK(about_resource); | |
440 scoped_ptr<DriveEntryProto> root(new DriveEntryProto); | |
441 root->mutable_file_info()->set_is_directory(true); | |
442 root->set_resource_id(about_resource->root_folder_id()); | |
443 root->set_title(util::kDriveMyDriveRootDirName); | |
444 callback.Run(error, root.Pass()); | |
445 } | |
446 | |
447 void FakeDriveFileSystem::GetEntryInfoByPathAfterGetParentEntryInfo( | |
448 const base::FilePath& base_name, | |
449 const GetEntryInfoCallback& callback, | |
450 FileError error, | |
451 scoped_ptr<DriveEntryProto> parent_entry_proto) { | |
452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
453 | |
454 if (error != FILE_ERROR_OK) { | |
455 callback.Run(error, scoped_ptr<DriveEntryProto>()); | |
456 return; | |
457 } | |
458 | |
459 DCHECK(parent_entry_proto); | |
460 drive_service_->GetResourceListInDirectory( | |
461 parent_entry_proto->resource_id(), | |
462 base::Bind( | |
463 &FakeDriveFileSystem::GetEntryInfoByPathAfterGetResourceList, | |
464 weak_ptr_factory_.GetWeakPtr(), base_name, callback)); | |
465 } | |
466 | |
467 void FakeDriveFileSystem::GetEntryInfoByPathAfterGetResourceList( | |
468 const base::FilePath& base_name, | |
469 const GetEntryInfoCallback& callback, | |
470 google_apis::GDataErrorCode gdata_error, | |
471 scoped_ptr<google_apis::ResourceList> resource_list) { | |
472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
473 | |
474 FileError error = util::GDataToFileError(gdata_error); | |
475 if (error != FILE_ERROR_OK) { | |
476 callback.Run(error, scoped_ptr<DriveEntryProto>()); | |
477 return; | |
478 } | |
479 | |
480 DCHECK(resource_list); | |
481 const ScopedVector<google_apis::ResourceEntry>& entries = | |
482 resource_list->entries(); | |
483 for (size_t i = 0; i < entries.size(); ++i) { | |
484 scoped_ptr<DriveEntryProto> entry(new DriveEntryProto( | |
485 ConvertResourceEntryToDriveEntryProto(*entries[i]))); | |
486 if (entry->base_name() == base_name.AsUTF8Unsafe()) { | |
487 // Found the target entry. | |
488 callback.Run(FILE_ERROR_OK, entry.Pass()); | |
489 return; | |
490 } | |
491 } | |
492 | |
493 callback.Run(FILE_ERROR_NOT_FOUND, scoped_ptr<DriveEntryProto>()); | |
494 } | |
495 | |
496 } // namespace test_util | |
497 } // namespace drive | |
OLD | NEW |