1 // Copyright (c) 2012 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.
5 #include "chrome/browser/service_process/service_process_control.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/metrics/histogram_base.h"
12 #include "base/metrics/histogram_delta_serialization.h"
13 #include "base/process/kill.h"
14 #include "base/process/launch.h"
15 #include "base/stl_util.h"
16 #include "base/threading/thread.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/upgrade_detector.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/service_messages.h"
23 #include "chrome/common/service_process_util.h"
24 #include "components/cloud_devices/common/cloud_devices_switches.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/common/child_process_host.h"
28 #include "google_apis/gaia/gaia_switches.h"
29 #include "ui/base/ui_base_switches.h"
31 using content::BrowserThread
;
32 using content::ChildProcessHost
;
34 // ServiceProcessControl implementation.
35 ServiceProcessControl::ServiceProcessControl() {
38 ServiceProcessControl::~ServiceProcessControl() {
41 void ServiceProcessControl::ConnectInternal() {
42 // If the channel has already been established then we run the task
45 RunConnectDoneTasks();
49 // Actually going to connect.
50 VLOG(1) << "Connecting to Service Process IPC Server";
52 // TODO(hclam): Handle error connecting to channel.
53 const IPC::ChannelHandle channel_id
= GetServiceProcessChannel();
54 SetChannel(new IPC::ChannelProxy(
56 IPC::Channel::MODE_NAMED_CLIENT
,
58 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get()));
61 void ServiceProcessControl::SetChannel(IPC::ChannelProxy
* channel
) {
62 channel_
.reset(channel
);
65 void ServiceProcessControl::RunConnectDoneTasks() {
66 // The tasks executed here may add more tasks to the vector. So copy
67 // them to the stack before executing them. This way recursion is
72 tasks
.swap(connect_success_tasks_
);
73 RunAllTasksHelper(&tasks
);
74 DCHECK(tasks
.empty());
75 connect_failure_tasks_
.clear();
77 tasks
.swap(connect_failure_tasks_
);
78 RunAllTasksHelper(&tasks
);
79 DCHECK(tasks
.empty());
80 connect_success_tasks_
.clear();
85 void ServiceProcessControl::RunAllTasksHelper(TaskList
* task_list
) {
86 TaskList::iterator index
= task_list
->begin();
87 while (index
!= task_list
->end()) {
89 index
= task_list
->erase(index
);
93 bool ServiceProcessControl::IsConnected() const {
94 return channel_
!= NULL
;
97 void ServiceProcessControl::Launch(const base::Closure
& success_task
,
98 const base::Closure
& failure_task
) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
101 base::Closure failure
= failure_task
;
102 if (!success_task
.is_null())
103 connect_success_tasks_
.push_back(success_task
);
105 if (!failure
.is_null())
106 connect_failure_tasks_
.push_back(failure
);
108 // If we already in the process of launching, then we are done.
112 // If the service process is already running then connects to it.
113 if (CheckServiceProcessReady()) {
118 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", SERVICE_EVENT_LAUNCH
,
121 // A service process should have a different mechanism for starting, but now
122 // we start it as if it is a child process.
124 #if defined(OS_LINUX)
125 int flags
= ChildProcessHost::CHILD_ALLOW_SELF
;
127 int flags
= ChildProcessHost::CHILD_NORMAL
;
130 base::FilePath exe_path
= ChildProcessHost::GetChildPath(flags
);
131 if (exe_path
.empty())
132 NOTREACHED() << "Unable to get service process binary name.";
134 CommandLine
* cmd_line
= new CommandLine(exe_path
);
135 cmd_line
->AppendSwitchASCII(switches::kProcessType
,
136 switches::kServiceProcess
);
138 static const char* const kSwitchesToCopy
[] = {
139 switches::kCloudPrintSetupProxy
,
140 switches::kCloudPrintURL
,
141 switches::kCloudPrintXmppEndpoint
,
143 switches::kEnableCloudPrintXps
,
145 switches::kEnableLogging
,
146 switches::kIgnoreUrlFetcherCertRequests
,
148 switches::kLoggingLevel
,
150 switches::kNoServiceAutorun
,
151 switches::kUserDataDir
,
154 switches::kWaitForDebugger
,
156 cmd_line
->CopySwitchesFrom(*CommandLine::ForCurrentProcess(),
158 arraysize(kSwitchesToCopy
));
160 // And then start the process asynchronously.
161 launcher_
= new Launcher(this, cmd_line
);
162 launcher_
->Run(base::Bind(&ServiceProcessControl::OnProcessLaunched
,
163 base::Unretained(this)));
166 void ServiceProcessControl::Disconnect() {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
171 void ServiceProcessControl::OnProcessLaunched() {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
173 if (launcher_
->launched()) {
174 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
175 SERVICE_EVENT_LAUNCHED
, SERVICE_EVENT_MAX
);
176 // After we have successfully created the service process we try to connect
177 // to it. The launch task is transfered to a connect task.
180 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
181 SERVICE_EVENT_LAUNCH_FAILED
, SERVICE_EVENT_MAX
);
182 // If we don't have process handle that means launching the service process
184 RunConnectDoneTasks();
187 // We don't need the launcher anymore.
191 bool ServiceProcessControl::OnMessageReceived(const IPC::Message
& message
) {
193 IPC_BEGIN_MESSAGE_MAP(ServiceProcessControl
, message
)
194 IPC_MESSAGE_HANDLER(ServiceHostMsg_CloudPrintProxy_Info
,
195 OnCloudPrintProxyInfo
)
196 IPC_MESSAGE_HANDLER(ServiceHostMsg_Histograms
, OnHistograms
)
197 IPC_MESSAGE_HANDLER(ServiceHostMsg_Printers
, OnPrinters
)
198 IPC_MESSAGE_UNHANDLED(handled
= false)
199 IPC_END_MESSAGE_MAP()
203 void ServiceProcessControl::OnChannelConnected(int32 peer_pid
) {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
206 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
207 SERVICE_EVENT_CHANNEL_CONNECTED
, SERVICE_EVENT_MAX
);
209 // We just established a channel with the service process. Notify it if an
210 // upgrade is available.
211 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
212 Send(new ServiceMsg_UpdateAvailable
);
214 if (registrar_
.IsEmpty())
215 registrar_
.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
216 content::NotificationService::AllSources());
218 RunConnectDoneTasks();
221 void ServiceProcessControl::OnChannelError() {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
224 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
225 SERVICE_EVENT_CHANNEL_ERROR
, SERVICE_EVENT_MAX
);
228 RunConnectDoneTasks();
231 bool ServiceProcessControl::Send(IPC::Message
* message
) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
235 return channel_
->Send(message
);
238 // content::NotificationObserver implementation.
239 void ServiceProcessControl::Observe(
241 const content::NotificationSource
& source
,
242 const content::NotificationDetails
& details
) {
243 if (type
== chrome::NOTIFICATION_UPGRADE_RECOMMENDED
) {
244 Send(new ServiceMsg_UpdateAvailable
);
248 void ServiceProcessControl::OnCloudPrintProxyInfo(
249 const cloud_print::CloudPrintProxyInfo
& proxy_info
) {
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
251 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
252 SERVICE_EVENT_INFO_REPLY
, SERVICE_EVENT_MAX
);
253 if (!cloud_print_info_callback_
.is_null()) {
254 cloud_print_info_callback_
.Run(proxy_info
);
255 cloud_print_info_callback_
.Reset();
259 void ServiceProcessControl::OnHistograms(
260 const std::vector
<std::string
>& pickled_histograms
) {
261 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
262 SERVICE_EVENT_HISTOGRAMS_REPLY
, SERVICE_EVENT_MAX
);
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
264 base::HistogramDeltaSerialization::DeserializeAndAddSamples(
266 RunHistogramsCallback();
269 void ServiceProcessControl::RunHistogramsCallback() {
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
271 if (!histograms_callback_
.is_null()) {
272 histograms_callback_
.Run();
273 histograms_callback_
.Reset();
275 histograms_timeout_callback_
.Cancel();
278 void ServiceProcessControl::OnPrinters(
279 const std::vector
<std::string
>& printers
) {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
281 UMA_HISTOGRAM_ENUMERATION(
282 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REPLY
, SERVICE_EVENT_MAX
);
283 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrinters", printers
.size());
284 if (!printers_callback_
.is_null()) {
285 printers_callback_
.Run(printers
);
286 printers_callback_
.Reset();
290 bool ServiceProcessControl::GetCloudPrintProxyInfo(
291 const CloudPrintProxyInfoCallback
& cloud_print_info_callback
) {
292 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
293 DCHECK(!cloud_print_info_callback
.is_null());
294 cloud_print_info_callback_
.Reset();
295 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
296 SERVICE_EVENT_INFO_REQUEST
, SERVICE_EVENT_MAX
);
297 if (!Send(new ServiceMsg_GetCloudPrintProxyInfo()))
299 cloud_print_info_callback_
= cloud_print_info_callback
;
303 bool ServiceProcessControl::GetHistograms(
304 const base::Closure
& histograms_callback
,
305 const base::TimeDelta
& timeout
) {
306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
307 DCHECK(!histograms_callback
.is_null());
308 histograms_callback_
.Reset();
310 // If the service process is already running then connect to it.
311 if (!CheckServiceProcessReady())
315 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
316 SERVICE_EVENT_HISTOGRAMS_REQUEST
,
319 if (!Send(new ServiceMsg_GetHistograms()))
322 // Run timeout task to make sure |histograms_callback| is called.
323 histograms_timeout_callback_
.Reset(
324 base::Bind(&ServiceProcessControl::RunHistogramsCallback
,
325 base::Unretained(this)));
326 BrowserThread::PostDelayedTask(BrowserThread::UI
, FROM_HERE
,
327 histograms_timeout_callback_
.callback(),
330 histograms_callback_
= histograms_callback
;
334 bool ServiceProcessControl::GetPrinters(
335 const PrintersCallback
& printers_callback
) {
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
337 DCHECK(!printers_callback
.is_null());
338 printers_callback_
.Reset();
339 UMA_HISTOGRAM_ENUMERATION(
340 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REQUEST
, SERVICE_EVENT_MAX
);
341 if (!Send(new ServiceMsg_GetPrinters()))
343 printers_callback_
= printers_callback
;
347 bool ServiceProcessControl::Shutdown() {
348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
349 bool ret
= Send(new ServiceMsg_Shutdown());
355 ServiceProcessControl
* ServiceProcessControl::GetInstance() {
356 return Singleton
<ServiceProcessControl
>::get();
359 ServiceProcessControl::Launcher::Launcher(ServiceProcessControl
* process
,
360 CommandLine
* cmd_line
)
365 process_handle_(base::kNullProcessHandle
) {
368 // Execute the command line to start the process asynchronously.
369 // After the command is executed, |task| is called with the process handle on
371 void ServiceProcessControl::Launcher::Run(const base::Closure
& task
) {
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
374 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER
, FROM_HERE
,
375 base::Bind(&Launcher::DoRun
, this));
378 ServiceProcessControl::Launcher::~Launcher() {
379 CloseProcessHandle();
383 void ServiceProcessControl::Launcher::Notify() {
384 DCHECK(!notify_task_
.is_null());
386 notify_task_
.Reset();
389 void ServiceProcessControl::Launcher::CloseProcessHandle() {
390 if (process_handle_
!= base::kNullProcessHandle
) {
391 base::CloseProcessHandle(process_handle_
);
392 process_handle_
= base::kNullProcessHandle
;
396 #if !defined(OS_MACOSX)
397 void ServiceProcessControl::Launcher::DoDetectLaunched() {
398 DCHECK(!notify_task_
.is_null());
400 const uint32 kMaxLaunchDetectRetries
= 10;
401 launched_
= CheckServiceProcessReady();
404 if (launched_
|| (retry_count_
>= kMaxLaunchDetectRetries
) ||
405 base::WaitForExitCodeWithTimeout(process_handle_
, &exit_code
,
406 base::TimeDelta())) {
407 CloseProcessHandle();
408 BrowserThread::PostTask(
409 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));
414 // If the service process is not launched yet then check again in 2 seconds.
415 const base::TimeDelta kDetectLaunchRetry
= base::TimeDelta::FromSeconds(2);
416 base::MessageLoop::current()->PostDelayedTask(
417 FROM_HERE
, base::Bind(&Launcher::DoDetectLaunched
, this),
421 void ServiceProcessControl::Launcher::DoRun() {
422 DCHECK(!notify_task_
.is_null());
424 base::LaunchOptions options
;
426 options
.start_hidden
= true;
428 if (base::LaunchProcess(*cmd_line_
, options
, &process_handle_
)) {
429 BrowserThread::PostTask(
430 BrowserThread::IO
, FROM_HERE
,
431 base::Bind(&Launcher::DoDetectLaunched
, this));
433 BrowserThread::PostTask(
434 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));