OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/service/service_process.h" | 5 #include "chrome/service/service_process.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/common/chrome_constants.h" | 11 #include "chrome/common/chrome_constants.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/common/json_pref_store.h" | 13 #include "chrome/common/json_pref_store.h" |
14 #include "chrome/service/cloud_print/cloud_print_proxy.h" | 14 #include "chrome/service/cloud_print/cloud_print_proxy.h" |
15 #include "chrome/service/service_ipc_server.h" | 15 #include "chrome/service/service_ipc_server.h" |
16 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
17 | 17 |
18 #if defined(ENABLE_REMOTING) | 18 #if defined(ENABLE_REMOTING) |
19 #include "remoting/base/constants.h" | |
20 #include "remoting/base/encoder_zlib.h" | 19 #include "remoting/base/encoder_zlib.h" |
21 #include "remoting/host/chromoting_host.h" | 20 #include "remoting/host/chromoting_host.h" |
22 #include "remoting/host/chromoting_host_context.h" | 21 #include "remoting/host/chromoting_host_context.h" |
23 #include "remoting/host/json_host_config.h" | 22 #include "remoting/host/host_config.h" |
24 | 23 |
25 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
26 #include "remoting/host/capturer_gdi.h" | 25 #include "remoting/host/capturer_gdi.h" |
27 #include "remoting/host/event_executor_win.h" | 26 #include "remoting/host/event_executor_win.h" |
28 #elif defined(OS_LINUX) | 27 #elif defined(OS_LINUX) |
29 #include "remoting/host/capturer_fake.h" | 28 #include "remoting/host/capturer_linux.h" |
30 #include "remoting/host/event_executor_linux.h" | 29 #include "remoting/host/event_executor_linux.h" |
31 #elif defined(OS_MACOSX) | 30 #elif defined(OS_MACOSX) |
32 #include "remoting/host/capturer_mac.h" | 31 #include "remoting/host/capturer_mac.h" |
33 #include "remoting/host/event_executor_mac.h" | 32 #include "remoting/host/event_executor_mac.h" |
34 #endif | 33 #endif |
35 #endif // defined(ENABLED_REMOTING) | 34 #endif // defined(ENABLED_REMOTING) |
36 | 35 |
37 ServiceProcess* g_service_process = NULL; | 36 ServiceProcess* g_service_process = NULL; |
38 | 37 |
39 ServiceProcess::ServiceProcess() | 38 ServiceProcess::ServiceProcess() : shutdown_event_(true, false) { |
40 : shutdown_event_(true, false), | |
41 main_message_loop_(NULL) { | |
42 DCHECK(!g_service_process); | 39 DCHECK(!g_service_process); |
43 g_service_process = this; | 40 g_service_process = this; |
44 } | 41 } |
45 | 42 |
46 bool ServiceProcess::Initialize(MessageLoop* message_loop) { | 43 bool ServiceProcess::Initialize() { |
47 main_message_loop_ = message_loop; | |
48 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 44 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
49 base::Thread::Options options; | 45 base::Thread::Options options; |
50 options.message_loop_type = MessageLoop::TYPE_IO; | 46 options.message_loop_type = MessageLoop::TYPE_IO; |
51 io_thread_.reset(new base::Thread("ServiceProcess_IO")); | 47 io_thread_.reset(new base::Thread("ServiceProcess_IO")); |
52 file_thread_.reset(new base::Thread("ServiceProcess_File")); | 48 file_thread_.reset(new base::Thread("ServiceProcess_File")); |
53 if (!io_thread_->StartWithOptions(options) || | 49 if (!io_thread_->StartWithOptions(options) || |
54 !file_thread_->StartWithOptions(options)) { | 50 !file_thread_->StartWithOptions(options)) { |
55 NOTREACHED(); | 51 NOTREACHED(); |
56 Teardown(); | 52 Teardown(); |
57 return false; | 53 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
71 #endif // defined(OS_WIN) | 67 #endif // defined(OS_WIN) |
72 | 68 |
73 std::replace(channel_name.begin(), channel_name.end(), '\\', '!'); | 69 std::replace(channel_name.begin(), channel_name.end(), '\\', '!'); |
74 channel_name.append("_service_ipc"); | 70 channel_name.append("_service_ipc"); |
75 ipc_server_.reset(new ServiceIPCServer(channel_name)); | 71 ipc_server_.reset(new ServiceIPCServer(channel_name)); |
76 ipc_server_->Init(); | 72 ipc_server_->Init(); |
77 return true; | 73 return true; |
78 } | 74 } |
79 | 75 |
80 bool ServiceProcess::Teardown() { | 76 bool ServiceProcess::Teardown() { |
81 if (service_prefs_.get()) { | 77 service_prefs_->WritePrefs(); |
82 service_prefs_->WritePrefs(); | 78 service_prefs_.reset(); |
83 service_prefs_.reset(); | |
84 } | |
85 cloud_print_proxy_.reset(); | 79 cloud_print_proxy_.reset(); |
86 | |
87 #if defined(ENABLE_REMOTING) | |
88 ShutdownChromotingHost(); | |
89 #endif | |
90 | |
91 ipc_server_.reset(); | 80 ipc_server_.reset(); |
92 // Signal this event before shutting down the service process. That way all | 81 // Signal this event before shutting down the service process. That way all |
93 // background threads can cleanup. | 82 // background threads can cleanup. |
94 shutdown_event_.Signal(); | 83 shutdown_event_.Signal(); |
95 io_thread_.reset(); | 84 io_thread_.reset(); |
96 file_thread_.reset(); | 85 file_thread_.reset(); |
97 // The NetworkChangeNotifier must be destroyed after all other threads that | 86 // The NetworkChangeNotifier must be destroyed after all other threads that |
98 // might use it have been shut down. | 87 // might use it have been shut down. |
99 network_change_notifier_.reset(); | 88 network_change_notifier_.reset(); |
100 | |
101 return true; | 89 return true; |
102 } | 90 } |
103 | 91 |
104 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { | 92 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { |
105 if (!cloud_print_proxy_.get()) { | 93 if (!cloud_print_proxy_.get()) { |
106 cloud_print_proxy_.reset(new CloudPrintProxy()); | 94 cloud_print_proxy_.reset(new CloudPrintProxy()); |
107 cloud_print_proxy_->Initialize(service_prefs_.get()); | 95 cloud_print_proxy_->Initialize(service_prefs_.get()); |
108 } | 96 } |
109 return cloud_print_proxy_.get(); | 97 return cloud_print_proxy_.get(); |
110 } | 98 } |
111 | 99 |
112 #if defined(ENABLE_REMOTING) | 100 #if defined(ENABLE_REMOTING) |
113 bool ServiceProcess::StartChromotingHost() { | 101 remoting::ChromotingHost* ServiceProcess::CreateChromotingHost( |
114 // We have already started. | 102 remoting::ChromotingHostContext* context, |
115 if (chromoting_context_.get()) | 103 remoting::MutableHostConfig* config) { |
116 return true; | |
117 | |
118 // Load chromoting config from the disk. | |
119 LoadChromotingConfig(); | |
120 | |
121 // Start the chromoting context first. | |
122 chromoting_context_.reset(new remoting::ChromotingHostContext()); | |
123 chromoting_context_->Start(); | |
124 | |
125 // Create capturer, encoder and executor. The ownership will be transfered | |
126 // to the chromoting host. | |
127 scoped_ptr<remoting::Capturer> capturer; | 104 scoped_ptr<remoting::Capturer> capturer; |
128 scoped_ptr<remoting::Encoder> encoder; | 105 scoped_ptr<remoting::Encoder> encoder; |
129 scoped_ptr<remoting::EventExecutor> executor; | 106 scoped_ptr<remoting::EventExecutor> executor; |
130 | 107 |
| 108 // Select the capturer and encoder from |config|. |
131 #if defined(OS_WIN) | 109 #if defined(OS_WIN) |
132 capturer.reset(new remoting::CapturerGdi()); | 110 capturer.reset(new remoting::CapturerGdi()); |
133 executor.reset(new remoting::EventExecutorWin()); | 111 executor.reset(new remoting::EventExecutorWin()); |
134 #elif defined(OS_LINUX) | 112 #elif defined(OS_LINUX) |
135 capturer.reset(new remoting::CapturerFake()); | 113 capturer.reset(new remoting::CapturerLinux()); |
136 executor.reset(new remoting::EventExecutorLinux()); | 114 executor.reset(new remoting::EventExecutorLinux()); |
137 #elif defined(OS_MACOSX) | 115 #elif defined(OS_MACOSX) |
138 capturer.reset(new remoting::CapturerMac()); | 116 capturer.reset(new remoting::CapturerMac()); |
139 executor.reset(new remoting::EventExecutorMac()); | 117 executor.reset(new remoting::EventExecutorMac()); |
140 #endif | 118 #endif |
141 encoder.reset(new remoting::EncoderZlib()); | 119 encoder.reset(new remoting::EncoderZlib()); |
142 | 120 |
143 // Create a chromoting host object. | 121 return new remoting::ChromotingHost(context, config, capturer.release(), |
144 chromoting_host_ = new remoting::ChromotingHost(chromoting_context_.get(), | 122 encoder.release(), executor.release()); |
145 chromoting_config_, | |
146 capturer.release(), | |
147 encoder.release(), | |
148 executor.release()); | |
149 | |
150 // Then start the chromoting host. | |
151 // When ChromotingHost is shutdown because of failure or a request that | |
152 // we made OnChromotingShutdown() is calls. | |
153 chromoting_host_->Start( | |
154 NewRunnableMethod(this, &ServiceProcess::OnChromotingHostShutdown)); | |
155 return true; | |
156 } | |
157 | |
158 bool ServiceProcess::ShutdownChromotingHost() { | |
159 // Chromoting host doesn't exist so return true. | |
160 if (!chromoting_host_) | |
161 return true; | |
162 | |
163 // Shutdown the chromoting host asynchronously. This will signal the host to | |
164 // shutdown, we'll actually wait for all threads to stop when we destroy | |
165 // the chromoting context. | |
166 chromoting_host_->Shutdown(); | |
167 chromoting_host_ = NULL; | |
168 return true; | |
169 } | |
170 | |
171 // A util function to update the login information to host config. | |
172 static void SaveChromotingConfigFunc(remoting::JsonHostConfig* config, | |
173 const std::string& login, | |
174 const std::string& token, | |
175 const std::string& host_id, | |
176 const std::string& host_name, | |
177 const std::string& private_key) { | |
178 config->SetString(remoting::kXmppLoginConfigPath, login); | |
179 config->SetString(remoting::kXmppAuthTokenConfigPath, token); | |
180 config->SetString(remoting::kHostIdConfigPath, host_id); | |
181 config->SetString(remoting::kHostNameConfigPath, host_name); | |
182 config->SetString(remoting::kPrivateKeyConfigPath, private_key); | |
183 } | |
184 | |
185 void ServiceProcess::SaveChromotingConfig(const std::string& login, | |
186 const std::string& token, | |
187 const std::string& host_id, | |
188 const std::string& host_name, | |
189 const std::string& private_key) { | |
190 // First we need to load the config first. | |
191 LoadChromotingConfig(); | |
192 | |
193 // And then do the update. | |
194 chromoting_config_->Update( | |
195 NewRunnableFunction(&SaveChromotingConfigFunc, chromoting_config_.get(), | |
196 login, token, host_id, host_name, private_key)); | |
197 } | |
198 | |
199 void ServiceProcess::LoadChromotingConfig() { | |
200 // TODO(hclam): We really should be doing this on IO thread so we are not | |
201 // blocked on file IOs. | |
202 if (chromoting_config_) | |
203 return; | |
204 | |
205 FilePath user_data_dir; | |
206 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
207 FilePath chromoting_config_path = | |
208 user_data_dir.Append(FILE_PATH_LITERAL(".ChromotingConfig.json")); | |
209 chromoting_config_ = new remoting::JsonHostConfig( | |
210 chromoting_config_path, file_thread_->message_loop_proxy()); | |
211 if (!chromoting_config_->Read()) { | |
212 LOG(INFO) << "Failed to read chromoting config file."; | |
213 } | |
214 } | |
215 | |
216 void ServiceProcess::OnChromotingHostShutdown() { | |
217 // TODO(hclam): Implement. | |
218 } | 123 } |
219 #endif | 124 #endif |
220 | 125 |
221 ServiceProcess::~ServiceProcess() { | 126 ServiceProcess::~ServiceProcess() { |
222 Teardown(); | 127 Teardown(); |
223 g_service_process = NULL; | 128 g_service_process = NULL; |
224 } | 129 } |
225 | |
226 // Disable refcounting for runnable method because it is really not needed | |
227 // when we post tasks on the main message loop. | |
228 DISABLE_RUNNABLE_METHOD_REFCOUNT(ServiceProcess); | |
OLD | NEW |