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/browser/service/service_process_control.h" | 5 #include "chrome/browser/service/service_process_control.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/file_path.h" | 10 #include "base/file_path.h" |
9 #include "base/process_util.h" | 11 #include "base/process_util.h" |
10 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
11 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
12 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
13 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/upgrade_detector.h" | 16 #include "chrome/browser/upgrade_detector.h" |
15 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 cmd_line->AppendSwitch(switches::kWaitForDebugger); | 139 cmd_line->AppendSwitch(switches::kWaitForDebugger); |
138 | 140 |
139 if (browser_command_line.HasSwitch(switches::kEnableLogging)) | 141 if (browser_command_line.HasSwitch(switches::kEnableLogging)) |
140 cmd_line->AppendSwitch(switches::kEnableLogging); | 142 cmd_line->AppendSwitch(switches::kEnableLogging); |
141 | 143 |
142 std::string locale = g_browser_process->GetApplicationLocale(); | 144 std::string locale = g_browser_process->GetApplicationLocale(); |
143 cmd_line->AppendSwitchASCII(switches::kLang, locale); | 145 cmd_line->AppendSwitchASCII(switches::kLang, locale); |
144 | 146 |
145 // And then start the process asynchronously. | 147 // And then start the process asynchronously. |
146 launcher_ = new Launcher(this, cmd_line); | 148 launcher_ = new Launcher(this, cmd_line); |
147 launcher_->Run( | 149 launcher_->Run(base::Bind(&ServiceProcessControl::OnProcessLaunched, |
148 NewRunnableMethod(this, &ServiceProcessControl::OnProcessLaunched)); | 150 base::Unretained(this))); |
149 } | 151 } |
150 | 152 |
151 void ServiceProcessControl::Disconnect() { | 153 void ServiceProcessControl::Disconnect() { |
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
153 channel_.reset(); | 155 channel_.reset(); |
154 } | 156 } |
155 | 157 |
156 void ServiceProcessControl::OnProcessLaunched() { | 158 void ServiceProcessControl::OnProcessLaunched() { |
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
158 if (launcher_->launched()) { | 160 if (launcher_->launched()) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 CommandLine* cmd_line) | 255 CommandLine* cmd_line) |
254 : process_(process), | 256 : process_(process), |
255 cmd_line_(cmd_line), | 257 cmd_line_(cmd_line), |
256 launched_(false), | 258 launched_(false), |
257 retry_count_(0) { | 259 retry_count_(0) { |
258 } | 260 } |
259 | 261 |
260 // Execute the command line to start the process asynchronously. | 262 // Execute the command line to start the process asynchronously. |
261 // After the command is executed, |task| is called with the process handle on | 263 // After the command is executed, |task| is called with the process handle on |
262 // the UI thread. | 264 // the UI thread. |
263 void ServiceProcessControl::Launcher::Run(Task* task) { | 265 void ServiceProcessControl::Launcher::Run(const base::Closure& task) { |
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
265 notify_task_.reset(task); | 267 notify_task_ = task; |
266 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 268 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
267 NewRunnableMethod(this, &Launcher::DoRun)); | 269 base::Bind(&Launcher::DoRun, this)); |
268 } | 270 } |
269 | 271 |
270 ServiceProcessControl::Launcher::~Launcher() {} | 272 ServiceProcessControl::Launcher::~Launcher() {} |
271 | 273 |
272 void ServiceProcessControl::Launcher::Notify() { | 274 void ServiceProcessControl::Launcher::Notify() { |
273 DCHECK(notify_task_.get()); | 275 DCHECK_EQ(false, notify_task_.is_null()); |
274 notify_task_->Run(); | 276 notify_task_.Run(); |
275 notify_task_.reset(); | 277 notify_task_.Reset(); |
276 } | 278 } |
277 | 279 |
278 #if !defined(OS_MACOSX) | 280 #if !defined(OS_MACOSX) |
279 void ServiceProcessControl::Launcher::DoDetectLaunched() { | 281 void ServiceProcessControl::Launcher::DoDetectLaunched() { |
280 DCHECK(notify_task_.get()); | 282 DCHECK_EQ(false, notify_task_.is_null()); |
| 283 |
281 const uint32 kMaxLaunchDetectRetries = 10; | 284 const uint32 kMaxLaunchDetectRetries = 10; |
282 launched_ = CheckServiceProcessReady(); | 285 launched_ = CheckServiceProcessReady(); |
283 if (launched_ || (retry_count_ >= kMaxLaunchDetectRetries)) { | 286 if (launched_ || (retry_count_ >= kMaxLaunchDetectRetries)) { |
284 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 287 BrowserThread::PostTask( |
285 NewRunnableMethod(this, &Launcher::Notify)); | 288 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); |
286 return; | 289 return; |
287 } | 290 } |
288 retry_count_++; | 291 retry_count_++; |
289 | 292 |
290 // If the service process is not launched yet then check again in 2 seconds. | 293 // If the service process is not launched yet then check again in 2 seconds. |
291 const int kDetectLaunchRetry = 2000; | 294 const int kDetectLaunchRetry = 2000; |
292 MessageLoop::current()->PostDelayedTask( | 295 MessageLoop::current()->PostDelayedTask( |
293 FROM_HERE, | 296 FROM_HERE, base::Bind(&Launcher::DoDetectLaunched, this), |
294 NewRunnableMethod(this, &Launcher::DoDetectLaunched), | |
295 kDetectLaunchRetry); | 297 kDetectLaunchRetry); |
296 } | 298 } |
297 | 299 |
298 void ServiceProcessControl::Launcher::DoRun() { | 300 void ServiceProcessControl::Launcher::DoRun() { |
299 DCHECK(notify_task_.get()); | 301 DCHECK_EQ(false, notify_task_.is_null()); |
| 302 |
300 base::LaunchOptions options; | 303 base::LaunchOptions options; |
301 #if defined(OS_WIN) | 304 #if defined(OS_WIN) |
302 options.start_hidden = true; | 305 options.start_hidden = true; |
303 #endif | 306 #endif |
304 if (base::LaunchProcess(*cmd_line_, options, NULL)) { | 307 if (base::LaunchProcess(*cmd_line_, options, NULL)) { |
305 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 308 BrowserThread::PostTask( |
306 NewRunnableMethod(this, | 309 BrowserThread::IO, FROM_HERE, |
307 &Launcher::DoDetectLaunched)); | 310 base::Bind(&Launcher::DoDetectLaunched, this)); |
308 } else { | 311 } else { |
309 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 312 BrowserThread::PostTask( |
310 NewRunnableMethod(this, &Launcher::Notify)); | 313 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); |
311 } | 314 } |
312 } | 315 } |
313 #endif // !OS_MACOSX | 316 #endif // !OS_MACOSX |
OLD | NEW |