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" |
19 #include "remoting/base/encoder_zlib.h" | 20 #include "remoting/base/encoder_zlib.h" |
20 #include "remoting/host/chromoting_host.h" | 21 #include "remoting/host/chromoting_host.h" |
21 #include "remoting/host/chromoting_host_context.h" | 22 #include "remoting/host/chromoting_host_context.h" |
22 #include "remoting/host/host_config.h" | 23 #include "remoting/host/json_host_config.h" |
23 | 24 |
24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
25 #include "remoting/host/capturer_gdi.h" | 26 #include "remoting/host/capturer_gdi.h" |
26 #include "remoting/host/event_executor_win.h" | 27 #include "remoting/host/event_executor_win.h" |
27 #elif defined(OS_LINUX) | 28 #elif defined(OS_LINUX) |
28 #include "remoting/host/capturer_linux.h" | 29 #include "remoting/host/capturer_fake.h" |
29 #include "remoting/host/event_executor_linux.h" | 30 #include "remoting/host/event_executor_linux.h" |
30 #elif defined(OS_MACOSX) | 31 #elif defined(OS_MACOSX) |
31 #include "remoting/host/capturer_mac.h" | 32 #include "remoting/host/capturer_mac.h" |
32 #include "remoting/host/event_executor_mac.h" | 33 #include "remoting/host/event_executor_mac.h" |
33 #endif | 34 #endif |
34 #endif // defined(ENABLED_REMOTING) | 35 #endif // defined(ENABLED_REMOTING) |
35 | 36 |
36 ServiceProcess* g_service_process = NULL; | 37 ServiceProcess* g_service_process = NULL; |
37 | 38 |
38 ServiceProcess::ServiceProcess() : shutdown_event_(true, false) { | 39 ServiceProcess::ServiceProcess() |
| 40 : shutdown_event_(true, false), |
| 41 main_message_loop_(NULL) { |
39 DCHECK(!g_service_process); | 42 DCHECK(!g_service_process); |
40 g_service_process = this; | 43 g_service_process = this; |
41 } | 44 } |
42 | 45 |
43 bool ServiceProcess::Initialize() { | 46 bool ServiceProcess::Initialize(MessageLoop* message_loop) { |
| 47 main_message_loop_ = message_loop; |
44 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 48 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
45 base::Thread::Options options; | 49 base::Thread::Options options; |
46 options.message_loop_type = MessageLoop::TYPE_IO; | 50 options.message_loop_type = MessageLoop::TYPE_IO; |
47 io_thread_.reset(new base::Thread("ServiceProcess_IO")); | 51 io_thread_.reset(new base::Thread("ServiceProcess_IO")); |
48 file_thread_.reset(new base::Thread("ServiceProcess_File")); | 52 file_thread_.reset(new base::Thread("ServiceProcess_File")); |
49 if (!io_thread_->StartWithOptions(options) || | 53 if (!io_thread_->StartWithOptions(options) || |
50 !file_thread_->StartWithOptions(options)) { | 54 !file_thread_->StartWithOptions(options)) { |
51 NOTREACHED(); | 55 NOTREACHED(); |
52 Teardown(); | 56 Teardown(); |
53 return false; | 57 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
67 #endif // defined(OS_WIN) | 71 #endif // defined(OS_WIN) |
68 | 72 |
69 std::replace(channel_name.begin(), channel_name.end(), '\\', '!'); | 73 std::replace(channel_name.begin(), channel_name.end(), '\\', '!'); |
70 channel_name.append("_service_ipc"); | 74 channel_name.append("_service_ipc"); |
71 ipc_server_.reset(new ServiceIPCServer(channel_name)); | 75 ipc_server_.reset(new ServiceIPCServer(channel_name)); |
72 ipc_server_->Init(); | 76 ipc_server_->Init(); |
73 return true; | 77 return true; |
74 } | 78 } |
75 | 79 |
76 bool ServiceProcess::Teardown() { | 80 bool ServiceProcess::Teardown() { |
77 service_prefs_->WritePrefs(); | 81 if (service_prefs_.get()) { |
78 service_prefs_.reset(); | 82 service_prefs_->WritePrefs(); |
| 83 service_prefs_.reset(); |
| 84 } |
79 cloud_print_proxy_.reset(); | 85 cloud_print_proxy_.reset(); |
| 86 |
| 87 #if defined(ENABLE_REMOTING) |
| 88 ShutdownChromotingHost(); |
| 89 #endif |
| 90 |
80 ipc_server_.reset(); | 91 ipc_server_.reset(); |
81 // Signal this event before shutting down the service process. That way all | 92 // Signal this event before shutting down the service process. That way all |
82 // background threads can cleanup. | 93 // background threads can cleanup. |
83 shutdown_event_.Signal(); | 94 shutdown_event_.Signal(); |
84 io_thread_.reset(); | 95 io_thread_.reset(); |
85 file_thread_.reset(); | 96 file_thread_.reset(); |
86 // The NetworkChangeNotifier must be destroyed after all other threads that | 97 // The NetworkChangeNotifier must be destroyed after all other threads that |
87 // might use it have been shut down. | 98 // might use it have been shut down. |
88 network_change_notifier_.reset(); | 99 network_change_notifier_.reset(); |
| 100 |
89 return true; | 101 return true; |
90 } | 102 } |
91 | 103 |
92 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { | 104 CloudPrintProxy* ServiceProcess::GetCloudPrintProxy() { |
93 if (!cloud_print_proxy_.get()) { | 105 if (!cloud_print_proxy_.get()) { |
94 cloud_print_proxy_.reset(new CloudPrintProxy()); | 106 cloud_print_proxy_.reset(new CloudPrintProxy()); |
95 cloud_print_proxy_->Initialize(service_prefs_.get()); | 107 cloud_print_proxy_->Initialize(service_prefs_.get()); |
96 } | 108 } |
97 return cloud_print_proxy_.get(); | 109 return cloud_print_proxy_.get(); |
98 } | 110 } |
99 | 111 |
100 #if defined(ENABLE_REMOTING) | 112 #if defined(ENABLE_REMOTING) |
101 remoting::ChromotingHost* ServiceProcess::CreateChromotingHost( | 113 bool ServiceProcess::StartChromotingHost() { |
102 remoting::ChromotingHostContext* context, | 114 // We have already started. |
103 remoting::MutableHostConfig* config) { | 115 if (chromoting_context_.get()) |
| 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. |
104 scoped_ptr<remoting::Capturer> capturer; | 127 scoped_ptr<remoting::Capturer> capturer; |
105 scoped_ptr<remoting::Encoder> encoder; | 128 scoped_ptr<remoting::Encoder> encoder; |
106 scoped_ptr<remoting::EventExecutor> executor; | 129 scoped_ptr<remoting::EventExecutor> executor; |
107 | 130 |
108 // Select the capturer and encoder from |config|. | |
109 #if defined(OS_WIN) | 131 #if defined(OS_WIN) |
110 capturer.reset(new remoting::CapturerGdi()); | 132 capturer.reset(new remoting::CapturerGdi()); |
111 executor.reset(new remoting::EventExecutorWin()); | 133 executor.reset(new remoting::EventExecutorWin()); |
112 #elif defined(OS_LINUX) | 134 #elif defined(OS_LINUX) |
113 capturer.reset(new remoting::CapturerLinux()); | 135 capturer.reset(new remoting::CapturerFake()); |
114 executor.reset(new remoting::EventExecutorLinux()); | 136 executor.reset(new remoting::EventExecutorLinux()); |
115 #elif defined(OS_MACOSX) | 137 #elif defined(OS_MACOSX) |
116 capturer.reset(new remoting::CapturerMac()); | 138 capturer.reset(new remoting::CapturerMac()); |
117 executor.reset(new remoting::EventExecutorMac()); | 139 executor.reset(new remoting::EventExecutorMac()); |
118 #endif | 140 #endif |
119 encoder.reset(new remoting::EncoderZlib()); | 141 encoder.reset(new remoting::EncoderZlib()); |
120 | 142 |
121 return new remoting::ChromotingHost(context, config, capturer.release(), | 143 // Create a chromoting host object. |
122 encoder.release(), executor.release()); | 144 chromoting_host_ = new remoting::ChromotingHost(chromoting_context_.get(), |
| 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. |
123 } | 218 } |
124 #endif | 219 #endif |
125 | 220 |
126 ServiceProcess::~ServiceProcess() { | 221 ServiceProcess::~ServiceProcess() { |
127 Teardown(); | 222 Teardown(); |
128 g_service_process = NULL; | 223 g_service_process = NULL; |
129 } | 224 } |
| 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 |