1 // Copyright (c) 2012 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.
5 #include "chrome/browser/sync/about_sync_util.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/values.h"
12 #include "chrome/browser/sync/profile_sync_service.h"
13 #include "chrome/common/chrome_version_info.h"
14 #include "components/signin/core/browser/signin_manager.h"
15 #include "sync/api/time.h"
16 #include "sync/internal_api/public/util/sync_string_conversions.h"
17 #include "sync/protocol/proto_enum_conversions.h"
19 using base::DictionaryValue
;
20 using base::ListValue
;
22 const char kIdentityTitle
[] = "Identity";
23 const char kDetailsKey
[] = "details";
27 // Creates a 'section' for display on about:sync, consisting of a title and a
28 // list of fields. Returns a pointer to the new section. Note that
29 // |parent_list|, not the caller, owns the newly added section.
30 base::ListValue
* AddSection(base::ListValue
* parent_list
,
31 const std::string
& title
) {
32 base::DictionaryValue
* section
= new base::DictionaryValue();
33 base::ListValue
* section_contents
= new base::ListValue();
34 section
->SetString("title", title
);
35 section
->Set("data", section_contents
);
36 section
->SetBoolean("is_sensitive", false);
37 parent_list
->Append(section
);
38 return section_contents
;
41 // Same as AddSection, but for data that should be elided when dumped into text
42 // form and posted in a public forum (e.g. unique identifiers).
43 base::ListValue
* AddSensitiveSection(base::ListValue
* parent_list
,
44 const std::string
& title
) {
45 base::DictionaryValue
* section
= new base::DictionaryValue();
46 base::ListValue
* section_contents
= new base::ListValue();
47 section
->SetString("title", title
);
48 section
->Set("data", section_contents
);
49 section
->SetBoolean("is_sensitive", true);
50 parent_list
->Append(section
);
51 return section_contents
;
54 // The following helper classes help manage the about:sync fields which will be
55 // populated in method in ConstructAboutInformation.
57 // Each instance of one of thse classes indicates a field in about:sync. Each
58 // field will be serialized to a DictionaryValue with entries for 'stat_name',
59 // 'stat_value' and 'is_valid'.
61 class StringSyncStat
{
63 StringSyncStat(base::ListValue
* section
, const std::string
& key
);
64 void SetValue(const std::string
& value
);
65 void SetValue(const base::string16
& value
);
68 // Owned by the |section| passed in during construction.
69 base::DictionaryValue
* stat_
;
72 StringSyncStat::StringSyncStat(base::ListValue
* section
,
73 const std::string
& key
) {
74 stat_
= new base::DictionaryValue();
75 stat_
->SetString("stat_name", key
);
76 stat_
->SetString("stat_value", "Uninitialized");
77 stat_
->SetBoolean("is_valid", false);
78 section
->Append(stat_
);
81 void StringSyncStat::SetValue(const std::string
& value
) {
82 stat_
->SetString("stat_value", value
);
83 stat_
->SetBoolean("is_valid", true);
86 void StringSyncStat::SetValue(const base::string16
& value
) {
87 stat_
->SetString("stat_value", value
);
88 stat_
->SetBoolean("is_valid", true);
93 BoolSyncStat(base::ListValue
* section
, const std::string
& key
);
94 void SetValue(bool value
);
97 // Owned by the |section| passed in during construction.
98 base::DictionaryValue
* stat_
;
101 BoolSyncStat::BoolSyncStat(base::ListValue
* section
, const std::string
& key
) {
102 stat_
= new base::DictionaryValue();
103 stat_
->SetString("stat_name", key
);
104 stat_
->SetBoolean("stat_value", false);
105 stat_
->SetBoolean("is_valid", false);
106 section
->Append(stat_
);
109 void BoolSyncStat::SetValue(bool value
) {
110 stat_
->SetBoolean("stat_value", value
);
111 stat_
->SetBoolean("is_valid", true);
116 IntSyncStat(base::ListValue
* section
, const std::string
& key
);
117 void SetValue(int value
);
120 // Owned by the |section| passed in during construction.
121 base::DictionaryValue
* stat_
;
124 IntSyncStat::IntSyncStat(base::ListValue
* section
, const std::string
& key
) {
125 stat_
= new base::DictionaryValue();
126 stat_
->SetString("stat_name", key
);
127 stat_
->SetInteger("stat_value", 0);
128 stat_
->SetBoolean("is_valid", false);
129 section
->Append(stat_
);
132 void IntSyncStat::SetValue(int value
) {
133 stat_
->SetInteger("stat_value", value
);
134 stat_
->SetBoolean("is_valid", true);
137 // Returns a string describing the chrome version environment. Version format:
138 // <Build Info> <OS> <Version number> (<Last change>)<channel or "-devel">
139 // If version information is unavailable, returns "invalid."
140 // TODO(zea): this approximately matches MakeUserAgentForSyncApi in
141 // sync_backend_host.cc. Unify the two if possible.
142 std::string
GetVersionString() {
143 // Build a version string that matches MakeUserAgentForSyncApi with the
144 // addition of channel info and proper OS names.
145 chrome::VersionInfo chrome_version
;
146 // GetVersionStringModifier returns empty string for stable channel or
147 // unofficial builds, the channel string otherwise. We want to have "-devel"
148 // for unofficial builds only.
149 std::string version_modifier
=
150 chrome::VersionInfo::GetVersionStringModifier();
151 if (version_modifier
.empty()) {
152 if (chrome::VersionInfo::GetChannel() !=
153 chrome::VersionInfo::CHANNEL_STABLE
) {
154 version_modifier
= "-devel";
157 version_modifier
= " " + version_modifier
;
159 return chrome_version
.Name() + " " + chrome_version
.OSType() + " " +
160 chrome_version
.Version() + " (" + chrome_version
.LastChange() + ")" +
164 std::string
GetTimeStr(base::Time time
, const std::string
& default_msg
) {
165 std::string time_str
;
167 time_str
= default_msg
;
169 time_str
= syncer::GetTimeDebugString(time
);
173 std::string
GetConnectionStatus(
174 const ProfileSyncService::SyncTokenStatus
& status
) {
176 switch (status
.connection_status
) {
177 case syncer::CONNECTION_NOT_ATTEMPTED
:
178 base::StringAppendF(&message
, "not attempted");
180 case syncer::CONNECTION_OK
:
182 &message
, "OK since %s",
183 GetTimeStr(status
.connection_status_update_time
, "n/a").c_str());
185 case syncer::CONNECTION_AUTH_ERROR
:
187 &message
, "auth error since %s",
188 GetTimeStr(status
.connection_status_update_time
, "n/a").c_str());
190 case syncer::CONNECTION_SERVER_ERROR
:
192 &message
, "server error since %s",
193 GetTimeStr(status
.connection_status_update_time
, "n/a").c_str());
203 namespace sync_ui_util
{
205 // This function both defines the structure of the message to be returned and
206 // its contents. Most of the message consists of simple fields in about:sync
207 // which are grouped into sections and populated with the help of the SyncStat
208 // classes defined above.
209 scoped_ptr
<base::DictionaryValue
> ConstructAboutInformation(
210 ProfileSyncService
* service
) {
211 scoped_ptr
<base::DictionaryValue
> about_info(new base::DictionaryValue());
213 // 'details': A list of sections.
214 base::ListValue
* stats_list
= new base::ListValue();
216 // The following lines define the sections and their fields. For each field,
217 // a class is instantiated, which allows us to reference the fields in
218 // 'setter' code later on in this function.
219 base::ListValue
* section_summary
= AddSection(stats_list
, "Summary");
220 StringSyncStat
summary_string(section_summary
, "Summary");
222 base::ListValue
* section_version
= AddSection(stats_list
, "Version Info");
223 StringSyncStat
client_version(section_version
, "Client Version");
224 StringSyncStat
server_url(section_version
, "Server URL");
226 base::ListValue
* section_identity
=
227 AddSensitiveSection(stats_list
, kIdentityTitle
);
228 StringSyncStat
sync_id(section_identity
, "Sync Client ID");
229 StringSyncStat
invalidator_id(section_identity
, "Invalidator Client ID");
230 StringSyncStat
username(section_identity
, "Username");
232 base::ListValue
* section_credentials
= AddSection(stats_list
, "Credentials");
233 StringSyncStat
request_token_time(section_credentials
, "Requested Token");
234 StringSyncStat
receive_token_time(section_credentials
, "Received Token");
235 StringSyncStat
token_request_status(section_credentials
,
236 "Token Request Status");
237 StringSyncStat
next_token_request(section_credentials
,
238 "Next Token Request");
240 base::ListValue
* section_local
= AddSection(stats_list
, "Local State");
241 StringSyncStat
server_connection(section_local
,
242 "Server Connection");
243 StringSyncStat
last_synced(section_local
, "Last Synced");
244 BoolSyncStat
is_setup_complete(section_local
,
245 "Sync First-Time Setup Complete");
246 StringSyncStat
backend_initialization(section_local
,
247 "Sync Backend Initialization");
248 BoolSyncStat
is_syncing(section_local
, "Syncing");
250 base::ListValue
* section_network
= AddSection(stats_list
, "Network");
251 BoolSyncStat
is_throttled(section_network
, "Throttled");
252 StringSyncStat
retry_time(section_network
, "Retry time (maybe stale)");
253 BoolSyncStat
are_notifications_enabled(section_network
,
254 "Notifications Enabled");
256 base::ListValue
* section_encryption
= AddSection(stats_list
, "Encryption");
257 BoolSyncStat
is_using_explicit_passphrase(section_encryption
,
258 "Explicit Passphrase");
259 BoolSyncStat
is_passphrase_required(section_encryption
,
260 "Passphrase Required");
261 BoolSyncStat
is_cryptographer_ready(section_encryption
,
262 "Cryptographer Ready");
263 BoolSyncStat
has_pending_keys(section_encryption
,
264 "Cryptographer Has Pending Keys");
265 StringSyncStat
encrypted_types(section_encryption
, "Encrypted Types");
266 BoolSyncStat
has_keystore_key(section_encryption
, "Has Keystore Key");
267 StringSyncStat
keystore_migration_time(section_encryption
,
268 "Keystore Migration Time");
269 StringSyncStat
passphrase_type(section_encryption
,
271 StringSyncStat
passphrase_time(section_encryption
,
274 base::ListValue
* section_last_session
= AddSection(
275 stats_list
, "Status from Last Completed Session");
276 StringSyncStat
session_source(section_last_session
, "Sync Source");
277 StringSyncStat
get_key_result(section_last_session
, "GetKey Step Result");
278 StringSyncStat
download_result(section_last_session
, "Download Step Result");
279 StringSyncStat
commit_result(section_last_session
, "Commit Step Result");
281 base::ListValue
* section_counters
= AddSection(stats_list
, "Running Totals");
282 IntSyncStat
notifications_received(section_counters
,
283 "Notifications Received");
284 IntSyncStat
updates_received(section_counters
, "Updates Downloaded");
285 IntSyncStat
tombstone_updates(section_counters
, "Tombstone Updates");
286 IntSyncStat
reflected_updates(section_counters
, "Reflected Updates");
287 IntSyncStat
successful_commits(section_counters
, "Successful Commits");
288 IntSyncStat
conflicts_resolved_local_wins(section_counters
,
289 "Conflicts Resolved: Client Wins");
290 IntSyncStat
conflicts_resolved_server_wins(section_counters
,
291 "Conflicts Resolved: Server Wins");
293 base::ListValue
*section_this_cycle
= AddSection(stats_list
,
294 "Transient Counters (this cycle)");
295 IntSyncStat
encryption_conflicts(section_this_cycle
, "Encryption Conflicts");
296 IntSyncStat
hierarchy_conflicts(section_this_cycle
, "Hierarchy Conflicts");
297 IntSyncStat
server_conflicts(section_this_cycle
, "Server Conflicts");
298 IntSyncStat
committed_items(section_this_cycle
, "Committed Items");
300 base::ListValue
* section_that_cycle
= AddSection(
301 stats_list
, "Transient Counters (last cycle of last completed session)");
302 IntSyncStat
updates_downloaded(section_that_cycle
, "Updates Downloaded");
303 IntSyncStat
committed_count(section_that_cycle
, "Committed Count");
304 IntSyncStat
entries(section_that_cycle
, "Entries");
306 base::ListValue
* section_nudge_info
= AddSection(
307 stats_list
, "Nudge Source Counters");
308 IntSyncStat
nudge_source_notification(
309 section_nudge_info
, "Server Invalidations");
310 IntSyncStat
nudge_source_local(section_nudge_info
, "Local Changes");
311 IntSyncStat
nudge_source_local_refresh(section_nudge_info
, "Local Refreshes");
313 // This list of sections belongs in the 'details' field of the returned
315 about_info
->Set(kDetailsKey
, stats_list
);
317 // Populate all the fields we declared above.
318 client_version
.SetValue(GetVersionString());
321 summary_string
.SetValue("Sync service does not exist");
322 return about_info
.Pass();
325 syncer::SyncStatus full_status
;
326 bool is_status_valid
= service
->QueryDetailedSyncStatus(&full_status
);
327 bool sync_active
= service
->IsSyncActive();
328 const syncer::sessions::SyncSessionSnapshot
& snapshot
=
329 service
->GetLastSessionSnapshot();
332 summary_string
.SetValue(service
->QuerySyncStatusSummaryString());
334 server_url
.SetValue(service
->sync_service_url().spec());
336 if (is_status_valid
&& !full_status
.sync_id
.empty())
337 sync_id
.SetValue(full_status
.sync_id
);
338 if (is_status_valid
&& !full_status
.invalidator_client_id
.empty())
339 invalidator_id
.SetValue(full_status
.invalidator_client_id
);
340 if (service
->signin())
341 username
.SetValue(service
->signin()->GetAuthenticatedUsername());
343 const ProfileSyncService::SyncTokenStatus
& token_status
=
344 service
->GetSyncTokenStatus();
345 server_connection
.SetValue(GetConnectionStatus(token_status
));
346 request_token_time
.SetValue(GetTimeStr(token_status
.token_request_time
,
348 receive_token_time
.SetValue(GetTimeStr(token_status
.token_receive_time
,
350 std::string err
= token_status
.last_get_token_error
.error_message();
351 token_request_status
.SetValue(err
.empty() ? "OK" : err
);
352 next_token_request
.SetValue(
353 GetTimeStr(token_status
.next_token_request_time
, "not scheduled"));
355 last_synced
.SetValue(service
->GetLastSyncedTimeString());
356 is_setup_complete
.SetValue(service
->HasSyncSetupCompleted());
357 backend_initialization
.SetValue(
358 service
->GetBackendInitializationStateString());
359 if (is_status_valid
) {
360 is_syncing
.SetValue(full_status
.syncing
);
361 retry_time
.SetValue(GetTimeStr(full_status
.retry_time
,
362 "Scheduler is not in backoff or throttled"));
365 if (snapshot
.is_initialized())
366 is_throttled
.SetValue(snapshot
.is_silenced());
367 if (is_status_valid
) {
368 are_notifications_enabled
.SetValue(
369 full_status
.notifications_enabled
);
373 is_using_explicit_passphrase
.SetValue(
374 service
->IsUsingSecondaryPassphrase());
375 is_passphrase_required
.SetValue(service
->IsPassphraseRequired());
376 passphrase_time
.SetValue(
377 GetTimeStr(service
->GetExplicitPassphraseTime(), "No Passphrase Time"));
379 if (is_status_valid
) {
380 is_cryptographer_ready
.SetValue(full_status
.cryptographer_ready
);
381 has_pending_keys
.SetValue(full_status
.crypto_has_pending_keys
);
382 encrypted_types
.SetValue(
383 ModelTypeSetToString(full_status
.encrypted_types
));
384 has_keystore_key
.SetValue(full_status
.has_keystore_key
);
385 keystore_migration_time
.SetValue(
386 GetTimeStr(full_status
.keystore_migration_time
, "Not Migrated"));
387 passphrase_type
.SetValue(
388 PassphraseTypeToString(full_status
.passphrase_type
));
391 if (snapshot
.is_initialized()) {
392 if (snapshot
.legacy_updates_source() !=
393 sync_pb::GetUpdatesCallerInfo::UNKNOWN
) {
394 session_source
.SetValue(
395 syncer::GetUpdatesSourceString(snapshot
.legacy_updates_source()));
397 get_key_result
.SetValue(
398 GetSyncerErrorString(
399 snapshot
.model_neutral_state().last_get_key_result
));
400 download_result
.SetValue(
401 GetSyncerErrorString(
402 snapshot
.model_neutral_state().last_download_updates_result
));
403 commit_result
.SetValue(
404 GetSyncerErrorString(
405 snapshot
.model_neutral_state().commit_result
));
408 if (is_status_valid
) {
409 notifications_received
.SetValue(full_status
.notifications_received
);
410 updates_received
.SetValue(full_status
.updates_received
);
411 tombstone_updates
.SetValue(full_status
.tombstone_updates_received
);
412 reflected_updates
.SetValue(full_status
.reflected_updates_received
);
413 successful_commits
.SetValue(full_status
.num_commits_total
);
414 conflicts_resolved_local_wins
.SetValue(
415 full_status
.num_local_overwrites_total
);
416 conflicts_resolved_server_wins
.SetValue(
417 full_status
.num_server_overwrites_total
);
420 if (is_status_valid
) {
421 encryption_conflicts
.SetValue(full_status
.encryption_conflicts
);
422 hierarchy_conflicts
.SetValue(full_status
.hierarchy_conflicts
);
423 server_conflicts
.SetValue(full_status
.server_conflicts
);
424 committed_items
.SetValue(full_status
.committed_count
);
427 if (is_status_valid
) {
428 nudge_source_notification
.SetValue(full_status
.nudge_source_notification
);
429 nudge_source_local
.SetValue(full_status
.nudge_source_local
);
430 nudge_source_local_refresh
.SetValue(full_status
.nudge_source_local_refresh
);
433 if (snapshot
.is_initialized()) {
434 updates_downloaded
.SetValue(
435 snapshot
.model_neutral_state().num_updates_downloaded_total
);
436 committed_count
.SetValue(
437 snapshot
.model_neutral_state().num_successful_commits
);
438 entries
.SetValue(snapshot
.num_entries());
441 // The values set from this point onwards do not belong in the
444 // We don't need to check is_status_valid here.
445 // full_status.sync_protocol_error is exported directly from the
446 // ProfileSyncService, even if the backend doesn't exist.
447 const bool actionable_error_detected
=
448 full_status
.sync_protocol_error
.error_type
!= syncer::UNKNOWN_ERROR
&&
449 full_status
.sync_protocol_error
.error_type
!= syncer::SYNC_SUCCESS
;
451 about_info
->SetBoolean("actionable_error_detected",
452 actionable_error_detected
);
454 // NOTE: We won't bother showing any of the following values unless
455 // actionable_error_detected is set.
457 base::ListValue
* actionable_error
= new base::ListValue();
458 about_info
->Set("actionable_error", actionable_error
);
460 StringSyncStat
error_type(actionable_error
, "Error Type");
461 StringSyncStat
action(actionable_error
, "Action");
462 StringSyncStat
url(actionable_error
, "URL");
463 StringSyncStat
description(actionable_error
, "Error Description");
465 if (actionable_error_detected
) {
466 error_type
.SetValue(syncer::GetSyncErrorTypeString(
467 full_status
.sync_protocol_error
.error_type
));
468 action
.SetValue(syncer::GetClientActionString(
469 full_status
.sync_protocol_error
.action
));
470 url
.SetValue(full_status
.sync_protocol_error
.url
);
471 description
.SetValue(full_status
.sync_protocol_error
.error_description
);
474 about_info
->SetBoolean("unrecoverable_error_detected",
475 service
->HasUnrecoverableError());
477 if (service
->HasUnrecoverableError()) {
478 tracked_objects::Location
loc(service
->unrecoverable_error_location());
479 std::string location_str
;
480 loc
.Write(true, true, &location_str
);
481 std::string unrecoverable_error_message
=
482 "Unrecoverable error detected at " + location_str
+
483 ": " + service
->unrecoverable_error_message();
484 about_info
->SetString("unrecoverable_error_message",
485 unrecoverable_error_message
);
488 about_info
->Set("type_status", service
->GetTypeStatusMap());
490 return about_info
.Pass();
493 } // namespace sync_ui_util