OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 ServiceProcess::ServiceProcess() | 118 ServiceProcess::ServiceProcess() |
119 : shutdown_event_(true, false), | 119 : shutdown_event_(true, false), |
120 main_message_loop_(NULL), | 120 main_message_loop_(NULL), |
121 enabled_services_(0), | 121 enabled_services_(0), |
122 update_available_(false) { | 122 update_available_(false) { |
123 DCHECK(!g_service_process); | 123 DCHECK(!g_service_process); |
124 g_service_process = this; | 124 g_service_process = this; |
125 } | 125 } |
126 | 126 |
127 bool ServiceProcess::Initialize(MessageLoopForUI* message_loop, | 127 bool ServiceProcess::Initialize(MessageLoopForUI* message_loop, |
128 const CommandLine& command_line) { | 128 const CommandLine& command_line, |
| 129 ServiceProcessState* state) { |
129 #if defined(TOOLKIT_USES_GTK) | 130 #if defined(TOOLKIT_USES_GTK) |
130 gfx::GtkInitFromCommandLine(command_line); | 131 gfx::GtkInitFromCommandLine(command_line); |
131 #endif | 132 #endif |
132 main_message_loop_ = message_loop; | 133 main_message_loop_ = message_loop; |
| 134 service_process_state_.reset(state); |
133 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 135 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
134 base::Thread::Options options; | 136 base::Thread::Options options; |
135 options.message_loop_type = MessageLoop::TYPE_IO; | 137 options.message_loop_type = MessageLoop::TYPE_IO; |
136 io_thread_.reset(new ServiceIOThread("ServiceProcess_IO")); | 138 io_thread_.reset(new ServiceIOThread("ServiceProcess_IO")); |
137 file_thread_.reset(new base::Thread("ServiceProcess_File")); | 139 file_thread_.reset(new base::Thread("ServiceProcess_File")); |
138 if (!io_thread_->StartWithOptions(options) || | 140 if (!io_thread_->StartWithOptions(options) || |
139 !file_thread_->StartWithOptions(options)) { | 141 !file_thread_->StartWithOptions(options)) { |
140 NOTREACHED(); | 142 NOTREACHED(); |
141 Teardown(); | 143 Teardown(); |
142 return false; | 144 return false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 // Then check if the cloud print proxy was previously enabled. | 194 // Then check if the cloud print proxy was previously enabled. |
193 service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, | 195 service_prefs_->GetBoolean(prefs::kCloudPrintProxyEnabled, |
194 &cloud_print_proxy_enabled); | 196 &cloud_print_proxy_enabled); |
195 } | 197 } |
196 | 198 |
197 if (cloud_print_proxy_enabled) { | 199 if (cloud_print_proxy_enabled) { |
198 GetCloudPrintProxy()->EnableForUser(lsid); | 200 GetCloudPrintProxy()->EnableForUser(lsid); |
199 } | 201 } |
200 | 202 |
201 VLOG(1) << "Starting Service Process IPC Server"; | 203 VLOG(1) << "Starting Service Process IPC Server"; |
202 ServiceProcessState* state = ServiceProcessState::GetInstance(); | 204 ipc_server_.reset(new ServiceIPCServer( |
203 ipc_server_.reset(new ServiceIPCServer(state->GetServiceProcessChannel())); | 205 service_process_state_->GetServiceProcessChannel())); |
204 ipc_server_->Init(); | 206 ipc_server_->Init(); |
205 | 207 |
206 // After the IPC server has started we signal that the service process is | 208 // After the IPC server has started we signal that the service process is |
207 // ready. | 209 // ready. |
208 if (!state->SignalReady(io_thread_->message_loop_proxy(), | 210 if (!state->SignalReady(io_thread_->message_loop_proxy(), |
209 NewRunnableMethod(this, &ServiceProcess::Shutdown))) { | 211 NewRunnableMethod(this, &ServiceProcess::Shutdown))) { |
210 return false; | 212 return false; |
211 } | 213 } |
212 | 214 |
213 // See if we need to stay running. | 215 // See if we need to stay running. |
214 ScheduleShutdownCheck(); | 216 ScheduleShutdownCheck(); |
215 return true; | 217 return true; |
216 } | 218 } |
217 | 219 |
218 bool ServiceProcess::Teardown() { | 220 bool ServiceProcess::Teardown() { |
219 service_prefs_.reset(); | 221 service_prefs_.reset(); |
220 cloud_print_proxy_.reset(); | 222 cloud_print_proxy_.reset(); |
221 | 223 |
222 ipc_server_.reset(); | 224 ipc_server_.reset(); |
223 // Signal this event before shutting down the service process. That way all | 225 // Signal this event before shutting down the service process. That way all |
224 // background threads can cleanup. | 226 // background threads can cleanup. |
225 shutdown_event_.Signal(); | 227 shutdown_event_.Signal(); |
226 io_thread_.reset(); | 228 io_thread_.reset(); |
227 file_thread_.reset(); | 229 file_thread_.reset(); |
228 // The NetworkChangeNotifier must be destroyed after all other threads that | 230 // The NetworkChangeNotifier must be destroyed after all other threads that |
229 // might use it have been shut down. | 231 // might use it have been shut down. |
230 network_change_notifier_.reset(); | 232 network_change_notifier_.reset(); |
231 | 233 |
232 ServiceProcessState::GetInstance()->SignalStopped(); | 234 service_process_state_->SignalStopped(); |
233 return true; | 235 return true; |
234 } | 236 } |
235 | 237 |
236 #if defined(ENABLE_REMOTING) | 238 #if defined(ENABLE_REMOTING) |
237 static void QuitMessageLoop(MessageLoop* message_loop) { | 239 static void QuitMessageLoop(MessageLoop* message_loop) { |
238 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 240 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
239 } | 241 } |
240 #endif | 242 #endif |
241 | 243 |
242 // This method is called when a shutdown command is received from IPC channel | 244 // This method is called when a shutdown command is received from IPC channel |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 ServiceProcess::GetServiceURLRequestContextGetter() { | 306 ServiceProcess::GetServiceURLRequestContextGetter() { |
305 DCHECK(request_context_getter_.get()); | 307 DCHECK(request_context_getter_.get()); |
306 return request_context_getter_.get(); | 308 return request_context_getter_.get(); |
307 } | 309 } |
308 | 310 |
309 void ServiceProcess::OnServiceEnabled() { | 311 void ServiceProcess::OnServiceEnabled() { |
310 enabled_services_++; | 312 enabled_services_++; |
311 if ((1 == enabled_services_) && | 313 if ((1 == enabled_services_) && |
312 !CommandLine::ForCurrentProcess()->HasSwitch( | 314 !CommandLine::ForCurrentProcess()->HasSwitch( |
313 switches::kNoServiceAutorun)) { | 315 switches::kNoServiceAutorun)) { |
314 ServiceProcessState::GetInstance()->AddToAutoRun(); | 316 if (!service_process_state_->AddToAutoRun()) { |
| 317 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition |
| 318 LOG(ERROR) << "Unable to AddToAutoRun"; |
| 319 } |
315 } | 320 } |
316 } | 321 } |
317 | 322 |
318 void ServiceProcess::OnServiceDisabled() { | 323 void ServiceProcess::OnServiceDisabled() { |
319 DCHECK_NE(enabled_services_, 0); | 324 DCHECK_NE(enabled_services_, 0); |
320 enabled_services_--; | 325 enabled_services_--; |
321 if (0 == enabled_services_) { | 326 if (0 == enabled_services_) { |
322 ServiceProcessState::GetInstance()->RemoveFromAutoRun(); | 327 if (!service_process_state_->RemoveFromAutoRun()) { |
| 328 // TODO(scottbyer/sanjeevr/dmaclach): Handle error condition |
| 329 LOG(ERROR) << "Unable to RemoveFromAutoRun"; |
| 330 } |
323 // We will wait for some time to respond to IPCs before shutting down. | 331 // We will wait for some time to respond to IPCs before shutting down. |
324 ScheduleShutdownCheck(); | 332 ScheduleShutdownCheck(); |
325 } | 333 } |
326 } | 334 } |
327 | 335 |
328 void ServiceProcess::ScheduleShutdownCheck() { | 336 void ServiceProcess::ScheduleShutdownCheck() { |
329 MessageLoop::current()->PostDelayedTask( | 337 MessageLoop::current()->PostDelayedTask( |
330 FROM_HERE, | 338 FROM_HERE, |
331 NewRunnableMethod(this, &ServiceProcess::ShutdownIfNeeded), | 339 NewRunnableMethod(this, &ServiceProcess::ShutdownIfNeeded), |
332 kShutdownDelay); | 340 kShutdownDelay); |
(...skipping 10 matching lines...) Expand all Loading... |
343 } else { | 351 } else { |
344 Shutdown(); | 352 Shutdown(); |
345 } | 353 } |
346 } | 354 } |
347 } | 355 } |
348 | 356 |
349 ServiceProcess::~ServiceProcess() { | 357 ServiceProcess::~ServiceProcess() { |
350 Teardown(); | 358 Teardown(); |
351 g_service_process = NULL; | 359 g_service_process = NULL; |
352 } | 360 } |
OLD | NEW |