Chromium Code Reviews| 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(); | |
|
Alpha Left Google
2010/12/16 21:32:44
Maybe move the logic of Start() here? I don't see
Sergey Ulanov
2010/12/17 00:05:43
Start() is also called from Enable().
| |
| 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::kHostEnabled, 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->email); | |
| 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 } | |
| 131 | |
| 132 bool ChromotingHostManager::IsEnabled() { | |
| 133 bool enabled; | |
| 134 if (!chromoting_config_->GetBoolean(remoting::kHostEnabled, &enabled)) { | |
| 135 enabled = false; | |
| 136 } | |
| 137 return enabled; | |
| 138 } | |
| 139 | |
| 140 void ChromotingHostManager::SetEnabled(bool enabled) { | |
| 141 chromoting_config_->SetBoolean(remoting::kHostEnabled, enabled); | |
| 142 chromoting_config_->Save(); | |
| 143 } | |
| 144 | |
| 145 void ChromotingHostManager::Start() { | |
| 146 // Don't do anything if we already started. | |
| 147 if (chromoting_host_.get()) | |
| 148 return; | |
| 149 | |
| 150 // Start the chromoting context first. | |
| 151 chromoting_context_.reset(new remoting::ChromotingHostContext()); | |
| 152 chromoting_context_->Start(); | |
| 153 | |
| 154 // Create a chromoting host object. | |
| 155 chromoting_host_ = remoting::ChromotingHost::Create(chromoting_context_.get(), | |
| 156 chromoting_config_); | |
| 157 | |
| 158 // Then start the chromoting host. | |
| 159 // When ChromotingHost is shutdown because of failure or a request that | |
| 160 // we made OnChromotingShutdown() is calls. | |
| 161 chromoting_host_->Start( | |
| 162 NewRunnableMethod(this, &ChromotingHostManager::OnShutdown)); | |
| 163 } | |
| 164 | |
| 165 void ChromotingHostManager::OnShutdown() { | |
| 166 } | |
| 167 | |
| 168 } // namespace remoting | |
| OLD | NEW |