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 "chrome/browser/sync/engine/net/server_connection_manager.h" | 11 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
12 #include "chrome/browser/sync/protocol/service_constants.h" | 12 #include "chrome/browser/sync/protocol/service_constants.h" |
13 #include "chrome/browser/sync/sessions/session_state.h" | 13 #include "chrome/browser/sync/sessions/session_state.h" |
14 #include "chrome/browser/sync/syncable/directory_manager.h" | 14 #include "chrome/browser/sync/syncable/directory_manager.h" |
15 | 15 |
16 namespace browser_sync { | 16 namespace browser_sync { |
17 | 17 |
18 const char* AllStatus::GetSyncStatusString(SyncStatus icon) { | 18 static const sync_api::SyncManager::Status init_status = |
19 const char* strings[] = {"OFFLINE", "OFFLINE_UNSYNCED", "SYNCING", "READY", | 19 { sync_api::SyncManager::Status::OFFLINE }; |
20 "CONFLICT", "OFFLINE_UNUSABLE"}; | |
21 COMPILE_ASSERT(arraysize(strings) == ICON_STATUS_COUNT, enum_indexed_array); | |
22 if (icon < 0 || icon >= static_cast<SyncStatus>(arraysize(strings))) | |
23 LOG(FATAL) << "Illegal Icon State:" << icon; | |
24 return strings[icon]; | |
25 } | |
26 | |
27 static const AllStatus::Status init_status = | |
28 { AllStatus::OFFLINE }; | |
29 | 20 |
30 AllStatus::AllStatus() : status_(init_status) { | 21 AllStatus::AllStatus() : status_(init_status) { |
31 status_.initial_sync_ended = true; | 22 status_.initial_sync_ended = true; |
32 status_.notifications_enabled = false; | 23 status_.notifications_enabled = false; |
33 } | 24 } |
34 | 25 |
35 AllStatus::~AllStatus() { | 26 AllStatus::~AllStatus() { |
36 } | 27 } |
37 | 28 |
38 AllStatus::Status AllStatus::CreateBlankStatus() const { | 29 sync_api::SyncManager::Status AllStatus::CreateBlankStatus() const { |
39 Status status = status_; | 30 // Status is initialized with the previous status value. Variables |
| 31 // whose values accumulate (e.g. lifetime counters like updates_received) |
| 32 // are not to be cleared here. |
| 33 sync_api::SyncManager::Status status = status_; |
40 status.syncing = true; | 34 status.syncing = true; |
41 status.unsynced_count = 0; | 35 status.unsynced_count = 0; |
42 status.conflicting_count = 0; | 36 status.conflicting_count = 0; |
43 status.initial_sync_ended = false; | 37 status.initial_sync_ended = false; |
44 status.syncer_stuck = false; | 38 status.syncer_stuck = false; |
45 status.max_consecutive_errors = 0; | 39 status.max_consecutive_errors = 0; |
46 status.server_broken = false; | 40 status.server_broken = false; |
47 status.updates_available = 0; | 41 status.updates_available = 0; |
48 status.updates_received = 0; | |
49 return status; | 42 return status; |
50 } | 43 } |
51 | 44 |
52 AllStatus::Status AllStatus::CalcSyncing(const SyncEngineEvent &event) const { | 45 sync_api::SyncManager::Status AllStatus::CalcSyncing( |
53 Status status = CreateBlankStatus(); | 46 const SyncEngineEvent &event) const { |
| 47 sync_api::SyncManager::Status status = CreateBlankStatus(); |
54 const sessions::SyncSessionSnapshot* snapshot = event.snapshot; | 48 const sessions::SyncSessionSnapshot* snapshot = event.snapshot; |
55 status.unsynced_count += static_cast<int>(snapshot->unsynced_count); | 49 status.unsynced_count += static_cast<int>(snapshot->unsynced_count); |
56 status.conflicting_count += snapshot->errors.num_conflicting_commits; | 50 status.conflicting_count += snapshot->errors.num_conflicting_commits; |
57 // The syncer may not be done yet, which could cause conflicting updates. | 51 // The syncer may not be done yet, which could cause conflicting updates. |
58 // But this is only used for status, so it is better to have visibility. | 52 // But this is only used for status, so it is better to have visibility. |
59 status.conflicting_count += snapshot->num_conflicting_updates; | 53 status.conflicting_count += snapshot->num_conflicting_updates; |
60 | 54 |
61 status.syncing |= snapshot->syncer_status.syncing; | 55 status.syncing |= snapshot->syncer_status.syncing; |
62 status.syncing = snapshot->has_more_to_sync && snapshot->is_silenced; | 56 status.syncing = snapshot->has_more_to_sync && snapshot->is_silenced; |
63 status.initial_sync_ended |= snapshot->is_share_usable; | 57 status.initial_sync_ended |= snapshot->is_share_usable; |
64 status.syncer_stuck |= snapshot->syncer_status.syncer_stuck; | 58 status.syncer_stuck |= snapshot->syncer_status.syncer_stuck; |
65 | 59 |
66 const sessions::ErrorCounters& errors(snapshot->errors); | 60 const sessions::ErrorCounters& errors(snapshot->errors); |
67 if (errors.consecutive_errors > status.max_consecutive_errors) | 61 if (errors.consecutive_errors > status.max_consecutive_errors) |
68 status.max_consecutive_errors = errors.consecutive_errors; | 62 status.max_consecutive_errors = errors.consecutive_errors; |
69 | 63 |
70 // 100 is an arbitrary limit. | 64 // 100 is an arbitrary limit. |
71 if (errors.consecutive_transient_error_commits > 100) | 65 if (errors.consecutive_transient_error_commits > 100) |
72 status.server_broken = true; | 66 status.server_broken = true; |
73 | 67 |
74 status.updates_available += snapshot->num_server_changes_remaining; | 68 status.updates_available += snapshot->num_server_changes_remaining; |
75 status.updates_received += snapshot->max_local_timestamp; | 69 |
| 70 // Accumulate update count only once per session to avoid double-counting. |
| 71 // TODO(ncarter): Make this realtime by having the syncer_status |
| 72 // counter preserve its value across sessions. http://crbug.com/26339 |
| 73 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { |
| 74 status.updates_received += |
| 75 snapshot->syncer_status.num_updates_downloaded_total; |
| 76 status.tombstone_updates_received += |
| 77 snapshot->syncer_status.num_tombstone_updates_downloaded_total; |
| 78 } |
76 return status; | 79 return status; |
77 } | 80 } |
78 | 81 |
79 void AllStatus::CalcStatusChanges() { | 82 void AllStatus::CalcStatusChanges() { |
80 const bool unsynced_changes = status_.unsynced_count > 0; | 83 const bool unsynced_changes = status_.unsynced_count > 0; |
81 const bool online = status_.authenticated && | 84 const bool online = status_.authenticated && |
82 status_.server_reachable && status_.server_up && !status_.server_broken; | 85 status_.server_reachable && status_.server_up && !status_.server_broken; |
83 if (online) { | 86 if (online) { |
84 if (status_.syncer_stuck) | 87 if (status_.syncer_stuck) |
85 status_.icon = CONFLICT; | 88 status_.summary = sync_api::SyncManager::Status::CONFLICT; |
86 else if (unsynced_changes || status_.syncing) | 89 else if (unsynced_changes || status_.syncing) |
87 status_.icon = SYNCING; | 90 status_.summary = sync_api::SyncManager::Status::SYNCING; |
88 else | 91 else |
89 status_.icon = READY; | 92 status_.summary = sync_api::SyncManager::Status::READY; |
90 } else if (!status_.initial_sync_ended) { | 93 } else if (!status_.initial_sync_ended) { |
91 status_.icon = OFFLINE_UNUSABLE; | 94 status_.summary = sync_api::SyncManager::Status::OFFLINE_UNUSABLE; |
92 } else if (unsynced_changes) { | 95 } else if (unsynced_changes) { |
93 status_.icon = OFFLINE_UNSYNCED; | 96 status_.summary = sync_api::SyncManager::Status::OFFLINE_UNSYNCED; |
94 } else { | 97 } else { |
95 status_.icon = OFFLINE; | 98 status_.summary = sync_api::SyncManager::Status::OFFLINE; |
96 } | 99 } |
97 } | 100 } |
98 | 101 |
99 void AllStatus::OnSyncEngineEvent(const SyncEngineEvent& event) { | 102 void AllStatus::OnSyncEngineEvent(const SyncEngineEvent& event) { |
100 ScopedStatusLock lock(this); | 103 ScopedStatusLock lock(this); |
101 switch (event.what_happened) { | 104 switch (event.what_happened) { |
102 case SyncEngineEvent::SYNC_CYCLE_ENDED: | 105 case SyncEngineEvent::SYNC_CYCLE_ENDED: |
103 case SyncEngineEvent::STATUS_CHANGED: | 106 case SyncEngineEvent::STATUS_CHANGED: |
104 status_ = CalcSyncing(event); | 107 status_ = CalcSyncing(event); |
105 break; | 108 break; |
(...skipping 21 matching lines...) Expand all Loading... |
127 if (!status_.authenticated) { | 130 if (!status_.authenticated) { |
128 status_ = CreateBlankStatus(); | 131 status_ = CreateBlankStatus(); |
129 } | 132 } |
130 status_.authenticated = true; | 133 status_.authenticated = true; |
131 } else { | 134 } else { |
132 status_.authenticated = false; | 135 status_.authenticated = false; |
133 } | 136 } |
134 } | 137 } |
135 } | 138 } |
136 | 139 |
137 AllStatus::Status AllStatus::status() const { | 140 sync_api::SyncManager::Status AllStatus::status() const { |
138 AutoLock lock(mutex_); | 141 AutoLock lock(mutex_); |
139 return status_; | 142 return status_; |
140 } | 143 } |
141 | 144 |
142 void AllStatus::SetNotificationsEnabled(bool notifications_enabled) { | 145 void AllStatus::SetNotificationsEnabled(bool notifications_enabled) { |
143 ScopedStatusLock lock(this); | 146 ScopedStatusLock lock(this); |
144 status_.notifications_enabled = notifications_enabled; | 147 status_.notifications_enabled = notifications_enabled; |
145 } | 148 } |
146 | 149 |
147 void AllStatus::IncrementNotificationsSent() { | 150 void AllStatus::IncrementNotificationsSent() { |
(...skipping 10 matching lines...) Expand all Loading... |
158 : allstatus_(allstatus) { | 161 : allstatus_(allstatus) { |
159 allstatus->mutex_.Acquire(); | 162 allstatus->mutex_.Acquire(); |
160 } | 163 } |
161 | 164 |
162 ScopedStatusLock::~ScopedStatusLock() { | 165 ScopedStatusLock::~ScopedStatusLock() { |
163 allstatus_->CalcStatusChanges(); | 166 allstatus_->CalcStatusChanges(); |
164 allstatus_->mutex_.Release(); | 167 allstatus_->mutex_.Release(); |
165 } | 168 } |
166 | 169 |
167 } // namespace browser_sync | 170 } // namespace browser_sync |
OLD | NEW |