OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 10 #pragma once |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 class SyncerThread : public base::RefCountedThreadSafe<SyncerThread>, | 43 class SyncerThread : public base::RefCountedThreadSafe<SyncerThread>, |
44 public sessions::SyncSession::Delegate { | 44 public sessions::SyncSession::Delegate { |
45 FRIEND_TEST_ALL_PREFIXES(SyncerThreadTest, CalculateSyncWaitTime); | 45 FRIEND_TEST_ALL_PREFIXES(SyncerThreadTest, CalculateSyncWaitTime); |
46 FRIEND_TEST_ALL_PREFIXES(SyncerThreadTest, CalculatePollingWaitTime); | 46 FRIEND_TEST_ALL_PREFIXES(SyncerThreadTest, CalculatePollingWaitTime); |
47 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Polling); | 47 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Polling); |
48 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Nudge); | 48 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Nudge); |
49 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, NudgeWithDataTypes); | 49 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, NudgeWithDataTypes); |
50 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, | 50 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, |
51 NudgeWithDataTypesCoalesced); | 51 NudgeWithDataTypesCoalesced); |
52 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, NudgeWithPayloads); | |
53 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, | |
54 NudgeWithPayloadsCoalesced); | |
55 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Throttling); | 52 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Throttling); |
56 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, AuthInvalid); | 53 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, AuthInvalid); |
57 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Pause); | 54 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, Pause); |
58 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, StartWhenNotConnected); | 55 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, StartWhenNotConnected); |
59 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, PauseWhenNotConnected); | 56 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, PauseWhenNotConnected); |
60 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, StopSyncPermanently); | 57 FRIEND_TEST_ALL_PREFIXES(SyncerThreadWithSyncerTest, StopSyncPermanently); |
61 friend class SyncerThreadWithSyncerTest; | 58 friend class SyncerThreadWithSyncerTest; |
62 friend class SyncerThreadFactory; | 59 friend class SyncerThreadFactory; |
63 public: | 60 public: |
64 // Encapsulates the parameters that make up an interval on which the | 61 // Encapsulates the parameters that make up an interval on which the |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 131 |
135 // Nudges the syncer to sync with a delay specified. This API is for access | 132 // Nudges the syncer to sync with a delay specified. This API is for access |
136 // from the SyncerThread's controller and will cause a mutex lock. | 133 // from the SyncerThread's controller and will cause a mutex lock. |
137 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); | 134 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); |
138 | 135 |
139 // Same as |NudgeSyncer|, but supports tracking the datatypes that caused | 136 // Same as |NudgeSyncer|, but supports tracking the datatypes that caused |
140 // the nudge to occur. | 137 // the nudge to occur. |
141 virtual void NudgeSyncerWithDataTypes( | 138 virtual void NudgeSyncerWithDataTypes( |
142 int milliseconds_from_now, | 139 int milliseconds_from_now, |
143 NudgeSource source, | 140 NudgeSource source, |
144 const syncable::ModelTypeBitSet& model_types); | 141 const syncable::ModelTypeBitSet& model_type); |
145 | |
146 // Same as |NudgeSyncer|, but supports including a payload for passing on to | |
147 // the download updates command. Datatypes with payloads are also considered | |
148 // to have caused a nudged to occur and treated accordingly. | |
149 virtual void NudgeSyncerWithPayloads( | |
150 int milliseconds_from_now, | |
151 NudgeSource source, | |
152 const sessions::TypePayloadMap& model_types_with_payloads); | |
153 | 142 |
154 void SetNotificationsEnabled(bool notifications_enabled); | 143 void SetNotificationsEnabled(bool notifications_enabled); |
155 | 144 |
156 // Call this when a directory is opened | 145 // Call this when a directory is opened |
157 void CreateSyncer(const std::string& dirname); | 146 void CreateSyncer(const std::string& dirname); |
158 | 147 |
159 // DDOS avoidance function. The argument and return value is in seconds | 148 // DDOS avoidance function. The argument and return value is in seconds |
160 static int GetRecommendedDelaySeconds(int base_delay_seconds); | 149 static int GetRecommendedDelaySeconds(int base_delay_seconds); |
161 | 150 |
162 protected: | 151 protected: |
(...skipping 30 matching lines...) Expand all Loading... |
193 Syncer* syncer_; | 182 Syncer* syncer_; |
194 | 183 |
195 // State of the server connection. | 184 // State of the server connection. |
196 bool connected_; | 185 bool connected_; |
197 | 186 |
198 // kUnknown if there is no pending nudge. (Theoretically, there | 187 // kUnknown if there is no pending nudge. (Theoretically, there |
199 // could be a pending nudge of type kUnknown, so it's better to | 188 // could be a pending nudge of type kUnknown, so it's better to |
200 // check pending_nudge_time_.) | 189 // check pending_nudge_time_.) |
201 NudgeSource pending_nudge_source_; | 190 NudgeSource pending_nudge_source_; |
202 | 191 |
203 // Map of all datatypes that are requesting a nudge. Can be union | 192 // BitSet of the datatypes that have triggered the current nudge |
204 // from multiple nudges that are coalesced. In addition, we | 193 // (can be union of various bitsets when multiple nudges are coalesced) |
205 // optionally track a payload associated with each datatype (most recent | 194 syncable::ModelTypeBitSet pending_nudge_types_; |
206 // payload overwrites old ones). These payloads are used by the download | |
207 // updates command and can contain datatype specific information the server | |
208 // might use. | |
209 sessions::TypePayloadMap pending_nudge_types_; | |
210 | 195 |
211 // null iff there is no pending nudge. | 196 // null iff there is no pending nudge. |
212 base::TimeTicks pending_nudge_time_; | 197 base::TimeTicks pending_nudge_time_; |
213 | 198 |
214 // The wait interval for to the current iteration of our main loop. This is | 199 // The wait interval for to the current iteration of our main loop. This is |
215 // only written to by the syncer thread, and since the only reader from a | 200 // only written to by the syncer thread, and since the only reader from a |
216 // different thread (NudgeSync) is called at totally random times, we don't | 201 // different thread (NudgeSync) is called at totally random times, we don't |
217 // really need to access mutually exclusively as the data races that exist | 202 // really need to access mutually exclusively as the data races that exist |
218 // are intrinsic, but do so anyway and avoid using 'volatile'. | 203 // are intrinsic, but do so anyway and avoid using 'volatile'. |
219 WaitInterval current_wait_interval_; | 204 WaitInterval current_wait_interval_; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // Builds a SyncSourceInfo and returns whether a nudge occurred in the | 273 // Builds a SyncSourceInfo and returns whether a nudge occurred in the |
289 // |was_nudged| parameter. | 274 // |was_nudged| parameter. |
290 sessions::SyncSourceInfo GetAndResetNudgeSource(bool was_throttled, | 275 sessions::SyncSourceInfo GetAndResetNudgeSource(bool was_throttled, |
291 bool continue_sync_cycle, | 276 bool continue_sync_cycle, |
292 bool* initial_sync, | 277 bool* initial_sync, |
293 bool* was_nudged); | 278 bool* was_nudged); |
294 | 279 |
295 sessions::SyncSourceInfo MakeSyncSourceInfo( | 280 sessions::SyncSourceInfo MakeSyncSourceInfo( |
296 bool nudged, | 281 bool nudged, |
297 NudgeSource nudge_source, | 282 NudgeSource nudge_source, |
298 const sessions::TypePayloadMap& model_types_with_payloads, | 283 const syncable::ModelTypeBitSet& nudge_types, |
299 bool* initial_sync); | 284 bool* initial_sync); |
300 | 285 |
301 int UserIdleTime(); | 286 int UserIdleTime(); |
302 | 287 |
303 void WaitUntilConnectedOrQuit(); | 288 void WaitUntilConnectedOrQuit(); |
304 | 289 |
305 // The thread will remain in this method until a resume is requested | 290 // The thread will remain in this method until a resume is requested |
306 // or shutdown is started. | 291 // or shutdown is started. |
307 void PauseUntilResumedOrQuit(); | 292 void PauseUntilResumedOrQuit(); |
308 | 293 |
(...skipping 26 matching lines...) Expand all Loading... |
335 // this bounds the "nominal" poll interval, while the the actual interval | 320 // this bounds the "nominal" poll interval, while the the actual interval |
336 // also takes previous failures into account. | 321 // also takes previous failures into account. |
337 int syncer_max_interval_; | 322 int syncer_max_interval_; |
338 | 323 |
339 // This causes syncer to start syncing ASAP. If the rate of requests is too | 324 // This causes syncer to start syncing ASAP. If the rate of requests is too |
340 // high the request will be silently dropped. mutex_ should be held when | 325 // high the request will be silently dropped. mutex_ should be held when |
341 // this is called. | 326 // this is called. |
342 void NudgeSyncImpl( | 327 void NudgeSyncImpl( |
343 int milliseconds_from_now, | 328 int milliseconds_from_now, |
344 NudgeSource source, | 329 NudgeSource source, |
345 const sessions::TypePayloadMap& model_types_with_payloads); | 330 const syncable::ModelTypeBitSet& model_types); |
346 | 331 |
347 #if defined(OS_LINUX) | 332 #if defined(OS_LINUX) |
348 // On Linux, we need this information in order to query idle time. | 333 // On Linux, we need this information in order to query idle time. |
349 scoped_ptr<IdleQueryLinux> idle_query_; | 334 scoped_ptr<IdleQueryLinux> idle_query_; |
350 #endif | 335 #endif |
351 | 336 |
352 scoped_ptr<sessions::SyncSessionContext> session_context_; | 337 scoped_ptr<sessions::SyncSessionContext> session_context_; |
353 | 338 |
354 // Set whenever the server instructs us to stop sending it requests until | 339 // Set whenever the server instructs us to stop sending it requests until |
355 // a specified time, and reset for each call to SyncShare. (Note that the | 340 // a specified time, and reset for each call to SyncShare. (Note that the |
356 // WaitInterval::THROTTLED contract is such that we don't call SyncShare at | 341 // WaitInterval::THROTTLED contract is such that we don't call SyncShare at |
357 // all until the "silenced until" embargo expires.) | 342 // all until the "silenced until" embargo expires.) |
358 base::TimeTicks silenced_until_; | 343 base::TimeTicks silenced_until_; |
359 | 344 |
360 // Useful for unit tests | 345 // Useful for unit tests |
361 bool disable_idle_detection_; | 346 bool disable_idle_detection_; |
362 | 347 |
363 DISALLOW_COPY_AND_ASSIGN(SyncerThread); | 348 DISALLOW_COPY_AND_ASSIGN(SyncerThread); |
364 }; | 349 }; |
365 | 350 |
366 } // namespace browser_sync | 351 } // namespace browser_sync |
367 | 352 |
368 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 353 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
OLD | NEW |