OLD | NEW |
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 "chrome/browser/chromeos/gdata/gdata_file_system.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 void RunTaskOnThread(scoped_refptr<base::MessageLoopProxy> relay_proxy, | 412 void RunTaskOnThread(scoped_refptr<base::MessageLoopProxy> relay_proxy, |
413 const base::Closure& task) { | 413 const base::Closure& task) { |
414 if (relay_proxy->BelongsToCurrentThread()) { | 414 if (relay_proxy->BelongsToCurrentThread()) { |
415 task.Run(); | 415 task.Run(); |
416 } else { | 416 } else { |
417 const bool posted = relay_proxy->PostTask(FROM_HERE, task); | 417 const bool posted = relay_proxy->PostTask(FROM_HERE, task); |
418 DCHECK(posted); | 418 DCHECK(posted); |
419 } | 419 } |
420 } | 420 } |
421 | 421 |
| 422 // Callback for GetEntryByResourceIdAsync. |
| 423 // Removes stale entry upon upload of file. |
| 424 void RemoveStaleEntryOnUpload(const std::string& resource_id, |
| 425 GDataDirectory* parent_dir, |
| 426 GDataEntry* existing_entry) { |
| 427 if (existing_entry && |
| 428 // This should always match, but just in case. |
| 429 existing_entry->parent() == parent_dir) { |
| 430 parent_dir->RemoveEntry(existing_entry); |
| 431 } else { |
| 432 LOG(ERROR) << "Entry for the existing file not found: " << resource_id; |
| 433 } |
| 434 } |
| 435 |
| 436 // Callback for GetEntryByResourceIdAsync. |
| 437 // Adds |entry| to |results|. Runs |callback| with |results| when |
| 438 // |run_callback| is true. |
| 439 void AddEntryToSearchResults( |
| 440 std::vector<SearchResultInfo>* results, |
| 441 const SearchCallback& callback, |
| 442 base::PlatformFileError error, |
| 443 bool run_callback, |
| 444 GDataEntry* entry) { |
| 445 // If a result is not present in our local file system snapshot, ignore it. |
| 446 // For example, this may happen if the entry has recently been added to the |
| 447 // drive (and we still haven't received its delta feed). |
| 448 if (entry) { |
| 449 const bool is_directory = entry->AsGDataDirectory() != NULL; |
| 450 results->push_back(SearchResultInfo(entry->GetFilePath(), is_directory)); |
| 451 } |
| 452 |
| 453 if (run_callback) { |
| 454 scoped_ptr<std::vector<SearchResultInfo> > result_vec(results); |
| 455 if (!callback.is_null()) |
| 456 callback.Run(error, result_vec.Pass()); |
| 457 } |
| 458 } |
| 459 |
422 // Runs task on UI thread. | 460 // Runs task on UI thread. |
423 void RunTaskOnUIThread(const base::Closure& task) { | 461 void RunTaskOnUIThread(const base::Closure& task) { |
424 RunTaskOnThread( | 462 RunTaskOnThread( |
425 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), task); | 463 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), task); |
426 } | 464 } |
427 | 465 |
428 // RelayCallback relays arguments for callback running on the given thread. | 466 // RelayCallback relays arguments for callback running on the given thread. |
429 template<typename CallbackType> | 467 template<typename CallbackType> |
430 struct RelayCallback; | 468 struct RelayCallback; |
431 | 469 |
(...skipping 2063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2495 | 2533 |
2496 if (error != base::PLATFORM_FILE_OK) { | 2534 if (error != base::PLATFORM_FILE_OK) { |
2497 if (!callback.is_null()) | 2535 if (!callback.is_null()) |
2498 callback.Run(error, scoped_ptr<std::vector<SearchResultInfo> >()); | 2536 callback.Run(error, scoped_ptr<std::vector<SearchResultInfo> >()); |
2499 return; | 2537 return; |
2500 } | 2538 } |
2501 | 2539 |
2502 // The search results will be returned using virtual directory. | 2540 // The search results will be returned using virtual directory. |
2503 // The directory is not really part of the file system, so it has no parent or | 2541 // The directory is not really part of the file system, so it has no parent or |
2504 // root. | 2542 // root. |
2505 scoped_ptr<std::vector<SearchResultInfo> > results( | 2543 std::vector<SearchResultInfo>* results(new std::vector<SearchResultInfo>()); |
2506 new std::vector<SearchResultInfo>()); | |
2507 | 2544 |
2508 DCHECK_EQ(1u, params->feed_list->size()); | 2545 DCHECK_EQ(1u, params->feed_list->size()); |
2509 DocumentFeed* feed = params->feed_list->at(0); | 2546 DocumentFeed* feed = params->feed_list->at(0); |
2510 | 2547 |
2511 // Go through all entires generated by the feed and add them to the search | 2548 // Go through all entires generated by the feed and add them to the search |
2512 // result directory. | 2549 // result directory. |
2513 for (size_t i = 0; i < feed->entries().size(); ++i) { | 2550 for (size_t i = 0; i < feed->entries().size(); ++i) { |
2514 DocumentEntry* doc = const_cast<DocumentEntry*>(feed->entries()[i]); | 2551 DocumentEntry* doc = const_cast<DocumentEntry*>(feed->entries()[i]); |
2515 scoped_ptr<GDataEntry> entry( | 2552 scoped_ptr<GDataEntry> entry( |
2516 GDataEntry::FromDocumentEntry(NULL, doc, root_.get())); | 2553 GDataEntry::FromDocumentEntry(NULL, doc, root_.get())); |
(...skipping 11 matching lines...) Expand all Loading... |
2528 scoped_ptr<GDataFile> entry_as_file(entry.release()->AsGDataFile()); | 2565 scoped_ptr<GDataFile> entry_as_file(entry.release()->AsGDataFile()); |
2529 root_->RefreshFile(entry_as_file.Pass()); | 2566 root_->RefreshFile(entry_as_file.Pass()); |
2530 // We shouldn't use entry object after this point. | 2567 // We shouldn't use entry object after this point. |
2531 DCHECK(!entry.get()); | 2568 DCHECK(!entry.get()); |
2532 } | 2569 } |
2533 | 2570 |
2534 // We will need information about result entry to create info for callback. | 2571 // We will need information about result entry to create info for callback. |
2535 // We can't use |entry| anymore, so we have to refetch entry from file | 2572 // We can't use |entry| anymore, so we have to refetch entry from file |
2536 // system. Also, |entry| doesn't have file path set before |RefreshFile| | 2573 // system. Also, |entry| doesn't have file path set before |RefreshFile| |
2537 // call, so we can't get file path from there. | 2574 // call, so we can't get file path from there. |
2538 GDataEntry* saved_entry = root_->GetEntryByResourceId(entry_resource_id); | 2575 root_->GetEntryByResourceIdAsync(entry_resource_id, |
2539 | 2576 base::Bind(&AddEntryToSearchResults, |
2540 // If a result is not present in our local file system snapshot, ignore it. | 2577 results, |
2541 // For example, this may happen if the entry has recently been added to the | 2578 callback, |
2542 // drive (and we still haven't received its delta feed). | 2579 error, |
2543 if (!saved_entry) | 2580 i+1 == feed->entries().size())); |
2544 continue; | |
2545 | |
2546 bool is_directory = saved_entry->AsGDataDirectory() != NULL; | |
2547 results->push_back(SearchResultInfo(saved_entry->GetFilePath(), | |
2548 is_directory)); | |
2549 } | 2581 } |
2550 | |
2551 if (!callback.is_null()) | |
2552 callback.Run(error, results.Pass()); | |
2553 } | 2582 } |
2554 | 2583 |
2555 void GDataFileSystem::Search(const std::string& search_query, | 2584 void GDataFileSystem::Search(const std::string& search_query, |
2556 const SearchCallback& callback) { | 2585 const SearchCallback& callback) { |
2557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 2586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
2558 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 2587 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
2559 RunTaskOnUIThread(base::Bind(&GDataFileSystem::SearchAsyncOnUIThread, | 2588 RunTaskOnUIThread(base::Bind(&GDataFileSystem::SearchAsyncOnUIThread, |
2560 ui_weak_ptr_, | 2589 ui_weak_ptr_, |
2561 search_query, | 2590 search_query, |
2562 CreateRelayCallback(callback))); | 2591 CreateRelayCallback(callback))); |
(...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3553 | 3582 |
3554 scoped_ptr<GDataEntry> new_entry( | 3583 scoped_ptr<GDataEntry> new_entry( |
3555 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); | 3584 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); |
3556 if (!new_entry.get()) { | 3585 if (!new_entry.get()) { |
3557 callback.Run(); | 3586 callback.Run(); |
3558 return; | 3587 return; |
3559 } | 3588 } |
3560 | 3589 |
3561 if (upload_mode == UPLOAD_EXISTING_FILE) { | 3590 if (upload_mode == UPLOAD_EXISTING_FILE) { |
3562 // Remove an existing entry, which should be present. | 3591 // Remove an existing entry, which should be present. |
3563 GDataEntry* existing_entry = root_->GetEntryByResourceId( | 3592 const std::string& resource_id = new_entry->resource_id(); |
3564 new_entry->resource_id()); | 3593 root_->GetEntryByResourceIdAsync(resource_id, |
3565 if (existing_entry && | 3594 base::Bind(&RemoveStaleEntryOnUpload, resource_id, parent_dir)); |
3566 // This should always match, but just in case. | |
3567 existing_entry->parent() == parent_dir) { | |
3568 parent_dir->RemoveEntry(existing_entry); | |
3569 } else { | |
3570 LOG(ERROR) << "Entry for the existing file not found: " | |
3571 << new_entry->resource_id(); | |
3572 } | |
3573 } | 3595 } |
3574 | 3596 |
3575 GDataFile* file = new_entry->AsGDataFile(); | 3597 GDataFile* file = new_entry->AsGDataFile(); |
3576 DCHECK(file); | 3598 DCHECK(file); |
3577 const std::string& resource_id = file->resource_id(); | 3599 const std::string& resource_id = file->resource_id(); |
3578 const std::string& md5 = file->file_md5(); | 3600 const std::string& md5 = file->file_md5(); |
3579 parent_dir->AddEntry(new_entry.release()); | 3601 parent_dir->AddEntry(new_entry.release()); |
3580 | 3602 |
3581 NotifyDirectoryChanged(virtual_dir_path); | 3603 NotifyDirectoryChanged(virtual_dir_path); |
3582 | 3604 |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3920 // must go through here. Removes the |file_path| from the remembered set so | 3942 // must go through here. Removes the |file_path| from the remembered set so |
3921 // that subsequent operations can open the file again. | 3943 // that subsequent operations can open the file again. |
3922 open_files_.erase(file_path); | 3944 open_files_.erase(file_path); |
3923 | 3945 |
3924 // Then invokes the user-supplied callback function. | 3946 // Then invokes the user-supplied callback function. |
3925 if (!callback.is_null()) | 3947 if (!callback.is_null()) |
3926 callback.Run(result); | 3948 callback.Run(result); |
3927 } | 3949 } |
3928 | 3950 |
3929 } // namespace gdata | 3951 } // namespace gdata |
OLD | NEW |