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/sync_file_system/drive_file_sync_service.h" | 5 #include "chrome/browser/sync_file_system/drive_file_sync_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
427 callback.Run(status); | 427 callback.Run(status); |
428 return; | 428 return; |
429 } | 429 } |
430 | 430 |
431 DCHECK(pending_batch_sync_origins_.empty()); | 431 DCHECK(pending_batch_sync_origins_.empty()); |
432 | 432 |
433 UpdateRegisteredOrigins(); | 433 UpdateRegisteredOrigins(); |
434 | 434 |
435 largest_fetched_changestamp_ = metadata_store_->GetLargestChangeStamp(); | 435 largest_fetched_changestamp_ = metadata_store_->GetLargestChangeStamp(); |
436 | 436 |
437 // Mark all the batch sync origins as 'pending' so that we can start | |
438 // batch sync when we're ready. | |
439 for (std::map<GURL, std::string>::const_iterator itr = | |
440 metadata_store_->batch_sync_origins().begin(); | |
441 itr != metadata_store_->batch_sync_origins().end(); | |
442 ++itr) { | |
443 pending_batch_sync_origins_.insert(itr->first); | |
444 } | |
445 | |
446 DriveMetadataStore::URLAndDriveMetadataList to_be_fetched_files; | 437 DriveMetadataStore::URLAndDriveMetadataList to_be_fetched_files; |
447 status = metadata_store_->GetToBeFetchedFiles(&to_be_fetched_files); | 438 status = metadata_store_->GetToBeFetchedFiles(&to_be_fetched_files); |
448 DCHECK_EQ(SYNC_STATUS_OK, status); | 439 DCHECK_EQ(SYNC_STATUS_OK, status); |
449 typedef DriveMetadataStore::URLAndDriveMetadataList::const_iterator iterator; | 440 typedef DriveMetadataStore::URLAndDriveMetadataList::const_iterator iterator; |
450 for (iterator itr = to_be_fetched_files.begin(); | 441 for (iterator itr = to_be_fetched_files.begin(); |
451 itr != to_be_fetched_files.end(); ++itr) { | 442 itr != to_be_fetched_files.end(); ++itr) { |
452 const FileSystemURL& url = itr->first; | 443 const FileSystemURL& url = itr->first; |
453 const DriveMetadata& metadata = itr->second; | 444 const DriveMetadata& metadata = itr->second; |
454 const std::string& resource_id = metadata.resource_id(); | 445 const std::string& resource_id = metadata.resource_id(); |
455 | 446 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 // Extension has been re-enabled. | 694 // Extension has been re-enabled. |
704 metadata_store_->EnableOrigin(origin, base::Bind(&EmptyStatusCallback)); | 695 metadata_store_->EnableOrigin(origin, base::Bind(&EmptyStatusCallback)); |
705 pending_batch_sync_origins_.insert(origin); | 696 pending_batch_sync_origins_.insert(origin); |
706 } | 697 } |
707 } | 698 } |
708 } | 699 } |
709 | 700 |
710 void DriveFileSyncService::StartBatchSync( | 701 void DriveFileSyncService::StartBatchSync( |
711 const SyncStatusCallback& callback) { | 702 const SyncStatusCallback& callback) { |
712 DCHECK(GetCurrentState() == REMOTE_SERVICE_OK || may_have_unfetched_changes_); | 703 DCHECK(GetCurrentState() == REMOTE_SERVICE_OK || may_have_unfetched_changes_); |
713 | 704 DCHECK(!pending_batch_sync_origins_.empty()); |
714 if (pending_batch_sync_origins_.empty()) { | |
715 callback.Run(SYNC_STATUS_OK); | |
716 return; | |
717 } | |
718 | 705 |
719 GURL origin = *pending_batch_sync_origins_.begin(); | 706 GURL origin = *pending_batch_sync_origins_.begin(); |
720 pending_batch_sync_origins_.erase(pending_batch_sync_origins_.begin()); | 707 pending_batch_sync_origins_.erase(pending_batch_sync_origins_.begin()); |
721 std::string resource_id = metadata_store_->GetResourceIdForOrigin(origin); | 708 std::string resource_id = metadata_store_->GetResourceIdForOrigin(origin); |
722 DCHECK(!resource_id.empty()); | 709 DCHECK(!resource_id.empty()); |
723 | 710 |
724 DCHECK(!metadata_store_->IsOriginDisabled(origin)); | 711 DCHECK(!metadata_store_->IsOriginDisabled(origin)); |
725 | 712 |
726 DVLOG(1) << "Start batch sync for:" << origin.spec(); | 713 DVLOG(1) << "Start batch sync for:" << origin.spec(); |
727 | 714 |
(...skipping 22 matching lines...) Expand all Loading... | |
750 AsWeakPtr(), origin, callback)); | 737 AsWeakPtr(), origin, callback)); |
751 return; | 738 return; |
752 } | 739 } |
753 | 740 |
754 if (status != SYNC_STATUS_OK) { | 741 if (status != SYNC_STATUS_OK) { |
755 callback.Run(status); | 742 callback.Run(status); |
756 return; | 743 return; |
757 } | 744 } |
758 | 745 |
759 // Add this origin to batch sync origin if it hasn't been already. | 746 // Add this origin to batch sync origin if it hasn't been already. |
747 // TODO(calvinlo): Added files to file metadata mapping, this can be removed | |
748 // next when batch_sync_origins is removed from DriveMetadataStore. | |
760 if (!metadata_store_->IsKnownOrigin(origin)) { | 749 if (!metadata_store_->IsKnownOrigin(origin)) { |
761 metadata_store_->AddBatchSyncOrigin(origin, resource_id); | 750 metadata_store_->AddBatchSyncOrigin(origin, resource_id); |
tzik
2013/05/16 12:11:34
could you move this around line 845?
I want regist
calvinlo
2013/05/17 04:40:58
Done. As discussed, in order to facilitate this ch
| |
762 pending_batch_sync_origins_.insert(origin); | 751 pending_batch_sync_origins_.insert(origin); |
763 } | 752 } |
764 | 753 |
765 callback.Run(SYNC_STATUS_OK); | 754 callback.Run(SYNC_STATUS_OK); |
766 } | 755 } |
767 | 756 |
768 void DriveFileSyncService::DidUninstallOrigin( | 757 void DriveFileSyncService::DidUninstallOrigin( |
769 const GURL& origin, | 758 const GURL& origin, |
770 const SyncStatusCallback& callback, | 759 const SyncStatusCallback& callback, |
771 google_apis::GDataErrorCode error) { | 760 google_apis::GDataErrorCode error) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
816 scoped_ptr<google_apis::ResourceList> feed) { | 805 scoped_ptr<google_apis::ResourceList> feed) { |
817 if (error != google_apis::HTTP_SUCCESS) { | 806 if (error != google_apis::HTTP_SUCCESS) { |
818 pending_batch_sync_origins_.insert(origin); | 807 pending_batch_sync_origins_.insert(origin); |
819 callback.Run(GDataErrorCodeToSyncStatusCodeWrapper(error)); | 808 callback.Run(GDataErrorCodeToSyncStatusCodeWrapper(error)); |
820 return; | 809 return; |
821 } | 810 } |
822 | 811 |
823 typedef ScopedVector<google_apis::ResourceEntry>::const_iterator iterator; | 812 typedef ScopedVector<google_apis::ResourceEntry>::const_iterator iterator; |
824 for (iterator itr = feed->entries().begin(); | 813 for (iterator itr = feed->entries().begin(); |
825 itr != feed->entries().end(); ++itr) { | 814 itr != feed->entries().end(); ++itr) { |
826 AppendRemoteChange(origin, **itr, largest_changestamp, | 815 const google_apis::ResourceEntry& entry = **itr; |
816 AppendRemoteChange(origin, entry, largest_changestamp, | |
827 RemoteChangeHandler::REMOTE_SYNC_TYPE_BATCH); | 817 RemoteChangeHandler::REMOTE_SYNC_TYPE_BATCH); |
818 | |
819 // Save to be fetched file to DB for restore in case of crash. | |
820 DriveMetadata metadata; | |
821 metadata.set_resource_id(entry.resource_id()); | |
822 metadata.set_md5_checksum(entry.file_md5()); | |
823 metadata.set_conflicted(false); | |
824 metadata.set_to_be_fetched(true); | |
825 | |
826 base::FilePath path = TitleToPath(entry.title()); | |
827 fileapi::FileSystemURL url(CreateSyncableFileSystemURL( | |
828 origin, kServiceName, path)); | |
829 metadata_store_->UpdateEntry(url, metadata, | |
830 base::Bind(&EmptyStatusCallback)); | |
828 } | 831 } |
829 | 832 |
830 GURL next_feed_url; | 833 GURL next_feed_url; |
831 if (feed->GetNextFeedURL(&next_feed_url)) { | 834 if (feed->GetNextFeedURL(&next_feed_url)) { |
832 api_util_->ContinueListing( | 835 api_util_->ContinueListing( |
833 next_feed_url, | 836 next_feed_url, |
834 base::Bind(&DriveFileSyncService::DidGetDirectoryContentForBatchSync, | 837 base::Bind(&DriveFileSyncService::DidGetDirectoryContentForBatchSync, |
835 AsWeakPtr(), | 838 AsWeakPtr(), |
836 callback, | 839 callback, |
837 origin, | 840 origin, |
838 largest_changestamp)); | 841 largest_changestamp)); |
839 return; | 842 return; |
840 } | 843 } |
841 | 844 |
842 // Move |origin| to the incremental sync origin set if the origin has no file. | 845 // Move |origin| to the incremental sync origin set if the origin has no file. |
843 if (metadata_store_->IsBatchSyncOrigin(origin) && | 846 if (metadata_store_->IsBatchSyncOrigin(origin) && |
844 !remote_change_handler_.HasChangesForOrigin(origin)) { | 847 !remote_change_handler_.HasChangesForOrigin(origin)) { |
845 metadata_store_->MoveBatchSyncOriginToIncremental(origin); | 848 metadata_store_->MoveBatchSyncOriginToIncremental(origin); |
tzik
2013/05/16 12:11:34
could you do this unconditionally?
calvinlo
2013/05/17 04:40:58
Done.
| |
846 } | 849 } |
847 | 850 |
848 may_have_unfetched_changes_ = true; | 851 may_have_unfetched_changes_ = true; |
849 callback.Run(SYNC_STATUS_OK); | 852 callback.Run(SYNC_STATUS_OK); |
850 } | 853 } |
851 | 854 |
852 void DriveFileSyncService::ApplyLocalChangeInternal( | 855 void DriveFileSyncService::ApplyLocalChangeInternal( |
853 scoped_ptr<ApplyLocalChangeParam> param, | 856 scoped_ptr<ApplyLocalChangeParam> param, |
854 SyncStatusCode status, | 857 SyncStatusCode status, |
855 const std::string& origin_resource_id) { | 858 const std::string& origin_resource_id) { |
(...skipping 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1939 return SYNC_STATUS_AUTHENTICATION_FAILED; | 1942 return SYNC_STATUS_AUTHENTICATION_FAILED; |
1940 return status; | 1943 return status; |
1941 } | 1944 } |
1942 | 1945 |
1943 void DriveFileSyncService::MaybeStartFetchChanges() { | 1946 void DriveFileSyncService::MaybeStartFetchChanges() { |
1944 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) | 1947 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) |
1945 return; | 1948 return; |
1946 | 1949 |
1947 // If we have pending_batch_sync_origins, try starting the batch sync. | 1950 // If we have pending_batch_sync_origins, try starting the batch sync. |
1948 if (!pending_batch_sync_origins_.empty()) { | 1951 if (!pending_batch_sync_origins_.empty()) { |
1949 if (GetCurrentState() == REMOTE_SERVICE_OK || may_have_unfetched_changes_) { | 1952 if (GetCurrentState() != REMOTE_SERVICE_OK && !may_have_unfetched_changes_) |
1950 task_manager_->ScheduleTaskIfIdle( | 1953 return; |
1951 base::Bind(&DriveFileSyncService::StartBatchSync, AsWeakPtr())); | 1954 |
1952 } | 1955 task_manager_->ScheduleTaskIfIdle( |
1953 return; | 1956 base::Bind(&DriveFileSyncService::StartBatchSync, AsWeakPtr())); |
1954 } | 1957 } |
1955 | 1958 |
1956 if (may_have_unfetched_changes_ && | 1959 if (may_have_unfetched_changes_ && |
1957 !metadata_store_->incremental_sync_origins().empty()) { | 1960 !metadata_store_->incremental_sync_origins().empty()) { |
1958 task_manager_->ScheduleTaskIfIdle( | 1961 task_manager_->ScheduleTaskIfIdle( |
1959 base::Bind(&DriveFileSyncService::FetchChangesForIncrementalSync, | 1962 base::Bind(&DriveFileSyncService::FetchChangesForIncrementalSync, |
1960 AsWeakPtr())); | 1963 AsWeakPtr())); |
1961 } | 1964 } |
1962 } | 1965 } |
1963 | 1966 |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2182 pending_batch_sync_origins_.insert(origin); | 2185 pending_batch_sync_origins_.insert(origin); |
2183 } | 2186 } |
2184 callback.Run(status, resource_id); | 2187 callback.Run(status, resource_id); |
2185 } | 2188 } |
2186 | 2189 |
2187 std::string DriveFileSyncService::sync_root_resource_id() { | 2190 std::string DriveFileSyncService::sync_root_resource_id() { |
2188 return metadata_store_->sync_root_directory(); | 2191 return metadata_store_->sync_root_directory(); |
2189 } | 2192 } |
2190 | 2193 |
2191 } // namespace sync_file_system | 2194 } // namespace sync_file_system |
OLD | NEW |