EME test page application.
[chromium-blink-merge.git] / chrome / browser / service_process / service_process_control.cc
blobb9cfa5714646323fd7f7b646fcbe501e351cb90e
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"
7 #include "base/bind.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
43 // and return.
44 if (channel_.get()) {
45 RunConnectDoneTasks();
46 return;
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(
55 channel_id,
56 IPC::Channel::MODE_NAMED_CLIENT,
57 this,
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
68 // avoided.
69 TaskList tasks;
71 if (IsConnected()) {
72 tasks.swap(connect_success_tasks_);
73 RunAllTasksHelper(&tasks);
74 DCHECK(tasks.empty());
75 connect_failure_tasks_.clear();
76 } else {
77 tasks.swap(connect_failure_tasks_);
78 RunAllTasksHelper(&tasks);
79 DCHECK(tasks.empty());
80 connect_success_tasks_.clear();
84 // static
85 void ServiceProcessControl::RunAllTasksHelper(TaskList* task_list) {
86 TaskList::iterator index = task_list->begin();
87 while (index != task_list->end()) {
88 (*index).Run();
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.
109 if (launcher_.get())
110 return;
112 // If the service process is already running then connects to it.
113 if (CheckServiceProcessReady()) {
114 ConnectInternal();
115 return;
118 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents", SERVICE_EVENT_LAUNCH,
119 SERVICE_EVENT_MAX);
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;
126 #else
127 int flags = ChildProcessHost::CHILD_NORMAL;
128 #endif
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,
142 #if defined(OS_WIN)
143 switches::kEnableCloudPrintXps,
144 #endif
145 switches::kEnableLogging,
146 switches::kIgnoreUrlFetcherCertRequests,
147 switches::kLang,
148 switches::kLoggingLevel,
149 switches::kLsoUrl,
150 switches::kNoServiceAutorun,
151 switches::kUserDataDir,
152 switches::kV,
153 switches::kVModule,
154 switches::kWaitForDebugger,
156 cmd_line->CopySwitchesFrom(*CommandLine::ForCurrentProcess(),
157 kSwitchesToCopy,
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));
168 channel_.reset();
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.
178 ConnectInternal();
179 } else {
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
183 // has failed.
184 RunConnectDoneTasks();
187 // We don't need the launcher anymore.
188 launcher_ = NULL;
191 bool ServiceProcessControl::OnMessageReceived(const IPC::Message& message) {
192 bool handled = true;
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()
200 return handled;
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);
213 } else {
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);
227 channel_.reset();
228 RunConnectDoneTasks();
231 bool ServiceProcessControl::Send(IPC::Message* message) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
233 if (!channel_.get())
234 return false;
235 return channel_->Send(message);
238 // content::NotificationObserver implementation.
239 void ServiceProcessControl::Observe(
240 int type,
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(
265 pickled_histograms);
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()))
298 return false;
299 cloud_print_info_callback_ = cloud_print_info_callback;
300 return true;
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())
312 return false;
313 ConnectInternal();
315 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceEvents",
316 SERVICE_EVENT_HISTOGRAMS_REQUEST,
317 SERVICE_EVENT_MAX);
319 if (!Send(new ServiceMsg_GetHistograms()))
320 return false;
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(),
328 timeout);
330 histograms_callback_ = histograms_callback;
331 return true;
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()))
342 return false;
343 printers_callback_ = printers_callback;
344 return true;
347 bool ServiceProcessControl::Shutdown() {
348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
349 bool ret = Send(new ServiceMsg_Shutdown());
350 channel_.reset();
351 return ret;
354 // static
355 ServiceProcessControl* ServiceProcessControl::GetInstance() {
356 return Singleton<ServiceProcessControl>::get();
359 ServiceProcessControl::Launcher::Launcher(ServiceProcessControl* process,
360 CommandLine* cmd_line)
361 : process_(process),
362 cmd_line_(cmd_line),
363 launched_(false),
364 retry_count_(0),
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
370 // the UI thread.
371 void ServiceProcessControl::Launcher::Run(const base::Closure& task) {
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
373 notify_task_ = task;
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());
385 notify_task_.Run();
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();
403 int exit_code = 0;
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));
410 return;
412 retry_count_++;
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),
418 kDetectLaunchRetry);
421 void ServiceProcessControl::Launcher::DoRun() {
422 DCHECK(!notify_task_.is_null());
424 base::LaunchOptions options;
425 #if defined(OS_WIN)
426 options.start_hidden = true;
427 #endif
428 if (base::LaunchProcess(*cmd_line_, options, &process_handle_)) {
429 BrowserThread::PostTask(
430 BrowserThread::IO, FROM_HERE,
431 base::Bind(&Launcher::DoDetectLaunched, this));
432 } else {
433 BrowserThread::PostTask(
434 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this));
437 #endif // !OS_MACOSX