OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/service/remoting/chromoting_host_manager.h" |
| 6 |
| 7 #include "base/path_service.h" |
| 8 #include "chrome/common/chrome_paths.h" |
| 9 #include "chrome/common/guid.h" |
| 10 #include "chrome/common/remoting/chromoting_host_info.h" |
| 11 #include "net/base/net_util.h" |
| 12 #include "remoting/base/constants.h" |
| 13 #include "remoting/host/chromoting_host_context.h" |
| 14 #include "remoting/host/json_host_config.h" |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 ChromotingHostManager::ChromotingHostManager(Observer* observer) |
| 19 : observer_(observer) { |
| 20 } |
| 21 |
| 22 void ChromotingHostManager::Initialize( |
| 23 base::MessageLoopProxy* file_message_loop) { |
| 24 FilePath user_data_dir; |
| 25 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 26 FilePath chromoting_config_path = |
| 27 user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json")); |
| 28 remoting::JsonHostConfig* config = new remoting::JsonHostConfig( |
| 29 chromoting_config_path, file_message_loop); |
| 30 if (!config->Read()) { |
| 31 VLOG(1) << "Failed to read chromoting config file."; |
| 32 } |
| 33 |
| 34 chromoting_config_ = config; |
| 35 |
| 36 if (!IsConfigInitialized()) { |
| 37 InitializeConfig(); |
| 38 } |
| 39 |
| 40 if (IsEnabled()) { |
| 41 Start(); |
| 42 } |
| 43 } |
| 44 |
| 45 void ChromotingHostManager::Teardown() { |
| 46 Stop(); |
| 47 } |
| 48 |
| 49 bool ChromotingHostManager::IsConfigInitialized() { |
| 50 std::string host_id; |
| 51 if (!chromoting_config_->GetString(remoting::kHostIdConfigPath, &host_id)) |
| 52 return false; |
| 53 |
| 54 return guid::IsValidGUID(host_id); |
| 55 } |
| 56 |
| 57 void ChromotingHostManager::InitializeConfig() { |
| 58 VLOG(1) << "Initializing static chromoting host parameters."; |
| 59 |
| 60 // TODO(hclam): This is a time consuming operation so we should run it on |
| 61 // a separate thread. |
| 62 remoting::HostKeyPair host_key_pair; |
| 63 host_key_pair.Generate(); |
| 64 std::string host_id(guid::GenerateGUID()); |
| 65 std::string hostname(net::GetHostName()); |
| 66 |
| 67 chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath, false); |
| 68 chromoting_config_->SetString(remoting::kHostIdConfigPath, host_id); |
| 69 chromoting_config_->SetString(remoting::kHostNameConfigPath, hostname); |
| 70 host_key_pair.Save(chromoting_config_); |
| 71 |
| 72 // Save updated values on the disk. |
| 73 chromoting_config_->Save(); |
| 74 } |
| 75 |
| 76 void ChromotingHostManager::SetCredentials(const std::string& login, |
| 77 const std::string& token) { |
| 78 chromoting_config_->SetString(remoting::kXmppLoginConfigPath, login); |
| 79 chromoting_config_->SetString(remoting::kXmppAuthTokenConfigPath, token); |
| 80 |
| 81 // Save updated values on the disk. |
| 82 chromoting_config_->Save(); |
| 83 } |
| 84 |
| 85 void ChromotingHostManager::Enable() { |
| 86 // We have already started. |
| 87 if (IsEnabled()) |
| 88 return; |
| 89 |
| 90 SetEnabled(true); |
| 91 observer_->OnChromotingHostEnabled(); |
| 92 |
| 93 Start(); |
| 94 } |
| 95 |
| 96 void ChromotingHostManager::Disable() { |
| 97 if (IsEnabled()) { |
| 98 SetEnabled(false); |
| 99 observer_->OnChromotingHostDisabled(); |
| 100 } |
| 101 |
| 102 Stop(); |
| 103 } |
| 104 |
| 105 void ChromotingHostManager::GetHostInfo(ChromotingHostInfo* host_info) { |
| 106 chromoting_config_->GetString(remoting::kHostIdConfigPath, |
| 107 &host_info->host_id); |
| 108 chromoting_config_->GetString(remoting::kHostNameConfigPath, |
| 109 &host_info->hostname); |
| 110 HostKeyPair key_pair; |
| 111 if (key_pair.Load(chromoting_config_)) { |
| 112 host_info->public_key = key_pair.GetPublicKey(); |
| 113 } |
| 114 |
| 115 host_info->enabled = IsEnabled(); |
| 116 |
| 117 chromoting_config_->GetString(remoting::kXmppLoginConfigPath, |
| 118 &host_info->login); |
| 119 } |
| 120 |
| 121 void ChromotingHostManager::Stop() { |
| 122 // Stop the host if it is started. |
| 123 if (chromoting_host_) { |
| 124 // Shutdown the chromoting host asynchronously. This will signal the host to |
| 125 // shutdown, we'll actually wait for all threads to stop when we destroy |
| 126 // the chromoting context. |
| 127 chromoting_host_->Shutdown(); |
| 128 chromoting_host_ = NULL; |
| 129 |
| 130 chromoting_context_->Stop(); |
| 131 chromoting_context_.reset(); |
| 132 } |
| 133 } |
| 134 |
| 135 bool ChromotingHostManager::IsEnabled() { |
| 136 bool enabled; |
| 137 if (!chromoting_config_->GetBoolean(remoting::kHostEnabledConfigPath, |
| 138 &enabled)) { |
| 139 enabled = false; |
| 140 } |
| 141 return enabled; |
| 142 } |
| 143 |
| 144 void ChromotingHostManager::SetEnabled(bool enabled) { |
| 145 chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath, |
| 146 enabled); |
| 147 chromoting_config_->Save(); |
| 148 } |
| 149 |
| 150 void ChromotingHostManager::Start() { |
| 151 // Don't do anything if we already started. |
| 152 if (chromoting_host_.get()) |
| 153 return; |
| 154 |
| 155 // Start the chromoting context first. |
| 156 chromoting_context_.reset(new remoting::ChromotingHostContext()); |
| 157 chromoting_context_->Start(); |
| 158 |
| 159 // Create a chromoting host object. |
| 160 chromoting_host_ = remoting::ChromotingHost::Create(chromoting_context_.get(), |
| 161 chromoting_config_); |
| 162 |
| 163 // Then start the chromoting host. |
| 164 // When ChromotingHost is shutdown because of failure or a request that |
| 165 // we made OnChromotingShutdown() is calls. |
| 166 chromoting_host_->Start( |
| 167 NewRunnableMethod(this, &ChromotingHostManager::OnShutdown)); |
| 168 } |
| 169 |
| 170 void ChromotingHostManager::OnShutdown() { |
| 171 } |
| 172 |
| 173 } // namespace remoting |
OLD | NEW |