File: sync_ui_util.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (526 lines) | stat: -rw-r--r-- 21,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/sync_ui_util.h"

#include <stdint.h>

#include <string>

#include "base/i18n/number_formatting.h"
#include "base/i18n/time_formatting.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/signin_error_controller_factory.h"
#include "chrome/browser/signin/signin_ui_util.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/locale_settings.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/sync_prefs.h"
#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
#include "components/sync/protocol/proto_enum_conversions.h"
#include "components/sync/protocol/sync_protocol_error.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "ui/base/l10n/l10n_util.h"

#if defined(OS_CHROMEOS)
#include "components/signin/core/account_id/account_id.h"
#include "components/user_manager/user_manager.h"
#else
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/sync/driver/sync_error_controller.h"
#endif  // defined(OS_CHROMEOS)

using browser_sync::ProfileSyncService;

typedef GoogleServiceAuthError AuthError;

namespace sync_ui_util {

namespace {

bool IsChromeDashboardEnabled() {
  const std::string group_name =
      base::FieldTrialList::FindFullName("ChromeDashboard");
  return group_name == "Enabled";
}

// Returns the message that should be displayed when the user is authenticated
// and can connect to the sync server. If the user hasn't yet authenticated, an
// empty string is returned.
base::string16 GetSyncedStateStatusLabel(ProfileSyncService* service,
                                         const SigninManagerBase& signin,
                                         StatusLabelStyle style,
                                         bool sync_everything) {
  if (!service || service->IsManaged()) {
    // User is signed in, but sync is disabled.
    return l10n_util::GetStringUTF16(IDS_SIGNED_IN_WITH_SYNC_DISABLED);
  } else if (!service->IsSyncRequested()) {
    // User is signed in, but sync has been stopped.
    return l10n_util::GetStringUTF16(IDS_SIGNED_IN_WITH_SYNC_SUPPRESSED);
  } else if (!service->IsSyncActive()) {
    // User is not signed in, or sync is still initializing.
    return base::string16();
  }

  // Message may also carry additional advice with an HTML link, if acceptable.
  switch (style) {
    case PLAIN_TEXT:
      return l10n_util::GetStringUTF16(
          sync_everything ? IDS_SYNC_ACCOUNT_SYNCING
                          : IDS_SYNC_ACCOUNT_SYNCING_CUSTOM_DATA_TYPES);
    case WITH_HTML:
      if (IsChromeDashboardEnabled()) {
        return l10n_util::GetStringFUTF16(
            IDS_SYNC_ACCOUNT_SYNCING_WITH_MANAGE_LINK_NEW,
            base::ASCIIToUTF16(chrome::kSyncGoogleDashboardURL));
      }
      return l10n_util::GetStringFUTF16(
          IDS_SYNC_ACCOUNT_SYNCING_WITH_MANAGE_LINK,
          base::ASCIIToUTF16(chrome::kSyncGoogleDashboardURL));
    default:
      NOTREACHED();
      return nullptr;
  }
}

void GetStatusForActionableError(const syncer::SyncProtocolError& error,
                                 base::string16* status_label,
                                 base::string16* link_label,
                                 ActionType* action_type) {
  DCHECK(status_label);
  DCHECK(link_label);
  switch (error.action) {
    case syncer::UPGRADE_CLIENT:
      status_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_UPGRADE_CLIENT));
      link_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_UPGRADE_CLIENT_LINK_LABEL));
      *action_type = UPGRADE_CLIENT;
      break;
    case syncer::ENABLE_SYNC_ON_ACCOUNT:
      status_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_STATUS_ENABLE_SYNC_ON_ACCOUNT));
      break;
    default:
      status_label->clear();
      break;
  }
}

void GetStatusForUnrecoverableError(Profile* profile,
                                    ProfileSyncService* service,
                                    base::string16* status_label,
                                    base::string16* link_label,
                                    ActionType* action_type) {
  // Unrecoverable error is sometimes accompanied by actionable error.
  // If status message is set display that message, otherwise show generic
  // unrecoverable error message.
  ProfileSyncService::Status status;
  service->QueryDetailedSyncStatus(&status);
  GetStatusForActionableError(status.sync_protocol_error, status_label,
                              link_label, action_type);
  if (status_label->empty()) {
    *action_type = REAUTHENTICATE;
    link_label->assign(
        l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));

#if !defined(OS_CHROMEOS)
    status_label->assign(l10n_util::GetStringUTF16(
        IDS_SYNC_STATUS_UNRECOVERABLE_ERROR));
    // The message for managed accounts is the same as that of the cros.
    if (SigninManagerFactory::GetForProfile(profile)->IsSignoutProhibited()) {
      status_label->assign(l10n_util::GetStringUTF16(
          IDS_SYNC_STATUS_UNRECOVERABLE_ERROR_NEEDS_SIGNOUT));
    }
#else
    status_label->assign(l10n_util::GetStringUTF16(
        IDS_SYNC_STATUS_UNRECOVERABLE_ERROR_NEEDS_SIGNOUT));
#endif
  }
}

// Depending on the authentication state, returns labels to be used to display
// information about the sync status.
void GetStatusForAuthError(Profile* profile,
                           const SigninManagerBase& signin_manager,
                           base::string16* status_label,
                           base::string16* link_label,
                           ActionType* action_type) {
  DCHECK(status_label);
  DCHECK(link_label);
  const GoogleServiceAuthError::State state =
      SigninErrorControllerFactory::GetForProfile(profile)->
          auth_error().state();
  switch (state) {
    case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
      status_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE));
      break;
    case GoogleServiceAuthError::CONNECTION_FAILED:
      status_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE));
      // Note that there is little the user can do if the server is not
      // reachable. Since attempting to re-connect is done automatically by
      // the Syncer, we do not show the (re)login link.
      break;
    case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
    case GoogleServiceAuthError::SERVICE_ERROR:
    case GoogleServiceAuthError::ACCOUNT_DELETED:
    case GoogleServiceAuthError::ACCOUNT_DISABLED:
    default:
      status_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_ERROR));
      link_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));
      *action_type = REAUTHENTICATE;
      break;
  }
}

// TODO(akalin): Write unit tests for these three functions below.

// status_label and link_label must either be both null or both non-null.
MessageType GetStatusInfo(Profile* profile,
                          ProfileSyncService* service,
                          const SigninManagerBase& signin,
                          StatusLabelStyle style,
                          base::string16* status_label,
                          base::string16* link_label,
                          ActionType* action_type) {
  DCHECK_EQ(status_label == nullptr, link_label == nullptr);

  MessageType result_type(SYNCED);

  if (!signin.IsAuthenticated())
    return PRE_SYNCED;

  if (!service || service->IsManaged() || service->IsFirstSetupComplete() ||
      !service->IsSyncRequested()) {
    // The order or priority is going to be: 1. Unrecoverable errors.
    // 2. Auth errors. 3. Protocol errors. 4. Passphrase errors.

    if (service && service->HasUnrecoverableError()) {
      if (status_label && link_label) {
        GetStatusForUnrecoverableError(profile, service, status_label,
                                       link_label, action_type);
      }
      return SYNC_ERROR;
    }

    // For auth errors first check if an auth is in progress.
    if (signin.AuthInProgress()) {
      if (status_label) {
        status_label->assign(
          l10n_util::GetStringUTF16(IDS_SYNC_AUTHENTICATING_LABEL));
      }
      return PRE_SYNCED;
    }

    PrefService* pref_service = profile->GetPrefs();
    syncer::SyncPrefs sync_prefs(pref_service);
    bool sync_everything = sync_prefs.HasKeepEverythingSynced();

    // Check for sync errors if the sync service is enabled.
    if (service) {
      // Since there is no auth in progress, check for an auth error first.
      AuthError auth_error =
          SigninErrorControllerFactory::GetForProfile(profile)->auth_error();
      if (auth_error.state() != AuthError::NONE) {
        if (status_label && link_label) {
          GetStatusForAuthError(profile, signin, status_label, link_label,
                                action_type);
        }
        return SYNC_ERROR;
      }

      // We don't have an auth error. Check for an actionable error.
      ProfileSyncService::Status status;
      service->QueryDetailedSyncStatus(&status);
      if (status_label && link_label) {
        GetStatusForActionableError(status.sync_protocol_error, status_label,
                                    link_label, action_type);
        if (!status_label->empty())
          return SYNC_ERROR;
      }

      // Check for a passphrase error.
      if (service->IsPassphraseRequired() &&
          service->IsPassphraseRequiredForDecryption()) {
        if (status_label && link_label) {
          status_label->assign(
              l10n_util::GetStringUTF16(IDS_SYNC_STATUS_NEEDS_PASSWORD));
          link_label->assign(
              l10n_util::GetStringUTF16(
                  IDS_SYNC_STATUS_NEEDS_PASSWORD_LINK_LABEL));
          *action_type = ENTER_PASSPHRASE;
        }
        return SYNC_ERROR;
      }

      // Check to see if sync has been disabled via the dasboard and needs to be
      // set up once again.
      if (!service->IsSyncRequested() &&
          status.sync_protocol_error.error_type == syncer::NOT_MY_BIRTHDAY) {
        if (status_label) {
          status_label->assign(GetSyncedStateStatusLabel(service, signin, style,
                                                         sync_everything));
        }
        return PRE_SYNCED;
      }
    }

    // There is no error. Display "Last synced..." message.
    if (status_label) {
      status_label->assign(
          GetSyncedStateStatusLabel(service, signin, style, sync_everything));
    }
    return SYNCED;
  } else {
    // Either show auth error information with a link to re-login, auth in prog,
    // or provide a link to continue with setup.
    if (service->IsFirstSetupInProgress()) {
      result_type = PRE_SYNCED;
      ProfileSyncService::Status status;
      service->QueryDetailedSyncStatus(&status);
      AuthError auth_error =
          SigninErrorControllerFactory::GetForProfile(profile)->auth_error();
      if (status_label) {
        status_label->assign(
            l10n_util::GetStringUTF16(IDS_SYNC_NTP_SETUP_IN_PROGRESS));
      }
      if (signin.AuthInProgress()) {
        if (status_label) {
          status_label->assign(
              l10n_util::GetStringUTF16(IDS_SYNC_AUTHENTICATING_LABEL));
        }
      } else if (auth_error.state() != AuthError::NONE &&
                 auth_error.state() != AuthError::TWO_FACTOR) {
        if (status_label && link_label) {
          GetStatusForAuthError(profile, signin, status_label, link_label,
                                action_type);
        }
        result_type = SYNC_ERROR;
      }
    } else if (service->HasUnrecoverableError()) {
      result_type = SYNC_ERROR;
      if (status_label && link_label) {
        GetStatusForUnrecoverableError(profile, service, status_label,
                                       link_label, action_type);
      }
    } else if (signin.IsAuthenticated()) {
      // The user is signed in, but sync has been stopped.
      result_type = PRE_SYNCED;
      if (status_label) {
        status_label->assign(
            l10n_util::GetStringUTF16(IDS_SIGNED_IN_WITH_SYNC_SUPPRESSED));
      }
    }
  }
  return result_type;
}

// Returns the status info for use on the new tab page, where we want slightly
// different information than in the settings panel.
MessageType GetStatusInfoForNewTabPage(Profile* profile,
                                       ProfileSyncService* service,
                                       const SigninManagerBase& signin,
                                       base::string16* status_label,
                                       base::string16* link_label) {
  DCHECK(status_label);
  DCHECK(link_label);

  if (service->IsFirstSetupComplete() && service->IsPassphraseRequired()) {
    if (service->passphrase_required_reason() == syncer::REASON_ENCRYPTION) {
      // First machine migrating to passwords.  Show as a promotion.
      if (status_label && link_label) {
        status_label->assign(
            l10n_util::GetStringFUTF16(
                IDS_SYNC_NTP_PASSWORD_PROMO,
                l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
        link_label->assign(
            l10n_util::GetStringUTF16(IDS_SYNC_NTP_PASSWORD_ENABLE));
      }
      return SYNC_PROMO;
    } else {
      // NOT first machine.
      // Show a link and present as an error ("needs attention").
      if (status_label && link_label) {
        status_label->assign(base::string16());
        link_label->assign(
            l10n_util::GetStringUTF16(IDS_SYNC_CONFIGURE_ENCRYPTION));
      }
      return SYNC_ERROR;
    }
  }

  // Fallback to default.
  ActionType action_type = NO_ACTION;
  return GetStatusInfo(profile, service, signin, WITH_HTML, status_label,
                       link_label, &action_type);
}

}  // namespace

MessageType GetStatusLabels(Profile* profile,
                            ProfileSyncService* service,
                            const SigninManagerBase& signin,
                            StatusLabelStyle style,
                            base::string16* status_label,
                            base::string16* link_label,
                            ActionType* action_type) {
  DCHECK(status_label);
  DCHECK(link_label);
  return sync_ui_util::GetStatusInfo(profile, service, signin, style,
                                     status_label, link_label, action_type);
}

MessageType GetStatusLabelsForNewTabPage(Profile* profile,
                                         ProfileSyncService* service,
                                         const SigninManagerBase& signin,
                                         base::string16* status_label,
                                         base::string16* link_label) {
  DCHECK(status_label);
  DCHECK(link_label);
  return sync_ui_util::GetStatusInfoForNewTabPage(profile, service, signin,
                                                  status_label, link_label);
}

#if !defined(OS_CHROMEOS)
void GetStatusLabelsForSyncGlobalError(const ProfileSyncService* service,
                                       base::string16* menu_label,
                                       base::string16* bubble_message,
                                       base::string16* bubble_accept_label) {
  DCHECK(menu_label);
  DCHECK(bubble_message);
  DCHECK(bubble_accept_label);
  *menu_label = base::string16();
  *bubble_message = base::string16();
  *bubble_accept_label = base::string16();

  // Only display an error if we've completed sync setup.
  if (!service->IsFirstSetupComplete())
    return;

  // Display a passphrase error if we have one.
  if (service->IsPassphraseRequired() &&
      service->IsPassphraseRequiredForDecryption()) {
    // This is not the first machine so ask user to enter passphrase.
    *menu_label = l10n_util::GetStringUTF16(
        IDS_SYNC_PASSPHRASE_ERROR_WRENCH_MENU_ITEM);
    *bubble_message = l10n_util::GetStringUTF16(
        IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_MESSAGE);
    *bubble_accept_label = l10n_util::GetStringUTF16(
        IDS_SYNC_PASSPHRASE_ERROR_BUBBLE_VIEW_ACCEPT);
    return;
  }
}

AvatarSyncErrorType GetMessagesForAvatarSyncError(Profile* profile,
                                                  int* content_string_id,
                                                  int* button_string_id) {
  ProfileSyncService* service =
      ProfileSyncServiceFactory::GetForProfile(profile);

  // The order or priority is going to be: 1. Unrecoverable errors.
  // 2. Auth errors. 3. Protocol errors. 4. Passphrase errors.
  if (service && service->HasUnrecoverableError()) {
    // An unrecoverable error is sometimes accompanied by an actionable error.
    // If an actionable error is not set to be UPGRADE_CLIENT, then show a
    // generic unrecoverable error message.
    ProfileSyncService::Status status;
    service->QueryDetailedSyncStatus(&status);
    if (status.sync_protocol_error.action != syncer::UPGRADE_CLIENT) {
      // Display different messages and buttons for managed accounts.
      if (SigninManagerFactory::GetForProfile(profile)->IsSignoutProhibited()) {
        // For a managed user, the user is directed to the signout
        // confirmation dialogue in the settings page.
        *content_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNOUT_MESSAGE;
        *button_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNOUT_BUTTON;
        return MANAGED_USER_UNRECOVERABLE_ERROR;
      }
      // For a non-managed user, we sign out on the user's behalf and prompt
      // the user to sign in again.
      *content_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNIN_AGAIN_MESSAGE;
      *button_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNIN_AGAIN_BUTTON;
      return UNRECOVERABLE_ERROR;
    }
  }

  // Check for an auth error.
  SigninErrorController* signin_error_controller =
      SigninErrorControllerFactory::GetForProfile(profile);
  if (signin_error_controller && signin_error_controller->HasError()) {
    if (profile->IsSupervised()) {
      // For a supervised user, no direct action can be taken to resolve an
      // auth token error.
      *content_string_id = IDS_SYNC_ERROR_USER_MENU_SUPERVISED_SIGNIN_MESSAGE;
      *button_string_id = 0;
      return SUPERVISED_USER_AUTH_ERROR;
    }
    // For a non-supervised user, the user can reauth to resolve the signin
    // error.
    *content_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNIN_MESSAGE;
    *button_string_id = IDS_SYNC_ERROR_USER_MENU_SIGNIN_BUTTON;
    return AUTH_ERROR;
  }

  // Check for sync errors if the sync service is enabled.
  if (service) {
    // Check for an actionable UPGRADE_CLIENT error.
    ProfileSyncService::Status status;
    service->QueryDetailedSyncStatus(&status);
    if (status.sync_protocol_error.action == syncer::UPGRADE_CLIENT) {
      *content_string_id = IDS_SYNC_ERROR_USER_MENU_UPGRADE_MESSAGE;
      *button_string_id = IDS_SYNC_ERROR_USER_MENU_UPGRADE_BUTTON;
      return UPGRADE_CLIENT_ERROR;
    }

    // Check for a sync passphrase error.
    syncer::SyncErrorController* sync_error_controller =
        service->sync_error_controller();
    if (sync_error_controller && sync_error_controller->HasError()) {
      *content_string_id = IDS_SYNC_ERROR_USER_MENU_PASSPHRASE_MESSAGE;
      *button_string_id = IDS_SYNC_ERROR_USER_MENU_PASSPHRASE_BUTTON;
      return PASSPHRASE_ERROR;
    }
  }

  // There is no error.
  return NO_SYNC_ERROR;
}
#endif

MessageType GetStatus(Profile* profile,
                      ProfileSyncService* service,
                      const SigninManagerBase& signin) {
  ActionType action_type = NO_ACTION;
  return sync_ui_util::GetStatusInfo(profile, service, signin, WITH_HTML,
                                     nullptr, nullptr, &action_type);
}

base::string16 ConstructTime(int64_t time_in_int) {
  base::Time time = base::Time::FromInternalValue(time_in_int);

  // If time is null the format function returns a time in 1969.
  if (time.is_null())
    return base::string16();
  return base::TimeFormatFriendlyDateAndTime(time);
}

}  // namespace sync_ui_util