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/location.h"
12 #include "base/metrics/histogram_base.h"
13 #include "base/metrics/histogram_delta_serialization.h"
14 #include "base/process/kill.h"
15 #include "base/process/launch.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/threading/thread.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/chrome_notification_types.h"
23 #include "chrome/browser/upgrade_detector.h"
24 #include "chrome/common/service_messages.h"
25 #include "chrome/common/service_process_util.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_service.h"
29 using content::BrowserThread
;
31 // ServiceProcessControl implementation.
32 ServiceProcessControl::ServiceProcessControl() {
35 ServiceProcessControl::~ServiceProcessControl() {
38 void ServiceProcessControl::ConnectInternal() {
39 // If the channel has already been established then we run the task
42 RunConnectDoneTasks();
46 // Actually going to connect.
47 DVLOG(1) << "Connecting to Service Process IPC Server";
49 // TODO(hclam): Handle error connecting to channel.
50 const IPC::ChannelHandle channel_id
= GetServiceProcessChannel();
51 SetChannel(IPC::ChannelProxy::Create(
53 IPC::Channel::MODE_NAMED_CLIENT
,
55 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get()));
58 void ServiceProcessControl::SetChannel(scoped_ptr
<IPC::ChannelProxy
> channel
) {
59 channel_
= channel
.Pass();
62 void ServiceProcessControl::RunConnectDoneTasks() {
63 // The tasks executed here may add more tasks to the vector. So copy
64 // them to the stack before executing them. This way recursion is
69 tasks
.swap(connect_success_tasks_
);
70 RunAllTasksHelper(&tasks
);
71 DCHECK(tasks
.empty());
72 connect_failure_tasks_
.clear();
74 tasks
.swap(connect_failure_tasks_
);
75 RunAllTasksHelper(&tasks
);
76 DCHECK(tasks
.empty());
77 connect_success_tasks_
.clear();
82 void ServiceProcessControl::RunAllTasksHelper(TaskList
* task_list
) {
83 TaskList::iterator index
= task_list
->begin();
84 while (index
!= task_list
->end()) {
86 index
= task_list
->erase(index
);
90 bool ServiceProcessControl::IsConnected() const {
91 return channel_
!= NULL
;
94 void ServiceProcessControl::Launch(const base::Closure
& success_task
,
95 const base::Closure
& failure_task
) {
96 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
98 base::Closure failure
= failure_task
;
99 if (!success_task
.is_null())
100 connect_success_tasks_
.push_back(success_task
);
102 if (!failure
.is_null())
103 connect_failure_tasks_
.push_back(failure
);
105 // If we already in the process of launching, then we are done.
109 // If the service process is already running then connects to it.
110 if (CheckServiceProcessReady()) {
115 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", SERVICE_EVENT_LAUNCH
,
118 scoped_ptr
<base::CommandLine
> cmd_line(CreateServiceProcessCommandLine());
119 // And then start the process asynchronously.
120 launcher_
= new Launcher(cmd_line
.Pass());
121 launcher_
->Run(base::Bind(&ServiceProcessControl::OnProcessLaunched
,
122 base::Unretained(this)));
125 void ServiceProcessControl::Disconnect() {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
130 void ServiceProcessControl::OnProcessLaunched() {
131 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 if (launcher_
->launched()) {
133 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
134 SERVICE_EVENT_LAUNCHED
, SERVICE_EVENT_MAX
);
135 // After we have successfully created the service process we try to connect
136 // to it. The launch task is transfered to a connect task.
139 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
140 SERVICE_EVENT_LAUNCH_FAILED
, SERVICE_EVENT_MAX
);
141 // If we don't have process handle that means launching the service process
143 RunConnectDoneTasks();
146 // We don't need the launcher anymore.
150 bool ServiceProcessControl::OnMessageReceived(const IPC::Message
& message
) {
152 IPC_BEGIN_MESSAGE_MAP(ServiceProcessControl
, message
)
153 IPC_MESSAGE_HANDLER(ServiceHostMsg_CloudPrintProxy_Info
,
154 OnCloudPrintProxyInfo
)
155 IPC_MESSAGE_HANDLER(ServiceHostMsg_Histograms
, OnHistograms
)
156 IPC_MESSAGE_HANDLER(ServiceHostMsg_Printers
, OnPrinters
)
157 IPC_MESSAGE_UNHANDLED(handled
= false)
158 IPC_END_MESSAGE_MAP()
162 void ServiceProcessControl::OnChannelConnected(int32 peer_pid
) {
163 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
165 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
166 SERVICE_EVENT_CHANNEL_CONNECTED
, SERVICE_EVENT_MAX
);
168 // We just established a channel with the service process. Notify it if an
169 // upgrade is available.
170 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
171 Send(new ServiceMsg_UpdateAvailable
);
173 if (registrar_
.IsEmpty())
174 registrar_
.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
175 content::NotificationService::AllSources());
177 RunConnectDoneTasks();
180 void ServiceProcessControl::OnChannelError() {
181 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
183 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
184 SERVICE_EVENT_CHANNEL_ERROR
, SERVICE_EVENT_MAX
);
187 RunConnectDoneTasks();
190 bool ServiceProcessControl::Send(IPC::Message
* message
) {
191 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
194 return channel_
->Send(message
);
197 // content::NotificationObserver implementation.
198 void ServiceProcessControl::Observe(
200 const content::NotificationSource
& source
,
201 const content::NotificationDetails
& details
) {
202 if (type
== chrome::NOTIFICATION_UPGRADE_RECOMMENDED
) {
203 Send(new ServiceMsg_UpdateAvailable
);
207 void ServiceProcessControl::OnCloudPrintProxyInfo(
208 const cloud_print::CloudPrintProxyInfo
& proxy_info
) {
209 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
210 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
211 SERVICE_EVENT_INFO_REPLY
, SERVICE_EVENT_MAX
);
212 if (!cloud_print_info_callback_
.is_null()) {
213 cloud_print_info_callback_
.Run(proxy_info
);
214 cloud_print_info_callback_
.Reset();
218 void ServiceProcessControl::OnHistograms(
219 const std::vector
<std::string
>& pickled_histograms
) {
220 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
221 SERVICE_EVENT_HISTOGRAMS_REPLY
, SERVICE_EVENT_MAX
);
222 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
223 base::HistogramDeltaSerialization::DeserializeAndAddSamples(
225 RunHistogramsCallback();
228 void ServiceProcessControl::RunHistogramsCallback() {
229 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
230 if (!histograms_callback_
.is_null()) {
231 histograms_callback_
.Run();
232 histograms_callback_
.Reset();
234 histograms_timeout_callback_
.Cancel();
237 void ServiceProcessControl::OnPrinters(
238 const std::vector
<std::string
>& printers
) {
239 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
240 UMA_HISTOGRAM_ENUMERATION(
241 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REPLY
, SERVICE_EVENT_MAX
);
242 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrinters", printers
.size());
243 if (!printers_callback_
.is_null()) {
244 printers_callback_
.Run(printers
);
245 printers_callback_
.Reset();
249 bool ServiceProcessControl::GetCloudPrintProxyInfo(
250 const CloudPrintProxyInfoCallback
& cloud_print_info_callback
) {
251 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
252 DCHECK(!cloud_print_info_callback
.is_null());
253 cloud_print_info_callback_
.Reset();
254 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
255 SERVICE_EVENT_INFO_REQUEST
, SERVICE_EVENT_MAX
);
256 if (!Send(new ServiceMsg_GetCloudPrintProxyInfo()))
258 cloud_print_info_callback_
= cloud_print_info_callback
;
262 bool ServiceProcessControl::GetHistograms(
263 const base::Closure
& histograms_callback
,
264 const base::TimeDelta
& timeout
) {
265 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
266 DCHECK(!histograms_callback
.is_null());
267 histograms_callback_
.Reset();
269 #if defined(OS_MACOSX)
270 // TODO(vitalybuka): Investigate why it crashes MAC http://crbug.com/406227.
274 // If the service process is already running then connect to it.
275 if (!CheckServiceProcessReady())
279 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
280 SERVICE_EVENT_HISTOGRAMS_REQUEST
,
283 if (!Send(new ServiceMsg_GetHistograms()))
286 // Run timeout task to make sure |histograms_callback| is called.
287 histograms_timeout_callback_
.Reset(
288 base::Bind(&ServiceProcessControl::RunHistogramsCallback
,
289 base::Unretained(this)));
290 BrowserThread::PostDelayedTask(BrowserThread::UI
, FROM_HERE
,
291 histograms_timeout_callback_
.callback(),
294 histograms_callback_
= histograms_callback
;
298 bool ServiceProcessControl::GetPrinters(
299 const PrintersCallback
& printers_callback
) {
300 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
301 DCHECK(!printers_callback
.is_null());
302 printers_callback_
.Reset();
303 UMA_HISTOGRAM_ENUMERATION(
304 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REQUEST
, SERVICE_EVENT_MAX
);
305 if (!Send(new ServiceMsg_GetPrinters()))
307 printers_callback_
= printers_callback
;
311 bool ServiceProcessControl::Shutdown() {
312 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
313 bool ret
= Send(new ServiceMsg_Shutdown());
319 ServiceProcessControl
* ServiceProcessControl::GetInstance() {
320 return base::Singleton
<ServiceProcessControl
>::get();
323 ServiceProcessControl::Launcher::Launcher(
324 scoped_ptr
<base::CommandLine
> cmd_line
)
325 : cmd_line_(cmd_line
.Pass()),
330 // Execute the command line to start the process asynchronously.
331 // After the command is executed, |task| is called with the process handle on
333 void ServiceProcessControl::Launcher::Run(const base::Closure
& task
) {
334 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
336 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER
, FROM_HERE
,
337 base::Bind(&Launcher::DoRun
, this));
340 ServiceProcessControl::Launcher::~Launcher() {
344 void ServiceProcessControl::Launcher::Notify() {
345 DCHECK(!notify_task_
.is_null());
347 notify_task_
.Reset();
350 #if !defined(OS_MACOSX)
351 void ServiceProcessControl::Launcher::DoDetectLaunched() {
352 DCHECK(!notify_task_
.is_null());
354 const uint32 kMaxLaunchDetectRetries
= 10;
355 launched_
= CheckServiceProcessReady();
358 if (launched_
|| (retry_count_
>= kMaxLaunchDetectRetries
) ||
359 process_
.WaitForExitWithTimeout(base::TimeDelta(), &exit_code
)) {
361 BrowserThread::PostTask(
362 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));
367 // If the service process is not launched yet then check again in 2 seconds.
368 const base::TimeDelta kDetectLaunchRetry
= base::TimeDelta::FromSeconds(2);
369 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
370 FROM_HERE
, base::Bind(&Launcher::DoDetectLaunched
, this),
374 void ServiceProcessControl::Launcher::DoRun() {
375 DCHECK(!notify_task_
.is_null());
377 base::LaunchOptions options
;
379 options
.start_hidden
= true;
381 process_
= base::LaunchProcess(*cmd_line_
, options
);
382 if (process_
.IsValid()) {
383 BrowserThread::PostTask(
384 BrowserThread::IO
, FROM_HERE
,
385 base::Bind(&Launcher::DoDetectLaunched
, this));
387 BrowserThread::PostTask(
388 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));