| 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/drive/change_list_loader.h" | 5 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 301 |
| 302 ChangeListLoader::ChangeListLoader( | 302 ChangeListLoader::ChangeListLoader( |
| 303 base::SequencedTaskRunner* blocking_task_runner, | 303 base::SequencedTaskRunner* blocking_task_runner, |
| 304 ResourceMetadata* resource_metadata, | 304 ResourceMetadata* resource_metadata, |
| 305 JobScheduler* scheduler, | 305 JobScheduler* scheduler, |
| 306 DriveServiceInterface* drive_service) | 306 DriveServiceInterface* drive_service) |
| 307 : blocking_task_runner_(blocking_task_runner), | 307 : blocking_task_runner_(blocking_task_runner), |
| 308 resource_metadata_(resource_metadata), | 308 resource_metadata_(resource_metadata), |
| 309 scheduler_(scheduler), | 309 scheduler_(scheduler), |
| 310 drive_service_(drive_service), | 310 drive_service_(drive_service), |
| 311 lock_count_(0), |
| 311 loaded_(false), | 312 loaded_(false), |
| 312 weak_ptr_factory_(this) { | 313 weak_ptr_factory_(this) { |
| 313 } | 314 } |
| 314 | 315 |
| 315 ChangeListLoader::~ChangeListLoader() { | 316 ChangeListLoader::~ChangeListLoader() { |
| 316 STLDeleteElements(&fast_fetch_feed_fetcher_set_); | 317 STLDeleteElements(&fast_fetch_feed_fetcher_set_); |
| 317 } | 318 } |
| 318 | 319 |
| 319 bool ChangeListLoader::IsRefreshing() const { | 320 bool ChangeListLoader::IsRefreshing() const { |
| 320 // Callback for change list loading is stored in pending_load_callback_[""]. | 321 // Callback for change list loading is stored in pending_load_callback_[""]. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 base::Bind( | 400 base::Bind( |
| 400 callback, | 401 callback, |
| 401 google_apis::HTTP_NO_CONTENT, | 402 google_apis::HTTP_NO_CONTENT, |
| 402 base::Passed(scoped_ptr<google_apis::AboutResource>( | 403 base::Passed(scoped_ptr<google_apis::AboutResource>( |
| 403 new google_apis::AboutResource(*cached_about_resource_))))); | 404 new google_apis::AboutResource(*cached_about_resource_))))); |
| 404 } else { | 405 } else { |
| 405 UpdateAboutResource(callback); | 406 UpdateAboutResource(callback); |
| 406 } | 407 } |
| 407 } | 408 } |
| 408 | 409 |
| 410 scoped_ptr<base::ScopedClosureRunner> ChangeListLoader::GetLock() { |
| 411 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 412 ++lock_count_; |
| 413 return make_scoped_ptr(new base::ScopedClosureRunner( |
| 414 base::Bind(&ChangeListLoader::Unlock, |
| 415 weak_ptr_factory_.GetWeakPtr()))); |
| 416 } |
| 417 |
| 409 void ChangeListLoader::LoadDirectoryIfNeededAfterGetEntry( | 418 void ChangeListLoader::LoadDirectoryIfNeededAfterGetEntry( |
| 410 const base::FilePath& directory_path, | 419 const base::FilePath& directory_path, |
| 411 const FileOperationCallback& callback, | 420 const FileOperationCallback& callback, |
| 412 bool should_try_loading_parent, | 421 bool should_try_loading_parent, |
| 413 const ResourceEntry* entry, | 422 const ResourceEntry* entry, |
| 414 FileError error) { | 423 FileError error) { |
| 415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 416 DCHECK(!callback.is_null()); | 425 DCHECK(!callback.is_null()); |
| 417 | 426 |
| 418 // Should load grand root first if My Drive's resource ID is not set. | 427 // Should load grand root first if My Drive's resource ID is not set. |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 | 756 |
| 748 ChangeListProcessor* change_list_processor = | 757 ChangeListProcessor* change_list_processor = |
| 749 new ChangeListProcessor(resource_metadata_); | 758 new ChangeListProcessor(resource_metadata_); |
| 750 // Don't send directory content change notification while performing | 759 // Don't send directory content change notification while performing |
| 751 // the initial content retrieval. | 760 // the initial content retrieval. |
| 752 const bool should_notify_changed_directories = is_delta_update; | 761 const bool should_notify_changed_directories = is_delta_update; |
| 753 | 762 |
| 754 util::Log(logging::LOG_INFO, | 763 util::Log(logging::LOG_INFO, |
| 755 "Apply change lists (is delta: %d)", | 764 "Apply change lists (is delta: %d)", |
| 756 is_delta_update); | 765 is_delta_update); |
| 757 base::PostTaskAndReplyWithResult( | 766 ApplyChange(base::Bind( |
| 767 base::IgnoreResult( |
| 768 &base::PostTaskAndReplyWithResult<FileError, FileError>), |
| 758 blocking_task_runner_, | 769 blocking_task_runner_, |
| 759 FROM_HERE, | 770 FROM_HERE, |
| 760 base::Bind(&ChangeListProcessor::Apply, | 771 base::Bind(&ChangeListProcessor::Apply, |
| 761 base::Unretained(change_list_processor), | 772 base::Unretained(change_list_processor), |
| 762 base::Passed(&about_resource), | 773 base::Passed(&about_resource), |
| 763 base::Passed(&change_lists), | 774 base::Passed(&change_lists), |
| 764 is_delta_update), | 775 is_delta_update), |
| 765 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterUpdate, | 776 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterUpdate, |
| 766 weak_ptr_factory_.GetWeakPtr(), | 777 weak_ptr_factory_.GetWeakPtr(), |
| 767 base::Owned(change_list_processor), | 778 base::Owned(change_list_processor), |
| 768 should_notify_changed_directories, | 779 should_notify_changed_directories, |
| 769 base::Time::Now())); | 780 base::Time::Now()))); |
| 770 } | 781 } |
| 771 | 782 |
| 772 void ChangeListLoader::LoadChangeListFromServerAfterUpdate( | 783 void ChangeListLoader::LoadChangeListFromServerAfterUpdate( |
| 773 ChangeListProcessor* change_list_processor, | 784 ChangeListProcessor* change_list_processor, |
| 774 bool should_notify_changed_directories, | 785 bool should_notify_changed_directories, |
| 775 const base::Time& start_time, | 786 const base::Time& start_time, |
| 776 FileError error) { | 787 FileError error) { |
| 777 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 788 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 778 | 789 |
| 779 const base::TimeDelta elapsed = base::Time::Now() - start_time; | 790 const base::TimeDelta elapsed = base::Time::Now() - start_time; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 | 866 |
| 856 if (error != FILE_ERROR_OK) { | 867 if (error != FILE_ERROR_OK) { |
| 857 LOG(ERROR) << "Failed to load directory: " | 868 LOG(ERROR) << "Failed to load directory: " |
| 858 << directory_fetch_info.local_id() | 869 << directory_fetch_info.local_id() |
| 859 << ": " << FileErrorToString(error); | 870 << ": " << FileErrorToString(error); |
| 860 callback.Run(error); | 871 callback.Run(error); |
| 861 return; | 872 return; |
| 862 } | 873 } |
| 863 | 874 |
| 864 base::FilePath* directory_path = new base::FilePath; | 875 base::FilePath* directory_path = new base::FilePath; |
| 865 base::PostTaskAndReplyWithResult( | 876 ApplyChange(base::Bind( |
| 877 base::IgnoreResult( |
| 878 &base::PostTaskAndReplyWithResult<FileError, FileError>), |
| 866 blocking_task_runner_, | 879 blocking_task_runner_, |
| 867 FROM_HERE, | 880 FROM_HERE, |
| 868 base::Bind(&ChangeListProcessor::RefreshDirectory, | 881 base::Bind(&ChangeListProcessor::RefreshDirectory, |
| 869 resource_metadata_, | 882 resource_metadata_, |
| 870 directory_fetch_info, | 883 directory_fetch_info, |
| 871 base::Passed(&change_lists), | 884 base::Passed(&change_lists), |
| 872 directory_path), | 885 directory_path), |
| 873 base::Bind(&ChangeListLoader::LoadDirectoryFromServerAfterRefresh, | 886 base::Bind(&ChangeListLoader::LoadDirectoryFromServerAfterRefresh, |
| 874 weak_ptr_factory_.GetWeakPtr(), | 887 weak_ptr_factory_.GetWeakPtr(), |
| 875 directory_fetch_info, | 888 directory_fetch_info, |
| 876 callback, | 889 callback, |
| 877 base::Owned(directory_path))); | 890 base::Owned(directory_path)))); |
| 878 } | 891 } |
| 879 | 892 |
| 880 void ChangeListLoader::LoadDirectoryFromServerAfterRefresh( | 893 void ChangeListLoader::LoadDirectoryFromServerAfterRefresh( |
| 881 const DirectoryFetchInfo& directory_fetch_info, | 894 const DirectoryFetchInfo& directory_fetch_info, |
| 882 const FileOperationCallback& callback, | 895 const FileOperationCallback& callback, |
| 883 const base::FilePath* directory_path, | 896 const base::FilePath* directory_path, |
| 884 FileError error) { | 897 FileError error) { |
| 885 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 898 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 886 DCHECK(!callback.is_null()); | 899 DCHECK(!callback.is_null()); |
| 887 | 900 |
| 888 DVLOG(1) << "Directory loaded: " << directory_fetch_info.ToString(); | 901 DVLOG(1) << "Directory loaded: " << directory_fetch_info.ToString(); |
| 889 callback.Run(error); | 902 callback.Run(error); |
| 890 // Also notify the observers. | 903 // Also notify the observers. |
| 891 if (error == FILE_ERROR_OK && !directory_path->empty()) { | 904 if (error == FILE_ERROR_OK && !directory_path->empty()) { |
| 892 FOR_EACH_OBSERVER(ChangeListLoaderObserver, observers_, | 905 FOR_EACH_OBSERVER(ChangeListLoaderObserver, observers_, |
| 893 OnDirectoryChanged(*directory_path)); | 906 OnDirectoryChanged(*directory_path)); |
| 894 } | 907 } |
| 895 } | 908 } |
| 896 | 909 |
| 910 void ChangeListLoader::ApplyChange(const base::Closure& closure) { |
| 911 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 912 DCHECK(!closure.is_null()); |
| 913 if (lock_count_ > 0) { |
| 914 pending_apply_closures_.push_back(closure); |
| 915 } else { |
| 916 closure.Run(); |
| 917 } |
| 918 } |
| 919 |
| 920 void ChangeListLoader::Unlock() { |
| 921 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 922 DCHECK_LT(0, lock_count_); |
| 923 |
| 924 if (--lock_count_ > 0) |
| 925 return; |
| 926 |
| 927 std::vector<base::Closure> callbacks; |
| 928 callbacks.swap(pending_apply_closures_); |
| 929 for (size_t i = 0; i < callbacks.size(); ++i) |
| 930 callbacks[i].Run(); |
| 931 } |
| 932 |
| 897 void ChangeListLoader::UpdateAboutResource( | 933 void ChangeListLoader::UpdateAboutResource( |
| 898 const google_apis::AboutResourceCallback& callback) { | 934 const google_apis::AboutResourceCallback& callback) { |
| 899 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 935 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 900 DCHECK(!callback.is_null()); | 936 DCHECK(!callback.is_null()); |
| 901 | 937 |
| 902 scheduler_->GetAboutResource( | 938 scheduler_->GetAboutResource( |
| 903 base::Bind(&ChangeListLoader::UpdateAboutResourceAfterGetAbout, | 939 base::Bind(&ChangeListLoader::UpdateAboutResourceAfterGetAbout, |
| 904 weak_ptr_factory_.GetWeakPtr(), | 940 weak_ptr_factory_.GetWeakPtr(), |
| 905 callback)); | 941 callback)); |
| 906 } | 942 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 924 | 960 |
| 925 cached_about_resource_.reset( | 961 cached_about_resource_.reset( |
| 926 new google_apis::AboutResource(*about_resource)); | 962 new google_apis::AboutResource(*about_resource)); |
| 927 } | 963 } |
| 928 | 964 |
| 929 callback.Run(status, about_resource.Pass()); | 965 callback.Run(status, about_resource.Pass()); |
| 930 } | 966 } |
| 931 | 967 |
| 932 } // namespace internal | 968 } // namespace internal |
| 933 } // namespace drive | 969 } // namespace drive |
| OLD | NEW |