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 28 matching lines...) Expand all Loading... |
39 using fileapi::FileSystemURL; | 39 using fileapi::FileSystemURL; |
40 | 40 |
41 namespace sync_file_system { | 41 namespace sync_file_system { |
42 | 42 |
43 namespace { | 43 namespace { |
44 | 44 |
45 const base::FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp"); | 45 const base::FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp"); |
46 const base::FilePath::CharType kSyncFileSystemDir[] = | 46 const base::FilePath::CharType kSyncFileSystemDir[] = |
47 FILE_PATH_LITERAL("Sync FileSystem"); | 47 FILE_PATH_LITERAL("Sync FileSystem"); |
48 | 48 |
49 // Incremental sync polling interval. | |
50 // TODO(calvinlo): Improve polling algorithm dependent on whether push | |
51 // notifications are on or off. | |
52 const int64 kMinimumPollingDelaySeconds = 5; | |
53 const int64 kMaximumPollingDelaySeconds = 10 * 60; // 10 min | |
54 const int64 kPollingDelaySecondsWithNotification = 4 * 60 * 60; // 4 hr | |
55 const double kDelayMultiplier = 1.6; | |
56 | |
57 bool CreateTemporaryFile(const base::FilePath& dir_path, | 49 bool CreateTemporaryFile(const base::FilePath& dir_path, |
58 base::FilePath* temp_file) { | 50 base::FilePath* temp_file) { |
59 return file_util::CreateDirectory(dir_path) && | 51 return file_util::CreateDirectory(dir_path) && |
60 file_util::CreateTemporaryFileInDir(dir_path, temp_file); | 52 file_util::CreateTemporaryFileInDir(dir_path, temp_file); |
61 } | 53 } |
62 | 54 |
63 void DeleteTemporaryFile(const base::FilePath& file_path) { | 55 void DeleteTemporaryFile(const base::FilePath& file_path) { |
64 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)) { | 56 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)) { |
65 content::BrowserThread::PostTask( | 57 content::BrowserThread::PostTask( |
66 content::BrowserThread::FILE, FROM_HERE, | 58 content::BrowserThread::FILE, FROM_HERE, |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 return left.changestamp > right.changestamp; | 260 return left.changestamp > right.changestamp; |
269 return false; | 261 return false; |
270 } | 262 } |
271 | 263 |
272 DriveFileSyncService::DriveFileSyncService(Profile* profile) | 264 DriveFileSyncService::DriveFileSyncService(Profile* profile) |
273 : profile_(profile), | 265 : profile_(profile), |
274 last_operation_status_(SYNC_STATUS_OK), | 266 last_operation_status_(SYNC_STATUS_OK), |
275 state_(REMOTE_SERVICE_OK), | 267 state_(REMOTE_SERVICE_OK), |
276 sync_enabled_(true), | 268 sync_enabled_(true), |
277 largest_fetched_changestamp_(0), | 269 largest_fetched_changestamp_(0), |
278 polling_delay_seconds_(kMinimumPollingDelaySeconds), | |
279 may_have_unfetched_changes_(false), | 270 may_have_unfetched_changes_(false), |
280 remote_change_processor_(NULL), | 271 remote_change_processor_(NULL), |
281 conflict_resolution_(kDefaultPolicy), | 272 conflict_resolution_(kDefaultPolicy), |
282 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 273 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
283 temporary_file_dir_ = | 274 temporary_file_dir_ = |
284 profile->GetPath().Append(kSyncFileSystemDir).Append(kTempDirName); | 275 profile->GetPath().Append(kSyncFileSystemDir).Append(kTempDirName); |
285 token_.reset(new TaskToken(AsWeakPtr())); | 276 token_.reset(new TaskToken(AsWeakPtr())); |
286 | 277 |
287 sync_client_.reset(new DriveFileSyncClient(profile)); | 278 sync_client_.reset(new DriveFileSyncClient(profile)); |
288 sync_client_->AddObserver(this); | 279 sync_client_->AddObserver(this); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 if (old_state == GetCurrentState()) | 552 if (old_state == GetCurrentState()) |
562 return; | 553 return; |
563 | 554 |
564 if (!enabled) | 555 if (!enabled) |
565 last_operation_status_ = SYNC_STATUS_SYNC_DISABLED; | 556 last_operation_status_ = SYNC_STATUS_SYNC_DISABLED; |
566 | 557 |
567 const char* status_message = enabled ? "Sync is enabled" : "Sync is disabled"; | 558 const char* status_message = enabled ? "Sync is enabled" : "Sync is disabled"; |
568 FOR_EACH_OBSERVER( | 559 FOR_EACH_OBSERVER( |
569 Observer, service_observers_, | 560 Observer, service_observers_, |
570 OnRemoteServiceStateUpdated(GetCurrentState(), status_message)); | 561 OnRemoteServiceStateUpdated(GetCurrentState(), status_message)); |
571 | |
572 if (GetCurrentState() == REMOTE_SERVICE_OK) { | |
573 UpdatePollingDelay(kMinimumPollingDelaySeconds); | |
574 SchedulePolling(); | |
575 } | |
576 } | 562 } |
577 | 563 |
578 SyncStatusCode DriveFileSyncService::SetConflictResolutionPolicy( | 564 SyncStatusCode DriveFileSyncService::SetConflictResolutionPolicy( |
579 ConflictResolutionPolicy resolution) { | 565 ConflictResolutionPolicy resolution) { |
580 conflict_resolution_ = resolution; | 566 conflict_resolution_ = resolution; |
581 return SYNC_STATUS_OK; | 567 return SYNC_STATUS_OK; |
582 } | 568 } |
583 | 569 |
584 ConflictResolutionPolicy | 570 ConflictResolutionPolicy |
585 DriveFileSyncService::GetConflictResolutionPolicy() const { | 571 DriveFileSyncService::GetConflictResolutionPolicy() const { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 void DriveFileSyncService::OnAuthenticated() { | 622 void DriveFileSyncService::OnAuthenticated() { |
637 if (state_ == REMOTE_SERVICE_OK) | 623 if (state_ == REMOTE_SERVICE_OK) |
638 return; | 624 return; |
639 DVLOG(1) << "OnAuthenticated"; | 625 DVLOG(1) << "OnAuthenticated"; |
640 state_ = REMOTE_SERVICE_OK; | 626 state_ = REMOTE_SERVICE_OK; |
641 if (GetCurrentState() != REMOTE_SERVICE_OK) | 627 if (GetCurrentState() != REMOTE_SERVICE_OK) |
642 return; | 628 return; |
643 FOR_EACH_OBSERVER( | 629 FOR_EACH_OBSERVER( |
644 Observer, service_observers_, | 630 Observer, service_observers_, |
645 OnRemoteServiceStateUpdated(GetCurrentState(), "Authenticated")); | 631 OnRemoteServiceStateUpdated(GetCurrentState(), "Authenticated")); |
646 UpdatePollingDelay(kMinimumPollingDelaySeconds); | |
647 | 632 |
648 may_have_unfetched_changes_ = true; | 633 may_have_unfetched_changes_ = true; |
649 MaybeStartFetchChanges(); | 634 MaybeStartFetchChanges(); |
650 } | 635 } |
651 | 636 |
652 void DriveFileSyncService::OnNetworkConnected() { | 637 void DriveFileSyncService::OnNetworkConnected() { |
653 if (state_ == REMOTE_SERVICE_OK) | 638 if (state_ == REMOTE_SERVICE_OK) |
654 return; | 639 return; |
655 DVLOG(1) << "OnNetworkConnected"; | 640 DVLOG(1) << "OnNetworkConnected"; |
656 state_ = REMOTE_SERVICE_OK; | 641 state_ = REMOTE_SERVICE_OK; |
657 if (GetCurrentState() != REMOTE_SERVICE_OK) | 642 if (GetCurrentState() != REMOTE_SERVICE_OK) |
658 return; | 643 return; |
659 FOR_EACH_OBSERVER( | 644 FOR_EACH_OBSERVER( |
660 Observer, service_observers_, | 645 Observer, service_observers_, |
661 OnRemoteServiceStateUpdated(GetCurrentState(), "Network connected")); | 646 OnRemoteServiceStateUpdated(GetCurrentState(), "Network connected")); |
662 UpdatePollingDelay(kMinimumPollingDelaySeconds); | |
663 | 647 |
664 may_have_unfetched_changes_ = true; | 648 may_have_unfetched_changes_ = true; |
665 MaybeStartFetchChanges(); | 649 MaybeStartFetchChanges(); |
666 } | 650 } |
667 | 651 |
668 // Called by CreateForTesting. | 652 // Called by CreateForTesting. |
669 DriveFileSyncService::DriveFileSyncService( | 653 DriveFileSyncService::DriveFileSyncService( |
670 Profile* profile, | 654 Profile* profile, |
671 const base::FilePath& base_dir, | 655 const base::FilePath& base_dir, |
672 scoped_ptr<DriveFileSyncClientInterface> sync_client, | 656 scoped_ptr<DriveFileSyncClientInterface> sync_client, |
673 scoped_ptr<DriveMetadataStore> metadata_store) | 657 scoped_ptr<DriveMetadataStore> metadata_store) |
674 : profile_(profile), | 658 : profile_(profile), |
675 last_operation_status_(SYNC_STATUS_OK), | 659 last_operation_status_(SYNC_STATUS_OK), |
676 state_(REMOTE_SERVICE_OK), | 660 state_(REMOTE_SERVICE_OK), |
677 sync_enabled_(true), | 661 sync_enabled_(true), |
678 largest_fetched_changestamp_(0), | 662 largest_fetched_changestamp_(0), |
679 polling_delay_seconds_(-1), | |
680 may_have_unfetched_changes_(false), | 663 may_have_unfetched_changes_(false), |
681 remote_change_processor_(NULL), | 664 remote_change_processor_(NULL), |
682 conflict_resolution_(kDefaultPolicy), | 665 conflict_resolution_(kDefaultPolicy), |
683 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 666 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
684 DCHECK(profile); | 667 DCHECK(profile); |
685 temporary_file_dir_ = base_dir.Append(kTempDirName); | 668 temporary_file_dir_ = base_dir.Append(kTempDirName); |
686 | 669 |
687 token_.reset(new TaskToken(AsWeakPtr())); | 670 token_.reset(new TaskToken(AsWeakPtr())); |
688 sync_client_ = sync_client.Pass(); | 671 sync_client_ = sync_client.Pass(); |
689 metadata_store_ = metadata_store.Pass(); | 672 metadata_store_ = metadata_store.Pass(); |
(...skipping 30 matching lines...) Expand all Loading... |
720 | 703 |
721 if (token_->task_type() != TASK_TYPE_NONE) { | 704 if (token_->task_type() != TASK_TYPE_NONE) { |
722 DVLOG(2) << "NotifyTaskDone: " << token_->description() | 705 DVLOG(2) << "NotifyTaskDone: " << token_->description() |
723 << ": finished with status=" << status | 706 << ": finished with status=" << status |
724 << " (" << SyncStatusCodeToString(status) << ")" | 707 << " (" << SyncStatusCodeToString(status) << ")" |
725 << " " << token_->location().ToString(); | 708 << " " << token_->location().ToString(); |
726 | 709 |
727 RemoteServiceState old_state = GetCurrentState(); | 710 RemoteServiceState old_state = GetCurrentState(); |
728 UpdateServiceState(); | 711 UpdateServiceState(); |
729 | 712 |
730 // Reset the polling delay. This will adjust the polling timer | |
731 // based on the current service state. | |
732 UpdatePollingDelay(polling_delay_seconds_); | |
733 | |
734 // Notify remote sync service state if the state has been changed. | 713 // Notify remote sync service state if the state has been changed. |
735 if (!token_->description().empty() || old_state != GetCurrentState()) { | 714 if (!token_->description().empty() || old_state != GetCurrentState()) { |
736 FOR_EACH_OBSERVER( | 715 FOR_EACH_OBSERVER( |
737 Observer, service_observers_, | 716 Observer, service_observers_, |
738 OnRemoteServiceStateUpdated(GetCurrentState(), | 717 OnRemoteServiceStateUpdated(GetCurrentState(), |
739 token_->done_description())); | 718 token_->done_description())); |
740 } | 719 } |
741 } | 720 } |
742 | 721 |
743 token_->ResetTask(FROM_HERE); | 722 token_->ResetTask(FROM_HERE); |
744 if (!pending_tasks_.empty()) { | 723 if (!pending_tasks_.empty()) { |
745 base::Closure closure = pending_tasks_.front(); | 724 base::Closure closure = pending_tasks_.front(); |
746 pending_tasks_.pop_front(); | 725 pending_tasks_.pop_front(); |
747 closure.Run(); | 726 closure.Run(); |
748 return; | 727 return; |
749 } | 728 } |
750 | 729 |
751 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) | 730 if (GetCurrentState() == REMOTE_SERVICE_DISABLED) |
752 return; | 731 return; |
753 | 732 |
754 MaybeStartFetchChanges(); | 733 MaybeStartFetchChanges(); |
755 | 734 |
756 SchedulePolling(); | |
757 | |
758 // Notify observer of the update of |pending_changes_|. | 735 // Notify observer of the update of |pending_changes_|. |
759 FOR_EACH_OBSERVER(Observer, service_observers_, | 736 FOR_EACH_OBSERVER(Observer, service_observers_, |
760 OnRemoteChangeQueueUpdated(pending_changes_.size())); | 737 OnRemoteChangeQueueUpdated(pending_changes_.size())); |
761 } | 738 } |
762 | 739 |
763 void DriveFileSyncService::UpdateServiceState() { | 740 void DriveFileSyncService::UpdateServiceState() { |
764 switch (last_operation_status_) { | 741 switch (last_operation_status_) { |
765 // Possible regular operation errors. | 742 // Possible regular operation errors. |
766 case SYNC_STATUS_OK: | 743 case SYNC_STATUS_OK: |
767 case SYNC_STATUS_FILE_BUSY: | 744 case SYNC_STATUS_FILE_BUSY: |
(...skipping 1422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2190 metadata_store_->SetOriginRootDirectory(*itr, std::string()); | 2167 metadata_store_->SetOriginRootDirectory(*itr, std::string()); |
2191 } | 2168 } |
2192 | 2169 |
2193 GURL next_feed; | 2170 GURL next_feed; |
2194 if (changes->GetNextFeedURL(&next_feed)) | 2171 if (changes->GetNextFeedURL(&next_feed)) |
2195 may_have_unfetched_changes_ = true; | 2172 may_have_unfetched_changes_ = true; |
2196 | 2173 |
2197 if (!changes->entries().empty()) | 2174 if (!changes->entries().empty()) |
2198 largest_fetched_changestamp_ = changes->entries().back()->changestamp(); | 2175 largest_fetched_changestamp_ = changes->entries().back()->changestamp(); |
2199 | 2176 |
2200 if (has_new_changes) { | |
2201 UpdatePollingDelay(kMinimumPollingDelaySeconds); | |
2202 } else { | |
2203 // If the change_queue_ was not updated, update the polling delay to wait | |
2204 // longer. | |
2205 UpdatePollingDelay(static_cast<int64>( | |
2206 kDelayMultiplier * polling_delay_seconds_)); | |
2207 } | |
2208 | |
2209 NotifyTaskDone(SYNC_STATUS_OK, token.Pass()); | 2177 NotifyTaskDone(SYNC_STATUS_OK, token.Pass()); |
2210 } | 2178 } |
2211 | 2179 |
2212 bool DriveFileSyncService::GetOriginForEntry( | 2180 bool DriveFileSyncService::GetOriginForEntry( |
2213 const google_apis::ResourceEntry& entry, | 2181 const google_apis::ResourceEntry& entry, |
2214 GURL* origin_out) { | 2182 GURL* origin_out) { |
2215 typedef ScopedVector<google_apis::Link>::const_iterator iterator; | 2183 typedef ScopedVector<google_apis::Link>::const_iterator iterator; |
2216 for (iterator itr = entry.links().begin(); | 2184 for (iterator itr = entry.links().begin(); |
2217 itr != entry.links().end(); ++itr) { | 2185 itr != entry.links().end(); ++itr) { |
2218 if ((*itr)->type() != google_apis::Link::LINK_PARENT) | 2186 if ((*itr)->type() != google_apis::Link::LINK_PARENT) |
(...skipping 11 matching lines...) Expand all Loading... |
2230 if ((*itr)->href().GetOrigin() != resource_link.GetOrigin() || | 2198 if ((*itr)->href().GetOrigin() != resource_link.GetOrigin() || |
2231 (*itr)->href().path() != resource_link.path()) | 2199 (*itr)->href().path() != resource_link.path()) |
2232 continue; | 2200 continue; |
2233 | 2201 |
2234 *origin_out = origin; | 2202 *origin_out = origin; |
2235 return true; | 2203 return true; |
2236 } | 2204 } |
2237 return false; | 2205 return false; |
2238 } | 2206 } |
2239 | 2207 |
2240 void DriveFileSyncService::SchedulePolling() { | |
2241 if (polling_timer_.IsRunning() || | |
2242 polling_delay_seconds_ < 0 || | |
2243 GetCurrentState() == REMOTE_SERVICE_DISABLED) | |
2244 return; | |
2245 | |
2246 DVLOG(1) << "Polling scheduled" | |
2247 << " (delay:" << polling_delay_seconds_ << "s)"; | |
2248 | |
2249 polling_timer_.Start( | |
2250 FROM_HERE, base::TimeDelta::FromSeconds(polling_delay_seconds_), | |
2251 base::Bind(&DriveFileSyncService::OnPollingTimerFired, AsWeakPtr())); | |
2252 } | |
2253 | |
2254 void DriveFileSyncService::OnPollingTimerFired() { | |
2255 may_have_unfetched_changes_ = true; | |
2256 MaybeStartFetchChanges(); | |
2257 } | |
2258 | |
2259 void DriveFileSyncService::UpdatePollingDelay(int64 new_delay_sec) { | |
2260 // polling_delay_seconds_ made negative to disable polling for testing. | |
2261 if (polling_delay_seconds_ < 0) | |
2262 return; | |
2263 | |
2264 if (state_ == REMOTE_SERVICE_TEMPORARY_UNAVAILABLE) { | |
2265 // If the service state is TEMPORARY_UNAVAILABLE, poll the service | |
2266 // with a modest duration (but more frequently than | |
2267 // kPollingDelaySecondsWithNotification) so that we have a mild chance | |
2268 // to recover the state. | |
2269 polling_delay_seconds_ = kMaximumPollingDelaySeconds; | |
2270 polling_timer_.Stop(); | |
2271 return; | |
2272 } | |
2273 | |
2274 int64 old_delay = polling_delay_seconds_; | |
2275 | |
2276 // Push notifications off. | |
2277 polling_delay_seconds_ = std::min(new_delay_sec, kMaximumPollingDelaySeconds); | |
2278 | |
2279 if (polling_delay_seconds_ < old_delay) | |
2280 polling_timer_.Stop(); | |
2281 } | |
2282 | |
2283 void DriveFileSyncService::NotifyObserversFileStatusChanged( | 2208 void DriveFileSyncService::NotifyObserversFileStatusChanged( |
2284 const FileSystemURL& url, | 2209 const FileSystemURL& url, |
2285 SyncFileStatus sync_status, | 2210 SyncFileStatus sync_status, |
2286 SyncAction action_taken, | 2211 SyncAction action_taken, |
2287 SyncDirection direction) { | 2212 SyncDirection direction) { |
2288 if (sync_status != SYNC_FILE_STATUS_SYNCED) { | 2213 if (sync_status != SYNC_FILE_STATUS_SYNCED) { |
2289 DCHECK_EQ(SYNC_ACTION_NONE, action_taken); | 2214 DCHECK_EQ(SYNC_ACTION_NONE, action_taken); |
2290 DCHECK_EQ(SYNC_DIRECTION_NONE, direction); | 2215 DCHECK_EQ(SYNC_DIRECTION_NONE, direction); |
2291 } | 2216 } |
2292 | 2217 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2359 pending_batch_sync_origins_.insert(origin); | 2284 pending_batch_sync_origins_.insert(origin); |
2360 } | 2285 } |
2361 callback.Run(status, resource_id); | 2286 callback.Run(status, resource_id); |
2362 } | 2287 } |
2363 | 2288 |
2364 std::string DriveFileSyncService::sync_root_resource_id() { | 2289 std::string DriveFileSyncService::sync_root_resource_id() { |
2365 return metadata_store_->sync_root_directory(); | 2290 return metadata_store_->sync_root_directory(); |
2366 } | 2291 } |
2367 | 2292 |
2368 } // namespace sync_file_system | 2293 } // namespace sync_file_system |
OLD | NEW |