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/remoting/setup_flow_get_status_step.h"
6
7#include "chrome/browser/remoting/setup_flow_register_step.h"
8#include "chrome/browser/service/service_process_control.h"
9#include "chrome/browser/service/service_process_control_manager.h"
10#include "grit/generated_resources.h"
11#include "ui/base/l10n/l10n_util.h"
12
13namespace remoting {
14
15SetupFlowGetStatusStep::SetupFlowGetStatusStep()
16    : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
17      status_requested_(false) {
18}
19
20SetupFlowGetStatusStep::~SetupFlowGetStatusStep() {
21  if (process_control_)
22    process_control_->RemoveMessageHandler(this);
23}
24
25void SetupFlowGetStatusStep::HandleMessage(const std::string& message,
26                                           const Value* arg) {
27}
28
29void SetupFlowGetStatusStep::Cancel() {
30  if (process_control_)
31    process_control_->RemoveMessageHandler(this);
32}
33
34void SetupFlowGetStatusStep::OnRemotingHostInfo(
35    const remoting::ChromotingHostInfo& host_info) {
36  if (status_requested_) {
37    flow()->context()->host_info = host_info;
38    status_requested_ = false;
39    FinishStep(new SetupFlowRegisterStep());
40  }
41}
42
43void SetupFlowGetStatusStep::DoStart() {
44  flow()->web_ui()->CallJavascriptFunction("showSettingUp");
45
46  process_control_ =
47      ServiceProcessControlManager::GetInstance()->GetProcessControl(
48          flow()->profile());
49  if (!process_control_->is_connected()) {
50    LaunchServiceProcess();
51  } else {
52    RequestStatus();
53  }
54}
55
56void SetupFlowGetStatusStep::LaunchServiceProcess() {
57  Task* done_task = task_factory_.NewRunnableMethod(
58      &SetupFlowGetStatusStep::OnServiceProcessLaunched);
59  process_control_->Launch(done_task, done_task);
60}
61
62void SetupFlowGetStatusStep::OnServiceProcessLaunched() {
63  if (!process_control_->is_connected()) {
64    // Failed to start service process.
65    FinishStep(new SetupFlowGetStatusErrorStep());
66  } else {
67    RequestStatus();
68  }
69}
70
71void SetupFlowGetStatusStep::RequestStatus() {
72  DCHECK(!status_requested_);
73
74  if (!process_control_->RequestRemotingHostStatus()) {
75    FinishStep(new SetupFlowGetStatusErrorStep());
76    return;
77  }
78
79  status_requested_ = true;
80  process_control_->AddMessageHandler(this);
81}
82
83SetupFlowGetStatusErrorStep::SetupFlowGetStatusErrorStep() { }
84SetupFlowGetStatusErrorStep::~SetupFlowGetStatusErrorStep() { }
85
86string16 SetupFlowGetStatusErrorStep::GetErrorMessage() {
87  return l10n_util::GetStringUTF16(IDS_REMOTING_SERVICE_PROCESS_FAILED_MESSAGE);
88}
89
90void SetupFlowGetStatusErrorStep::Retry() {
91  FinishStep(new SetupFlowGetStatusStep());
92}
93
94}  // namespace remoting
95