[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "sync/engine/sync_scheduler_impl.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <cstring> |
| 9 | |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 10 | #include "base/auto_reset.h" |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 12 | #include "base/bind_helpers.h" |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
| 14 | #include "base/location.h" |
| 15 | #include "base/logging.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
[email protected] | f706df5 | 2012-08-15 06:18:36 | [diff] [blame] | 17 | #include "sync/engine/backoff_delay_provider.h" |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 18 | #include "sync/engine/syncer.h" |
[email protected] | ba85740 | 2013-09-03 21:20:54 | [diff] [blame] | 19 | #include "sync/notifier/object_id_invalidation_map.h" |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 20 | #include "sync/protocol/proto_enum_conversions.h" |
| 21 | #include "sync/protocol/sync.pb.h" |
| 22 | #include "sync/util/data_type_histogram.h" |
| 23 | #include "sync/util/logging.h" |
| 24 | |
| 25 | using base::TimeDelta; |
| 26 | using base::TimeTicks; |
| 27 | |
| 28 | namespace syncer { |
| 29 | |
| 30 | using sessions::SyncSession; |
| 31 | using sessions::SyncSessionSnapshot; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 32 | using sync_pb::GetUpdatesCallerInfo; |
| 33 | |
| 34 | namespace { |
[email protected] | 4494c5d | 2012-08-13 01:07:42 | [diff] [blame] | 35 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 36 | bool ShouldRequestEarlyExit(const SyncProtocolError& error) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 37 | switch (error.error_type) { |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 38 | case SYNC_SUCCESS: |
| 39 | case MIGRATION_DONE: |
| 40 | case THROTTLED: |
| 41 | case TRANSIENT_ERROR: |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 42 | return false; |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 43 | case NOT_MY_BIRTHDAY: |
| 44 | case CLEAR_PENDING: |
[email protected] | 5a496e5 | 2013-05-29 20:44:57 | [diff] [blame] | 45 | case DISABLED_BY_ADMIN: |
[email protected] | 484fff5 | 2014-06-11 06:34:03 | [diff] [blame] | 46 | case USER_ROLLBACK: |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 47 | // If we send terminate sync early then |sync_cycle_ended| notification |
| 48 | // would not be sent. If there were no actions then |ACTIONABLE_ERROR| |
| 49 | // notification wouldnt be sent either. Then the UI layer would be left |
| 50 | // waiting forever. So assert we would send something. |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 51 | DCHECK_NE(error.action, UNKNOWN_ACTION); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 52 | return true; |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 53 | case INVALID_CREDENTIAL: |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 54 | // The notification for this is handled by PostAndProcessHeaders|. |
| 55 | // Server does no have to send any action for this. |
| 56 | return true; |
| 57 | // Make the default a NOTREACHED. So if a new error is introduced we |
| 58 | // think about its expected functionality. |
| 59 | default: |
| 60 | NOTREACHED(); |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool IsActionableError( |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 66 | const SyncProtocolError& error) { |
| 67 | return (error.action != UNKNOWN_ACTION); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 68 | } |
| 69 | } // namespace |
| 70 | |
| 71 | ConfigurationParams::ConfigurationParams() |
[email protected] | 310512c | 2012-07-31 19:44:25 | [diff] [blame] | 72 | : source(GetUpdatesCallerInfo::UNKNOWN) {} |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 73 | ConfigurationParams::ConfigurationParams( |
| 74 | const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource& source, |
[email protected] | bb7d12a | 2012-09-01 03:50:29 | [diff] [blame] | 75 | ModelTypeSet types_to_download, |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 76 | const ModelSafeRoutingInfo& routing_info, |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 77 | const base::Closure& ready_task, |
| 78 | const base::Closure& retry_task) |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 79 | : source(source), |
| 80 | types_to_download(types_to_download), |
| 81 | routing_info(routing_info), |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 82 | ready_task(ready_task), |
| 83 | retry_task(retry_task) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 84 | DCHECK(!ready_task.is_null()); |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 85 | DCHECK(!retry_task.is_null()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 86 | } |
| 87 | ConfigurationParams::~ConfigurationParams() {} |
| 88 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 89 | SyncSchedulerImpl::WaitInterval::WaitInterval() |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 90 | : mode(UNKNOWN) {} |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 91 | |
| 92 | SyncSchedulerImpl::WaitInterval::WaitInterval(Mode mode, TimeDelta length) |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 93 | : mode(mode), length(length) {} |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 94 | |
| 95 | SyncSchedulerImpl::WaitInterval::~WaitInterval() {} |
| 96 | |
| 97 | #define ENUM_CASE(x) case x: return #x; break; |
| 98 | |
| 99 | const char* SyncSchedulerImpl::WaitInterval::GetModeString(Mode mode) { |
| 100 | switch (mode) { |
| 101 | ENUM_CASE(UNKNOWN); |
| 102 | ENUM_CASE(EXPONENTIAL_BACKOFF); |
| 103 | ENUM_CASE(THROTTLED); |
| 104 | } |
| 105 | NOTREACHED(); |
| 106 | return ""; |
| 107 | } |
| 108 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 109 | GetUpdatesCallerInfo::GetUpdatesSource GetUpdatesFromNudgeSource( |
| 110 | NudgeSource source) { |
| 111 | switch (source) { |
| 112 | case NUDGE_SOURCE_NOTIFICATION: |
| 113 | return GetUpdatesCallerInfo::NOTIFICATION; |
| 114 | case NUDGE_SOURCE_LOCAL: |
| 115 | return GetUpdatesCallerInfo::LOCAL; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 116 | case NUDGE_SOURCE_LOCAL_REFRESH: |
| 117 | return GetUpdatesCallerInfo::DATATYPE_REFRESH; |
| 118 | case NUDGE_SOURCE_UNKNOWN: |
| 119 | return GetUpdatesCallerInfo::UNKNOWN; |
| 120 | default: |
| 121 | NOTREACHED(); |
| 122 | return GetUpdatesCallerInfo::UNKNOWN; |
| 123 | } |
| 124 | } |
| 125 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 126 | // Helper macros to log with the syncer thread name; useful when there |
| 127 | // are multiple syncer threads involved. |
| 128 | |
| 129 | #define SLOG(severity) LOG(severity) << name_ << ": " |
| 130 | |
| 131 | #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " |
| 132 | |
| 133 | #define SDVLOG_LOC(from_here, verbose_level) \ |
| 134 | DVLOG_LOC(from_here, verbose_level) << name_ << ": " |
| 135 | |
| 136 | namespace { |
| 137 | |
| 138 | const int kDefaultSessionsCommitDelaySeconds = 10; |
| 139 | |
| 140 | bool IsConfigRelatedUpdateSourceValue( |
| 141 | GetUpdatesCallerInfo::GetUpdatesSource source) { |
| 142 | switch (source) { |
| 143 | case GetUpdatesCallerInfo::RECONFIGURATION: |
| 144 | case GetUpdatesCallerInfo::MIGRATION: |
| 145 | case GetUpdatesCallerInfo::NEW_CLIENT: |
| 146 | case GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE: |
| 147 | return true; |
| 148 | default: |
| 149 | return false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } // namespace |
| 154 | |
| 155 | SyncSchedulerImpl::SyncSchedulerImpl(const std::string& name, |
[email protected] | f706df5 | 2012-08-15 06:18:36 | [diff] [blame] | 156 | BackoffDelayProvider* delay_provider, |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 157 | sessions::SyncSessionContext* context, |
| 158 | Syncer* syncer) |
[email protected] | 37d5b347 | 2013-10-10 16:20:36 | [diff] [blame] | 159 | : name_(name), |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 160 | started_(false), |
| 161 | syncer_short_poll_interval_seconds_( |
| 162 | TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds)), |
| 163 | syncer_long_poll_interval_seconds_( |
| 164 | TimeDelta::FromSeconds(kDefaultLongPollIntervalSeconds)), |
| 165 | sessions_commit_delay_( |
| 166 | TimeDelta::FromSeconds(kDefaultSessionsCommitDelaySeconds)), |
| 167 | mode_(NORMAL_MODE), |
[email protected] | f706df5 | 2012-08-15 06:18:36 | [diff] [blame] | 168 | delay_provider_(delay_provider), |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 169 | syncer_(syncer), |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 170 | session_context_(context), |
[email protected] | a43e34bd | 2013-07-13 15:04:42 | [diff] [blame] | 171 | no_scheduling_allowed_(false), |
[email protected] | 37d5b347 | 2013-10-10 16:20:36 | [diff] [blame] | 172 | do_poll_after_credentials_updated_(false), |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 173 | next_sync_session_job_priority_(NORMAL_PRIORITY), |
[email protected] | 37d5b347 | 2013-10-10 16:20:36 | [diff] [blame] | 174 | weak_ptr_factory_(this), |
| 175 | weak_ptr_factory_for_weak_handle_(this) { |
| 176 | weak_handle_this_ = MakeWeakHandle( |
| 177 | weak_ptr_factory_for_weak_handle_.GetWeakPtr()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | SyncSchedulerImpl::~SyncSchedulerImpl() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 181 | DCHECK(CalledOnValidThread()); |
[email protected] | ea29851b | 2013-09-19 01:17:02 | [diff] [blame] | 182 | Stop(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void SyncSchedulerImpl::OnCredentialsUpdated() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 186 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 187 | |
[email protected] | 5fffc50 | 2013-02-08 02:29:22 | [diff] [blame] | 188 | if (HttpResponse::SYNC_AUTH_ERROR == |
| 189 | session_context_->connection_manager()->server_status()) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 190 | OnServerConnectionErrorFixed(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void SyncSchedulerImpl::OnConnectionStatusChange() { |
[email protected] | 7e31df8f | 2013-03-05 04:14:53 | [diff] [blame] | 195 | if (HttpResponse::CONNECTION_UNAVAILABLE == |
| 196 | session_context_->connection_manager()->server_status()) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 197 | // Optimistically assume that the connection is fixed and try |
| 198 | // connecting. |
| 199 | OnServerConnectionErrorFixed(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void SyncSchedulerImpl::OnServerConnectionErrorFixed() { |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 204 | // There could be a pending nudge or configuration job in several cases: |
| 205 | // |
| 206 | // 1. We're in exponential backoff. |
| 207 | // 2. We're silenced / throttled. |
| 208 | // 3. A nudge was saved previously due to not having a valid auth token. |
| 209 | // 4. A nudge was scheduled + saved while in configuration mode. |
| 210 | // |
| 211 | // In all cases except (2), we want to retry contacting the server. We |
[email protected] | e0428b1 | 2013-12-16 18:52:43 | [diff] [blame] | 212 | // call TryCanaryJob to achieve this, and note that nothing -- not even a |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 213 | // canary job -- can bypass a THROTTLED WaitInterval. The only thing that |
| 214 | // has the authority to do that is the Unthrottle timer. |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 215 | TryCanaryJob(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 218 | void SyncSchedulerImpl::Start(Mode mode) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 219 | DCHECK(CalledOnValidThread()); |
[email protected] | 21cb28a | 2013-05-07 03:52:45 | [diff] [blame] | 220 | std::string thread_name = base::MessageLoop::current()->thread_name(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 221 | if (thread_name.empty()) |
| 222 | thread_name = "<Main thread>"; |
| 223 | SDVLOG(2) << "Start called from thread " |
| 224 | << thread_name << " with mode " << GetModeString(mode); |
| 225 | if (!started_) { |
| 226 | started_ = true; |
| 227 | SendInitialSnapshot(); |
| 228 | } |
| 229 | |
| 230 | DCHECK(!session_context_->account_name().empty()); |
| 231 | DCHECK(syncer_.get()); |
| 232 | Mode old_mode = mode_; |
| 233 | mode_ = mode; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 234 | AdjustPolling(UPDATE_INTERVAL); // Will kick start poll timer if needed. |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 235 | |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 236 | if (old_mode != mode_ && mode_ == NORMAL_MODE) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 237 | // We just got back to normal mode. Let's try to run the work that was |
| 238 | // queued up while we were configuring. |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 239 | |
| 240 | // Update our current time before checking IsRetryRequired(). |
| 241 | nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); |
[email protected] | eb540e4 | 2014-03-04 04:19:57 | [diff] [blame] | 242 | if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY)) { |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 243 | TrySyncSessionJob(); |
| 244 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 248 | ModelTypeSet SyncSchedulerImpl::GetEnabledAndUnthrottledTypes() { |
[email protected] | 0662c3e | 2014-03-20 20:21:27 | [diff] [blame] | 249 | ModelTypeSet enabled_types = session_context_->GetEnabledTypes(); |
[email protected] | c9daa430 | 2014-02-12 08:45:16 | [diff] [blame] | 250 | ModelTypeSet enabled_protocol_types = |
| 251 | Intersection(ProtocolTypes(), enabled_types); |
[email protected] | a957548 | 2013-12-04 02:51:36 | [diff] [blame] | 252 | ModelTypeSet throttled_types = nudge_tracker_.GetThrottledTypes(); |
[email protected] | c9daa430 | 2014-02-12 08:45:16 | [diff] [blame] | 253 | return Difference(enabled_protocol_types, throttled_types); |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 256 | void SyncSchedulerImpl::SendInitialSnapshot() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 257 | DCHECK(CalledOnValidThread()); |
[email protected] | 0a6e961 | 2013-08-03 01:41:42 | [diff] [blame] | 258 | scoped_ptr<SyncSession> dummy(SyncSession::Build(session_context_, this)); |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 259 | SyncCycleEvent event(SyncCycleEvent::STATUS_CHANGED); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 260 | event.snapshot = dummy->TakeSnapshot(); |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 261 | FOR_EACH_OBSERVER(SyncEngineEventListener, |
| 262 | *session_context_->listeners(), |
| 263 | OnSyncCycleEvent(event)); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | namespace { |
| 267 | |
[email protected] | cb024352 | 2012-09-20 21:53:34 | [diff] [blame] | 268 | // Helper to extract the routing info corresponding to types in |
[email protected] | 79dc04cce | 2013-02-15 17:59:48 | [diff] [blame] | 269 | // |types_to_download| from |current_routes|. |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 270 | void BuildModelSafeParams( |
[email protected] | bb7d12a | 2012-09-01 03:50:29 | [diff] [blame] | 271 | ModelTypeSet types_to_download, |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 272 | const ModelSafeRoutingInfo& current_routes, |
[email protected] | cb024352 | 2012-09-20 21:53:34 | [diff] [blame] | 273 | ModelSafeRoutingInfo* result_routes) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 274 | for (ModelTypeSet::Iterator iter = types_to_download.First(); iter.Good(); |
| 275 | iter.Inc()) { |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 276 | ModelType type = iter.Get(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 277 | ModelSafeRoutingInfo::const_iterator route = current_routes.find(type); |
| 278 | DCHECK(route != current_routes.end()); |
| 279 | ModelSafeGroup group = route->second; |
| 280 | (*result_routes)[type] = group; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | } // namespace. |
| 285 | |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 286 | void SyncSchedulerImpl::ScheduleConfiguration( |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 287 | const ConfigurationParams& params) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 288 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 289 | DCHECK(IsConfigRelatedUpdateSourceValue(params.source)); |
| 290 | DCHECK_EQ(CONFIGURATION_MODE, mode_); |
| 291 | DCHECK(!params.ready_task.is_null()); |
[email protected] | ecf01bd | 2013-01-10 00:42:42 | [diff] [blame] | 292 | CHECK(started_) << "Scheduler must be running to configure."; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 293 | SDVLOG(2) << "Reconfiguring syncer."; |
| 294 | |
| 295 | // Only one configuration is allowed at a time. Verify we're not waiting |
| 296 | // for a pending configure job. |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 297 | DCHECK(!pending_configure_params_); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 298 | |
[email protected] | d45f0d9 | 2012-07-20 17:25:41 | [diff] [blame] | 299 | ModelSafeRoutingInfo restricted_routes; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 300 | BuildModelSafeParams(params.types_to_download, |
| 301 | params.routing_info, |
[email protected] | cb024352 | 2012-09-20 21:53:34 | [diff] [blame] | 302 | &restricted_routes); |
[email protected] | 2fdf6199 | 2014-01-17 23:21:59 | [diff] [blame] | 303 | session_context_->SetRoutingInfo(restricted_routes); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 304 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 305 | // Only reconfigure if we have types to download. |
| 306 | if (!params.types_to_download.Empty()) { |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 307 | pending_configure_params_.reset(new ConfigurationParams(params)); |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 308 | TrySyncSessionJob(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 309 | } else { |
| 310 | SDVLOG(2) << "No change in routing info, calling ready task directly."; |
| 311 | params.ready_task.Run(); |
| 312 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 315 | bool SyncSchedulerImpl::CanRunJobNow(JobPriority priority) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 316 | DCHECK(CalledOnValidThread()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 317 | if (wait_interval_ && wait_interval_->mode == WaitInterval::THROTTLED) { |
| 318 | SDVLOG(1) << "Unable to run a job because we're throttled."; |
| 319 | return false; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 320 | } |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 321 | |
| 322 | if (wait_interval_ |
| 323 | && wait_interval_->mode == WaitInterval::EXPONENTIAL_BACKOFF |
| 324 | && priority != CANARY_PRIORITY) { |
| 325 | SDVLOG(1) << "Unable to run a job because we're backing off."; |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | if (session_context_->connection_manager()->HasInvalidAuthToken()) { |
| 330 | SDVLOG(1) << "Unable to run a job because we have no valid auth token."; |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | return true; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 337 | bool SyncSchedulerImpl::CanRunNudgeJobNow(JobPriority priority) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 338 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 339 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 340 | if (!CanRunJobNow(priority)) { |
| 341 | SDVLOG(1) << "Unable to run a nudge job right now"; |
| 342 | return false; |
| 343 | } |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 344 | |
[email protected] | 0662c3e | 2014-03-20 20:21:27 | [diff] [blame] | 345 | const ModelTypeSet enabled_types = session_context_->GetEnabledTypes(); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 346 | if (nudge_tracker_.GetThrottledTypes().HasAll(enabled_types)) { |
| 347 | SDVLOG(1) << "Not running a nudge because we're fully type throttled."; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 348 | return false; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 349 | } |
| 350 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 351 | if (mode_ == CONFIGURATION_MODE) { |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 352 | SDVLOG(1) << "Not running nudge because we're in configuration mode."; |
| 353 | return false; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 356 | return true; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 359 | void SyncSchedulerImpl::ScheduleLocalNudge( |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 360 | const TimeDelta& desired_delay, |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 361 | ModelTypeSet types, |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 362 | const tracked_objects::Location& nudge_location) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 363 | DCHECK(CalledOnValidThread()); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 364 | DCHECK(!types.Empty()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 365 | |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 366 | SDVLOG_LOC(nudge_location, 2) |
| 367 | << "Scheduling sync because of local change to " |
| 368 | << ModelTypeSetToString(types); |
| 369 | UpdateNudgeTimeRecords(types); |
| 370 | nudge_tracker_.RecordLocalChange(types); |
| 371 | ScheduleNudgeImpl(desired_delay, nudge_location); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 374 | void SyncSchedulerImpl::ScheduleLocalRefreshRequest( |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 375 | const TimeDelta& desired_delay, |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 376 | ModelTypeSet types, |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 377 | const tracked_objects::Location& nudge_location) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 378 | DCHECK(CalledOnValidThread()); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 379 | DCHECK(!types.Empty()); |
| 380 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 381 | SDVLOG_LOC(nudge_location, 2) |
[email protected] | 580cda6 | 2013-05-16 23:37:30 | [diff] [blame] | 382 | << "Scheduling sync because of local refresh request for " |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 383 | << ModelTypeSetToString(types); |
| 384 | nudge_tracker_.RecordLocalRefreshRequest(types); |
| 385 | ScheduleNudgeImpl(desired_delay, nudge_location); |
| 386 | } |
| 387 | |
| 388 | void SyncSchedulerImpl::ScheduleInvalidationNudge( |
| 389 | const TimeDelta& desired_delay, |
[email protected] | 51e6efd | 2014-07-08 23:38:21 | [diff] [blame^] | 390 | syncer::ModelType model_type, |
| 391 | scoped_ptr<InvalidationInterface> invalidation, |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 392 | const tracked_objects::Location& nudge_location) { |
| 393 | DCHECK(CalledOnValidThread()); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 394 | |
| 395 | SDVLOG_LOC(nudge_location, 2) |
| 396 | << "Scheduling sync because we received invalidation for " |
[email protected] | 51e6efd | 2014-07-08 23:38:21 | [diff] [blame^] | 397 | << ModelTypeToString(model_type); |
| 398 | nudge_tracker_.RecordRemoteInvalidation(model_type, invalidation.Pass()); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 399 | ScheduleNudgeImpl(desired_delay, nudge_location); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 402 | // TODO(zea): Consider adding separate throttling/backoff for datatype |
| 403 | // refresh requests. |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 404 | void SyncSchedulerImpl::ScheduleNudgeImpl( |
| 405 | const TimeDelta& delay, |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 406 | const tracked_objects::Location& nudge_location) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 407 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 408 | |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 409 | if (no_scheduling_allowed_) { |
| 410 | NOTREACHED() << "Illegal to schedule job while session in progress."; |
| 411 | return; |
| 412 | } |
| 413 | |
[email protected] | ecf01bd | 2013-01-10 00:42:42 | [diff] [blame] | 414 | if (!started_) { |
| 415 | SDVLOG_LOC(nudge_location, 2) |
| 416 | << "Dropping nudge, scheduler is not running."; |
| 417 | return; |
| 418 | } |
| 419 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 420 | SDVLOG_LOC(nudge_location, 2) |
| 421 | << "In ScheduleNudgeImpl with delay " |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 422 | << delay.InMilliseconds() << " ms"; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 423 | |
| 424 | if (!CanRunNudgeJobNow(NORMAL_PRIORITY)) |
| 425 | return; |
| 426 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 427 | TimeTicks incoming_run_time = TimeTicks::Now() + delay; |
| 428 | if (!scheduled_nudge_time_.is_null() && |
| 429 | (scheduled_nudge_time_ < incoming_run_time)) { |
| 430 | // Old job arrives sooner than this one. Don't reschedule it. |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 434 | // Either there is no existing nudge in flight or the incoming nudge should be |
| 435 | // made to arrive first (preempt) the existing nudge. We reschedule in either |
| 436 | // case. |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 437 | SDVLOG_LOC(nudge_location, 2) |
| 438 | << "Scheduling a nudge with " |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 439 | << delay.InMilliseconds() << " ms delay"; |
| 440 | scheduled_nudge_time_ = incoming_run_time; |
| 441 | pending_wakeup_timer_.Start( |
| 442 | nudge_location, |
| 443 | delay, |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 444 | base::Bind(&SyncSchedulerImpl::PerformDelayedNudge, |
| 445 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | const char* SyncSchedulerImpl::GetModeString(SyncScheduler::Mode mode) { |
| 449 | switch (mode) { |
| 450 | ENUM_CASE(CONFIGURATION_MODE); |
| 451 | ENUM_CASE(NORMAL_MODE); |
| 452 | } |
| 453 | return ""; |
| 454 | } |
| 455 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 456 | void SyncSchedulerImpl::DoNudgeSyncSessionJob(JobPriority priority) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 457 | DCHECK(CalledOnValidThread()); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 458 | DCHECK(CanRunNudgeJobNow(priority)); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 459 | |
[email protected] | a957548 | 2013-12-04 02:51:36 | [diff] [blame] | 460 | DVLOG(2) << "Will run normal mode sync cycle with types " |
[email protected] | 0662c3e | 2014-03-20 20:21:27 | [diff] [blame] | 461 | << ModelTypeSetToString(session_context_->GetEnabledTypes()); |
[email protected] | 0a6e961 | 2013-08-03 01:41:42 | [diff] [blame] | 462 | scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 463 | bool premature_exit = !syncer_->NormalSyncShare( |
| 464 | GetEnabledAndUnthrottledTypes(), |
| 465 | nudge_tracker_, |
| 466 | session.get()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 467 | AdjustPolling(FORCE_RESET); |
[email protected] | a43e34bd | 2013-07-13 15:04:42 | [diff] [blame] | 468 | // Don't run poll job till the next time poll timer fires. |
| 469 | do_poll_after_credentials_updated_ = false; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 470 | |
| 471 | bool success = !premature_exit |
| 472 | && !sessions::HasSyncerError( |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 473 | session->status_controller().model_neutral_state()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 474 | |
| 475 | if (success) { |
| 476 | // That cycle took care of any outstanding work we had. |
| 477 | SDVLOG(2) << "Nudge succeeded."; |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 478 | nudge_tracker_.RecordSuccessfulSyncCycle(); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 479 | scheduled_nudge_time_ = base::TimeTicks(); |
| 480 | |
| 481 | // If we're here, then we successfully reached the server. End all backoff. |
| 482 | wait_interval_.reset(); |
| 483 | NotifyRetryTime(base::Time()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 484 | } else { |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 485 | HandleFailure(session->status_controller().model_neutral_state()); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 486 | } |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 489 | void SyncSchedulerImpl::DoConfigurationSyncSessionJob(JobPriority priority) { |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 490 | DCHECK(CalledOnValidThread()); |
| 491 | DCHECK_EQ(mode_, CONFIGURATION_MODE); |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 492 | DCHECK(pending_configure_params_ != NULL); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 493 | |
| 494 | if (!CanRunJobNow(priority)) { |
| 495 | SDVLOG(2) << "Unable to run configure job right now."; |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 496 | if (!pending_configure_params_->retry_task.is_null()) { |
| 497 | pending_configure_params_->retry_task.Run(); |
| 498 | pending_configure_params_->retry_task.Reset(); |
| 499 | } |
| 500 | return; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 501 | } |
| 502 | |
[email protected] | a957548 | 2013-12-04 02:51:36 | [diff] [blame] | 503 | SDVLOG(2) << "Will run configure SyncShare with types " |
[email protected] | 0662c3e | 2014-03-20 20:21:27 | [diff] [blame] | 504 | << ModelTypeSetToString(session_context_->GetEnabledTypes()); |
[email protected] | 0a6e961 | 2013-08-03 01:41:42 | [diff] [blame] | 505 | scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 506 | bool premature_exit = !syncer_->ConfigureSyncShare( |
[email protected] | f49ac0f6 | 2014-03-25 02:00:25 | [diff] [blame] | 507 | pending_configure_params_->types_to_download, |
[email protected] | 0a6e961 | 2013-08-03 01:41:42 | [diff] [blame] | 508 | pending_configure_params_->source, |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 509 | session.get()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 510 | AdjustPolling(FORCE_RESET); |
[email protected] | a43e34bd | 2013-07-13 15:04:42 | [diff] [blame] | 511 | // Don't run poll job till the next time poll timer fires. |
| 512 | do_poll_after_credentials_updated_ = false; |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 513 | |
| 514 | bool success = !premature_exit |
| 515 | && !sessions::HasSyncerError( |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 516 | session->status_controller().model_neutral_state()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 517 | |
| 518 | if (success) { |
| 519 | SDVLOG(2) << "Configure succeeded."; |
| 520 | pending_configure_params_->ready_task.Run(); |
| 521 | pending_configure_params_.reset(); |
| 522 | |
| 523 | // If we're here, then we successfully reached the server. End all backoff. |
| 524 | wait_interval_.reset(); |
| 525 | NotifyRetryTime(base::Time()); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 526 | } else { |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 527 | HandleFailure(session->status_controller().model_neutral_state()); |
[email protected] | cf6a024 | 2013-11-20 04:13:59 | [diff] [blame] | 528 | // Sync cycle might receive response from server that causes scheduler to |
| 529 | // stop and draws pending_configure_params_ invalid. |
| 530 | if (started_ && !pending_configure_params_->retry_task.is_null()) { |
[email protected] | 3316044 | 2013-11-17 17:44:47 | [diff] [blame] | 531 | pending_configure_params_->retry_task.Run(); |
| 532 | pending_configure_params_->retry_task.Reset(); |
| 533 | } |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 534 | } |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 535 | } |
| 536 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 537 | void SyncSchedulerImpl::HandleFailure( |
| 538 | const sessions::ModelNeutralState& model_neutral_state) { |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 539 | if (IsCurrentlyThrottled()) { |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 540 | SDVLOG(2) << "Was throttled during previous sync cycle."; |
| 541 | RestartWaiting(); |
[email protected] | 580cda6 | 2013-05-16 23:37:30 | [diff] [blame] | 542 | } else if (!IsBackingOff()) { |
| 543 | // Setup our backoff if this is our first such failure. |
| 544 | TimeDelta length = delay_provider_->GetDelay( |
| 545 | delay_provider_->GetInitialDelay(model_neutral_state)); |
| 546 | wait_interval_.reset( |
| 547 | new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 548 | SDVLOG(2) << "Sync cycle failed. Will back off for " |
| 549 | << wait_interval_->length.InMilliseconds() << "ms."; |
| 550 | RestartWaiting(); |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 551 | } |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 554 | void SyncSchedulerImpl::DoPollSyncSessionJob() { |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 555 | base::AutoReset<bool> protector(&no_scheduling_allowed_, true); |
| 556 | |
[email protected] | a957548 | 2013-12-04 02:51:36 | [diff] [blame] | 557 | SDVLOG(2) << "Polling with types " |
[email protected] | 3dfc7b79 | 2014-01-14 16:17:43 | [diff] [blame] | 558 | << ModelTypeSetToString(GetEnabledAndUnthrottledTypes()); |
[email protected] | 0a6e961 | 2013-08-03 01:41:42 | [diff] [blame] | 559 | scoped_ptr<SyncSession> session(SyncSession::Build(session_context_, this)); |
[email protected] | de84cfd | 2013-07-03 03:44:20 | [diff] [blame] | 560 | syncer_->PollSyncShare( |
| 561 | GetEnabledAndUnthrottledTypes(), |
| 562 | session.get()); |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 563 | |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 564 | AdjustPolling(FORCE_RESET); |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 565 | |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 566 | if (IsCurrentlyThrottled()) { |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 567 | SDVLOG(2) << "Poll request got us throttled."; |
| 568 | // The OnSilencedUntil() call set up the WaitInterval for us. All we need |
| 569 | // to do is start the timer. |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 570 | RestartWaiting(); |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 571 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 572 | } |
| 573 | |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 574 | void SyncSchedulerImpl::UpdateNudgeTimeRecords(ModelTypeSet types) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 575 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 576 | base::TimeTicks now = TimeTicks::Now(); |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 577 | // Update timing information for how often datatypes are triggering nudges. |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 578 | for (ModelTypeSet::Iterator iter = types.First(); iter.Good(); iter.Inc()) { |
| 579 | base::TimeTicks previous = last_local_nudges_by_model_type_[iter.Get()]; |
| 580 | last_local_nudges_by_model_type_[iter.Get()] = now; |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 581 | if (previous.is_null()) |
| 582 | continue; |
| 583 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 584 | #define PER_DATA_TYPE_MACRO(type_str) \ |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 585 | SYNC_FREQ_HISTOGRAM("Sync.Freq" type_str, now - previous); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 586 | SYNC_DATA_TYPE_HISTOGRAM(iter.Get()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 587 | #undef PER_DATA_TYPE_MACRO |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 588 | } |
[email protected] | d9f025b | 2012-09-07 12:57:14 | [diff] [blame] | 589 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 590 | |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 591 | TimeDelta SyncSchedulerImpl::GetPollInterval() { |
| 592 | return (!session_context_->notifications_enabled() || |
| 593 | !session_context_->ShouldFetchUpdatesBeforeCommit()) ? |
| 594 | syncer_short_poll_interval_seconds_ : |
| 595 | syncer_long_poll_interval_seconds_; |
| 596 | } |
| 597 | |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 598 | void SyncSchedulerImpl::AdjustPolling(PollAdjustType type) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 599 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 600 | |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 601 | TimeDelta poll = GetPollInterval(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 602 | bool rate_changed = !poll_timer_.IsRunning() || |
| 603 | poll != poll_timer_.GetCurrentDelay(); |
| 604 | |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 605 | if (type == FORCE_RESET) { |
| 606 | last_poll_reset_ = base::TimeTicks::Now(); |
| 607 | if (!rate_changed) |
| 608 | poll_timer_.Reset(); |
| 609 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 610 | |
| 611 | if (!rate_changed) |
| 612 | return; |
| 613 | |
| 614 | // Adjust poll rate. |
| 615 | poll_timer_.Stop(); |
| 616 | poll_timer_.Start(FROM_HERE, poll, this, |
| 617 | &SyncSchedulerImpl::PollTimerCallback); |
| 618 | } |
| 619 | |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 620 | void SyncSchedulerImpl::RestartWaiting() { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 621 | CHECK(wait_interval_.get()); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 622 | DCHECK(wait_interval_->length >= TimeDelta::FromSeconds(0)); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 623 | NotifyRetryTime(base::Time::Now() + wait_interval_->length); |
| 624 | SDVLOG(2) << "Starting WaitInterval timer of length " |
| 625 | << wait_interval_->length.InMilliseconds() << "ms."; |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 626 | if (wait_interval_->mode == WaitInterval::THROTTLED) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 627 | pending_wakeup_timer_.Start( |
| 628 | FROM_HERE, |
| 629 | wait_interval_->length, |
| 630 | base::Bind(&SyncSchedulerImpl::Unthrottle, |
| 631 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 632 | } else { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 633 | pending_wakeup_timer_.Start( |
| 634 | FROM_HERE, |
| 635 | wait_interval_->length, |
[email protected] | 580cda6 | 2013-05-16 23:37:30 | [diff] [blame] | 636 | base::Bind(&SyncSchedulerImpl::ExponentialBackoffRetry, |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 637 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 638 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 639 | } |
| 640 | |
[email protected] | ea29851b | 2013-09-19 01:17:02 | [diff] [blame] | 641 | void SyncSchedulerImpl::Stop() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 642 | DCHECK(CalledOnValidThread()); |
[email protected] | ea29851b | 2013-09-19 01:17:02 | [diff] [blame] | 643 | SDVLOG(2) << "Stop called"; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 644 | |
| 645 | // Kill any in-flight method calls. |
| 646 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 647 | wait_interval_.reset(); |
[email protected] | dcdda431 | 2013-02-26 20:33:05 | [diff] [blame] | 648 | NotifyRetryTime(base::Time()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 649 | poll_timer_.Stop(); |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 650 | pending_wakeup_timer_.Stop(); |
[email protected] | 0248d92 | 2013-04-18 04:52:10 | [diff] [blame] | 651 | pending_configure_params_.reset(); |
| 652 | if (started_) |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 653 | started_ = false; |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 654 | } |
| 655 | |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 656 | // This is the only place where we invoke DoSyncSessionJob with canary |
| 657 | // privileges. Everyone else should use NORMAL_PRIORITY. |
| 658 | void SyncSchedulerImpl::TryCanaryJob() { |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 659 | next_sync_session_job_priority_ = CANARY_PRIORITY; |
| 660 | TrySyncSessionJob(); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 663 | void SyncSchedulerImpl::TrySyncSessionJob() { |
[email protected] | e0428b1 | 2013-12-16 18:52:43 | [diff] [blame] | 664 | // Post call to TrySyncSessionJobImpl on current thread. Later request for |
| 665 | // access token will be here. |
| 666 | base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 667 | &SyncSchedulerImpl::TrySyncSessionJobImpl, |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 668 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | e0428b1 | 2013-12-16 18:52:43 | [diff] [blame] | 669 | } |
| 670 | |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 671 | void SyncSchedulerImpl::TrySyncSessionJobImpl() { |
| 672 | JobPriority priority = next_sync_session_job_priority_; |
| 673 | next_sync_session_job_priority_ = NORMAL_PRIORITY; |
| 674 | |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 675 | nudge_tracker_.SetSyncCycleStartTime(base::TimeTicks::Now()); |
| 676 | |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 677 | DCHECK(CalledOnValidThread()); |
| 678 | if (mode_ == CONFIGURATION_MODE) { |
| 679 | if (pending_configure_params_) { |
| 680 | SDVLOG(2) << "Found pending configure job"; |
| 681 | DoConfigurationSyncSessionJob(priority); |
| 682 | } |
[email protected] | 3dfc7b79 | 2014-01-14 16:17:43 | [diff] [blame] | 683 | } else if (CanRunNudgeJobNow(priority)) { |
| 684 | if (nudge_tracker_.IsSyncRequired()) { |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 685 | SDVLOG(2) << "Found pending nudge job"; |
| 686 | DoNudgeSyncSessionJob(priority); |
| 687 | } else if (do_poll_after_credentials_updated_ || |
| 688 | ((base::TimeTicks::Now() - last_poll_reset_) >= GetPollInterval())) { |
| 689 | DoPollSyncSessionJob(); |
[email protected] | e0428b1 | 2013-12-16 18:52:43 | [diff] [blame] | 690 | // Poll timer fires infrequently. Usually by this time access token is |
| 691 | // already expired and poll job will fail with auth error. Set flag to |
| 692 | // retry poll once ProfileSyncService gets new access token, TryCanaryJob |
| 693 | // will be called after access token is retrieved. |
| 694 | if (HttpResponse::SYNC_AUTH_ERROR == |
| 695 | session_context_->connection_manager()->server_status()) { |
| 696 | do_poll_after_credentials_updated_ = true; |
| 697 | } |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 698 | } |
| 699 | } |
[email protected] | e0428b1 | 2013-12-16 18:52:43 | [diff] [blame] | 700 | |
| 701 | if (priority == CANARY_PRIORITY) { |
| 702 | // If this is canary job then whatever result was don't run poll job till |
| 703 | // the next time poll timer fires. |
| 704 | do_poll_after_credentials_updated_ = false; |
| 705 | } |
| 706 | |
| 707 | if (IsBackingOff() && !pending_wakeup_timer_.IsRunning()) { |
| 708 | // If we succeeded, our wait interval would have been cleared. If it hasn't |
| 709 | // been cleared, then we should increase our backoff interval and schedule |
| 710 | // another retry. |
| 711 | TimeDelta length = delay_provider_->GetDelay(wait_interval_->length); |
| 712 | wait_interval_.reset( |
| 713 | new WaitInterval(WaitInterval::EXPONENTIAL_BACKOFF, length)); |
| 714 | SDVLOG(2) << "Sync cycle failed. Will back off for " |
| 715 | << wait_interval_->length.InMilliseconds() << "ms."; |
| 716 | RestartWaiting(); |
| 717 | } |
[email protected] | 5c135b6 | 2013-11-18 20:54:14 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 720 | void SyncSchedulerImpl::PollTimerCallback() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 721 | DCHECK(CalledOnValidThread()); |
[email protected] | 93afb3b | 2013-03-20 21:01:27 | [diff] [blame] | 722 | if (no_scheduling_allowed_) { |
| 723 | // The no_scheduling_allowed_ flag is set by a function-scoped AutoReset in |
| 724 | // functions that are called only on the sync thread. This function is also |
| 725 | // called only on the sync thread, and only when it is posted by an expiring |
| 726 | // timer. If we find that no_scheduling_allowed_ is set here, then |
| 727 | // something is very wrong. Maybe someone mistakenly called us directly, or |
| 728 | // mishandled the book-keeping for no_scheduling_allowed_. |
| 729 | NOTREACHED() << "Illegal to schedule job while session in progress."; |
| 730 | return; |
| 731 | } |
| 732 | |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 733 | TrySyncSessionJob(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 734 | } |
| 735 | |
[email protected] | 3dfc7b79 | 2014-01-14 16:17:43 | [diff] [blame] | 736 | void SyncSchedulerImpl::RetryTimerCallback() { |
| 737 | TrySyncSessionJob(); |
| 738 | } |
| 739 | |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 740 | void SyncSchedulerImpl::Unthrottle() { |
| 741 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 742 | DCHECK_EQ(WaitInterval::THROTTLED, wait_interval_->mode); |
[email protected] | eeac662c | 2012-10-30 20:31:18 | [diff] [blame] | 743 | |
[email protected] | f643ae0a | 2013-01-26 03:37:24 | [diff] [blame] | 744 | // We're no longer throttled, so clear the wait interval. |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 745 | wait_interval_.reset(); |
[email protected] | dcdda431 | 2013-02-26 20:33:05 | [diff] [blame] | 746 | NotifyRetryTime(base::Time()); |
[email protected] | eb370fc | 2014-03-21 22:14:35 | [diff] [blame] | 747 | NotifyThrottledTypesChanged(nudge_tracker_.GetThrottledTypes()); |
[email protected] | f643ae0a | 2013-01-26 03:37:24 | [diff] [blame] | 748 | |
| 749 | // We treat this as a 'canary' in the sense that it was originally scheduled |
| 750 | // to run some time ago, failed, and we now want to retry, versus a job that |
| 751 | // was just created (e.g via ScheduleNudgeImpl). The main implication is |
| 752 | // that we're careful to update routing info (etc) with such potentially |
| 753 | // stale canary jobs. |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 754 | TryCanaryJob(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 755 | } |
| 756 | |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 757 | void SyncSchedulerImpl::TypeUnthrottle(base::TimeTicks unthrottle_time) { |
| 758 | DCHECK(CalledOnValidThread()); |
| 759 | nudge_tracker_.UpdateTypeThrottlingState(unthrottle_time); |
| 760 | NotifyThrottledTypesChanged(nudge_tracker_.GetThrottledTypes()); |
| 761 | |
| 762 | if (nudge_tracker_.IsAnyTypeThrottled()) { |
[email protected] | 543489a61 | 2014-02-06 09:12:07 | [diff] [blame] | 763 | const base::TimeTicks now = base::TimeTicks::Now(); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 764 | base::TimeDelta time_until_next_unthrottle = |
[email protected] | 543489a61 | 2014-02-06 09:12:07 | [diff] [blame] | 765 | nudge_tracker_.GetTimeUntilNextUnthrottle(now); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 766 | type_unthrottle_timer_.Start( |
| 767 | FROM_HERE, |
| 768 | time_until_next_unthrottle, |
| 769 | base::Bind(&SyncSchedulerImpl::TypeUnthrottle, |
| 770 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 543489a61 | 2014-02-06 09:12:07 | [diff] [blame] | 771 | now + time_until_next_unthrottle)); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // Maybe this is a good time to run a nudge job. Let's try it. |
| 775 | if (nudge_tracker_.IsSyncRequired() && CanRunNudgeJobNow(NORMAL_PRIORITY)) |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 776 | TrySyncSessionJob(); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void SyncSchedulerImpl::PerformDelayedNudge() { |
| 780 | // Circumstances may have changed since we scheduled this delayed nudge. |
| 781 | // We must check to see if it's OK to run the job before we do so. |
| 782 | if (CanRunNudgeJobNow(NORMAL_PRIORITY)) |
[email protected] | fd405e4 | 2014-01-04 03:45:53 | [diff] [blame] | 783 | TrySyncSessionJob(); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 784 | |
| 785 | // We're not responsible for setting up any retries here. The functions that |
| 786 | // first put us into a state that prevents successful sync cycles (eg. global |
| 787 | // throttling, type throttling, network errors, transient errors) will also |
| 788 | // setup the appropriate retry logic (eg. retry after timeout, exponential |
| 789 | // backoff, retry when the network changes). |
| 790 | } |
| 791 | |
[email protected] | 580cda6 | 2013-05-16 23:37:30 | [diff] [blame] | 792 | void SyncSchedulerImpl::ExponentialBackoffRetry() { |
| 793 | TryCanaryJob(); |
[email protected] | 580cda6 | 2013-05-16 23:37:30 | [diff] [blame] | 794 | } |
| 795 | |
[email protected] | dcdda431 | 2013-02-26 20:33:05 | [diff] [blame] | 796 | void SyncSchedulerImpl::NotifyRetryTime(base::Time retry_time) { |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 797 | FOR_EACH_OBSERVER(SyncEngineEventListener, |
| 798 | *session_context_->listeners(), |
| 799 | OnRetryTimeChanged(retry_time)); |
[email protected] | dcdda431 | 2013-02-26 20:33:05 | [diff] [blame] | 800 | } |
| 801 | |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 802 | void SyncSchedulerImpl::NotifyThrottledTypesChanged(ModelTypeSet types) { |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 803 | FOR_EACH_OBSERVER(SyncEngineEventListener, |
| 804 | *session_context_->listeners(), |
| 805 | OnThrottledTypesChanged(types)); |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 806 | } |
| 807 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 808 | bool SyncSchedulerImpl::IsBackingOff() const { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 809 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 810 | return wait_interval_.get() && wait_interval_->mode == |
| 811 | WaitInterval::EXPONENTIAL_BACKOFF; |
| 812 | } |
| 813 | |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 814 | void SyncSchedulerImpl::OnThrottled(const base::TimeDelta& throttle_duration) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 815 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 816 | wait_interval_.reset(new WaitInterval(WaitInterval::THROTTLED, |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 817 | throttle_duration)); |
[email protected] | dcdda431 | 2013-02-26 20:33:05 | [diff] [blame] | 818 | NotifyRetryTime(base::Time::Now() + wait_interval_->length); |
[email protected] | eb370fc | 2014-03-21 22:14:35 | [diff] [blame] | 819 | NotifyThrottledTypesChanged(ModelTypeSet::All()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | d9d8f9f | 2013-06-14 19:45:30 | [diff] [blame] | 822 | void SyncSchedulerImpl::OnTypesThrottled( |
| 823 | ModelTypeSet types, |
| 824 | const base::TimeDelta& throttle_duration) { |
| 825 | base::TimeTicks now = base::TimeTicks::Now(); |
| 826 | |
| 827 | nudge_tracker_.SetTypesThrottledUntil(types, throttle_duration, now); |
| 828 | base::TimeDelta time_until_next_unthrottle = |
| 829 | nudge_tracker_.GetTimeUntilNextUnthrottle(now); |
| 830 | type_unthrottle_timer_.Start( |
| 831 | FROM_HERE, |
| 832 | time_until_next_unthrottle, |
| 833 | base::Bind(&SyncSchedulerImpl::TypeUnthrottle, |
| 834 | weak_ptr_factory_.GetWeakPtr(), |
| 835 | now + time_until_next_unthrottle)); |
| 836 | NotifyThrottledTypesChanged(nudge_tracker_.GetThrottledTypes()); |
| 837 | } |
| 838 | |
| 839 | bool SyncSchedulerImpl::IsCurrentlyThrottled() { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 840 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 841 | return wait_interval_.get() && wait_interval_->mode == |
| 842 | WaitInterval::THROTTLED; |
| 843 | } |
| 844 | |
| 845 | void SyncSchedulerImpl::OnReceivedShortPollIntervalUpdate( |
| 846 | const base::TimeDelta& new_interval) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 847 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 848 | syncer_short_poll_interval_seconds_ = new_interval; |
| 849 | } |
| 850 | |
| 851 | void SyncSchedulerImpl::OnReceivedLongPollIntervalUpdate( |
| 852 | const base::TimeDelta& new_interval) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 853 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 854 | syncer_long_poll_interval_seconds_ = new_interval; |
| 855 | } |
| 856 | |
| 857 | void SyncSchedulerImpl::OnReceivedSessionsCommitDelay( |
| 858 | const base::TimeDelta& new_delay) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 859 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 860 | sessions_commit_delay_ = new_delay; |
| 861 | } |
| 862 | |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 863 | void SyncSchedulerImpl::OnReceivedClientInvalidationHintBufferSize(int size) { |
| 864 | if (size > 0) |
| 865 | nudge_tracker_.SetHintBufferSize(size); |
| 866 | else |
| 867 | NOTREACHED() << "Hint buffer size should be > 0."; |
| 868 | } |
| 869 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 870 | void SyncSchedulerImpl::OnSyncProtocolError( |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 871 | const SyncProtocolError& sync_protocol_error) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 872 | DCHECK(CalledOnValidThread()); |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 873 | if (ShouldRequestEarlyExit(sync_protocol_error)) { |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 874 | SDVLOG(2) << "Sync Scheduler requesting early exit."; |
[email protected] | ea29851b | 2013-09-19 01:17:02 | [diff] [blame] | 875 | Stop(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 876 | } |
[email protected] | 325c350 | 2014-02-11 13:56:43 | [diff] [blame] | 877 | if (IsActionableError(sync_protocol_error)) { |
| 878 | SDVLOG(2) << "OnActionableError"; |
| 879 | FOR_EACH_OBSERVER(SyncEngineEventListener, |
| 880 | *session_context_->listeners(), |
| 881 | OnActionableError(sync_protocol_error)); |
| 882 | } |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 883 | } |
| 884 | |
[email protected] | 3dfc7b79 | 2014-01-14 16:17:43 | [diff] [blame] | 885 | void SyncSchedulerImpl::OnReceivedGuRetryDelay(const base::TimeDelta& delay) { |
[email protected] | 3a48972 | 2014-02-03 23:35:59 | [diff] [blame] | 886 | nudge_tracker_.SetNextRetryTime(TimeTicks::Now() + delay); |
[email protected] | 3dfc7b79 | 2014-01-14 16:17:43 | [diff] [blame] | 887 | retry_timer_.Start(FROM_HERE, delay, this, |
| 888 | &SyncSchedulerImpl::RetryTimerCallback); |
| 889 | } |
| 890 | |
[email protected] | b99f548 | 2014-02-12 23:47:37 | [diff] [blame] | 891 | void SyncSchedulerImpl::OnReceivedMigrationRequest(ModelTypeSet types) { |
| 892 | FOR_EACH_OBSERVER(SyncEngineEventListener, |
| 893 | *session_context_->listeners(), |
| 894 | OnMigrationRequested(types)); |
| 895 | } |
| 896 | |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 897 | void SyncSchedulerImpl::SetNotificationsEnabled(bool notifications_enabled) { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 898 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 899 | session_context_->set_notifications_enabled(notifications_enabled); |
[email protected] | 3e29907 | 2013-05-09 05:51:05 | [diff] [blame] | 900 | if (notifications_enabled) |
| 901 | nudge_tracker_.OnInvalidationsEnabled(); |
| 902 | else |
| 903 | nudge_tracker_.OnInvalidationsDisabled(); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | base::TimeDelta SyncSchedulerImpl::GetSessionsCommitDelay() const { |
[email protected] | 622618f | 2013-04-06 00:22:28 | [diff] [blame] | 907 | DCHECK(CalledOnValidThread()); |
[email protected] | 6d2dc98 | 2012-07-19 22:19:18 | [diff] [blame] | 908 | return sessions_commit_delay_; |
| 909 | } |
| 910 | |
| 911 | #undef SDVLOG_LOC |
| 912 | |
| 913 | #undef SDVLOG |
| 914 | |
| 915 | #undef SLOG |
| 916 | |
| 917 | #undef ENUM_CASE |
| 918 | |
| 919 | } // namespace syncer |