summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/sync_setup_flow.cc
blob: 9806c7684f870971ad9b8135fb28e6ab3028cb9c (plain)
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// 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_setup_flow.h"

#include "base/callback.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/sync_setup_flow_handler.h"
#include "chrome/browser/sync/syncable/model_type.h"
#include "chrome/browser/sync/user_selectable_sync_type.h"
#include "chrome/browser/sync/util/oauth.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/gaia/google_service_auth_error.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"

namespace {

// Helper function to disable password sync.
void DisablePasswordSync(ProfileSyncService* service) {
  syncable::ModelTypeSet types = service->GetPreferredDataTypes();
  types.Remove(syncable::PASSWORDS);
  service->OnUserChoseDatatypes(false, types);
}

// Returns the next step for the non-fatal error case.
SyncSetupWizard::State GetStepForNonFatalError(ProfileSyncService* service) {
  // TODO(sync): Update this error handling to allow different platforms to
  // display the error appropriately (http://crbug.com/92722) instead of
  // navigating to a LOGIN state that is not supported on every platform.
  if (service->IsPassphraseRequired()) {
#if defined(OS_CHROMEOS)
    // On ChromeOS, we never want to request login information; this state
    // always represents an invalid secondary passphrase.
    // TODO(sync): correctly handle auth errors on ChromeOS: crosbug.com/24647.
    return SyncSetupWizard::ENTER_PASSPHRASE;
#else
    if (service->IsUsingSecondaryPassphrase())
      return SyncSetupWizard::ENTER_PASSPHRASE;
    return SyncSetupWizard::GetLoginState();
#endif
  }

  const GoogleServiceAuthError& error = service->GetAuthError();
  if (error.state() == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
      error.state() == GoogleServiceAuthError::CAPTCHA_REQUIRED ||
      error.state() == GoogleServiceAuthError::ACCOUNT_DELETED ||
      error.state() == GoogleServiceAuthError::ACCOUNT_DISABLED ||
      error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
    return SyncSetupWizard::GetLoginState();
  }

  NOTREACHED();
  return SyncSetupWizard::FATAL_ERROR;
}

bool HasConfigurationChanged(const SyncConfiguration& configuration,
                             Profile* profile) {
  CHECK(profile);

  // This function must be updated every time a new sync datatype is added to
  // the sync preferences page.
  COMPILE_ASSERT(17 == syncable::MODEL_TYPE_COUNT,
                 UpdateCustomConfigHistogram);

  // If service is null or if this is a first time configuration, return true.
  ProfileSyncService* service = profile->GetProfileSyncService();
  if (!service || !service->HasSyncSetupCompleted())
    return true;

  if ((configuration.set_secondary_passphrase ||
       configuration.set_gaia_passphrase) &&
      !service->IsUsingSecondaryPassphrase()) {
    return true;
  }

  if (configuration.encrypt_all != service->EncryptEverythingEnabled())
    return true;

  PrefService* pref_service = profile->GetPrefs();
  CHECK(pref_service);

  if (configuration.sync_everything !=
      pref_service->GetBoolean(prefs::kSyncKeepEverythingSynced)) {
    return true;
  }

  // Only check the data types that are explicitly listed on the sync
  // preferences page.
  const syncable::ModelTypeSet types = configuration.data_types;
  if (((types.Has(syncable::BOOKMARKS)) !=
       pref_service->GetBoolean(prefs::kSyncBookmarks)) ||
      ((types.Has(syncable::PREFERENCES)) !=
       pref_service->GetBoolean(prefs::kSyncPreferences)) ||
      ((types.Has(syncable::THEMES)) !=
       pref_service->GetBoolean(prefs::kSyncThemes)) ||
      ((types.Has(syncable::PASSWORDS)) !=
       pref_service->GetBoolean(prefs::kSyncPasswords)) ||
      ((types.Has(syncable::AUTOFILL)) !=
       pref_service->GetBoolean(prefs::kSyncAutofill)) ||
      ((types.Has(syncable::EXTENSIONS)) !=
       pref_service->GetBoolean(prefs::kSyncExtensions)) ||
      ((types.Has(syncable::TYPED_URLS)) !=
       pref_service->GetBoolean(prefs::kSyncTypedUrls)) ||
      ((types.Has(syncable::SESSIONS)) !=
       pref_service->GetBoolean(prefs::kSyncSessions)) ||
      ((types.Has(syncable::APPS)) !=
       pref_service->GetBoolean(prefs::kSyncApps))) {
    return true;
  }

  return false;
}

void UpdateHistogram(const SyncConfiguration& configuration,
                     const ProfileSyncService* service) {
  if (!service)
    return;
  Profile* profile = service->profile();
  if (HasConfigurationChanged(configuration, profile)) {
    UMA_HISTOGRAM_BOOLEAN("Sync.SyncEverything",
                          configuration.sync_everything);
    if (!configuration.sync_everything) {
      // Only log the data types that are explicitly listed on the sync
      // preferences page.
      const syncable::ModelTypeSet types = configuration.data_types;
      if (types.Has(syncable::BOOKMARKS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::BOOKMARKS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::PREFERENCES)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::PREFERENCES,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::PASSWORDS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::PASSWORDS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::AUTOFILL)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::AUTOFILL,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::THEMES)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::THEMES,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::TYPED_URLS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::TYPED_URLS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::EXTENSIONS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::EXTENSIONS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::SESSIONS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::SESSIONS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      if (types.Has(syncable::APPS)) {
        UMA_HISTOGRAM_ENUMERATION(
            "Sync.CustomSync",
            browser_sync::user_selectable_type::APPS,
            browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT + 1);
      }
      COMPILE_ASSERT(17 == syncable::MODEL_TYPE_COUNT,
                     UpdateCustomConfigHistogram);
      COMPILE_ASSERT(
          9 == browser_sync::user_selectable_type::SELECTABLE_DATATYPE_COUNT,
          UpdateCustomConfigHistogram);
    }
    UMA_HISTOGRAM_BOOLEAN("Sync.EncryptAllData", configuration.encrypt_all);
    UMA_HISTOGRAM_BOOLEAN("Sync.CustomPassphrase",
                          configuration.set_gaia_passphrase ||
                          configuration.set_secondary_passphrase);
  }
}

}  // namespace

SyncConfiguration::SyncConfiguration()
    : encrypt_all(false),
      sync_everything(false),
      set_secondary_passphrase(false),
      set_gaia_passphrase(false) {
}

SyncConfiguration::~SyncConfiguration() {}

SyncSetupFlow::~SyncSetupFlow() {
  flow_handler_->SetFlow(NULL);
}

// static
SyncSetupFlow* SyncSetupFlow::Run(ProfileSyncService* service,
                                  SyncSetupFlowContainer* container,
                                  SyncSetupWizard::State start,
                                  SyncSetupWizard::State end) {
  if (start == SyncSetupWizard::NONFATAL_ERROR)
    start = GetStepForNonFatalError(service);
  if ((start == SyncSetupWizard::CONFIGURE ||
       start == SyncSetupWizard::SYNC_EVERYTHING ||
       start == SyncSetupWizard::ENTER_PASSPHRASE) &&
      !service->sync_initialized()) {
    // We are trying to open configuration window, but the backend isn't ready.
    // We just return NULL. This has the effect of the flow getting reset, and
    // the user's action has no effect.
    LOG(ERROR) << "Attempted to show sync configure before backend ready.";
    return NULL;
  }
  return new SyncSetupFlow(start, end, container, service);
}

void SyncSetupFlow::GetArgsForGaiaLogin(DictionaryValue* args) {
  const GoogleServiceAuthError& error = service_->GetAuthError();
  if (!last_attempted_user_email_.empty()) {
    args->SetString("user", last_attempted_user_email_);
    args->SetInteger("error", error.state());
    args->SetBoolean("editable_user", true);
  } else {
    string16 user;
    user = UTF8ToUTF16(service_->profile()->GetPrefs()->GetString(
        prefs::kGoogleServicesUsername));
    args->SetString("user", user);
    args->SetInteger("error", 0);
    args->SetBoolean("editable_user", user.empty());
  }

  args->SetString("captchaUrl", error.captcha().image_url.spec());
}

void SyncSetupFlow::GetArgsForConfigure(DictionaryValue* args) {
  // The SYNC_EVERYTHING case will set this to true.
  args->SetBoolean("showSyncEverythingPage", false);

  args->SetBoolean("syncAllDataTypes",
      service_->profile()->GetPrefs()->GetBoolean(
          prefs::kSyncKeepEverythingSynced));

  // Bookmarks, Preferences, and Themes are launched for good, there's no
  // going back now.  Check if the other data types are registered though.
  const syncable::ModelTypeSet registered_types =
      service_->GetRegisteredDataTypes();
  const syncable::ModelTypeSet preferred_types =
      service_->GetPreferredDataTypes();
  args->SetBoolean("passwordsRegistered",
      registered_types.Has(syncable::PASSWORDS));
  args->SetBoolean("autofillRegistered",
      registered_types.Has(syncable::AUTOFILL));
  args->SetBoolean("extensionsRegistered",
      registered_types.Has(syncable::EXTENSIONS));
  args->SetBoolean("typedUrlsRegistered",
      registered_types.Has(syncable::TYPED_URLS));
  args->SetBoolean("appsRegistered",
      registered_types.Has(syncable::APPS));
  args->SetBoolean("sessionsRegistered",
      registered_types.Has(syncable::SESSIONS));
  args->SetBoolean("syncBookmarks",
                   preferred_types.Has(syncable::BOOKMARKS));
  args->SetBoolean("syncPreferences",
                   preferred_types.Has(syncable::PREFERENCES));
  args->SetBoolean("syncThemes",
                   preferred_types.Has(syncable::THEMES));
  args->SetBoolean("syncPasswords",
                   preferred_types.Has(syncable::PASSWORDS));
  args->SetBoolean("syncAutofill",
                   preferred_types.Has(syncable::AUTOFILL));
  args->SetBoolean("syncExtensions",
                   preferred_types.Has(syncable::EXTENSIONS));
  args->SetBoolean("syncSessions",
                   preferred_types.Has(syncable::SESSIONS));
  args->SetBoolean("syncTypedUrls",
                   preferred_types.Has(syncable::TYPED_URLS));
  args->SetBoolean("syncApps",
                   preferred_types.Has(syncable::APPS));

  args->SetBoolean("encryptionEnabled",
      !CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableSyncEncryption));

  bool encrypt_all = service_->EncryptEverythingEnabled();
  if (service_->encryption_pending())
    encrypt_all = true;
  args->SetBoolean("encryptAllData", encrypt_all);

  // Load the parameters for the encryption tab.
  args->SetBoolean("usePassphrase", service_->IsUsingSecondaryPassphrase());

  // Determine if we need a passphrase or not, and if so, prompt the user.
  if (service_->IsPassphraseRequiredForDecryption()) {
    // We need a passphrase, so we have to prompt the user.
    args->SetBoolean("show_passphrase", true);
    // Tell the UI layer what kind of passphrase we need.
    args->SetBoolean("need_google_passphrase",
                     !service_->IsUsingSecondaryPassphrase());
    args->SetBoolean("passphrase_creation_rejected",
                     user_tried_creating_explicit_passphrase_);
    args->SetBoolean("passphrase_setting_rejected",
                     user_tried_setting_passphrase_);
  }
}

bool SyncSetupFlow::AttachSyncSetupHandler(SyncSetupFlowHandler* handler) {
  if (flow_handler_)
    return false;

  flow_handler_ = handler;
  handler->SetFlow(this);
  ActivateState(current_state_);
  return true;
}

bool SyncSetupFlow::IsAttached() const {
  return flow_handler_ != NULL;
}

void SyncSetupFlow::Advance(SyncSetupWizard::State advance_state) {
  if (!ShouldAdvance(advance_state)) {
    LOG(WARNING) << "Invalid state change from "
                 << current_state_ << " to " << advance_state;
    return;
  }

  if (flow_handler_)
    ActivateState(advance_state);
}

void SyncSetupFlow::Focus() {
  // This gets called from SyncSetupWizard::Focus(), and might get called
  // before flow_handler_ is set in AttachSyncSetupHandler() (which gets
  // called asynchronously after the UI initializes).
  if (flow_handler_)
    flow_handler_->Focus();
}

// A callback to notify the delegate that the dialog closed.
// TODO(rickcam): Bug 90713: Handle OAUTH_LOGIN case here
void SyncSetupFlow::OnDialogClosed(const std::string& json_retval) {
  DCHECK(json_retval.empty());
  container_->set_flow(NULL);  // Sever ties from the wizard.
  // If we've reached the end, mark it.  This could be a discrete run, in which
  // case it's already set, but it simplifes the logic to do it this way.
  if (current_state_ == end_state_)
    service_->SetSyncSetupCompleted();

  // Record the state at which the user cancelled the signon dialog.
  switch (current_state_) {
    case SyncSetupWizard::GAIA_LOGIN:
      ProfileSyncService::SyncEvent(
          ProfileSyncService::CANCEL_FROM_SIGNON_WITHOUT_AUTH);
      break;
    case SyncSetupWizard::GAIA_SUCCESS:
      ProfileSyncService::SyncEvent(
          ProfileSyncService::CANCEL_DURING_SIGNON);
      break;
    case SyncSetupWizard::CONFIGURE:
    case SyncSetupWizard::ENTER_PASSPHRASE:
    case SyncSetupWizard::SETTING_UP:
      // TODO(atwilson): Treat a close during ENTER_PASSPHRASE like a
      // Cancel + Skip (i.e. call OnPassphraseCancel()). http://crbug.com/74645
      ProfileSyncService::SyncEvent(
          ProfileSyncService::CANCEL_DURING_CONFIGURE);
      break;
    case SyncSetupWizard::DONE:
      // TODO(sync): rename this histogram; it's tracking authorization AND
      // initial sync download time.
      UMA_HISTOGRAM_MEDIUM_TIMES("Sync.UserPerceivedAuthorizationTime",
                                 base::TimeTicks::Now() - login_start_time_);
      break;
    default:
      break;
  }

  service_->SetUIShouldDepictAuthInProgress(false);
  service_->OnUserCancelledDialog();
  delete this;
}

void SyncSetupFlow::OnUserSubmittedAuth(const std::string& username,
                                        const std::string& password,
                                        const std::string& captcha,
                                        const std::string& access_code) {
  last_attempted_user_email_ = username;
  service_->SetUIShouldDepictAuthInProgress(true);

  // If we're just being called to provide an ASP, then pass it to the
  // SigninManager and wait for the next step.
  if (!access_code.empty()) {
    service_->signin()->ProvideSecondFactorAccessCode(access_code);
    return;
  }

  // Kick off a sign-in through the signin manager.
  SigninManager* signin = service_->signin();
  signin->StartSignIn(username,
                      password,
                      signin->GetLoginAuthError().captcha().token,
                      captcha);
}

void SyncSetupFlow::OnUserSubmittedOAuth(
    const std::string& oauth1_request_token) {
  service_->signin()->StartOAuthSignIn(oauth1_request_token);
}

void SyncSetupFlow::OnUserConfigured(const SyncConfiguration& configuration) {
  // Update sync histograms. This is a no-op if |configuration| has not changed.
  UpdateHistogram(configuration, service_);

  // Go to the "loading..." screen.
  Advance(SyncSetupWizard::SETTING_UP);

  // Note: encryption will not occur until OnUserChoseDatatypes is called.
  if (configuration.encrypt_all)
    service_->EnableEncryptEverything();

  bool set_new_decryption_passphrase = false;
  if (configuration.set_gaia_passphrase &&
      !configuration.gaia_passphrase.empty()) {
    // Caller passed a gaia passphrase. This is illegal if we are currently
    // using a secondary passphrase.
    DCHECK(!service_->IsUsingSecondaryPassphrase());
    service_->SetPassphrase(configuration.gaia_passphrase, false);
    // Since the user entered the passphrase manually, set this flag so we can
    // report an error if the passphrase setting failed.
    user_tried_setting_passphrase_ = true;
    set_new_decryption_passphrase = true;
  }

  // Set the secondary passphrase, either as a decryption passphrase, or
  // as an attempt to encrypt the user's data using this new passphrase.
  if (configuration.set_secondary_passphrase &&
      !configuration.secondary_passphrase.empty()) {
    service_->SetPassphrase(configuration.secondary_passphrase, true);
    if (service_->IsUsingSecondaryPassphrase()) {
      user_tried_setting_passphrase_ = true;
      set_new_decryption_passphrase = true;
    } else {
      user_tried_creating_explicit_passphrase_ = true;
    }
  }

  service_->OnUserChoseDatatypes(configuration.sync_everything,
                                 configuration.data_types);

  // See if we are done configuring (if we don't need a passphrase, and don't
  // need to hang around waiting for encryption to happen, just exit). This call
  // to IsPassphraseRequiredForDecryption() takes into account the data types
  // we just enabled/disabled.
  if (!service_->IsPassphraseRequiredForDecryption() &&
      !service_->encryption_pending()) {
    Advance(SyncSetupWizard::DONE);
  } else if (!set_new_decryption_passphrase) {
    // We need a passphrase, but the user did not provide one, so transition
    // directly to ENTER_PASSPHRASE (otherwise we'll have to wait until
    // the sync engine generates another OnPassphraseRequired() at the end of
    // the sync cycle which can take a long time).
    Advance(SyncSetupWizard::ENTER_PASSPHRASE);
  }
}

void SyncSetupFlow::OnPassphraseEntry(const std::string& passphrase) {
  Advance(SyncSetupWizard::SETTING_UP);
  service_->SetPassphrase(passphrase, true);
  user_tried_setting_passphrase_ = true;
}

void SyncSetupFlow::OnPassphraseCancel() {
  // If the user cancels when being asked for the passphrase,
  // just disable encrypted sync and continue setting up.
  if (current_state_ == SyncSetupWizard::ENTER_PASSPHRASE)
    DisablePasswordSync(service_);

  Advance(SyncSetupWizard::SETTING_UP);
}

// Use static Run method to get an instance.
SyncSetupFlow::SyncSetupFlow(SyncSetupWizard::State start_state,
                             SyncSetupWizard::State end_state,
                             SyncSetupFlowContainer* container,
                             ProfileSyncService* service)
    : container_(container),
      current_state_(start_state),
      end_state_(end_state),
      login_start_time_(base::TimeTicks::Now()),
      flow_handler_(NULL),
      service_(service),
      user_tried_creating_explicit_passphrase_(false),
      user_tried_setting_passphrase_(false) {
}

// Returns true if the flow should advance to |state| based on |current_state_|.
bool SyncSetupFlow::ShouldAdvance(SyncSetupWizard::State state) {
  switch (state) {
    case SyncSetupWizard::OAUTH_LOGIN:
      return current_state_ == SyncSetupWizard::FATAL_ERROR ||
             current_state_ == SyncSetupWizard::OAUTH_LOGIN ||
             current_state_ == SyncSetupWizard::SETTING_UP;
    case SyncSetupWizard::GAIA_LOGIN:
      return current_state_ == SyncSetupWizard::FATAL_ERROR ||
             current_state_ == SyncSetupWizard::GAIA_LOGIN ||
             current_state_ == SyncSetupWizard::SETTING_UP;
    case SyncSetupWizard::GAIA_SUCCESS:
      return current_state_ == SyncSetupWizard::GAIA_LOGIN ||
             current_state_ == SyncSetupWizard::OAUTH_LOGIN;
    case SyncSetupWizard::SYNC_EVERYTHING:  // Intentionally fall through.
    case SyncSetupWizard::CONFIGURE:
      return current_state_ == SyncSetupWizard::GAIA_SUCCESS;
    case SyncSetupWizard::ENTER_PASSPHRASE:
      return (service_->auto_start_enabled() &&
              current_state_ == SyncSetupWizard::GAIA_LOGIN) ||
             current_state_ == SyncSetupWizard::SYNC_EVERYTHING ||
             current_state_ == SyncSetupWizard::CONFIGURE ||
             current_state_ == SyncSetupWizard::SETTING_UP;
    case SyncSetupWizard::SETUP_ABORTED_BY_PENDING_CLEAR:
      return current_state_ != SyncSetupWizard::ABORT;
    case SyncSetupWizard::SETTING_UP:
      return current_state_ == SyncSetupWizard::SYNC_EVERYTHING ||
             current_state_ == SyncSetupWizard::CONFIGURE ||
             current_state_ == SyncSetupWizard::ENTER_PASSPHRASE;
    case SyncSetupWizard::NONFATAL_ERROR:  // Intentionally fall through.
    case SyncSetupWizard::FATAL_ERROR:
      return current_state_ != SyncSetupWizard::ABORT;
    case SyncSetupWizard::ABORT:
      return true;
    case SyncSetupWizard::DONE:
      return current_state_ == SyncSetupWizard::SETTING_UP ||
             current_state_ == SyncSetupWizard::ENTER_PASSPHRASE;
    default:
      NOTREACHED() << "Unhandled State: " << state;
      return false;
  }
}

void SyncSetupFlow::ActivateState(SyncSetupWizard::State state) {
  DCHECK(flow_handler_);

  if (state == SyncSetupWizard::NONFATAL_ERROR)
    state = GetStepForNonFatalError(service_);

  current_state_ = state;

  switch (state) {
    case SyncSetupWizard::OAUTH_LOGIN: {
      flow_handler_->ShowOAuthLogin();
      break;
    }
    case SyncSetupWizard::GAIA_LOGIN: {
      DictionaryValue args;
      GetArgsForGaiaLogin(&args);
      flow_handler_->ShowGaiaLogin(args);
      break;
    }
    case SyncSetupWizard::GAIA_SUCCESS:
      // Authentication is complete now.
      service_->SetUIShouldDepictAuthInProgress(false);
      if (end_state_ == SyncSetupWizard::GAIA_SUCCESS) {
        flow_handler_->ShowGaiaSuccessAndClose();
        break;
      }
      flow_handler_->ShowGaiaSuccessAndSettingUp();
      break;
    case SyncSetupWizard::SYNC_EVERYTHING: {
      DictionaryValue args;
      GetArgsForConfigure(&args);
      args.SetBoolean("showSyncEverythingPage", true);
      flow_handler_->ShowConfigure(args);
      break;
    }
    case SyncSetupWizard::CONFIGURE: {
      DictionaryValue args;
      GetArgsForConfigure(&args);
      flow_handler_->ShowConfigure(args);
      break;
    }
    case SyncSetupWizard::ENTER_PASSPHRASE: {
      DictionaryValue args;
      GetArgsForConfigure(&args);
      // TODO(atwilson): Remove ShowPassphraseEntry in favor of using
      // ShowConfigure() - http://crbug.com/90786.
      flow_handler_->ShowPassphraseEntry(args);
      break;
    }
    case SyncSetupWizard::SETUP_ABORTED_BY_PENDING_CLEAR: {
      // TODO(sync): We should expose a real "display an error" API on
      // SyncSetupFlowHandler (crbug.com/92722) but for now just transition
      // to the login state with a special error code.
      DictionaryValue args;
      GetArgsForGaiaLogin(&args);
      args.SetInteger("error", GoogleServiceAuthError::SERVICE_UNAVAILABLE);
      current_state_ = SyncSetupWizard::GAIA_LOGIN;
      flow_handler_->ShowGaiaLogin(args);
      break;
    }
    case SyncSetupWizard::SETTING_UP: {
      flow_handler_->ShowSettingUp();
      break;
    }
    case SyncSetupWizard::FATAL_ERROR: {
      // This shows the user the "Could not connect to server" error.
      // TODO(sync): Update this error handling to allow different platforms to
      // display the error appropriately (http://crbug.com/92722).
      DictionaryValue args;
      GetArgsForGaiaLogin(&args);
      args.SetBoolean("fatalError", true);
      current_state_ = SyncSetupWizard::GAIA_LOGIN;
      flow_handler_->ShowGaiaLogin(args);
      break;
    }
    case SyncSetupWizard::DONE:
    case SyncSetupWizard::ABORT:
      flow_handler_->ShowSetupDone(UTF8ToUTF16(
          service_->profile()->GetPrefs()->GetString(
          prefs::kGoogleServicesUsername)));
      break;
    default:
      NOTREACHED() << "Invalid advance state: " << state;
  }
}