| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // This is the default implementation of SyncerThread whose Stop implementation | 6 // This is the default implementation of SyncerThread whose Stop implementation |
| 7 // does not support a timeout, but is greatly simplified. | 7 // does not support a timeout, but is greatly simplified. |
| 8 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 8 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
| 9 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 9 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
| 10 | 10 |
| 11 #include <list> | 11 #include <list> |
| 12 #include <map> | 12 #include <map> |
| 13 #include <queue> | 13 #include <queue> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/condition_variable.h" | 17 #include "base/condition_variable.h" |
| 18 #include "base/ref_counted.h" | 18 #include "base/ref_counted.h" |
| 19 #include "base/scoped_ptr.h" | 19 #include "base/scoped_ptr.h" |
| 20 #include "base/thread.h" | 20 #include "base/thread.h" |
| 21 #include "base/time.h" | 21 #include "base/time.h" |
| 22 #include "base/waitable_event.h" | 22 #include "base/waitable_event.h" |
| 23 #include "chrome/browser/sync/engine/all_status.h" | 23 #include "chrome/browser/sync/engine/all_status.h" |
| 24 #include "chrome/browser/sync/engine/client_command_channel.h" | 24 #include "chrome/browser/sync/sessions/sync_session.h" |
| 25 #include "chrome/browser/sync/util/event_sys-inl.h" | 25 #include "chrome/browser/sync/util/event_sys-inl.h" |
| 26 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST | 26 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 27 | 27 |
| 28 class EventListenerHookup; | 28 class EventListenerHookup; |
| 29 | 29 |
| 30 namespace syncable { | 30 namespace syncable { |
| 31 class DirectoryManager; | 31 class DirectoryManager; |
| 32 struct DirectoryManagerEvent; | 32 struct DirectoryManagerEvent; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace browser_sync { | 35 namespace browser_sync { |
| 36 | 36 |
| 37 class ModelSafeWorker; | 37 class ModelSafeWorker; |
| 38 class ServerConnectionManager; | 38 class ServerConnectionManager; |
| 39 class Syncer; | 39 class Syncer; |
| 40 class TalkMediator; | 40 class TalkMediator; |
| 41 class URLFactory; | 41 class URLFactory; |
| 42 struct ServerConnectionEvent; | 42 struct ServerConnectionEvent; |
| 43 struct SyncerEvent; | 43 struct SyncerEvent; |
| 44 struct SyncerShutdownEvent; | 44 struct SyncerShutdownEvent; |
| 45 struct TalkMediatorEvent; | 45 struct TalkMediatorEvent; |
| 46 | 46 |
| 47 class SyncerThreadFactory { | 47 class SyncerThread : public base::RefCountedThreadSafe<SyncerThread>, |
| 48 public: | 48 public sessions::SyncSession::Delegate { |
| 49 // Creates a SyncerThread based on the default (or user-overridden) | |
| 50 // implementation. The thread does not start running until you call Start(), | |
| 51 // which will cause it to check-and-wait for certain conditions to be met | |
| 52 // (such as valid connection with Server established, syncable::Directory has | |
| 53 // been opened) before performing an intial sync with a server. It uses | |
| 54 // |connection_manager| to detect valid connections, and |mgr| to detect the | |
| 55 // opening of a Directory, which will cause it to create a Syncer object for | |
| 56 // said Directory, and assign |model_safe_worker| to it. |connection_manager| | |
| 57 // and |mgr| should outlive the SyncerThread. You must stop the thread by | |
| 58 // calling Stop before destroying the object. Stopping will first tear down | |
| 59 // the Syncer object, allowing it to finish work in progress, before joining | |
| 60 // the Stop-calling thread with the internal thread. | |
| 61 static SyncerThread* Create(ClientCommandChannel* command_channel, | |
| 62 syncable::DirectoryManager* mgr, | |
| 63 ServerConnectionManager* connection_manager, AllStatus* all_status, | |
| 64 ModelSafeWorker* model_safe_worker); | |
| 65 private: | |
| 66 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncerThreadFactory); | |
| 67 }; | |
| 68 | |
| 69 class SyncerThread : public base::RefCountedThreadSafe<SyncerThread> { | |
| 70 FRIEND_TEST(SyncerThreadTest, CalculateSyncWaitTime); | 49 FRIEND_TEST(SyncerThreadTest, CalculateSyncWaitTime); |
| 71 FRIEND_TEST(SyncerThreadTest, CalculatePollingWaitTime); | 50 FRIEND_TEST(SyncerThreadTest, CalculatePollingWaitTime); |
| 72 FRIEND_TEST(SyncerThreadWithSyncerTest, Polling); | 51 FRIEND_TEST(SyncerThreadWithSyncerTest, Polling); |
| 73 FRIEND_TEST(SyncerThreadWithSyncerTest, Nudge); | 52 FRIEND_TEST(SyncerThreadWithSyncerTest, Nudge); |
| 74 FRIEND_TEST(SyncerThreadWithSyncerTest, Throttling); | 53 FRIEND_TEST(SyncerThreadWithSyncerTest, Throttling); |
| 75 friend class SyncerThreadWithSyncerTest; | 54 friend class SyncerThreadWithSyncerTest; |
| 76 friend class SyncerThreadFactory; | 55 friend class SyncerThreadFactory; |
| 77 public: | 56 public: |
| 78 // Encapsulates the parameters that make up an interval on which the | 57 // Encapsulates the parameters that make up an interval on which the |
| 79 // syncer thread is sleeping. | 58 // syncer thread is sleeping. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 110 }; | 89 }; |
| 111 // Server can overwrite these values via client commands. | 90 // Server can overwrite these values via client commands. |
| 112 // Standard short poll. This is used when XMPP is off. | 91 // Standard short poll. This is used when XMPP is off. |
| 113 static const int kDefaultShortPollIntervalSeconds; | 92 static const int kDefaultShortPollIntervalSeconds; |
| 114 // Long poll is used when XMPP is on. | 93 // Long poll is used when XMPP is on. |
| 115 static const int kDefaultLongPollIntervalSeconds; | 94 static const int kDefaultLongPollIntervalSeconds; |
| 116 // 30 minutes by default. If exponential backoff kicks in, this is the | 95 // 30 minutes by default. If exponential backoff kicks in, this is the |
| 117 // longest possible poll interval. | 96 // longest possible poll interval. |
| 118 static const int kDefaultMaxPollIntervalMs; | 97 static const int kDefaultMaxPollIntervalMs; |
| 119 | 98 |
| 99 SyncerThread(sessions::SyncSessionContext* context, AllStatus* all_status); |
| 120 virtual ~SyncerThread(); | 100 virtual ~SyncerThread(); |
| 121 | 101 |
| 122 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr); | 102 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr); |
| 123 | 103 |
| 124 // Starts a syncer thread. | 104 // Starts a syncer thread. |
| 125 // Returns true if it creates a thread or if there's currently a thread | 105 // Returns true if it creates a thread or if there's currently a thread |
| 126 // running and false otherwise. | 106 // running and false otherwise. |
| 127 virtual bool Start(); | 107 virtual bool Start(); |
| 128 | 108 |
| 129 // Stop processing. |max_wait| doesn't do anything in this version. | 109 // Stop processing. |max_wait| doesn't do anything in this version. |
| 130 virtual bool Stop(int max_wait); | 110 virtual bool Stop(int max_wait); |
| 131 | 111 |
| 132 // Nudges the syncer to sync with a delay specified. This API is for access | 112 // Nudges the syncer to sync with a delay specified. This API is for access |
| 133 // from the SyncerThread's controller and will cause a mutex lock. | 113 // from the SyncerThread's controller and will cause a mutex lock. |
| 134 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); | 114 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); |
| 135 | 115 |
| 136 // Registers this thread to watch talk mediator events. | 116 // Registers this thread to watch talk mediator events. |
| 137 virtual void WatchTalkMediator(TalkMediator* talk_mediator); | 117 virtual void WatchTalkMediator(TalkMediator* talk_mediator); |
| 138 | 118 |
| 139 virtual void WatchClientCommands(ClientCommandChannel* channel); | 119 virtual SyncerEventChannel* relay_channel(); |
| 140 | |
| 141 virtual SyncerEventChannel* channel(); | |
| 142 | 120 |
| 143 protected: | 121 protected: |
| 144 SyncerThread(); // Necessary for temporary pthreads-based PIMPL impl. | |
| 145 SyncerThread(ClientCommandChannel* command_channel, | |
| 146 syncable::DirectoryManager* mgr, | |
| 147 ServerConnectionManager* connection_manager, AllStatus* all_status, | |
| 148 ModelSafeWorker* model_safe_worker); | |
| 149 virtual void ThreadMain(); | 122 virtual void ThreadMain(); |
| 150 void ThreadMainLoop(); | 123 void ThreadMainLoop(); |
| 151 | 124 |
| 152 virtual void SetConnected(bool connected) { | 125 virtual void SetConnected(bool connected) { |
| 153 DCHECK(!thread_.IsRunning()); | 126 DCHECK(!thread_.IsRunning()); |
| 154 vault_.connected_ = connected; | 127 vault_.connected_ = connected; |
| 155 } | 128 } |
| 156 | 129 |
| 157 virtual void SetSyncerPollingInterval(base::TimeDelta interval) { | 130 virtual void SetSyncerPollingInterval(base::TimeDelta interval) { |
| 158 // TODO(timsteele): Use TimeDelta internally. | 131 // TODO(timsteele): Use TimeDelta internally. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 193 |
| 221 private: | 194 private: |
| 222 // Threshold multipler for how long before user should be considered idle. | 195 // Threshold multipler for how long before user should be considered idle. |
| 223 static const int kPollBackoffThresholdMultiplier = 10; | 196 static const int kPollBackoffThresholdMultiplier = 10; |
| 224 | 197 |
| 225 friend void* RunSyncerThread(void* syncer_thread); | 198 friend void* RunSyncerThread(void* syncer_thread); |
| 226 void* Run(); | 199 void* Run(); |
| 227 void HandleDirectoryManagerEvent( | 200 void HandleDirectoryManagerEvent( |
| 228 const syncable::DirectoryManagerEvent& event); | 201 const syncable::DirectoryManagerEvent& event); |
| 229 void HandleSyncerEvent(const SyncerEvent& event); | 202 void HandleSyncerEvent(const SyncerEvent& event); |
| 230 void HandleClientCommand(ClientCommandChannel::EventType event); | 203 |
| 204 // SyncSession::Delegate implementation. |
| 205 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until); |
| 206 virtual bool IsSyncingCurrentlySilenced(); |
| 207 virtual void OnReceivedShortPollIntervalUpdate( |
| 208 const base::TimeDelta& new_interval); |
| 209 virtual void OnReceivedLongPollIntervalUpdate( |
| 210 const base::TimeDelta& new_interval); |
| 231 | 211 |
| 232 void HandleServerConnectionEvent(const ServerConnectionEvent& event); | 212 void HandleServerConnectionEvent(const ServerConnectionEvent& event); |
| 233 | 213 |
| 234 void HandleTalkMediatorEvent(const TalkMediatorEvent& event); | 214 void HandleTalkMediatorEvent(const TalkMediatorEvent& event); |
| 235 | 215 |
| 236 void SyncMain(Syncer* syncer); | 216 void SyncMain(Syncer* syncer); |
| 237 | 217 |
| 238 // Calculates the next sync wait time and exponential backoff state. | 218 // Calculates the next sync wait time and exponential backoff state. |
| 239 // last_poll_wait is the time duration of the previous polling timeout which | 219 // last_poll_wait is the time duration of the previous polling timeout which |
| 240 // was used. user_idle_milliseconds is updated by this method, and is a report | 220 // was used. user_idle_milliseconds is updated by this method, and is a report |
| (...skipping 26 matching lines...) Expand all Loading... |
| 267 void SetUpdatesSource(bool nudged, NudgeSource nudge_source, | 247 void SetUpdatesSource(bool nudged, NudgeSource nudge_source, |
| 268 bool* initial_sync); | 248 bool* initial_sync); |
| 269 | 249 |
| 270 // For unit tests only. | 250 // For unit tests only. |
| 271 virtual void DisableIdleDetection() { disable_idle_detection_ = true; } | 251 virtual void DisableIdleDetection() { disable_idle_detection_ = true; } |
| 272 | 252 |
| 273 // State of the notification framework is tracked by these values. | 253 // State of the notification framework is tracked by these values. |
| 274 bool p2p_authenticated_; | 254 bool p2p_authenticated_; |
| 275 bool p2p_subscribed_; | 255 bool p2p_subscribed_; |
| 276 | 256 |
| 277 scoped_ptr<EventListenerHookup> client_command_hookup_; | |
| 278 scoped_ptr<EventListenerHookup> conn_mgr_hookup_; | 257 scoped_ptr<EventListenerHookup> conn_mgr_hookup_; |
| 279 const AllStatus* allstatus_; | 258 const AllStatus* allstatus_; |
| 280 | 259 |
| 281 syncable::DirectoryManager* dirman_; | |
| 282 ServerConnectionManager* scm_; | |
| 283 | |
| 284 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be | 260 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be |
| 285 // updated by the server. | 261 // updated by the server. |
| 286 int syncer_short_poll_interval_seconds_; | 262 int syncer_short_poll_interval_seconds_; |
| 287 int syncer_long_poll_interval_seconds_; | 263 int syncer_long_poll_interval_seconds_; |
| 288 | 264 |
| 289 // The time we wait between polls in seconds. This is used as lower bound on | 265 // The time we wait between polls in seconds. This is used as lower bound on |
| 290 // our wait time. Updated once per loop from the command line flag. | 266 // our wait time. Updated once per loop from the command line flag. |
| 291 int syncer_polling_interval_; | 267 int syncer_polling_interval_; |
| 292 | 268 |
| 293 // The upper bound on the nominal wait between polls in seconds. Note that | 269 // The upper bound on the nominal wait between polls in seconds. Note that |
| 294 // this bounds the "nominal" poll interval, while the the actual interval | 270 // this bounds the "nominal" poll interval, while the the actual interval |
| 295 // also takes previous failures into account. | 271 // also takes previous failures into account. |
| 296 int syncer_max_interval_; | 272 int syncer_max_interval_; |
| 297 | 273 |
| 298 scoped_ptr<SyncerEventChannel> syncer_event_channel_; | |
| 299 | |
| 300 // This causes syncer to start syncing ASAP. If the rate of requests is too | 274 // This causes syncer to start syncing ASAP. If the rate of requests is too |
| 301 // high the request will be silently dropped. mutex_ should be held when | 275 // high the request will be silently dropped. mutex_ should be held when |
| 302 // this is called. | 276 // this is called. |
| 303 void NudgeSyncImpl(int milliseconds_from_now, NudgeSource source); | 277 void NudgeSyncImpl(int milliseconds_from_now, NudgeSource source); |
| 304 | 278 |
| 305 scoped_ptr<EventListenerHookup> talk_mediator_hookup_; | 279 scoped_ptr<EventListenerHookup> talk_mediator_hookup_; |
| 306 ClientCommandChannel* const command_channel_; | |
| 307 scoped_ptr<EventListenerHookup> directory_manager_hookup_; | 280 scoped_ptr<EventListenerHookup> directory_manager_hookup_; |
| 308 scoped_ptr<EventListenerHookup> syncer_events_; | 281 scoped_ptr<EventListenerHookup> syncer_events_; |
| 309 | 282 |
| 310 // Handles any tasks that will result in model changes (modifications of | 283 scoped_ptr<sessions::SyncSessionContext> session_context_; |
| 311 // syncable::Entries). Pass this to the syncer created and managed by |this|. | 284 |
| 312 // Only non-null in syncapi case. | 285 // Events from the Syncer's syncer_event_channel are first processed by the |
| 313 scoped_ptr<ModelSafeWorker> model_safe_worker_; | 286 // SyncerThread and then get relayed onto this channel for consumers. |
| 287 // TODO(timsteele): Wow did this confused me. I had removed the channel from |
| 288 // here thinking there was only one, and then realized this relay was |
| 289 // happening. Is this strict event handling order needed?! |
| 290 scoped_ptr<SyncerEventChannel> syncer_event_relay_channel_; |
| 291 |
| 292 // Set whenever the server instructs us to stop sending it requests until |
| 293 // a specified time, and reset for each call to SyncShare. (Note that the |
| 294 // WaitInterval::THROTTLED contract is such that we don't call SyncShare at |
| 295 // all until the "silenced until" embargo expires.) |
| 296 base::TimeTicks silenced_until_; |
| 314 | 297 |
| 315 // Useful for unit tests | 298 // Useful for unit tests |
| 316 bool disable_idle_detection_; | 299 bool disable_idle_detection_; |
| 317 | 300 |
| 318 DISALLOW_COPY_AND_ASSIGN(SyncerThread); | 301 DISALLOW_COPY_AND_ASSIGN(SyncerThread); |
| 319 }; | 302 }; |
| 320 | 303 |
| 321 } // namespace browser_sync | 304 } // namespace browser_sync |
| 322 | 305 |
| 323 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 306 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
| OLD | NEW |