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/service_messages.h"
22 #include "chrome/common/service_process_util.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/notification_service.h"
26 using content::BrowserThread
;
28 // ServiceProcessControl implementation.
29 ServiceProcessControl::ServiceProcessControl() {
32 ServiceProcessControl::~ServiceProcessControl() {
35 void ServiceProcessControl::ConnectInternal() {
36 // If the channel has already been established then we run the task
39 RunConnectDoneTasks();
43 // Actually going to connect.
44 DVLOG(1) << "Connecting to Service Process IPC Server";
46 // TODO(hclam): Handle error connecting to channel.
47 const IPC::ChannelHandle channel_id
= GetServiceProcessChannel();
48 SetChannel(IPC::ChannelProxy::Create(
50 IPC::Channel::MODE_NAMED_CLIENT
,
52 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get()));
55 void ServiceProcessControl::SetChannel(scoped_ptr
<IPC::ChannelProxy
> channel
) {
56 channel_
= channel
.Pass();
59 void ServiceProcessControl::RunConnectDoneTasks() {
60 // The tasks executed here may add more tasks to the vector. So copy
61 // them to the stack before executing them. This way recursion is
66 tasks
.swap(connect_success_tasks_
);
67 RunAllTasksHelper(&tasks
);
68 DCHECK(tasks
.empty());
69 connect_failure_tasks_
.clear();
71 tasks
.swap(connect_failure_tasks_
);
72 RunAllTasksHelper(&tasks
);
73 DCHECK(tasks
.empty());
74 connect_success_tasks_
.clear();
79 void ServiceProcessControl::RunAllTasksHelper(TaskList
* task_list
) {
80 TaskList::iterator index
= task_list
->begin();
81 while (index
!= task_list
->end()) {
83 index
= task_list
->erase(index
);
87 bool ServiceProcessControl::IsConnected() const {
88 return channel_
!= NULL
;
91 void ServiceProcessControl::Launch(const base::Closure
& success_task
,
92 const base::Closure
& failure_task
) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
95 base::Closure failure
= failure_task
;
96 if (!success_task
.is_null())
97 connect_success_tasks_
.push_back(success_task
);
99 if (!failure
.is_null())
100 connect_failure_tasks_
.push_back(failure
);
102 // If we already in the process of launching, then we are done.
106 // If the service process is already running then connects to it.
107 if (CheckServiceProcessReady()) {
112 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", SERVICE_EVENT_LAUNCH
,
115 scoped_ptr
<base::CommandLine
> cmd_line(CreateServiceProcessCommandLine());
116 // And then start the process asynchronously.
117 launcher_
= new Launcher(cmd_line
.Pass());
118 launcher_
->Run(base::Bind(&ServiceProcessControl::OnProcessLaunched
,
119 base::Unretained(this)));
122 void ServiceProcessControl::Disconnect() {
123 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
127 void ServiceProcessControl::OnProcessLaunched() {
128 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
129 if (launcher_
->launched()) {
130 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
131 SERVICE_EVENT_LAUNCHED
, SERVICE_EVENT_MAX
);
132 // After we have successfully created the service process we try to connect
133 // to it. The launch task is transfered to a connect task.
136 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
137 SERVICE_EVENT_LAUNCH_FAILED
, SERVICE_EVENT_MAX
);
138 // If we don't have process handle that means launching the service process
140 RunConnectDoneTasks();
143 // We don't need the launcher anymore.
147 bool ServiceProcessControl::OnMessageReceived(const IPC::Message
& message
) {
149 IPC_BEGIN_MESSAGE_MAP(ServiceProcessControl
, message
)
150 IPC_MESSAGE_HANDLER(ServiceHostMsg_CloudPrintProxy_Info
,
151 OnCloudPrintProxyInfo
)
152 IPC_MESSAGE_HANDLER(ServiceHostMsg_Histograms
, OnHistograms
)
153 IPC_MESSAGE_HANDLER(ServiceHostMsg_Printers
, OnPrinters
)
154 IPC_MESSAGE_UNHANDLED(handled
= false)
155 IPC_END_MESSAGE_MAP()
159 void ServiceProcessControl::OnChannelConnected(int32 peer_pid
) {
160 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
162 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
163 SERVICE_EVENT_CHANNEL_CONNECTED
, SERVICE_EVENT_MAX
);
165 // We just established a channel with the service process. Notify it if an
166 // upgrade is available.
167 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
168 Send(new ServiceMsg_UpdateAvailable
);
170 if (registrar_
.IsEmpty())
171 registrar_
.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED
,
172 content::NotificationService::AllSources());
174 RunConnectDoneTasks();
177 void ServiceProcessControl::OnChannelError() {
178 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
180 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
181 SERVICE_EVENT_CHANNEL_ERROR
, SERVICE_EVENT_MAX
);
184 RunConnectDoneTasks();
187 bool ServiceProcessControl::Send(IPC::Message
* message
) {
188 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
191 return channel_
->Send(message
);
194 // content::NotificationObserver implementation.
195 void ServiceProcessControl::Observe(
197 const content::NotificationSource
& source
,
198 const content::NotificationDetails
& details
) {
199 if (type
== chrome::NOTIFICATION_UPGRADE_RECOMMENDED
) {
200 Send(new ServiceMsg_UpdateAvailable
);
204 void ServiceProcessControl::OnCloudPrintProxyInfo(
205 const cloud_print::CloudPrintProxyInfo
& proxy_info
) {
206 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
207 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
208 SERVICE_EVENT_INFO_REPLY
, SERVICE_EVENT_MAX
);
209 if (!cloud_print_info_callback_
.is_null()) {
210 cloud_print_info_callback_
.Run(proxy_info
);
211 cloud_print_info_callback_
.Reset();
215 void ServiceProcessControl::OnHistograms(
216 const std::vector
<std::string
>& pickled_histograms
) {
217 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
218 SERVICE_EVENT_HISTOGRAMS_REPLY
, SERVICE_EVENT_MAX
);
219 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
220 base::HistogramDeltaSerialization::DeserializeAndAddSamples(
222 RunHistogramsCallback();
225 void ServiceProcessControl::RunHistogramsCallback() {
226 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 if (!histograms_callback_
.is_null()) {
228 histograms_callback_
.Run();
229 histograms_callback_
.Reset();
231 histograms_timeout_callback_
.Cancel();
234 void ServiceProcessControl::OnPrinters(
235 const std::vector
<std::string
>& printers
) {
236 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
237 UMA_HISTOGRAM_ENUMERATION(
238 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REPLY
, SERVICE_EVENT_MAX
);
239 UMA_HISTOGRAM_COUNTS_10000("CloudPrint.AvailablePrinters", printers
.size());
240 if (!printers_callback_
.is_null()) {
241 printers_callback_
.Run(printers
);
242 printers_callback_
.Reset();
246 bool ServiceProcessControl::GetCloudPrintProxyInfo(
247 const CloudPrintProxyInfoCallback
& cloud_print_info_callback
) {
248 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
249 DCHECK(!cloud_print_info_callback
.is_null());
250 cloud_print_info_callback_
.Reset();
251 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
252 SERVICE_EVENT_INFO_REQUEST
, SERVICE_EVENT_MAX
);
253 if (!Send(new ServiceMsg_GetCloudPrintProxyInfo()))
255 cloud_print_info_callback_
= cloud_print_info_callback
;
259 bool ServiceProcessControl::GetHistograms(
260 const base::Closure
& histograms_callback
,
261 const base::TimeDelta
& timeout
) {
262 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
263 DCHECK(!histograms_callback
.is_null());
264 histograms_callback_
.Reset();
266 #if defined(OS_MACOSX)
267 // TODO(vitalybuka): Investigate why it crashes MAC http://crbug.com/406227.
271 // If the service process is already running then connect to it.
272 if (!CheckServiceProcessReady())
276 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
277 SERVICE_EVENT_HISTOGRAMS_REQUEST
,
280 if (!Send(new ServiceMsg_GetHistograms()))
283 // Run timeout task to make sure |histograms_callback| is called.
284 histograms_timeout_callback_
.Reset(
285 base::Bind(&ServiceProcessControl::RunHistogramsCallback
,
286 base::Unretained(this)));
287 BrowserThread::PostDelayedTask(BrowserThread::UI
, FROM_HERE
,
288 histograms_timeout_callback_
.callback(),
291 histograms_callback_
= histograms_callback
;
295 bool ServiceProcessControl::GetPrinters(
296 const PrintersCallback
& printers_callback
) {
297 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
298 DCHECK(!printers_callback
.is_null());
299 printers_callback_
.Reset();
300 UMA_HISTOGRAM_ENUMERATION(
301 "CloudPrint.ServiceEvents", SERVICE_PRINTERS_REQUEST
, SERVICE_EVENT_MAX
);
302 if (!Send(new ServiceMsg_GetPrinters()))
304 printers_callback_
= printers_callback
;
308 bool ServiceProcessControl::Shutdown() {
309 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
310 bool ret
= Send(new ServiceMsg_Shutdown());
316 ServiceProcessControl
* ServiceProcessControl::GetInstance() {
317 return Singleton
<ServiceProcessControl
>::get();
320 ServiceProcessControl::Launcher::Launcher(
321 scoped_ptr
<base::CommandLine
> cmd_line
)
322 : cmd_line_(cmd_line
.Pass()),
327 // Execute the command line to start the process asynchronously.
328 // After the command is executed, |task| is called with the process handle on
330 void ServiceProcessControl::Launcher::Run(const base::Closure
& task
) {
331 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
333 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER
, FROM_HERE
,
334 base::Bind(&Launcher::DoRun
, this));
337 ServiceProcessControl::Launcher::~Launcher() {
341 void ServiceProcessControl::Launcher::Notify() {
342 DCHECK(!notify_task_
.is_null());
344 notify_task_
.Reset();
347 #if !defined(OS_MACOSX)
348 void ServiceProcessControl::Launcher::DoDetectLaunched() {
349 DCHECK(!notify_task_
.is_null());
351 const uint32 kMaxLaunchDetectRetries
= 10;
352 launched_
= CheckServiceProcessReady();
355 if (launched_
|| (retry_count_
>= kMaxLaunchDetectRetries
) ||
356 process_
.WaitForExitWithTimeout(base::TimeDelta(), &exit_code
)) {
358 BrowserThread::PostTask(
359 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));
364 // If the service process is not launched yet then check again in 2 seconds.
365 const base::TimeDelta kDetectLaunchRetry
= base::TimeDelta::FromSeconds(2);
366 base::MessageLoop::current()->PostDelayedTask(
367 FROM_HERE
, base::Bind(&Launcher::DoDetectLaunched
, this),
371 void ServiceProcessControl::Launcher::DoRun() {
372 DCHECK(!notify_task_
.is_null());
374 base::LaunchOptions options
;
376 options
.start_hidden
= true;
378 process_
= base::LaunchProcess(*cmd_line_
, options
);
379 if (process_
.IsValid()) {
380 BrowserThread::PostTask(
381 BrowserThread::IO
, FROM_HERE
,
382 base::Bind(&Launcher::DoDetectLaunched
, this));
384 BrowserThread::PostTask(
385 BrowserThread::UI
, FROM_HERE
, base::Bind(&Launcher::Notify
, this));