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 main_message_loop_(NULL) { | |
21 } | |
22 | |
23 void ChromotingHostManager::Initialize( | |
24 MessageLoopForUI* main_message_loop, | |
25 base::MessageLoopProxy* file_message_loop) { | |
26 FilePath user_data_dir; | |
27 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
28 FilePath chromoting_config_path = | |
29 user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json")); | |
30 remoting::JsonHostConfig* config = new remoting::JsonHostConfig( | |
31 chromoting_config_path, file_message_loop); | |
32 if (!config->Read()) { | |
33 VLOG(1) << "Failed to read chromoting config file."; | |
34 } | |
35 | |
36 main_message_loop_ = main_message_loop; | |
37 chromoting_config_ = config; | |
38 | |
39 if (!IsConfigInitialized()) { | |
40 InitializeConfig(); | |
41 } | |
42 | |
43 if (IsEnabled()) { | |
44 // TODO(wez): Need to callback the Observer so that ServiceProcess | |
45 // knows to stay alive to service Chromoting requests. | |
46 // This will go away once we have a more consistent model for the | |
47 // service process internals. | |
48 observer_->OnChromotingHostEnabled(); | |
49 Start(); | |
50 } | |
51 } | |
52 | |
53 void ChromotingHostManager::Teardown(Task* done_task) { | |
54 Stop(done_task); | |
55 } | |
56 | |
57 ChromotingHostManager::~ChromotingHostManager() { | |
58 DCHECK(!chromoting_host_); | |
59 DCHECK(!chromoting_context_.get()); | |
60 } | |
61 | |
62 bool ChromotingHostManager::IsConfigInitialized() { | |
63 std::string host_id; | |
64 if (!chromoting_config_->GetString(remoting::kHostIdConfigPath, &host_id)) | |
65 return false; | |
66 | |
67 return guid::IsValidGUID(host_id); | |
68 } | |
69 | |
70 void ChromotingHostManager::InitializeConfig() { | |
71 VLOG(1) << "Initializing static chromoting host parameters."; | |
72 | |
73 // TODO(hclam): This is a time consuming operation so we should run it on | |
74 // a separate thread. | |
75 remoting::HostKeyPair host_key_pair; | |
76 host_key_pair.Generate(); | |
77 std::string host_id(guid::GenerateGUID()); | |
78 std::string hostname(net::GetHostName()); | |
79 | |
80 chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath, false); | |
81 chromoting_config_->SetString(remoting::kHostIdConfigPath, host_id); | |
82 chromoting_config_->SetString(remoting::kHostNameConfigPath, hostname); | |
83 host_key_pair.Save(chromoting_config_); | |
84 | |
85 // Save updated values on the disk. | |
86 chromoting_config_->Save(); | |
87 } | |
88 | |
89 void ChromotingHostManager::SetCredentials(const std::string& login, | |
90 const std::string& token) { | |
91 chromoting_config_->SetString(remoting::kXmppLoginConfigPath, login); | |
92 chromoting_config_->SetString(remoting::kXmppAuthTokenConfigPath, token); | |
93 | |
94 // Save updated values on the disk. | |
95 chromoting_config_->Save(); | |
96 } | |
97 | |
98 void ChromotingHostManager::Enable() { | |
99 // We have already started. | |
100 if (IsEnabled()) | |
101 return; | |
102 | |
103 SetEnabled(true); | |
104 observer_->OnChromotingHostEnabled(); | |
105 | |
106 Start(); | |
107 } | |
108 | |
109 void ChromotingHostManager::Disable() { | |
110 if (!IsEnabled()) | |
111 return; | |
112 | |
113 SetEnabled(false); | |
114 | |
115 // TODO(hclam): Immediately reporting will cause threading problems | |
116 // ServiceProcess thinks we can shutdown safely. | |
117 observer_->OnChromotingHostDisabled(); | |
118 | |
119 Stop(NULL); | |
120 } | |
121 | |
122 void ChromotingHostManager::GetHostInfo(ChromotingHostInfo* host_info) { | |
123 chromoting_config_->GetString(remoting::kHostIdConfigPath, | |
124 &host_info->host_id); | |
125 chromoting_config_->GetString(remoting::kHostNameConfigPath, | |
126 &host_info->hostname); | |
127 HostKeyPair key_pair; | |
128 if (key_pair.Load(chromoting_config_)) { | |
129 host_info->public_key = key_pair.GetPublicKey(); | |
130 } | |
131 | |
132 host_info->enabled = IsEnabled(); | |
133 | |
134 chromoting_config_->GetString(remoting::kXmppLoginConfigPath, | |
135 &host_info->login); | |
136 } | |
137 | |
138 void ChromotingHostManager::Stop(Task* done_task) { | |
139 // Stop the host if it is started. | |
140 if (chromoting_host_) { | |
141 // Save the shutdown task, it will be executed when chromoting host | |
142 // is stopped. | |
143 shutdown_task_.reset(done_task); | |
144 | |
145 // Shutdown the chromoting host asynchronously. | |
146 chromoting_host_->Shutdown(); | |
147 } else if (done_task) { | |
148 done_task->Run(); | |
149 delete done_task; | |
150 } | |
151 } | |
152 | |
153 bool ChromotingHostManager::IsEnabled() { | |
154 bool enabled; | |
155 if (!chromoting_config_->GetBoolean(remoting::kHostEnabledConfigPath, | |
156 &enabled)) { | |
157 enabled = false; | |
158 } | |
159 return enabled; | |
160 } | |
161 | |
162 void ChromotingHostManager::SetEnabled(bool enabled) { | |
163 chromoting_config_->SetBoolean(remoting::kHostEnabledConfigPath, | |
164 enabled); | |
165 chromoting_config_->Save(); | |
166 } | |
167 | |
168 void ChromotingHostManager::Start() { | |
169 // Don't do anything if we already started. | |
170 if (chromoting_host_.get()) | |
171 return; | |
172 | |
173 // Start the chromoting context first. | |
174 chromoting_context_.reset( | |
175 new remoting::ChromotingHostContext(main_message_loop_)); | |
176 chromoting_context_->Start(); | |
177 | |
178 // Create a chromoting host object. | |
179 chromoting_host_ = remoting::ChromotingHost::Create(chromoting_context_.get(), | |
180 chromoting_config_); | |
181 | |
182 // Then start the chromoting host. | |
183 // When ChromotingHost is shutdown because of failure or a request that | |
184 // we made OnChromotingShutdown() is calls. | |
185 chromoting_host_->Start( | |
186 NewRunnableMethod(this, &ChromotingHostManager::OnShutdown)); | |
187 } | |
188 | |
189 void ChromotingHostManager::OnShutdown() { | |
190 if (MessageLoop::current() != main_message_loop_) { | |
191 main_message_loop_->PostTask(FROM_HERE, | |
192 NewRunnableMethod(this, &ChromotingHostManager::OnShutdown)); | |
193 return; | |
194 } | |
195 chromoting_context_->Stop(); | |
196 chromoting_context_.reset(); | |
197 chromoting_host_ = NULL; | |
198 | |
199 if (shutdown_task_.get()) { | |
200 shutdown_task_->Run(); | |
201 shutdown_task_.reset(); | |
202 } | |
203 } | |
204 | |
205 } // namespace remoting | |
OLD | NEW |