| 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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 util::GetDriveMyDriveRootPath(), &entry); | 292 util::GetDriveMyDriveRootPath(), &entry); |
| 293 if (error != FILE_ERROR_OK || !entry.resource_id().empty()) | 293 if (error != FILE_ERROR_OK || !entry.resource_id().empty()) |
| 294 return error; | 294 return error; |
| 295 | 295 |
| 296 entry.set_resource_id(about_resource.root_folder_id()); | 296 entry.set_resource_id(about_resource.root_folder_id()); |
| 297 return resource_metadata->RefreshEntry(entry); | 297 return resource_metadata->RefreshEntry(entry); |
| 298 } | 298 } |
| 299 | 299 |
| 300 } // namespace | 300 } // namespace |
| 301 | 301 |
| 302 LoaderController::LoaderController() |
| 303 : lock_count_(0), |
| 304 weak_ptr_factory_(this) { |
| 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 306 } |
| 307 |
| 308 LoaderController::~LoaderController() { |
| 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 310 } |
| 311 |
| 312 scoped_ptr<base::ScopedClosureRunner> LoaderController::GetLock() { |
| 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 314 |
| 315 ++lock_count_; |
| 316 return make_scoped_ptr(new base::ScopedClosureRunner( |
| 317 base::Bind(&LoaderController::Unlock, |
| 318 weak_ptr_factory_.GetWeakPtr()))); |
| 319 } |
| 320 |
| 321 void LoaderController::ScheduleRun(const base::Closure& task) { |
| 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 323 DCHECK(!task.is_null()); |
| 324 |
| 325 if (lock_count_ > 0) { |
| 326 pending_tasks_.push_back(task); |
| 327 } else { |
| 328 task.Run(); |
| 329 } |
| 330 } |
| 331 |
| 332 void LoaderController::Unlock() { |
| 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 334 DCHECK_LT(0, lock_count_); |
| 335 |
| 336 if (--lock_count_ > 0) |
| 337 return; |
| 338 |
| 339 std::vector<base::Closure> tasks; |
| 340 tasks.swap(pending_tasks_); |
| 341 for (size_t i = 0; i < tasks.size(); ++i) |
| 342 tasks[i].Run(); |
| 343 } |
| 344 |
| 302 ChangeListLoader::ChangeListLoader( | 345 ChangeListLoader::ChangeListLoader( |
| 303 base::SequencedTaskRunner* blocking_task_runner, | 346 base::SequencedTaskRunner* blocking_task_runner, |
| 304 ResourceMetadata* resource_metadata, | 347 ResourceMetadata* resource_metadata, |
| 305 JobScheduler* scheduler, | 348 JobScheduler* scheduler, |
| 306 DriveServiceInterface* drive_service) | 349 DriveServiceInterface* drive_service, |
| 350 LoaderController* loader_controller) |
| 307 : blocking_task_runner_(blocking_task_runner), | 351 : blocking_task_runner_(blocking_task_runner), |
| 308 resource_metadata_(resource_metadata), | 352 resource_metadata_(resource_metadata), |
| 309 scheduler_(scheduler), | 353 scheduler_(scheduler), |
| 310 drive_service_(drive_service), | 354 drive_service_(drive_service), |
| 311 lock_count_(0), | 355 loader_controller_(loader_controller), |
| 312 loaded_(false), | 356 loaded_(false), |
| 313 weak_ptr_factory_(this) { | 357 weak_ptr_factory_(this) { |
| 314 } | 358 } |
| 315 | 359 |
| 316 ChangeListLoader::~ChangeListLoader() { | 360 ChangeListLoader::~ChangeListLoader() { |
| 317 STLDeleteElements(&fast_fetch_feed_fetcher_set_); | 361 STLDeleteElements(&fast_fetch_feed_fetcher_set_); |
| 318 } | 362 } |
| 319 | 363 |
| 320 bool ChangeListLoader::IsRefreshing() const { | 364 bool ChangeListLoader::IsRefreshing() const { |
| 321 // Callback for change list loading is stored in pending_load_callback_[""]. | 365 // Callback for change list loading is stored in pending_load_callback_[""]. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 base::Bind( | 444 base::Bind( |
| 401 callback, | 445 callback, |
| 402 google_apis::HTTP_NO_CONTENT, | 446 google_apis::HTTP_NO_CONTENT, |
| 403 base::Passed(scoped_ptr<google_apis::AboutResource>( | 447 base::Passed(scoped_ptr<google_apis::AboutResource>( |
| 404 new google_apis::AboutResource(*cached_about_resource_))))); | 448 new google_apis::AboutResource(*cached_about_resource_))))); |
| 405 } else { | 449 } else { |
| 406 UpdateAboutResource(callback); | 450 UpdateAboutResource(callback); |
| 407 } | 451 } |
| 408 } | 452 } |
| 409 | 453 |
| 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 | |
| 418 void ChangeListLoader::LoadDirectoryIfNeededAfterGetEntry( | 454 void ChangeListLoader::LoadDirectoryIfNeededAfterGetEntry( |
| 419 const base::FilePath& directory_path, | 455 const base::FilePath& directory_path, |
| 420 const FileOperationCallback& callback, | 456 const FileOperationCallback& callback, |
| 421 bool should_try_loading_parent, | 457 bool should_try_loading_parent, |
| 422 const ResourceEntry* entry, | 458 const ResourceEntry* entry, |
| 423 FileError error) { | 459 FileError error) { |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 425 DCHECK(!callback.is_null()); | 461 DCHECK(!callback.is_null()); |
| 426 | 462 |
| 427 // Should load grand root first if My Drive's resource ID is not set. | 463 // 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... |
| 756 | 792 |
| 757 ChangeListProcessor* change_list_processor = | 793 ChangeListProcessor* change_list_processor = |
| 758 new ChangeListProcessor(resource_metadata_); | 794 new ChangeListProcessor(resource_metadata_); |
| 759 // Don't send directory content change notification while performing | 795 // Don't send directory content change notification while performing |
| 760 // the initial content retrieval. | 796 // the initial content retrieval. |
| 761 const bool should_notify_changed_directories = is_delta_update; | 797 const bool should_notify_changed_directories = is_delta_update; |
| 762 | 798 |
| 763 util::Log(logging::LOG_INFO, | 799 util::Log(logging::LOG_INFO, |
| 764 "Apply change lists (is delta: %d)", | 800 "Apply change lists (is delta: %d)", |
| 765 is_delta_update); | 801 is_delta_update); |
| 766 ApplyChange(base::Bind( | 802 loader_controller_->ScheduleRun(base::Bind( |
| 767 base::IgnoreResult( | 803 base::IgnoreResult( |
| 768 &base::PostTaskAndReplyWithResult<FileError, FileError>), | 804 &base::PostTaskAndReplyWithResult<FileError, FileError>), |
| 769 blocking_task_runner_, | 805 blocking_task_runner_, |
| 770 FROM_HERE, | 806 FROM_HERE, |
| 771 base::Bind(&ChangeListProcessor::Apply, | 807 base::Bind(&ChangeListProcessor::Apply, |
| 772 base::Unretained(change_list_processor), | 808 base::Unretained(change_list_processor), |
| 773 base::Passed(&about_resource), | 809 base::Passed(&about_resource), |
| 774 base::Passed(&change_lists), | 810 base::Passed(&change_lists), |
| 775 is_delta_update), | 811 is_delta_update), |
| 776 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterUpdate, | 812 base::Bind(&ChangeListLoader::LoadChangeListFromServerAfterUpdate, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 | 902 |
| 867 if (error != FILE_ERROR_OK) { | 903 if (error != FILE_ERROR_OK) { |
| 868 LOG(ERROR) << "Failed to load directory: " | 904 LOG(ERROR) << "Failed to load directory: " |
| 869 << directory_fetch_info.local_id() | 905 << directory_fetch_info.local_id() |
| 870 << ": " << FileErrorToString(error); | 906 << ": " << FileErrorToString(error); |
| 871 callback.Run(error); | 907 callback.Run(error); |
| 872 return; | 908 return; |
| 873 } | 909 } |
| 874 | 910 |
| 875 base::FilePath* directory_path = new base::FilePath; | 911 base::FilePath* directory_path = new base::FilePath; |
| 876 ApplyChange(base::Bind( | 912 loader_controller_->ScheduleRun(base::Bind( |
| 877 base::IgnoreResult( | 913 base::IgnoreResult( |
| 878 &base::PostTaskAndReplyWithResult<FileError, FileError>), | 914 &base::PostTaskAndReplyWithResult<FileError, FileError>), |
| 879 blocking_task_runner_, | 915 blocking_task_runner_, |
| 880 FROM_HERE, | 916 FROM_HERE, |
| 881 base::Bind(&ChangeListProcessor::RefreshDirectory, | 917 base::Bind(&ChangeListProcessor::RefreshDirectory, |
| 882 resource_metadata_, | 918 resource_metadata_, |
| 883 directory_fetch_info, | 919 directory_fetch_info, |
| 884 base::Passed(&change_lists), | 920 base::Passed(&change_lists), |
| 885 directory_path), | 921 directory_path), |
| 886 base::Bind(&ChangeListLoader::LoadDirectoryFromServerAfterRefresh, | 922 base::Bind(&ChangeListLoader::LoadDirectoryFromServerAfterRefresh, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 900 | 936 |
| 901 DVLOG(1) << "Directory loaded: " << directory_fetch_info.ToString(); | 937 DVLOG(1) << "Directory loaded: " << directory_fetch_info.ToString(); |
| 902 callback.Run(error); | 938 callback.Run(error); |
| 903 // Also notify the observers. | 939 // Also notify the observers. |
| 904 if (error == FILE_ERROR_OK && !directory_path->empty()) { | 940 if (error == FILE_ERROR_OK && !directory_path->empty()) { |
| 905 FOR_EACH_OBSERVER(ChangeListLoaderObserver, observers_, | 941 FOR_EACH_OBSERVER(ChangeListLoaderObserver, observers_, |
| 906 OnDirectoryChanged(*directory_path)); | 942 OnDirectoryChanged(*directory_path)); |
| 907 } | 943 } |
| 908 } | 944 } |
| 909 | 945 |
| 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 | |
| 933 void ChangeListLoader::UpdateAboutResource( | 946 void ChangeListLoader::UpdateAboutResource( |
| 934 const google_apis::AboutResourceCallback& callback) { | 947 const google_apis::AboutResourceCallback& callback) { |
| 935 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 948 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 936 DCHECK(!callback.is_null()); | 949 DCHECK(!callback.is_null()); |
| 937 | 950 |
| 938 scheduler_->GetAboutResource( | 951 scheduler_->GetAboutResource( |
| 939 base::Bind(&ChangeListLoader::UpdateAboutResourceAfterGetAbout, | 952 base::Bind(&ChangeListLoader::UpdateAboutResourceAfterGetAbout, |
| 940 weak_ptr_factory_.GetWeakPtr(), | 953 weak_ptr_factory_.GetWeakPtr(), |
| 941 callback)); | 954 callback)); |
| 942 } | 955 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 960 | 973 |
| 961 cached_about_resource_.reset( | 974 cached_about_resource_.reset( |
| 962 new google_apis::AboutResource(*about_resource)); | 975 new google_apis::AboutResource(*about_resource)); |
| 963 } | 976 } |
| 964 | 977 |
| 965 callback.Run(status, about_resource.Pass()); | 978 callback.Run(status, about_resource.Pass()); |
| 966 } | 979 } |
| 967 | 980 |
| 968 } // namespace internal | 981 } // namespace internal |
| 969 } // namespace drive | 982 } // namespace drive |
| OLD | NEW |