summaryrefslogtreecommitdiffstats
path: root/chrome/service/remoting/chromoting_host_manager.cc
blob: aaffdac601f65cba18bc4b152416dd42f4d02ad5 (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
// 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(
    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.";
  }

  chromoting_config_ = config;

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

  if (IsEnabled()) {
    Start();
  }
}

void ChromotingHostManager::Teardown() {
  Stop();
}

ChromotingHostManager::~ChromotingHostManager() {}

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()) {
    SetEnabled(false);
    observer_->OnChromotingHostDisabled();
  }

  Stop();
}

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() {
  // Stop the host if it is started.
  if (chromoting_host_) {
    // Shutdown the chromoting host asynchronously. This will signal the host to
    // shutdown, we'll actually wait for all threads to stop when we destroy
    // the chromoting context.
    chromoting_host_->Shutdown();
    chromoting_host_ = NULL;

    chromoting_context_->Stop();
    chromoting_context_.reset();
  }
}

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());
  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() {
}

}  // namespace remoting