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 "sync/engine/sync_scheduler_impl.h" | 5 #include "sync/engine/sync_scheduler_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 started_ = true; | 225 started_ = true; |
226 SendInitialSnapshot(); | 226 SendInitialSnapshot(); |
227 } | 227 } |
228 | 228 |
229 DCHECK(!session_context_->account_name().empty()); | 229 DCHECK(!session_context_->account_name().empty()); |
230 DCHECK(syncer_.get()); | 230 DCHECK(syncer_.get()); |
231 Mode old_mode = mode_; | 231 Mode old_mode = mode_; |
232 mode_ = mode; | 232 mode_ = mode; |
233 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. | 233 AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. |
234 | 234 |
235 if (old_mode != mode_ && | 235 if (old_mode != mode_ && mode_ == NORMAL_MODE) { |
236 mode_ == NORMAL_MODE && | |
237 (nudge_tracker_.IsSyncRequired() || | |
238 nudge_tracker_.IsRetryRequired(base::TimeTicks::Now())) && | |
239 CanRunNudgeJobNow(NORMAL_PRIORITY)) { | |
240 // We just got back to normal mode. Let's try to run the work that was | 236 // We just got back to normal mode. Let's try to run the work that was |
241 // queued up while we were configuring. | 237 // queued up while we were configuring. |
242 TrySyncSessionJob(); | 238 |
| 239 // Update our current time before checking IsRetryRequired(). |
| 240 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); |
| 241 if ((nudge_tracker_.IsSyncRequired() || nudge_tracker_.IsRetryRequired()) && |
| 242 CanRunNudgeJobNow(NORMAL_PRIORITY)) { |
| 243 TrySyncSessionJob(); |
| 244 } |
243 } | 245 } |
244 } | 246 } |
245 | 247 |
246 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { | 248 ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { |
247 ModelTypeSet enabled_types = session_context_->enabled_types(); | 249 ModelTypeSet enabled_types = session_context_->enabled_types(); |
248 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); | 250 ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); |
249 return Difference(enabled_types, throttled_types); | 251 return Difference(enabled_types, throttled_types); |
250 } | 252 } |
251 | 253 |
252 void SyncSchedulerImpl::SendInitialSnapshot() { | 254 void SyncSchedulerImpl::SendInitialSnapshot() { |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 // Don't run poll job till the next time poll timer fires. | 465 // Don't run poll job till the next time poll timer fires. |
464 do_poll_after_credentials_updated_ = false; | 466 do_poll_after_credentials_updated_ = false; |
465 | 467 |
466 bool success = !premature_exit | 468 bool success = !premature_exit |
467 && !sessions::HasSyncerError( | 469 && !sessions::HasSyncerError( |
468 session->status_controller().model_neutral_state()); | 470 session->status_controller().model_neutral_state()); |
469 | 471 |
470 if (success) { | 472 if (success) { |
471 // That cycle took care of any outstanding work we had. | 473 // That cycle took care of any outstanding work we had. |
472 SDVLOG(2) << "Nudge succeeded."; | 474 SDVLOG(2) << "Nudge succeeded."; |
473 nudge_tracker_.RecordSuccessfulSyncCycle(base::TimeTicks::Now()); | 475 nudge_tracker_.RecordSuccessfulSyncCycle(); |
474 scheduled_nudge_time_ = base::TimeTicks(); | 476 scheduled_nudge_time_ = base::TimeTicks(); |
475 | 477 |
476 // If we're here, then we successfully reached the server. End all backoff. | 478 // If we're here, then we successfully reached the server. End all backoff. |
477 wait_interval_.reset(); | 479 wait_interval_.reset(); |
478 NotifyRetryTime(base::Time()); | 480 NotifyRetryTime(base::Time()); |
479 return; | |
480 } else { | 481 } else { |
481 HandleFailure(session->status_controller().model_neutral_state()); | 482 HandleFailure(session->status_controller().model_neutral_state()); |
482 } | 483 } |
483 } | 484 } |
484 | 485 |
485 void SyncSchedulerImpl::DoConfigurationSyncSessionJob(JobPriority priority) { | 486 void SyncSchedulerImpl::DoConfigurationSyncSessionJob(JobPriority priority) { |
486 DCHECK(CalledOnValidThread()); | 487 DCHECK(CalledOnValidThread()); |
487 DCHECK_EQ(mode_, CONFIGURATION_MODE); | 488 DCHECK_EQ(mode_, CONFIGURATION_MODE); |
488 DCHECK(pending_configure_params_ != NULL); | 489 DCHECK(pending_configure_params_ != NULL); |
489 | 490 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 | 574 |
574 base::AutoReset<bool> protector(&no_scheduling_allowed_, true); | 575 base::AutoReset<bool> protector(&no_scheduling_allowed_, true); |
575 | 576 |
576 SDVLOG(2) << "Retrying with types " | 577 SDVLOG(2) << "Retrying with types " |
577 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); | 578 << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); |
578 scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); | 579 scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
579 if (syncer_->RetrySyncShare(GetEnabledAndUnthrottledTypes(), | 580 if (syncer_->RetrySyncShare(GetEnabledAndUnthrottledTypes(), |
580 session.get()) && | 581 session.get()) && |
581 !sessions::HasSyncerError( | 582 !sessions::HasSyncerError( |
582 session->status_controller().model_neutral_state())) { | 583 session->status_controller().model_neutral_state())) { |
583 nudge_tracker_.RecordSuccessfulSyncCycle(base::TimeTicks::Now()); | 584 nudge_tracker_.RecordSuccessfulSyncCycle(); |
584 } else { | 585 } else { |
585 HandleFailure(session->status_controller().model_neutral_state()); | 586 HandleFailure(session->status_controller().model_neutral_state()); |
586 } | 587 } |
587 } | 588 } |
588 | 589 |
589 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { | 590 void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { |
590 DCHECK(CalledOnValidThread()); | 591 DCHECK(CalledOnValidThread()); |
591 base::TimeTicks now = TimeTicks::Now(); | 592 base::TimeTicks now = TimeTicks::Now(); |
592 // Update timing information for how often datatypes are triggering nudges. | 593 // Update timing information for how often datatypes are triggering nudges. |
593 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { | 594 for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 // access token will be here. | 681 // access token will be here. |
681 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 682 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
682 &SyncSchedulerImpl::TrySyncSessionJobImpl, | 683 &SyncSchedulerImpl::TrySyncSessionJobImpl, |
683 weak_ptr_factory_.GetWeakPtr())); | 684 weak_ptr_factory_.GetWeakPtr())); |
684 } | 685 } |
685 | 686 |
686 void SyncSchedulerImpl::TrySyncSessionJobImpl() { | 687 void SyncSchedulerImpl::TrySyncSessionJobImpl() { |
687 JobPriority priority = next_sync_session_job_priority_; | 688 JobPriority priority = next_sync_session_job_priority_; |
688 next_sync_session_job_priority_ = NORMAL_PRIORITY; | 689 next_sync_session_job_priority_ = NORMAL_PRIORITY; |
689 | 690 |
| 691 nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); |
| 692 |
690 DCHECK(CalledOnValidThread()); | 693 DCHECK(CalledOnValidThread()); |
691 if (mode_ == CONFIGURATION_MODE) { | 694 if (mode_ == CONFIGURATION_MODE) { |
692 if (pending_configure_params_) { | 695 if (pending_configure_params_) { |
693 SDVLOG(2) << "Found pending configure job"; | 696 SDVLOG(2) << "Found pending configure job"; |
694 DoConfigurationSyncSessionJob(priority); | 697 DoConfigurationSyncSessionJob(priority); |
695 } | 698 } |
696 } else if (CanRunNudgeJobNow(priority)) { | 699 } else if (CanRunNudgeJobNow(priority)) { |
697 if (nudge_tracker_.IsSyncRequired()) { | 700 if (nudge_tracker_.IsSyncRequired()) { |
698 SDVLOG(2) << "Found pending nudge job"; | 701 SDVLOG(2) << "Found pending nudge job"; |
699 DoNudgeSyncSessionJob(priority); | 702 DoNudgeSyncSessionJob(priority); |
700 } else if (nudge_tracker_.IsRetryRequired(base::TimeTicks::Now())) { | 703 } else if (nudge_tracker_.IsRetryRequired()) { |
701 DoRetrySyncSessionJob(); | 704 DoRetrySyncSessionJob(); |
702 } else if (do_poll_after_credentials_updated_ || | 705 } else if (do_poll_after_credentials_updated_ || |
703 ((base::TimeTicks::Now() - last_poll_reset_) >= GetPollInterval())) { | 706 ((base::TimeTicks::Now() - last_poll_reset_) >= GetPollInterval())) { |
704 DoPollSyncSessionJob(); | 707 DoPollSyncSessionJob(); |
705 // Poll timer fires infrequently. Usually by this time access token is | 708 // Poll timer fires infrequently. Usually by this time access token is |
706 // already expired and poll job will fail with auth error. Set flag to | 709 // already expired and poll job will fail with auth error. Set flag to |
707 // retry poll once ProfileSyncService gets new access token, TryCanaryJob | 710 // retry poll once ProfileSyncService gets new access token, TryCanaryJob |
708 // will be called after access token is retrieved. | 711 // will be called after access token is retrieved. |
709 if (HttpResponse::SYNC_AUTH_ERROR == | 712 if (HttpResponse::SYNC_AUTH_ERROR == |
710 session_context_->connection_manager()->server_status()) { | 713 session_context_->connection_manager()->server_status()) { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 if (ShouldRequestEarlyExit( | 902 if (ShouldRequestEarlyExit( |
900 snapshot.model_neutral_state().sync_protocol_error)) { | 903 snapshot.model_neutral_state().sync_protocol_error)) { |
901 SDVLOG(2) << "Sync Scheduler requesting early exit."; | 904 SDVLOG(2) << "Sync Scheduler requesting early exit."; |
902 Stop(); | 905 Stop(); |
903 } | 906 } |
904 if (IsActionableError(snapshot.model_neutral_state().sync_protocol_error)) | 907 if (IsActionableError(snapshot.model_neutral_state().sync_protocol_error)) |
905 OnActionableError(snapshot); | 908 OnActionableError(snapshot); |
906 } | 909 } |
907 | 910 |
908 void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) { | 911 void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) { |
909 nudge_tracker_.set_next_retry_time(base::TimeTicks::Now() + delay); | 912 nudge_tracker_.SetNextRetryTime(TimeTicks::Now() + delay); |
910 retry_timer_.Start(FROM_HERE, delay, this, | 913 retry_timer_.Start(FROM_HERE, delay, this, |
911 &SyncSchedulerImpl::RetryTimerCallback); | 914 &SyncSchedulerImpl::RetryTimerCallback); |
912 } | 915 } |
913 | 916 |
914 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { | 917 void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { |
915 DCHECK(CalledOnValidThread()); | 918 DCHECK(CalledOnValidThread()); |
916 session_context_->set_notifications_enabled(notifications_enabled); | 919 session_context_->set_notifications_enabled(notifications_enabled); |
917 if (notifications_enabled) | 920 if (notifications_enabled) |
918 nudge_tracker_.OnInvalidationsEnabled(); | 921 nudge_tracker_.OnInvalidationsEnabled(); |
919 else | 922 else |
920 nudge_tracker_.OnInvalidationsDisabled(); | 923 nudge_tracker_.OnInvalidationsDisabled(); |
921 } | 924 } |
922 | 925 |
923 base::TimeDelta SyncSchedulerImpl::GetSessionsCommitDelay() const { | 926 base::TimeDelta SyncSchedulerImpl::GetSessionsCommitDelay() const { |
924 DCHECK(CalledOnValidThread()); | 927 DCHECK(CalledOnValidThread()); |
925 return sessions_commit_delay_; | 928 return sessions_commit_delay_; |
926 } | 929 } |
927 | 930 |
928 #undef SDVLOG_LOC | 931 #undef SDVLOG_LOC |
929 | 932 |
930 #undef SDVLOG | 933 #undef SDVLOG |
931 | 934 |
932 #undef SLOG | 935 #undef SLOG |
933 | 936 |
934 #undef ENUM_CASE | 937 #undef ENUM_CASE |
935 | 938 |
936 } // namespace syncer | 939 } // namespace syncer |
OLD | NEW |