| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ | 5 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ |
| 6 #define COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ | 6 #define COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/threading/non_thread_safe.h" | 19 #include "base/threading/non_thread_safe.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
| 22 #include "components/sync/base/weak_handle.h" | 22 #include "components/sync/base/weak_handle.h" |
| 23 #include "components/sync/engine/polling_constants.h" | 23 #include "components/sync/engine/polling_constants.h" |
| 24 #include "components/sync/engine_impl/cycle/nudge_tracker.h" |
| 25 #include "components/sync/engine_impl/cycle/sync_cycle.h" |
| 26 #include "components/sync/engine_impl/cycle/sync_cycle_context.h" |
| 24 #include "components/sync/engine_impl/net/server_connection_manager.h" | 27 #include "components/sync/engine_impl/net/server_connection_manager.h" |
| 25 #include "components/sync/engine_impl/nudge_source.h" | 28 #include "components/sync/engine_impl/nudge_source.h" |
| 26 #include "components/sync/engine_impl/sync_scheduler.h" | 29 #include "components/sync/engine_impl/sync_scheduler.h" |
| 27 #include "components/sync/engine_impl/syncer.h" | 30 #include "components/sync/engine_impl/syncer.h" |
| 28 #include "components/sync/sessions_impl/nudge_tracker.h" | |
| 29 #include "components/sync/sessions_impl/sync_session.h" | |
| 30 #include "components/sync/sessions_impl/sync_session_context.h" | |
| 31 | 31 |
| 32 namespace syncer { | 32 namespace syncer { |
| 33 | 33 |
| 34 class BackoffDelayProvider; | 34 class BackoffDelayProvider; |
| 35 | |
| 36 namespace sessions { | |
| 37 struct ModelNeutralState; | 35 struct ModelNeutralState; |
| 38 } | |
| 39 | 36 |
| 40 class SyncSchedulerImpl : public SyncScheduler, public base::NonThreadSafe { | 37 class SyncSchedulerImpl : public SyncScheduler, public base::NonThreadSafe { |
| 41 public: | 38 public: |
| 42 // |name| is a display string to identify the syncer thread. Takes | 39 // |name| is a display string to identify the syncer thread. Takes |
| 43 // |ownership of |syncer| and |delay_provider|. | 40 // |ownership of |syncer| and |delay_provider|. |
| 44 SyncSchedulerImpl(const std::string& name, | 41 SyncSchedulerImpl(const std::string& name, |
| 45 BackoffDelayProvider* delay_provider, | 42 BackoffDelayProvider* delay_provider, |
| 46 sessions::SyncSessionContext* context, | 43 SyncCycleContext* context, |
| 47 Syncer* syncer); | 44 Syncer* syncer); |
| 48 | 45 |
| 49 // Calls Stop(). | 46 // Calls Stop(). |
| 50 ~SyncSchedulerImpl() override; | 47 ~SyncSchedulerImpl() override; |
| 51 | 48 |
| 52 void Start(Mode mode, base::Time last_poll_time) override; | 49 void Start(Mode mode, base::Time last_poll_time) override; |
| 53 void ScheduleConfiguration(const ConfigurationParams& params) override; | 50 void ScheduleConfiguration(const ConfigurationParams& params) override; |
| 54 void ScheduleClearServerData(const ClearParams& params) override; | 51 void ScheduleClearServerData(const ClearParams& params) override; |
| 55 void Stop() override; | 52 void Stop() override; |
| 56 void ScheduleLocalNudge( | 53 void ScheduleLocalNudge( |
| 57 ModelTypeSet types, | 54 ModelTypeSet types, |
| 58 const tracked_objects::Location& nudge_location) override; | 55 const tracked_objects::Location& nudge_location) override; |
| 59 void ScheduleLocalRefreshRequest( | 56 void ScheduleLocalRefreshRequest( |
| 60 ModelTypeSet types, | 57 ModelTypeSet types, |
| 61 const tracked_objects::Location& nudge_location) override; | 58 const tracked_objects::Location& nudge_location) override; |
| 62 void ScheduleInvalidationNudge( | 59 void ScheduleInvalidationNudge( |
| 63 syncer::ModelType type, | 60 syncer::ModelType type, |
| 64 std::unique_ptr<InvalidationInterface> invalidation, | 61 std::unique_ptr<InvalidationInterface> invalidation, |
| 65 const tracked_objects::Location& nudge_location) override; | 62 const tracked_objects::Location& nudge_location) override; |
| 66 void ScheduleInitialSyncNudge(syncer::ModelType model_type) override; | 63 void ScheduleInitialSyncNudge(syncer::ModelType model_type) override; |
| 67 void SetNotificationsEnabled(bool notifications_enabled) override; | 64 void SetNotificationsEnabled(bool notifications_enabled) override; |
| 68 | 65 |
| 69 void OnCredentialsUpdated() override; | 66 void OnCredentialsUpdated() override; |
| 70 void OnConnectionStatusChange() override; | 67 void OnConnectionStatusChange() override; |
| 71 | 68 |
| 72 // SyncSession::Delegate implementation. | 69 // SyncCycle::Delegate implementation. |
| 73 void OnThrottled(const base::TimeDelta& throttle_duration) override; | 70 void OnThrottled(const base::TimeDelta& throttle_duration) override; |
| 74 void OnTypesThrottled(ModelTypeSet types, | 71 void OnTypesThrottled(ModelTypeSet types, |
| 75 const base::TimeDelta& throttle_duration) override; | 72 const base::TimeDelta& throttle_duration) override; |
| 76 bool IsCurrentlyThrottled() override; | 73 bool IsCurrentlyThrottled() override; |
| 77 void OnReceivedShortPollIntervalUpdate( | 74 void OnReceivedShortPollIntervalUpdate( |
| 78 const base::TimeDelta& new_interval) override; | 75 const base::TimeDelta& new_interval) override; |
| 79 void OnReceivedLongPollIntervalUpdate( | 76 void OnReceivedLongPollIntervalUpdate( |
| 80 const base::TimeDelta& new_interval) override; | 77 const base::TimeDelta& new_interval) override; |
| 81 void OnReceivedCustomNudgeDelays( | 78 void OnReceivedCustomNudgeDelays( |
| 82 const std::map<ModelType, base::TimeDelta>& nudge_delays) override; | 79 const std::map<ModelType, base::TimeDelta>& nudge_delays) override; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 135 |
| 139 Mode mode; | 136 Mode mode; |
| 140 base::TimeDelta length; | 137 base::TimeDelta length; |
| 141 }; | 138 }; |
| 142 | 139 |
| 143 static const char* GetModeString(Mode mode); | 140 static const char* GetModeString(Mode mode); |
| 144 | 141 |
| 145 void SetDefaultNudgeDelay(base::TimeDelta delay_ms); | 142 void SetDefaultNudgeDelay(base::TimeDelta delay_ms); |
| 146 | 143 |
| 147 // Invoke the syncer to perform a nudge job. | 144 // Invoke the syncer to perform a nudge job. |
| 148 void DoNudgeSyncSessionJob(JobPriority priority); | 145 void DoNudgeSyncCycleJob(JobPriority priority); |
| 149 | 146 |
| 150 // Invoke the syncer to perform a configuration job. | 147 // Invoke the syncer to perform a configuration job. |
| 151 void DoConfigurationSyncSessionJob(JobPriority priority); | 148 void DoConfigurationSyncCycleJob(JobPriority priority); |
| 152 | 149 |
| 153 void DoClearServerDataSyncSessionJob(JobPriority priority); | 150 void DoClearServerDataSyncCycleJob(JobPriority priority); |
| 154 | 151 |
| 155 // Helper function for Do{Nudge,Configuration,Poll}SyncSessionJob. | 152 // Helper function for Do{Nudge,Configuration,Poll}SyncCycleJob. |
| 156 void HandleSuccess(); | 153 void HandleSuccess(); |
| 157 | 154 |
| 158 // Helper function for Do{Nudge,Configuration,Poll}SyncSessionJob. | 155 // Helper function for Do{Nudge,Configuration,Poll}SyncCycleJob. |
| 159 void HandleFailure(const sessions::ModelNeutralState& model_neutral_state); | 156 void HandleFailure(const ModelNeutralState& model_neutral_state); |
| 160 | 157 |
| 161 // Invoke the Syncer to perform a poll job. | 158 // Invoke the Syncer to perform a poll job. |
| 162 void DoPollSyncSessionJob(); | 159 void DoPollSyncCycleJob(); |
| 163 | 160 |
| 164 // Helper function to calculate poll interval. | 161 // Helper function to calculate poll interval. |
| 165 base::TimeDelta GetPollInterval(); | 162 base::TimeDelta GetPollInterval(); |
| 166 | 163 |
| 167 // Adjusts the poll timer to account for new poll interval, and possibly | 164 // Adjusts the poll timer to account for new poll interval, and possibly |
| 168 // resets the poll interval, depedning on the flag's value. | 165 // resets the poll interval, depedning on the flag's value. |
| 169 void AdjustPolling(PollAdjustType type); | 166 void AdjustPolling(PollAdjustType type); |
| 170 | 167 |
| 171 // Helper to restart waiting with |wait_interval_|'s timer. | 168 // Helper to restart waiting with |wait_interval_|'s timer. |
| 172 void RestartWaiting(); | 169 void RestartWaiting(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 187 // Helper to signal listeners about changed retry time. | 184 // Helper to signal listeners about changed retry time. |
| 188 void NotifyRetryTime(base::Time retry_time); | 185 void NotifyRetryTime(base::Time retry_time); |
| 189 | 186 |
| 190 // Helper to signal listeners about changed throttled types. | 187 // Helper to signal listeners about changed throttled types. |
| 191 void NotifyThrottledTypesChanged(ModelTypeSet types); | 188 void NotifyThrottledTypesChanged(ModelTypeSet types); |
| 192 | 189 |
| 193 // Looks for pending work and, if it finds any, run this work at "canary" | 190 // Looks for pending work and, if it finds any, run this work at "canary" |
| 194 // priority. | 191 // priority. |
| 195 void TryCanaryJob(); | 192 void TryCanaryJob(); |
| 196 | 193 |
| 197 // At the moment TrySyncSessionJob just posts call to TrySyncSessionJobImpl on | 194 // At the moment TrySyncCycleJob just posts call to TrySyncCycleJobImpl on |
| 198 // current thread. In the future it will request access token here. | 195 // current thread. In the future it will request access token here. |
| 199 void TrySyncSessionJob(); | 196 void TrySyncCycleJob(); |
| 200 void TrySyncSessionJobImpl(); | 197 void TrySyncCycleJobImpl(); |
| 201 | 198 |
| 202 // Transitions out of the THROTTLED WaitInterval then calls TryCanaryJob(). | 199 // Transitions out of the THROTTLED WaitInterval then calls TryCanaryJob(). |
| 203 void Unthrottle(); | 200 void Unthrottle(); |
| 204 | 201 |
| 205 // Called when a per-type throttling interval expires. | 202 // Called when a per-type throttling interval expires. |
| 206 void TypeUnthrottle(base::TimeTicks unthrottle_time); | 203 void TypeUnthrottle(base::TimeTicks unthrottle_time); |
| 207 | 204 |
| 208 // Runs a normal nudge job when the scheduled timer expires. | 205 // Runs a normal nudge job when the scheduled timer expires. |
| 209 void PerformDelayedNudge(); | 206 void PerformDelayedNudge(); |
| 210 | 207 |
| 211 // Attempts to exit EXPONENTIAL_BACKOFF by calling TryCanaryJob(). | 208 // Attempts to exit EXPONENTIAL_BACKOFF by calling TryCanaryJob(). |
| 212 void ExponentialBackoffRetry(); | 209 void ExponentialBackoffRetry(); |
| 213 | 210 |
| 214 // Called when the root cause of the current connection error is fixed. | 211 // Called when the root cause of the current connection error is fixed. |
| 215 void OnServerConnectionErrorFixed(); | 212 void OnServerConnectionErrorFixed(); |
| 216 | 213 |
| 217 // Creates a session for a poll and performs the sync. | 214 // Creates a cycle for a poll and performs the sync. |
| 218 void PollTimerCallback(); | 215 void PollTimerCallback(); |
| 219 | 216 |
| 220 // Creates a session for a retry and performs the sync. | 217 // Creates a cycle for a retry and performs the sync. |
| 221 void RetryTimerCallback(); | 218 void RetryTimerCallback(); |
| 222 | 219 |
| 223 // Returns the set of types that are enabled and not currently throttled. | 220 // Returns the set of types that are enabled and not currently throttled. |
| 224 ModelTypeSet GetEnabledAndUnthrottledTypes(); | 221 ModelTypeSet GetEnabledAndUnthrottledTypes(); |
| 225 | 222 |
| 226 // Called as we are started to broadcast an initial session snapshot | 223 // Called as we are started to broadcast an initial cycle snapshot |
| 227 // containing data like initial_sync_ended. Important when the client starts | 224 // containing data like initial_sync_ended. Important when the client starts |
| 228 // up and does not need to perform an initial sync. | 225 // up and does not need to perform an initial sync. |
| 229 void SendInitialSnapshot(); | 226 void SendInitialSnapshot(); |
| 230 | 227 |
| 231 // This is used for histogramming and analysis of ScheduleNudge* APIs. | 228 // This is used for histogramming and analysis of ScheduleNudge* APIs. |
| 232 // SyncScheduler is the ultimate choke-point for all such invocations (with | 229 // SyncScheduler is the ultimate choke-point for all such invocations (with |
| 233 // and without InvalidationState variants, all NudgeSources, etc) and as such | 230 // and without InvalidationState variants, all NudgeSources, etc) and as such |
| 234 // is the most flexible place to do this bookkeeping. | 231 // is the most flexible place to do this bookkeeping. |
| 235 void UpdateNudgeTimeRecords(ModelTypeSet types); | 232 void UpdateNudgeTimeRecords(ModelTypeSet types); |
| 236 | 233 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 // Storage for variables related to an in-progress configure request. Note | 267 // Storage for variables related to an in-progress configure request. Note |
| 271 // that (mode_ != CONFIGURATION_MODE) \implies !pending_configure_params_. | 268 // that (mode_ != CONFIGURATION_MODE) \implies !pending_configure_params_. |
| 272 std::unique_ptr<ConfigurationParams> pending_configure_params_; | 269 std::unique_ptr<ConfigurationParams> pending_configure_params_; |
| 273 | 270 |
| 274 std::unique_ptr<ClearParams> pending_clear_params_; | 271 std::unique_ptr<ClearParams> pending_clear_params_; |
| 275 | 272 |
| 276 // If we have a nudge pending to run soon, it will be listed here. | 273 // If we have a nudge pending to run soon, it will be listed here. |
| 277 base::TimeTicks scheduled_nudge_time_; | 274 base::TimeTicks scheduled_nudge_time_; |
| 278 | 275 |
| 279 // Keeps track of work that the syncer needs to handle. | 276 // Keeps track of work that the syncer needs to handle. |
| 280 sessions::NudgeTracker nudge_tracker_; | 277 NudgeTracker nudge_tracker_; |
| 281 | 278 |
| 282 // Invoked to run through the sync cycle. | 279 // Invoked to run through the sync cycle. |
| 283 std::unique_ptr<Syncer> syncer_; | 280 std::unique_ptr<Syncer> syncer_; |
| 284 | 281 |
| 285 sessions::SyncSessionContext* session_context_; | 282 SyncCycleContext* cycle_context_; |
| 286 | 283 |
| 287 // A map tracking LOCAL NudgeSource invocations of ScheduleNudge* APIs, | 284 // A map tracking LOCAL NudgeSource invocations of ScheduleNudge* APIs, |
| 288 // organized by datatype. Each datatype that was part of the types requested | 285 // organized by datatype. Each datatype that was part of the types requested |
| 289 // in the call will have its TimeTicks value updated. | 286 // in the call will have its TimeTicks value updated. |
| 290 typedef std::map<ModelType, base::TimeTicks> ModelTypeTimeMap; | 287 typedef std::map<ModelType, base::TimeTicks> ModelTypeTimeMap; |
| 291 ModelTypeTimeMap last_local_nudges_by_model_type_; | 288 ModelTypeTimeMap last_local_nudges_by_model_type_; |
| 292 | 289 |
| 293 // Used as an "anti-reentrancy defensive assertion". | 290 // Used as an "anti-reentrancy defensive assertion". |
| 294 // While true, it is illegal for any new scheduling activity to take place. | 291 // While true, it is illegal for any new scheduling activity to take place. |
| 295 // Ensures that higher layers don't break this law in response to events that | 292 // Ensures that higher layers don't break this law in response to events that |
| 296 // take place during a sync cycle. We call this out because such violations | 293 // take place during a sync cycle. We call this out because such violations |
| 297 // could result in tight sync loops hitting sync servers. | 294 // could result in tight sync loops hitting sync servers. |
| 298 bool no_scheduling_allowed_; | 295 bool no_scheduling_allowed_; |
| 299 | 296 |
| 300 // TryJob might get called for multiple reasons. It should only call | 297 // TryJob might get called for multiple reasons. It should only call |
| 301 // DoPollSyncSessionJob after some time since the last attempt. | 298 // DoPollSyncCycleJob after some time since the last attempt. |
| 302 // last_poll_reset_ keeps track of when was last attempt. | 299 // last_poll_reset_ keeps track of when was last attempt. |
| 303 base::TimeTicks last_poll_reset_; | 300 base::TimeTicks last_poll_reset_; |
| 304 | 301 |
| 305 // next_sync_session_job_priority_ defines which priority will be used next | 302 // next_sync_cycle_job_priority_ defines which priority will be used next |
| 306 // time TrySyncSessionJobImpl is called. CANARY_PRIORITY allows syncer to run | 303 // time TrySyncCycleJobImpl is called. CANARY_PRIORITY allows syncer to run |
| 307 // even if scheduler is in exponential backoff. This is needed for events that | 304 // even if scheduler is in exponential backoff. This is needed for events that |
| 308 // have chance of resolving previous error (e.g. network connection change | 305 // have chance of resolving previous error (e.g. network connection change |
| 309 // after NETWORK_UNAVAILABLE error). | 306 // after NETWORK_UNAVAILABLE error). |
| 310 // It is reset back to NORMAL_PRIORITY on every call to TrySyncSessionJobImpl. | 307 // It is reset back to NORMAL_PRIORITY on every call to TrySyncCycleJobImpl. |
| 311 JobPriority next_sync_session_job_priority_; | 308 JobPriority next_sync_cycle_job_priority_; |
| 312 | 309 |
| 313 // One-shot timer for scheduling GU retry according to delay set by server. | 310 // One-shot timer for scheduling GU retry according to delay set by server. |
| 314 base::OneShotTimer retry_timer_; | 311 base::OneShotTimer retry_timer_; |
| 315 | 312 |
| 316 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_; | 313 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_; |
| 317 | 314 |
| 318 // A second factory specially for weak_handle_this_, to allow the handle | 315 // A second factory specially for weak_handle_this_, to allow the handle |
| 319 // to be const and alleviate threading concerns. | 316 // to be const and alleviate threading concerns. |
| 320 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_for_weak_handle_; | 317 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_for_weak_handle_; |
| 321 | 318 |
| 322 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); | 319 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl); |
| 323 }; | 320 }; |
| 324 | 321 |
| 325 } // namespace syncer | 322 } // namespace syncer |
| 326 | 323 |
| 327 #endif // COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ | 324 #endif // COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_IMPL_H_ |
| OLD | NEW |