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 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <list> | 12 #include <list> |
13 #include <map> | 13 #include <map> |
14 #include <queue> | 14 #include <queue> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
18 #include "base/condition_variable.h" | 18 #include "base/condition_variable.h" |
19 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
20 #include "base/ref_counted.h" | 20 #include "base/ref_counted.h" |
21 #include "base/scoped_ptr.h" | 21 #include "base/scoped_ptr.h" |
22 #include "base/thread.h" | 22 #include "base/thread.h" |
23 #include "base/time.h" | 23 #include "base/time.h" |
24 #include "base/waitable_event.h" | 24 #include "base/waitable_event.h" |
25 #include "chrome/browser/sync/engine/all_status.h" | |
26 #if defined(OS_LINUX) | 25 #if defined(OS_LINUX) |
27 #include "chrome/browser/sync/engine/idle_query_linux.h" | 26 #include "chrome/browser/sync/engine/idle_query_linux.h" |
28 #endif | 27 #endif |
29 #include "chrome/browser/sync/sessions/sync_session.h" | 28 #include "chrome/browser/sync/sessions/sync_session.h" |
30 #include "chrome/common/deprecated/event_sys-inl.h" | 29 #include "chrome/common/deprecated/event_sys-inl.h" |
31 | 30 |
32 class EventListenerHookup; | 31 class EventListenerHookup; |
33 | 32 |
34 namespace syncable { | 33 namespace syncable { |
35 class DirectoryManager; | 34 class DirectoryManager; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 kContinuation | 95 kContinuation |
97 }; | 96 }; |
98 // Server can overwrite these values via client commands. | 97 // Server can overwrite these values via client commands. |
99 // Standard short poll. This is used when XMPP is off. | 98 // Standard short poll. This is used when XMPP is off. |
100 static const int kDefaultShortPollIntervalSeconds; | 99 static const int kDefaultShortPollIntervalSeconds; |
101 // Long poll is used when XMPP is on. | 100 // Long poll is used when XMPP is on. |
102 static const int kDefaultLongPollIntervalSeconds; | 101 static const int kDefaultLongPollIntervalSeconds; |
103 // 30 minutes by default. If exponential backoff kicks in, this is the | 102 // 30 minutes by default. If exponential backoff kicks in, this is the |
104 // longest possible poll interval. | 103 // longest possible poll interval. |
105 static const int kDefaultMaxPollIntervalMs; | 104 static const int kDefaultMaxPollIntervalMs; |
| 105 // Maximum interval for exponential backoff. |
| 106 static const int kMaxBackoffSeconds; |
106 | 107 |
107 SyncerThread(sessions::SyncSessionContext* context, AllStatus* all_status); | 108 explicit SyncerThread(sessions::SyncSessionContext* context); |
108 virtual ~SyncerThread(); | 109 virtual ~SyncerThread(); |
109 | 110 |
110 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr); | 111 virtual void WatchConnectionManager(ServerConnectionManager* conn_mgr); |
111 | 112 |
112 // Starts a syncer thread. | 113 // Starts a syncer thread. |
113 // Returns true if it creates a thread or if there's currently a thread | 114 // Returns true if it creates a thread or if there's currently a thread |
114 // running and false otherwise. | 115 // running and false otherwise. |
115 virtual bool Start(); | 116 virtual bool Start(); |
116 | 117 |
117 // Stop processing. |max_wait| doesn't do anything in this version. | 118 // Stop processing. |max_wait| doesn't do anything in this version. |
(...skipping 13 matching lines...) Expand all Loading... |
131 virtual bool RequestResume(); | 132 virtual bool RequestResume(); |
132 | 133 |
133 // Nudges the syncer to sync with a delay specified. This API is for access | 134 // Nudges the syncer to sync with a delay specified. This API is for access |
134 // from the SyncerThread's controller and will cause a mutex lock. | 135 // from the SyncerThread's controller and will cause a mutex lock. |
135 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); | 136 virtual void NudgeSyncer(int milliseconds_from_now, NudgeSource source); |
136 | 137 |
137 void SetNotificationsEnabled(bool notifications_enabled); | 138 void SetNotificationsEnabled(bool notifications_enabled); |
138 | 139 |
139 virtual SyncerEventChannel* relay_channel(); | 140 virtual SyncerEventChannel* relay_channel(); |
140 | 141 |
| 142 // DDOS avoidance function. The argument and return value is in seconds |
| 143 static int GetRecommendedDelaySeconds(int base_delay_seconds); |
| 144 |
141 protected: | 145 protected: |
142 virtual void ThreadMain(); | 146 virtual void ThreadMain(); |
143 void ThreadMainLoop(); | 147 void ThreadMainLoop(); |
144 | 148 |
145 virtual void SetConnected(bool connected) { | 149 virtual void SetConnected(bool connected) { |
146 DCHECK(!thread_.IsRunning()); | 150 DCHECK(!thread_.IsRunning()); |
147 vault_.connected_ = connected; | 151 vault_.connected_ = connected; |
148 } | 152 } |
149 | 153 |
150 virtual void SetSyncerPollingInterval(base::TimeDelta interval) { | 154 virtual void SetSyncerPollingInterval(base::TimeDelta interval) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 // Calculates the next sync wait time and exponential backoff state. | 251 // Calculates the next sync wait time and exponential backoff state. |
248 // last_poll_wait is the time duration of the previous polling timeout which | 252 // last_poll_wait is the time duration of the previous polling timeout which |
249 // was used. user_idle_milliseconds is updated by this method, and is a report | 253 // was used. user_idle_milliseconds is updated by this method, and is a report |
250 // of the full amount of time since the last period of activity for the user. | 254 // of the full amount of time since the last period of activity for the user. |
251 // The continue_sync_cycle parameter is used to determine whether or not we | 255 // The continue_sync_cycle parameter is used to determine whether or not we |
252 // are calculating a polling wait time that is a continuation of an sync cycle | 256 // are calculating a polling wait time that is a continuation of an sync cycle |
253 // which terminated while the syncer still had work to do. was_nudged is used | 257 // which terminated while the syncer still had work to do. was_nudged is used |
254 // in case of exponential backoff so we only allow one nudge per backoff | 258 // in case of exponential backoff so we only allow one nudge per backoff |
255 // interval. | 259 // interval. |
256 WaitInterval CalculatePollingWaitTime( | 260 WaitInterval CalculatePollingWaitTime( |
257 const AllStatus::Status& status, | |
258 int last_poll_wait, // in s | 261 int last_poll_wait, // in s |
259 int* user_idle_milliseconds, | 262 int* user_idle_milliseconds, |
260 bool* continue_sync_cycle, | 263 bool* continue_sync_cycle, |
261 bool was_nudged); | 264 bool was_nudged); |
262 | 265 |
263 // Helper to above function, considers effect of user idle time. | 266 // Helper to above function, considers effect of user idle time. |
264 virtual int CalculateSyncWaitTime(int last_wait, int user_idle_ms); | 267 virtual int CalculateSyncWaitTime(int last_wait, int user_idle_ms); |
265 | 268 |
266 // Sets the source value of the controlled syncer's updates_source value. | 269 // Sets the source value of the controlled syncer's updates_source value. |
267 // The initial sync boolean is updated if read as a sentinel. The following | 270 // The initial sync boolean is updated if read as a sentinel. The following |
(...skipping 26 matching lines...) Expand all Loading... |
294 // This sets all conditions for syncer thread termination but does not | 297 // This sets all conditions for syncer thread termination but does not |
295 // actually join threads. It is expected that Stop will be called at some | 298 // actually join threads. It is expected that Stop will be called at some |
296 // time after to fully stop and clean up. | 299 // time after to fully stop and clean up. |
297 void RequestSyncerExitAndSetThreadStopConditions(); | 300 void RequestSyncerExitAndSetThreadStopConditions(); |
298 | 301 |
299 // State of the notification framework is tracked by these values. | 302 // State of the notification framework is tracked by these values. |
300 bool p2p_authenticated_; | 303 bool p2p_authenticated_; |
301 bool p2p_subscribed_; | 304 bool p2p_subscribed_; |
302 | 305 |
303 scoped_ptr<EventListenerHookup> conn_mgr_hookup_; | 306 scoped_ptr<EventListenerHookup> conn_mgr_hookup_; |
304 const AllStatus* allstatus_; | |
305 | 307 |
306 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be | 308 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be |
307 // updated by the server. | 309 // updated by the server. |
308 int syncer_short_poll_interval_seconds_; | 310 int syncer_short_poll_interval_seconds_; |
309 int syncer_long_poll_interval_seconds_; | 311 int syncer_long_poll_interval_seconds_; |
310 | 312 |
311 // The time we wait between polls in seconds. This is used as lower bound on | 313 // The time we wait between polls in seconds. This is used as lower bound on |
312 // our wait time. Updated once per loop from the command line flag. | 314 // our wait time. Updated once per loop from the command line flag. |
313 int syncer_polling_interval_; | 315 int syncer_polling_interval_; |
314 | 316 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 349 |
348 // Useful for unit tests | 350 // Useful for unit tests |
349 bool disable_idle_detection_; | 351 bool disable_idle_detection_; |
350 | 352 |
351 DISALLOW_COPY_AND_ASSIGN(SyncerThread); | 353 DISALLOW_COPY_AND_ASSIGN(SyncerThread); |
352 }; | 354 }; |
353 | 355 |
354 } // namespace browser_sync | 356 } // namespace browser_sync |
355 | 357 |
356 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ | 358 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
OLD | NEW |