Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A class to run the syncer on a thread. | 5 // A class to run the syncer on a thread. |
| 6 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 6 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| 7 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 7 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| 8 #pragma once | 8 #pragma once |
| 9 | 9 |
| 10 #include "base/callback.h" | |
| 10 #include "base/linked_ptr.h" | 11 #include "base/linked_ptr.h" |
| 11 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 12 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
| 13 #include "base/task.h" | 14 #include "base/task.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "base/timer.h" | 17 #include "base/timer.h" |
| 17 #include "chrome/browser/sync/engine/nudge_source.h" | 18 #include "chrome/browser/sync/engine/nudge_source.h" |
| 18 #include "chrome/browser/sync/engine/polling_constants.h" | 19 #include "chrome/browser/sync/engine/polling_constants.h" |
| 19 #include "chrome/browser/sync/engine/syncer.h" | 20 #include "chrome/browser/sync/engine/syncer.h" |
| 20 #include "chrome/browser/sync/syncable/model_type_payload_map.h" | 21 #include "chrome/browser/sync/syncable/model_type_payload_map.h" |
| 22 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | |
| 21 #include "chrome/browser/sync/sessions/sync_session.h" | 23 #include "chrome/browser/sync/sessions/sync_session.h" |
| 22 #include "chrome/browser/sync/sessions/sync_session_context.h" | 24 #include "chrome/browser/sync/sessions/sync_session_context.h" |
| 23 | 25 |
| 24 namespace browser_sync { | 26 namespace browser_sync { |
| 25 | 27 |
| 26 struct ServerConnectionEvent; | 28 struct ServerConnectionEvent; |
| 27 | 29 |
| 28 namespace s3 { | 30 namespace s3 { |
| 29 | 31 |
| 30 class SyncerThread : public sessions::SyncSession::Delegate { | 32 class SyncerThread : public sessions::SyncSession::Delegate, |
| 33 public ServerConnectionEventListener { | |
| 31 public: | 34 public: |
| 32 enum Mode { | 35 enum Mode { |
| 33 // In this mode, the thread only performs configuration tasks. This is | 36 // In this mode, the thread only performs configuration tasks. This is |
| 34 // designed to make the case where we want to download updates for a | 37 // designed to make the case where we want to download updates for a |
| 35 // specific type only, and not continue syncing until we are moved into | 38 // specific type only, and not continue syncing until we are moved into |
| 36 // normal mode. | 39 // normal mode. |
| 37 CONFIGURATION_MODE, | 40 CONFIGURATION_MODE, |
| 38 // Resumes polling and allows nudges, drops configuration tasks. Runs | 41 // Resumes polling and allows nudges, drops configuration tasks. Runs |
| 39 // through entire sync cycle. | 42 // through entire sync cycle. |
| 40 NORMAL_MODE, | 43 NORMAL_MODE, |
| 41 }; | 44 }; |
| 42 | 45 |
| 43 // Takes ownership of both |context| and |syncer|. | 46 // Takes ownership of both |context| and |syncer|. |
| 44 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer); | 47 SyncerThread(sessions::SyncSessionContext* context, Syncer* syncer); |
| 45 virtual ~SyncerThread(); | 48 virtual ~SyncerThread(); |
| 46 | 49 |
| 50 typedef Callback0::Type ModeChangeCallback; | |
| 51 | |
| 47 // Change the mode of operation. | 52 // Change the mode of operation. |
| 48 // We don't use a lock when changing modes, so we won't cause currently | 53 // We don't use a lock when changing modes, so we won't cause currently |
| 49 // scheduled jobs to adhere to the new mode. We could protect it, but it | 54 // scheduled jobs to adhere to the new mode. We could protect it, but it |
| 50 // doesn't buy very much as a) a session could already be in progress and it | 55 // doesn't buy very much as a) a session could already be in progress and it |
| 51 // will continue no matter what, b) the scheduled sessions already contain | 56 // will continue no matter what, b) the scheduled sessions already contain |
| 52 // all their required state and won't be affected by potential change at | 57 // all their required state and won't be affected by potential change at |
| 53 // higher levels (i.e. the registrar), and c) we service tasks FIFO, so once | 58 // higher levels (i.e. the registrar), and c) we service tasks FIFO, so once |
| 54 // the mode changes all future jobs will be run against the updated mode. | 59 // the mode changes all future jobs will be run against the updated mode. |
| 55 void Start(Mode mode); | 60 // If supplied, |callback| will be invoked when the mode has been |
| 61 // changed to |mode| *from the SyncerThread*, and not from the caller | |
| 62 // thread. | |
| 63 void Start(Mode mode, ModeChangeCallback* callback); | |
| 56 | 64 |
| 57 // Joins on the thread as soon as possible (currently running session | 65 // Joins on the thread as soon as possible (currently running session |
| 58 // completes). | 66 // completes). |
| 59 void Stop(); | 67 void Stop(); |
| 60 | 68 |
| 61 // The meat and potatoes. | 69 // The meat and potatoes. |
| 62 void ScheduleNudge(const base::TimeDelta& delay, NudgeSource source, | 70 void ScheduleNudge(const base::TimeDelta& delay, NudgeSource source, |
| 63 const syncable::ModelTypeBitSet& types); | 71 const syncable::ModelTypeBitSet& types); |
| 64 void ScheduleNudgeWithPayloads( | 72 void ScheduleNudgeWithPayloads( |
| 65 const base::TimeDelta& delay, NudgeSource source, | 73 const base::TimeDelta& delay, NudgeSource source, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 77 | 85 |
| 78 // SyncSession::Delegate implementation. | 86 // SyncSession::Delegate implementation. |
| 79 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until); | 87 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until); |
| 80 virtual bool IsSyncingCurrentlySilenced(); | 88 virtual bool IsSyncingCurrentlySilenced(); |
| 81 virtual void OnReceivedShortPollIntervalUpdate( | 89 virtual void OnReceivedShortPollIntervalUpdate( |
| 82 const base::TimeDelta& new_interval); | 90 const base::TimeDelta& new_interval); |
| 83 virtual void OnReceivedLongPollIntervalUpdate( | 91 virtual void OnReceivedLongPollIntervalUpdate( |
| 84 const base::TimeDelta& new_interval); | 92 const base::TimeDelta& new_interval); |
| 85 virtual void OnShouldStopSyncingPermanently(); | 93 virtual void OnShouldStopSyncingPermanently(); |
| 86 | 94 |
| 95 // ServerConnectionEventListener implementation. | |
| 96 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute? | |
| 97 virtual void OnServerConnectionEvent(const ServerConnectionEvent2& event); | |
| 98 | |
| 87 private: | 99 private: |
| 88 friend class SyncerThread2Test; | 100 friend class SyncerThread2Test; |
| 89 | 101 |
| 90 // State pertaining to exponential backoff or throttling periods. | 102 // State pertaining to exponential backoff or throttling periods. |
| 91 struct WaitInterval; | 103 struct WaitInterval; |
| 92 | 104 |
| 93 // An enum used to describe jobs for scheduling purposes. | 105 // An enum used to describe jobs for scheduling purposes. |
| 94 enum SyncSessionJobPurpose { | 106 enum SyncSessionJobPurpose { |
| 95 // Our poll timer schedules POLL jobs periodically based on a server | 107 // Our poll timer schedules POLL jobs periodically based on a server |
| 96 // assigned poll interval. | 108 // assigned poll interval. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 126 SyncSessionJobPurpose purpose, | 138 SyncSessionJobPurpose purpose, |
| 127 sessions::SyncSession* session); | 139 sessions::SyncSession* session); |
| 128 | 140 |
| 129 // Invoke the Syncer to perform a sync. | 141 // Invoke the Syncer to perform a sync. |
| 130 void DoSyncSessionJob(const SyncSessionJob& job); | 142 void DoSyncSessionJob(const SyncSessionJob& job); |
| 131 | 143 |
| 132 // Called after the Syncer has performed the sync represented by |job|, to | 144 // Called after the Syncer has performed the sync represented by |job|, to |
| 133 // reset our state. | 145 // reset our state. |
| 134 void FinishSyncSessionJob(const SyncSessionJob& job); | 146 void FinishSyncSessionJob(const SyncSessionJob& job); |
| 135 | 147 |
| 148 void UpdateCarryoverSessionState(const SyncSessionJob& old_job); | |
|
Nicolas Zea
2011/03/25 18:53:03
Why is it important to do this?
tim (not reviewing)
2011/03/28 20:02:25
added comment.
| |
| 149 | |
| 136 // Helper to FinishSyncSessionJob to schedule the next sync operation. | 150 // Helper to FinishSyncSessionJob to schedule the next sync operation. |
| 137 void ScheduleNextSync(const SyncSessionJob& old_job); | 151 void ScheduleNextSync(const SyncSessionJob& old_job); |
| 138 | 152 |
| 139 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. | 153 // Helper to configure polling intervals. Used by Start and ScheduleNextSync. |
| 140 void AdjustPolling(const SyncSessionJob* old_job); | 154 void AdjustPolling(const SyncSessionJob* old_job); |
| 141 | 155 |
| 142 // Helper to ScheduleNextSync in case of consecutive sync errors. | 156 // Helper to ScheduleNextSync in case of consecutive sync errors. |
| 143 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job); | 157 void HandleConsecutiveContinuationError(const SyncSessionJob& old_job); |
| 144 | 158 |
| 145 // Determines if it is legal to run a sync job for |purpose| at | 159 // Determines if it is legal to run a sync job for |purpose| at |
| 146 // |scheduled_start|. This checks current operational mode, backoff or | 160 // |scheduled_start|. This checks current operational mode, backoff or |
| 147 // throttling, freshness (so we don't make redundant syncs), and connection. | 161 // throttling, freshness (so we don't make redundant syncs), and connection. |
| 148 bool ShouldRunJob(SyncSessionJobPurpose purpose, | 162 bool ShouldRunJob(SyncSessionJobPurpose purpose, |
| 149 const base::TimeTicks& scheduled_start); | 163 const base::TimeTicks& scheduled_start); |
| 150 | 164 |
| 151 // 'Impl' here refers to real implementation of public functions, running on | 165 // 'Impl' here refers to real implementation of public functions, running on |
| 152 // |thread_|. | 166 // |thread_|. |
| 153 void StartImpl(Mode mode); | 167 void StartImpl(Mode mode, linked_ptr<ModeChangeCallback> callback); |
| 154 void ScheduleNudgeImpl( | 168 void ScheduleNudgeImpl( |
| 155 const base::TimeDelta& delay, | 169 const base::TimeDelta& delay, |
| 156 NudgeSource source, | 170 NudgeSource source, |
| 157 const syncable::ModelTypePayloadMap& types_with_payloads); | 171 const syncable::ModelTypePayloadMap& types_with_payloads); |
| 158 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, | 172 void ScheduleConfigImpl(const ModelSafeRoutingInfo& routing_info, |
| 159 const std::vector<ModelSafeWorker*>& workers); | 173 const std::vector<ModelSafeWorker*>& workers); |
| 160 void ScheduleClearUserDataImpl(); | 174 void ScheduleClearUserDataImpl(); |
| 161 | 175 |
| 162 // Returns true if the client is currently in exponential backoff. | 176 // Returns true if the client is currently in exponential backoff. |
| 163 bool IsBackingOff() const; | 177 bool IsBackingOff() const; |
| 164 | 178 |
| 165 // Helper to signal all listeners registered with |session_context_|. | 179 // Helper to signal all listeners registered with |session_context_|. |
| 166 void Notify(SyncEngineEvent::EventCause cause); | 180 void Notify(SyncEngineEvent::EventCause cause); |
| 167 | 181 |
| 168 // ServerConnectionEventListener implementation. | |
| 169 // TODO(tim): schedule a nudge when valid connection detected? in 1 minute? | |
| 170 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event); | |
| 171 | |
| 172 // Callback to change backoff state. | 182 // Callback to change backoff state. |
| 173 void DoCanaryJob(); | 183 void DoCanaryJob(); |
| 174 void Unthrottle(); | 184 void Unthrottle(); |
| 175 | 185 |
| 176 // Creates a session for a poll and performs the sync. | 186 // Creates a session for a poll and performs the sync. |
| 177 void PollTimerCallback(); | 187 void PollTimerCallback(); |
| 178 | 188 |
| 179 // Assign |start| and |end| to appropriate SyncerStep values for the | 189 // Assign |start| and |end| to appropriate SyncerStep values for the |
| 180 // specified |purpose|. | 190 // specified |purpose|. |
| 181 void SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, | 191 void SetSyncerStepsForPurpose(SyncSessionJobPurpose purpose, |
| 182 SyncerStep* start, | 192 SyncerStep* start, |
| 183 SyncerStep* end); | 193 SyncerStep* end); |
| 184 | 194 |
| 195 // Initializes the hookup between the ServerConnectionManager and us. | |
| 196 void WatchConnectionManager(); | |
| 197 | |
| 198 // Used to update |server_connection_ok_|, see below. | |
| 199 void CheckServerConnectionManagerStatus( | |
| 200 HttpResponse::ServerConnectionCode code); | |
| 201 | |
| 202 // Called once the first time thread_ is started to broadcast an initial | |
| 203 // session snapshot containing data like initial_sync_ended. Important when | |
| 204 // the client starts up and does not need to perform an initial sync. | |
| 205 void SendInitialSnapshot(); | |
| 206 | |
| 185 base::Thread thread_; | 207 base::Thread thread_; |
| 186 | 208 |
| 187 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be | 209 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be |
| 188 // updated by the server. | 210 // updated by the server. |
| 189 base::TimeDelta syncer_short_poll_interval_seconds_; | 211 base::TimeDelta syncer_short_poll_interval_seconds_; |
| 190 base::TimeDelta syncer_long_poll_interval_seconds_; | 212 base::TimeDelta syncer_long_poll_interval_seconds_; |
| 191 | 213 |
| 192 // Periodic timer for polling. See AdjustPolling. | 214 // Periodic timer for polling. See AdjustPolling. |
| 193 base::RepeatingTimer<SyncerThread> poll_timer_; | 215 base::RepeatingTimer<SyncerThread> poll_timer_; |
| 194 | 216 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 220 | 242 |
| 221 } // namespace s3 | 243 } // namespace s3 |
| 222 | 244 |
| 223 } // namespace browser_sync | 245 } // namespace browser_sync |
| 224 | 246 |
| 225 // The SyncerThread manages its own internal thread and thus outlives it. We | 247 // The SyncerThread manages its own internal thread and thus outlives it. We |
| 226 // don't need refcounting for posting tasks to this internal thread. | 248 // don't need refcounting for posting tasks to this internal thread. |
| 227 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread); | 249 DISABLE_RUNNABLE_METHOD_REFCOUNT(browser_sync::s3::SyncerThread); |
| 228 | 250 |
| 229 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ | 251 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD2_H_ |
| OLD | NEW |