1// Copyright (c) 2011 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.
4
5#include "chrome/browser/sync/sync_setup_wizard.h"
6
7#include <stddef.h>
8#include <ostream>
9
10#include "base/logging.h"
11#include "chrome/browser/sync/sync_setup_flow.h"
12
13SyncSetupWizard::SyncSetupWizard(ProfileSyncService* service)
14    : service_(service),
15      flow_container_(new SyncSetupFlowContainer()) {
16}
17
18SyncSetupWizard::~SyncSetupWizard() {
19  delete flow_container_;
20}
21
22void SyncSetupWizard::Step(State advance_state) {
23  SyncSetupFlow* flow = flow_container_->get_flow();
24  if (flow) {
25    // A setup flow is in progress and dialog is currently showing.
26    flow->Advance(advance_state);
27  } else if (!service_->HasSyncSetupCompleted()) {
28    if (IsTerminalState(advance_state))
29      return;
30    // No flow is in progress, and we have never escorted the user all the
31    // way through the wizard flow.
32    flow_container_->set_flow(
33        SyncSetupFlow::Run(service_, flow_container_, advance_state, DONE));
34  } else {
35    // No flow in progress, but we've finished the wizard flow once before.
36    // This is just a discrete run.
37    if (IsTerminalState(advance_state))
38      return;  // Nothing to do.
39    flow_container_->set_flow(SyncSetupFlow::Run(service_, flow_container_,
40        advance_state, GetEndStateForDiscreteRun(advance_state)));
41  }
42}
43
44// static
45bool SyncSetupWizard::IsTerminalState(State advance_state) {
46  return advance_state == GAIA_SUCCESS ||
47         advance_state == DONE ||
48         advance_state == DONE_FIRST_TIME ||
49         advance_state == FATAL_ERROR ||
50         advance_state == SETUP_ABORTED_BY_PENDING_CLEAR;
51}
52
53bool SyncSetupWizard::IsVisible() const {
54  return flow_container_->get_flow() != NULL;
55}
56
57void SyncSetupWizard::Focus() {
58  SyncSetupFlow* flow = flow_container_->get_flow();
59  if (flow) {
60    flow->Focus();
61  }
62}
63
64SyncSetupFlow* SyncSetupWizard::AttachSyncSetupHandler(
65    SyncSetupFlowHandler* handler) {
66  SyncSetupFlow* flow = flow_container_->get_flow();
67  if (!flow)
68    return NULL;
69
70  flow->AttachSyncSetupHandler(handler);
71  return flow;
72}
73
74// static
75SyncSetupWizard::State SyncSetupWizard::GetEndStateForDiscreteRun(
76    State start_state) {
77  State result = FATAL_ERROR;
78  if (start_state == GAIA_LOGIN) {
79    result = GAIA_SUCCESS;
80  } else if (start_state == ENTER_PASSPHRASE ||
81             start_state == SYNC_EVERYTHING ||
82             start_state == CONFIGURE ||
83             start_state == PASSPHRASE_MIGRATION) {
84    result = DONE;
85  }
86  DCHECK_NE(FATAL_ERROR, result) <<
87      "Invalid start state for discrete run: " << start_state;
88  return result;
89}
90