summaryrefslogtreecommitdiffstats
path: root/chrome/service/remoting/chromoting_host_manager.cc
blob: b24eb4b34471080c80515306be5c740a93e6b002 (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
// Copyright (c) 2010 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/service/remoting/chromoting_host_manager.h"

#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/guid.h"
#include "chrome/common/remoting/chromoting_host_info.h"
#include "net/base/net_util.h"
#include "remoting/base/constants.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/json_host_config.h"

namespace remoting {

ChromotingHostManager::ChromotingHostManager(Observer* observer)
    : observer_(observer) {
}

void ChromotingHostManager::Initialize(
    MessageLoopForUI* main_message_loop,
    base::MessageLoopProxy* file_message_loop) {
  FilePath user_data_dir;
  PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
  FilePath chromoting_config_path =
      user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json"));
  remoting::JsonHostConfig* config = new remoting::JsonHostConfig(
      chromoting_config_path, file_message_loop);
  if (!config->Read()) {
    VLOG(1) << "Failed to read chromoting config file.";
  }

  main_message_loop_ = main_message_loop;
  chromoting_config_ = config;

  if (!IsConfigInitialized()) {
    InitializeConfig();
  }

  if (IsEnabled()) {
    // TODO(wez): Need to callback the Observer so that ServiceProcess
    // knows to stay alive to service Chromoting requests.
    // This will go away once we have a more consistent model for the
    // service process internals.
    observer_->OnChromotingHostEnabled();
    Start();
  }
}

void ChromotingHostManager::Teardown(Task* done_task) {
  Stop(done_task);
}

ChromotingHostManager::~ChromotingHostManager() {
  DCHECK(!chromoting_host_);
  DCHECK(!chromoting_context_.get());
}

bool ChromotingHostManager::IsConfigInitialized() {
  std::string host_id;
  if (!chromoting_config_->GetString(remoting::kHostIdConfigPath, &host_id))
    return false;

  return guid::IsValidGUID(host_id);
}

void ChromotingHostManager::InitializeConfig() {
  VLOG(1) << "Initializing static chromoting host parameters.";

  // TODO(hclam): This is a time consuming operation so we should run it on
  // a separate thread.
  remoting::HostKeyPair host_key_pair;
  host_key_pair.Generate();
  std::string host_id(guid::GenerateGUID());
  std::string hostname(net::GetHostName());

  chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath, false);
  chromoting_config_->SetString(remoting::kHostIdConfigPath, host_id);
  chromoting_config_->SetString(remoting::kHostNameConfigPath, hostname);
  host_key_pair.Save(chromoting_config_);

  // Save updated values on the disk.
  chromoting_config_->Save();
}

void ChromotingHostManager::SetCredentials(const std::string& login,
                                           const std::string& token) {
  chromoting_config_->SetString(remoting::kXmppLoginConfigPath, login);
  chromoting_config_->SetString(remoting::kXmppAuthTokenConfigPath, token);

  // Save updated values on the disk.
  chromoting_config_->Save();
}

void ChromotingHostManager::Enable() {
  // We have already started.
  if (IsEnabled())
    return;

  SetEnabled(true);
  observer_->OnChromotingHostEnabled();

  Start();
}

void ChromotingHostManager::Disable() {
  if (!IsEnabled())
    return;

  SetEnabled(false);

  // TODO(hclam): Immediately reporting will cause threading problems
  // ServiceProcess thinks we can shutdown safely.
  observer_->OnChromotingHostDisabled();

  Stop(NULL);
}

void ChromotingHostManager::GetHostInfo(ChromotingHostInfo* host_info) {
  chromoting_config_->GetString(remoting::kHostIdConfigPath,
                                &host_info->host_id);
  chromoting_config_->GetString(remoting::kHostNameConfigPath,
                                &host_info->hostname);
  HostKeyPair key_pair;
  if (key_pair.Load(chromoting_config_)) {
    host_info->public_key = key_pair.GetPublicKey();
  }

  host_info->enabled = IsEnabled();

  chromoting_config_->GetString(remoting::kXmppLoginConfigPath,
                                &host_info->login);
}

void ChromotingHostManager::Stop(Task* done_task) {
  // Stop the host if it is started.
  if (chromoting_host_) {
    // Save the shutdown task, it will be executed when chromoting host
    // is stopped.
    shutdown_task_.reset(done_task);

    // Shutdown the chromoting host asynchronously.
    chromoting_host_->Shutdown();
  } else if (done_task) {
    done_task->Run();
    delete done_task;
  }
}

bool ChromotingHostManager::IsEnabled() {
  bool enabled;
  if (!chromoting_config_->GetBoolean(remoting::kHostEnabledConfigPath,
                                      &enabled)) {
    enabled = false;
  }
  return enabled;
}

void ChromotingHostManager::SetEnabled(bool enabled) {
  chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath,
                                 enabled);
  chromoting_config_->Save();
}

void ChromotingHostManager::Start() {
  // Don't do anything if we already started.
  if (chromoting_host_.get())
    return;

  // Start the chromoting context first.
  chromoting_context_.reset(
      new remoting::ChromotingHostContext(main_message_loop_));
  chromoting_context_->Start();

  // Create a chromoting host object.
  chromoting_host_ = remoting::ChromotingHost::Create(chromoting_context_.get(),
                                                      chromoting_config_);

  // Then start the chromoting host.
  // When ChromotingHost is shutdown because of failure or a request that
  // we made OnChromotingShutdown() is calls.
  chromoting_host_->Start(
      NewRunnableMethod(this, &ChromotingHostManager::OnShutdown));
}

void ChromotingHostManager::OnShutdown() {
  if (MessageLoop::current() != main_message_loop_) {
    main_message_loop_->PostTask(FROM_HERE,
        NewRunnableMethod(this, &ChromotingHostManager::OnShutdown));
    return;
  }
  chromoting_context_->Stop();
  chromoting_context_.reset();
  chromoting_host_ = NULL;

  if (shutdown_task_.get()) {
    shutdown_task_->Run();
    shutdown_task_.reset();
  }
}

}  // namespace remoting