| 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/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 const FileOperationCallback& callback, | 350 const FileOperationCallback& callback, |
| 351 google_apis::GDataErrorCode status, | 351 google_apis::GDataErrorCode status, |
| 352 scoped_ptr<google_apis::AboutResource> about_resource) { | 352 scoped_ptr<google_apis::AboutResource> about_resource) { |
| 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 354 DCHECK(!callback.is_null()); | 354 DCHECK(!callback.is_null()); |
| 355 | 355 |
| 356 int64 remote_changestamp = 0; | 356 int64 remote_changestamp = 0; |
| 357 if (util::GDataToDriveFileError(status) == DRIVE_FILE_OK) { | 357 if (util::GDataToDriveFileError(status) == DRIVE_FILE_OK) { |
| 358 DCHECK(about_resource); | 358 DCHECK(about_resource); |
| 359 remote_changestamp = about_resource->largest_change_id(); | 359 remote_changestamp = about_resource->largest_change_id(); |
| 360 last_known_remote_changestamp_ = remote_changestamp; |
| 360 } | 361 } |
| 361 | 362 |
| 362 const DirectoryFetchInfo directory_fetch_info(directory_resource_id, | 363 const DirectoryFetchInfo directory_fetch_info(directory_resource_id, |
| 363 remote_changestamp); | 364 remote_changestamp); |
| 364 DoLoadDirectoryFromServer(directory_fetch_info, callback); | 365 DoLoadDirectoryFromServer(directory_fetch_info, callback); |
| 365 } | 366 } |
| 366 | 367 |
| 367 void ChangeListLoader::DoLoadDirectoryFromServer( | 368 void ChangeListLoader::DoLoadDirectoryFromServer( |
| 368 const DirectoryFetchInfo& directory_fetch_info, | 369 const DirectoryFetchInfo& directory_fetch_info, |
| 369 const FileOperationCallback& callback) { | 370 const FileOperationCallback& callback) { |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 // If the directory of interest is already scheduled to be fetched, add the | 734 // If the directory of interest is already scheduled to be fetched, add the |
| 734 // callback to the pending list and return. | 735 // callback to the pending list and return. |
| 735 LoadCallbackMap::iterator it = pending_load_callback_.find(resource_id); | 736 LoadCallbackMap::iterator it = pending_load_callback_.find(resource_id); |
| 736 if (it != pending_load_callback_.end()) { | 737 if (it != pending_load_callback_.end()) { |
| 737 it->second.push_back(callback); | 738 it->second.push_back(callback); |
| 738 return; | 739 return; |
| 739 } | 740 } |
| 740 | 741 |
| 741 // If the directory's changestamp is up-to-date, just schedule to run the | 742 // If the directory's changestamp is up-to-date, just schedule to run the |
| 742 // callback, as there is no need to fetch the directory. | 743 // callback, as there is no need to fetch the directory. |
| 743 if (directory_fetch_info.changestamp() >= last_known_remote_changestamp_) { | 744 // Note that |last_known_remote_changestamp_| is 0 when it is not received |
| 745 // yet. In that case we conservatively assume that we need to fetch. |
| 746 if (last_known_remote_changestamp_ > 0 && |
| 747 directory_fetch_info.changestamp() >= last_known_remote_changestamp_) { |
| 744 base::MessageLoopProxy::current()->PostTask( | 748 base::MessageLoopProxy::current()->PostTask( |
| 745 FROM_HERE, | 749 FROM_HERE, |
| 746 base::Bind(callback, DRIVE_FILE_OK)); | 750 base::Bind(callback, DRIVE_FILE_OK)); |
| 747 return; | 751 return; |
| 748 } | 752 } |
| 749 | 753 |
| 750 // The directory should be fetched. Add a dummy task to so ScheduleRun() | 754 // The directory should be fetched. Add a dummy task to so ScheduleRun() |
| 751 // can check that the directory is being fetched. | 755 // can check that the directory is being fetched. |
| 752 pending_load_callback_[resource_id].push_back( | 756 pending_load_callback_[resource_id].push_back( |
| 753 base::Bind(&util::EmptyFileOperationCallback)); | 757 base::Bind(&util::EmptyFileOperationCallback)); |
| 758 |
| 759 // Start fetching the directory content, and mark it with the changestamp |
| 760 // |last_known_remote_changestamp_|. To be precise, instead we need to call |
| 761 // GetAboutResource() to get the latest changestamp. However, |
| 762 // - It is costly to do GetAboutResource HTTP request every time. |
| 763 // - The chance using an old value is small; it only happens when LoadIfNeeded |
| 764 // is called during one GetAboutResource roundtrip time of a feed fetching. |
| 765 // - Even if the value is old, it just marks the directory as older. It may |
| 766 // trigger one future unnecessary re-fetch, but it'll never lose data, etc. |
| 767 DirectoryFetchInfo new_directory_fetch_info( |
| 768 directory_fetch_info.resource_id(), |
| 769 std::max(directory_fetch_info.changestamp(), |
| 770 last_known_remote_changestamp_)); |
| 754 DoLoadDirectoryFromServer( | 771 DoLoadDirectoryFromServer( |
| 755 directory_fetch_info, | 772 new_directory_fetch_info, |
| 756 base::Bind(&ChangeListLoader::OnDirectoryLoadComplete, | 773 base::Bind(&ChangeListLoader::OnDirectoryLoadComplete, |
| 757 weak_ptr_factory_.GetWeakPtr(), | 774 weak_ptr_factory_.GetWeakPtr(), |
| 758 directory_fetch_info, | 775 directory_fetch_info, |
| 759 callback)); | 776 callback)); |
| 760 } | 777 } |
| 761 | 778 |
| 762 void ChangeListLoader::NotifyDirectoryChangedAfterApplyFeed( | 779 void ChangeListLoader::NotifyDirectoryChangedAfterApplyFeed( |
| 763 bool should_notify_changed_directories, | 780 bool should_notify_changed_directories, |
| 764 const base::Closure& update_finished_callback) { | 781 const base::Closure& update_finished_callback) { |
| 765 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 for (size_t i = 0; i < callbacks.size(); ++i) { | 873 for (size_t i = 0; i < callbacks.size(); ++i) { |
| 857 base::MessageLoopProxy::current()->PostTask( | 874 base::MessageLoopProxy::current()->PostTask( |
| 858 FROM_HERE, | 875 FROM_HERE, |
| 859 base::Bind(callbacks[i], error)); | 876 base::Bind(callbacks[i], error)); |
| 860 } | 877 } |
| 861 pending_load_callback_.erase(it); | 878 pending_load_callback_.erase(it); |
| 862 } | 879 } |
| 863 } | 880 } |
| 864 | 881 |
| 865 } // namespace drive | 882 } // namespace drive |
| OLD | NEW |