OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 #include "chrome/browser/sync/engine/all_status.h" | 5 #include "chrome/browser/sync/engine/all_status.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/port.h" | 10 #include "base/port.h" |
11 #include "base/rand_util.h" | |
12 #include "chrome/browser/sync/engine/auth_watcher.h" | 11 #include "chrome/browser/sync/engine/auth_watcher.h" |
13 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | 12 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
14 #include "chrome/browser/sync/engine/syncer.h" | 13 #include "chrome/browser/sync/engine/syncer.h" |
15 #include "chrome/browser/sync/engine/syncer_thread.h" | 14 #include "chrome/browser/sync/engine/syncer_thread.h" |
16 #include "chrome/browser/sync/protocol/service_constants.h" | 15 #include "chrome/browser/sync/protocol/service_constants.h" |
17 #include "chrome/browser/sync/sessions/session_state.h" | 16 #include "chrome/browser/sync/sessions/session_state.h" |
18 #include "chrome/browser/sync/syncable/directory_manager.h" | 17 #include "chrome/browser/sync/syncable/directory_manager.h" |
19 #include "chrome/common/deprecated/event_sys-inl.h" | 18 #include "chrome/common/deprecated/event_sys-inl.h" |
20 #include "jingle/notifier/listener/talk_mediator.h" | 19 #include "jingle/notifier/listener/talk_mediator.h" |
21 | 20 |
22 namespace browser_sync { | 21 namespace browser_sync { |
23 | 22 |
24 static const time_t kMinSyncObserveInterval = 10; // seconds | 23 static const time_t kMinSyncObserveInterval = 10; // seconds |
25 | 24 |
26 // Backoff interval randomization factor. | |
27 static const int kBackoffRandomizationFactor = 2; | |
28 | |
29 const int AllStatus::kMaxBackoffSeconds = 60 * 60 * 4; // 4 hours. | |
30 | |
31 const char* AllStatus::GetSyncStatusString(SyncStatus icon) { | 25 const char* AllStatus::GetSyncStatusString(SyncStatus icon) { |
32 const char* strings[] = {"OFFLINE", "OFFLINE_UNSYNCED", "SYNCING", "READY", | 26 const char* strings[] = {"OFFLINE", "OFFLINE_UNSYNCED", "SYNCING", "READY", |
33 "CONFLICT", "OFFLINE_UNUSABLE"}; | 27 "CONFLICT", "OFFLINE_UNUSABLE"}; |
34 COMPILE_ASSERT(arraysize(strings) == ICON_STATUS_COUNT, enum_indexed_array); | 28 COMPILE_ASSERT(arraysize(strings) == ICON_STATUS_COUNT, enum_indexed_array); |
35 if (icon < 0 || icon >= static_cast<SyncStatus>(arraysize(strings))) | 29 if (icon < 0 || icon >= static_cast<SyncStatus>(arraysize(strings))) |
36 LOG(FATAL) << "Illegal Icon State:" << icon; | 30 LOG(FATAL) << "Illegal Icon State:" << icon; |
37 return strings[icon]; | 31 return strings[icon]; |
38 } | 32 } |
39 | 33 |
40 static const AllStatus::Status init_status = | 34 static const AllStatus::Status init_status = |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 status_.server_up = IsGoodReplyFromServer(event.connection_code); | 223 status_.server_up = IsGoodReplyFromServer(event.connection_code); |
230 status_.server_reachable = event.server_reachable; | 224 status_.server_reachable = event.server_reachable; |
231 } | 225 } |
232 } | 226 } |
233 | 227 |
234 AllStatus::Status AllStatus::status() const { | 228 AllStatus::Status AllStatus::status() const { |
235 AutoLock lock(mutex_); | 229 AutoLock lock(mutex_); |
236 return status_; | 230 return status_; |
237 } | 231 } |
238 | 232 |
239 int AllStatus::GetRecommendedDelaySeconds(int base_delay_seconds) { | |
240 if (base_delay_seconds >= kMaxBackoffSeconds) | |
241 return kMaxBackoffSeconds; | |
242 | |
243 // This calculates approx. base_delay_seconds * 2 +/- base_delay_seconds / 2 | |
244 int backoff_s = (0 == base_delay_seconds) ? 1 : | |
245 base_delay_seconds * kBackoffRandomizationFactor; | |
246 | |
247 // Flip a coin to randomize backoff interval by +/- 50%. | |
248 int rand_sign = base::RandInt(0, 1) * 2 - 1; | |
249 | |
250 // Truncation is adequate for rounding here. | |
251 backoff_s = backoff_s + | |
252 (rand_sign * (base_delay_seconds / kBackoffRandomizationFactor)); | |
253 | |
254 // Cap the backoff interval. | |
255 backoff_s = std::min(backoff_s, kMaxBackoffSeconds); | |
256 | |
257 return backoff_s; | |
258 } | |
259 | |
260 int AllStatus::GetRecommendedDelay(int base_delay_ms) const { | |
261 return GetRecommendedDelaySeconds(base_delay_ms / 1000) * 1000; | |
262 } | |
263 | |
264 void AllStatus::SetNotificationsEnabled(bool notifications_enabled) { | 233 void AllStatus::SetNotificationsEnabled(bool notifications_enabled) { |
265 ScopedStatusLockWithNotify lock(this); | 234 ScopedStatusLockWithNotify lock(this); |
266 status_.notifications_enabled = notifications_enabled; | 235 status_.notifications_enabled = notifications_enabled; |
267 } | 236 } |
268 | 237 |
269 void AllStatus::IncrementNotificationsSent() { | 238 void AllStatus::IncrementNotificationsSent() { |
270 ScopedStatusLockWithNotify lock(this); | 239 ScopedStatusLockWithNotify lock(this); |
271 ++status_.notifications_sent; | 240 ++status_.notifications_sent; |
272 } | 241 } |
273 | 242 |
(...skipping 18 matching lines...) Expand all Loading... |
292 allstatus_->mutex_.Release(); | 261 allstatus_->mutex_.Release(); |
293 if (event_.what_changed) | 262 if (event_.what_changed) |
294 allstatus_->channel()->NotifyListeners(event_); | 263 allstatus_->channel()->NotifyListeners(event_); |
295 } | 264 } |
296 | 265 |
297 void ScopedStatusLockWithNotify::NotifyOverQuota() { | 266 void ScopedStatusLockWithNotify::NotifyOverQuota() { |
298 event_.what_changed |= AllStatusEvent::OVER_QUOTA; | 267 event_.what_changed |= AllStatusEvent::OVER_QUOTA; |
299 } | 268 } |
300 | 269 |
301 } // namespace browser_sync | 270 } // namespace browser_sync |
OLD | NEW |