OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // A class to run the syncer on a thread. |
| 6 // |
| 7 |
| 8 #ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
| 9 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
| 10 |
| 11 #include <list> |
| 12 #include <map> |
| 13 #include <queue> |
| 14 #include <vector> |
| 15 |
| 16 #include "base/basictypes.h" |
| 17 #include "base/scoped_ptr.h" |
| 18 #include "chrome/browser/sync/engine/all_status.h" |
| 19 #include "chrome/browser/sync/engine/client_command_channel.h" |
| 20 #include "chrome/browser/sync/util/event_sys-inl.h" |
| 21 #include "chrome/browser/sync/util/pthread_helpers.h" |
| 22 #include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST |
| 23 |
| 24 class EventListenerHookup; |
| 25 |
| 26 namespace syncable { |
| 27 class DirectoryManager; |
| 28 struct DirectoryManagerEvent; |
| 29 } |
| 30 |
| 31 namespace browser_sync { |
| 32 |
| 33 class ModelSafeWorker; |
| 34 class ServerConnectionManager; |
| 35 class Syncer; |
| 36 class TalkMediator; |
| 37 class URLFactory; |
| 38 struct ServerConnectionEvent; |
| 39 struct SyncerEvent; |
| 40 struct SyncerShutdownEvent; |
| 41 struct TalkMediatorEvent; |
| 42 |
| 43 class SyncerThread { |
| 44 FRIEND_TEST(SyncerThreadTest, CalculateSyncWaitTime); |
| 45 FRIEND_TEST(SyncerThreadTest, CalculatePollingWaitTime); |
| 46 |
| 47 public: |
| 48 friend class SyncerThreadTest; |
| 49 |
| 50 enum NudgeSource { |
| 51 kUnknown = 0, |
| 52 kNotification, |
| 53 kLocal, |
| 54 kContinuation |
| 55 }; |
| 56 |
| 57 // Server can overwrite these values via client commands. |
| 58 // Standard short poll. This is used when XMPP is off. |
| 59 static const int kDefaultShortPollIntervalSeconds = 60; |
| 60 // Long poll is used when XMPP is on. |
| 61 static const int kDefaultLongPollIntervalSeconds = 3600; |
| 62 // 30 minutes by default. If exponential backoff kicks in, this is |
| 63 // the longest possible poll interval. |
| 64 static const int kDefaultMaxPollIntervalMs = 30 * 60 * 1000; |
| 65 |
| 66 SyncerThread( |
| 67 ClientCommandChannel* command_channel, |
| 68 syncable::DirectoryManager* mgr, |
| 69 ServerConnectionManager* connection_manager, |
| 70 AllStatus* all_status, |
| 71 ModelSafeWorker* model_safe_worker); |
| 72 ~SyncerThread(); |
| 73 |
| 74 void WatchConnectionManager(ServerConnectionManager* conn_mgr); |
| 75 // Creates and starts a syncer thread. |
| 76 // Returns true if it creates a thread or if there's currently a thread |
| 77 // running and false otherwise. |
| 78 bool Start(); |
| 79 |
| 80 // Stop processing. A max wait of at least 2*server RTT time is recommended. |
| 81 // returns true if we stopped, false otherwise. |
| 82 bool Stop(int max_wait); |
| 83 |
| 84 // Nudges the syncer to sync with a delay specified. This API is for access |
| 85 // from the SyncerThread's controller and will cause a mutex lock. |
| 86 bool NudgeSyncer(int milliseconds_from_now, NudgeSource source); |
| 87 |
| 88 // Registers this thread to watch talk mediator events. |
| 89 void WatchTalkMediator(TalkMediator* talk_mediator); |
| 90 |
| 91 void WatchClientCommands(ClientCommandChannel* channel); |
| 92 |
| 93 SyncerEventChannel* channel(); |
| 94 |
| 95 private: |
| 96 // A few members to gate the rate at which we nudge the syncer. |
| 97 enum { |
| 98 kNudgeRateLimitCount = 6, |
| 99 kNudgeRateLimitTime = 180, |
| 100 }; |
| 101 |
| 102 // A queue of all scheduled nudges. One insertion for every call to |
| 103 // NudgeQueue(). |
| 104 typedef std::pair<timespec, NudgeSource> NudgeObject; |
| 105 |
| 106 struct IsTimeSpecGreater { |
| 107 inline bool operator() (const NudgeObject& lhs, const NudgeObject& rhs) { |
| 108 return lhs.first.tv_sec == rhs.first.tv_sec ? |
| 109 lhs.first.tv_nsec > rhs.first.tv_nsec : |
| 110 lhs.first.tv_sec > rhs.first.tv_sec; |
| 111 } |
| 112 }; |
| 113 |
| 114 typedef std::priority_queue<NudgeObject, |
| 115 std::vector<NudgeObject>, IsTimeSpecGreater> |
| 116 NudgeQueue; |
| 117 |
| 118 // Threshold multipler for how long before user should be considered idle. |
| 119 static const int kPollBackoffThresholdMultiplier = 10; |
| 120 |
| 121 friend void* RunSyncerThread(void* syncer_thread); |
| 122 void* Run(); |
| 123 void HandleDirectoryManagerEvent( |
| 124 const syncable::DirectoryManagerEvent& event); |
| 125 void HandleSyncerEvent(const SyncerEvent& event); |
| 126 void HandleClientCommand(ClientCommandChannel::EventType event); |
| 127 |
| 128 void HandleServerConnectionEvent(const ServerConnectionEvent& event); |
| 129 |
| 130 void HandleTalkMediatorEvent(const TalkMediatorEvent& event); |
| 131 |
| 132 void* ThreadMain(); |
| 133 void ThreadMainLoop(); |
| 134 |
| 135 void SyncMain(Syncer* syncer); |
| 136 |
| 137 // Calculates the next sync wait time in seconds. last_poll_wait is the time |
| 138 // duration of the previous polling timeout which was used. |
| 139 // user_idle_milliseconds is updated by this method, and is a report of the |
| 140 // full amount of time since the last period of activity for the user. The |
| 141 // continue_sync_cycle parameter is used to determine whether or not we are |
| 142 // calculating a polling wait time that is a continuation of an sync cycle |
| 143 // which terminated while the syncer still had work to do. |
| 144 int CalculatePollingWaitTime( |
| 145 const AllStatus::Status& status, |
| 146 int last_poll_wait, // in s |
| 147 int* user_idle_milliseconds, |
| 148 bool* continue_sync_cycle); |
| 149 // Helper to above function, considers effect of user idle time. |
| 150 int CalculateSyncWaitTime(int last_wait, int user_idle_ms); |
| 151 |
| 152 // Sets the source value of the controlled syncer's updates_source value. |
| 153 // The initial sync boolean is updated if read as a sentinel. The following |
| 154 // two methods work in concert to achieve this goal. |
| 155 void UpdateNudgeSource(const timespec& now, bool* continue_sync_cycle, |
| 156 bool* initial_sync); |
| 157 void SetUpdatesSource(bool nudged, NudgeSource nudge_source, |
| 158 bool* initial_sync); |
| 159 |
| 160 // for unit tests only |
| 161 void DisableIdleDetection() { disable_idle_detection_ = true; } |
| 162 |
| 163 // false when we want to stop the thread. |
| 164 bool stop_syncer_thread_; |
| 165 |
| 166 // we use one mutex for all members except the channel. |
| 167 PThreadMutex mutex_; |
| 168 typedef PThreadScopedLock<PThreadMutex> MutexLock; |
| 169 |
| 170 // Handle of the running thread. |
| 171 pthread_t thread_; |
| 172 bool thread_running_; |
| 173 |
| 174 // Gets signaled whenever a thread outside of the syncer thread |
| 175 // changes a member variable. |
| 176 PThreadCondVar changed_; |
| 177 |
| 178 // State of the server connection |
| 179 bool connected_; |
| 180 |
| 181 // State of the notification framework is tracked by these values. |
| 182 bool p2p_authenticated_; |
| 183 bool p2p_subscribed_; |
| 184 |
| 185 scoped_ptr<EventListenerHookup> client_command_hookup_; |
| 186 scoped_ptr<EventListenerHookup> conn_mgr_hookup_; |
| 187 const AllStatus* allstatus_; |
| 188 |
| 189 Syncer* syncer_; |
| 190 |
| 191 syncable::DirectoryManager* dirman_; |
| 192 ServerConnectionManager* scm_; |
| 193 |
| 194 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be |
| 195 // updated by the server. |
| 196 int syncer_short_poll_interval_seconds_; |
| 197 int syncer_long_poll_interval_seconds_; |
| 198 |
| 199 // The time we wait between polls in seconds. This is used as lower bound on |
| 200 // our wait time. Updated once per loop from the command line flag. |
| 201 int syncer_polling_interval_; |
| 202 |
| 203 // The upper bound on the nominal wait between polls in seconds. Note that |
| 204 // this bounds the "nominal" poll interval, while the the actual interval |
| 205 // also takes previous failures into account. |
| 206 int syncer_max_interval_; |
| 207 |
| 208 scoped_ptr<SyncerEventChannel> syncer_event_channel_; |
| 209 |
| 210 // This causes syncer to start syncing ASAP. If the rate of requests is |
| 211 // too high the request will be silently dropped. mutex_ should be held when |
| 212 // this is called. |
| 213 void NudgeSyncImpl(int milliseconds_from_now, NudgeSource source); |
| 214 |
| 215 NudgeQueue nudge_queue_; |
| 216 |
| 217 scoped_ptr<EventListenerHookup> talk_mediator_hookup_; |
| 218 ClientCommandChannel* const command_channel_; |
| 219 scoped_ptr<EventListenerHookup> directory_manager_hookup_; |
| 220 scoped_ptr<EventListenerHookup> syncer_events_; |
| 221 |
| 222 // Handles any tasks that will result in model changes (modifications of |
| 223 // syncable::Entries). Pass this to the syncer created and managed by |this|. |
| 224 // Only non-null in syncapi case. |
| 225 scoped_ptr<ModelSafeWorker> model_safe_worker_; |
| 226 |
| 227 // Useful for unit tests |
| 228 bool disable_idle_detection_; |
| 229 |
| 230 DISALLOW_COPY_AND_ASSIGN(SyncerThread); |
| 231 }; |
| 232 |
| 233 } // namespace browser_sync |
| 234 |
| 235 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_THREAD_H_ |
OLD | NEW |