Chromium Code Reviews| 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 if (run_callback && !callback.is_null()) | |
| 453 callback.Run(error, scoped_ptr<std::vector<SearchResultInfo> >(results)); | |
|
hshi1
2012/07/11 00:23:44
Is it possible that |callback| is null? It seems t
achuithb
2012/07/11 00:38:10
I don't think the callback would be NULL since we'
| |
| 454 } | |
| 455 | |
| 422 // Runs task on UI thread. | 456 // Runs task on UI thread. |
| 423 void RunTaskOnUIThread(const base::Closure& task) { | 457 void RunTaskOnUIThread(const base::Closure& task) { |
| 424 RunTaskOnThread( | 458 RunTaskOnThread( |
| 425 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), task); | 459 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), task); |
| 426 } | 460 } |
| 427 | 461 |
| 428 // RelayCallback relays arguments for callback running on the given thread. | 462 // RelayCallback relays arguments for callback running on the given thread. |
| 429 template<typename CallbackType> | 463 template<typename CallbackType> |
| 430 struct RelayCallback; | 464 struct RelayCallback; |
| 431 | 465 |
| (...skipping 2063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2495 | 2529 |
| 2496 if (error != base::PLATFORM_FILE_OK) { | 2530 if (error != base::PLATFORM_FILE_OK) { |
| 2497 if (!callback.is_null()) | 2531 if (!callback.is_null()) |
| 2498 callback.Run(error, scoped_ptr<std::vector<SearchResultInfo> >()); | 2532 callback.Run(error, scoped_ptr<std::vector<SearchResultInfo> >()); |
| 2499 return; | 2533 return; |
| 2500 } | 2534 } |
| 2501 | 2535 |
| 2502 // The search results will be returned using virtual directory. | 2536 // 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 | 2537 // The directory is not really part of the file system, so it has no parent or |
| 2504 // root. | 2538 // root. |
| 2505 scoped_ptr<std::vector<SearchResultInfo> > results( | 2539 std::vector<SearchResultInfo>* results(new std::vector<SearchResultInfo>()); |
| 2506 new std::vector<SearchResultInfo>()); | |
| 2507 | 2540 |
| 2508 DCHECK_EQ(1u, params->feed_list->size()); | 2541 DCHECK_EQ(1u, params->feed_list->size()); |
| 2509 DocumentFeed* feed = params->feed_list->at(0); | 2542 DocumentFeed* feed = params->feed_list->at(0); |
| 2510 | 2543 |
| 2511 // Go through all entires generated by the feed and add them to the search | 2544 // Go through all entires generated by the feed and add them to the search |
| 2512 // result directory. | 2545 // result directory. |
| 2513 for (size_t i = 0; i < feed->entries().size(); ++i) { | 2546 for (size_t i = 0; i < feed->entries().size(); ++i) { |
| 2514 DocumentEntry* doc = const_cast<DocumentEntry*>(feed->entries()[i]); | 2547 DocumentEntry* doc = const_cast<DocumentEntry*>(feed->entries()[i]); |
| 2515 scoped_ptr<GDataEntry> entry( | 2548 scoped_ptr<GDataEntry> entry( |
| 2516 GDataEntry::FromDocumentEntry(NULL, doc, root_.get())); | 2549 GDataEntry::FromDocumentEntry(NULL, doc, root_.get())); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2528 scoped_ptr<GDataFile> entry_as_file(entry.release()->AsGDataFile()); | 2561 scoped_ptr<GDataFile> entry_as_file(entry.release()->AsGDataFile()); |
| 2529 root_->RefreshFile(entry_as_file.Pass()); | 2562 root_->RefreshFile(entry_as_file.Pass()); |
| 2530 // We shouldn't use entry object after this point. | 2563 // We shouldn't use entry object after this point. |
| 2531 DCHECK(!entry.get()); | 2564 DCHECK(!entry.get()); |
| 2532 } | 2565 } |
| 2533 | 2566 |
| 2534 // We will need information about result entry to create info for callback. | 2567 // 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 | 2568 // 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| | 2569 // system. Also, |entry| doesn't have file path set before |RefreshFile| |
| 2537 // call, so we can't get file path from there. | 2570 // call, so we can't get file path from there. |
| 2538 GDataEntry* saved_entry = root_->GetEntryByResourceId(entry_resource_id); | 2571 root_->GetEntryByResourceIdAsync(entry_resource_id, |
| 2539 | 2572 base::Bind(&AddEntryToSearchResults, |
| 2540 // If a result is not present in our local file system snapshot, ignore it. | 2573 results, |
| 2541 // For example, this may happen if the entry has recently been added to the | 2574 callback, |
| 2542 // drive (and we still haven't received its delta feed). | 2575 error, |
| 2543 if (!saved_entry) | 2576 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 } | 2577 } |
| 2550 | |
| 2551 if (!callback.is_null()) | |
| 2552 callback.Run(error, results.Pass()); | |
| 2553 } | 2578 } |
| 2554 | 2579 |
| 2555 void GDataFileSystem::Search(const std::string& search_query, | 2580 void GDataFileSystem::Search(const std::string& search_query, |
| 2556 const SearchCallback& callback) { | 2581 const SearchCallback& callback) { |
| 2557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | 2582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 2558 BrowserThread::CurrentlyOn(BrowserThread::IO)); | 2583 BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 2559 RunTaskOnUIThread(base::Bind(&GDataFileSystem::SearchAsyncOnUIThread, | 2584 RunTaskOnUIThread(base::Bind(&GDataFileSystem::SearchAsyncOnUIThread, |
| 2560 ui_weak_ptr_, | 2585 ui_weak_ptr_, |
| 2561 search_query, | 2586 search_query, |
| 2562 CreateRelayCallback(callback))); | 2587 CreateRelayCallback(callback))); |
| (...skipping 990 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3553 | 3578 |
| 3554 scoped_ptr<GDataEntry> new_entry( | 3579 scoped_ptr<GDataEntry> new_entry( |
| 3555 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); | 3580 GDataEntry::FromDocumentEntry(parent_dir, entry, root_.get())); |
| 3556 if (!new_entry.get()) { | 3581 if (!new_entry.get()) { |
| 3557 callback.Run(); | 3582 callback.Run(); |
| 3558 return; | 3583 return; |
| 3559 } | 3584 } |
| 3560 | 3585 |
| 3561 if (upload_mode == UPLOAD_EXISTING_FILE) { | 3586 if (upload_mode == UPLOAD_EXISTING_FILE) { |
| 3562 // Remove an existing entry, which should be present. | 3587 // Remove an existing entry, which should be present. |
| 3563 GDataEntry* existing_entry = root_->GetEntryByResourceId( | 3588 const std::string& resource_id = new_entry->resource_id(); |
| 3564 new_entry->resource_id()); | 3589 root_->GetEntryByResourceIdAsync(resource_id, |
| 3565 if (existing_entry && | 3590 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 } | 3591 } |
| 3574 | 3592 |
| 3575 GDataFile* file = new_entry->AsGDataFile(); | 3593 GDataFile* file = new_entry->AsGDataFile(); |
| 3576 DCHECK(file); | 3594 DCHECK(file); |
| 3577 const std::string& resource_id = file->resource_id(); | 3595 const std::string& resource_id = file->resource_id(); |
| 3578 const std::string& md5 = file->file_md5(); | 3596 const std::string& md5 = file->file_md5(); |
| 3579 parent_dir->AddEntry(new_entry.release()); | 3597 parent_dir->AddEntry(new_entry.release()); |
| 3580 | 3598 |
| 3581 NotifyDirectoryChanged(virtual_dir_path); | 3599 NotifyDirectoryChanged(virtual_dir_path); |
| 3582 | 3600 |
| (...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 | 3938 // must go through here. Removes the |file_path| from the remembered set so |
| 3921 // that subsequent operations can open the file again. | 3939 // that subsequent operations can open the file again. |
| 3922 open_files_.erase(file_path); | 3940 open_files_.erase(file_path); |
| 3923 | 3941 |
| 3924 // Then invokes the user-supplied callback function. | 3942 // Then invokes the user-supplied callback function. |
| 3925 if (!callback.is_null()) | 3943 if (!callback.is_null()) |
| 3926 callback.Run(result); | 3944 callback.Run(result); |
| 3927 } | 3945 } |
| 3928 | 3946 |
| 3929 } // namespace gdata | 3947 } // namespace gdata |
| OLD | NEW |