OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <windows.h> | 5 #include <windows.h> |
6 #include <shlwapi.h> | 6 #include <shlwapi.h> |
7 | 7 |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/file_version_info.h" | 12 #include "base/file_version_info.h" |
| 13 #include "base/files/file_path.h" |
13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/process/launch.h" |
16 #include "base/rand_util.h" // For PreRead experiment. | 18 #include "base/rand_util.h" // For PreRead experiment. |
17 #include "base/sha1.h" // For PreRead experiment. | 19 #include "base/sha1.h" // For PreRead experiment. |
18 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
22 #include "base/version.h" | 24 #include "base/version.h" |
| 25 #include "base/win/scoped_process_information.h" |
| 26 #include "base/win/startup_information.h" |
23 #include "base/win/windows_version.h" | 27 #include "base/win/windows_version.h" |
24 #include "chrome/app/chrome_crash_reporter_client.h" | 28 #include "chrome/app/chrome_crash_reporter_client.h" |
25 #include "chrome/app/client_util.h" | 29 #include "chrome/app/client_util.h" |
26 #include "chrome/app/image_pre_reader_win.h" | 30 #include "chrome/app/image_pre_reader_win.h" |
27 #include "chrome/common/chrome_constants.h" | 31 #include "chrome/common/chrome_constants.h" |
28 #include "chrome/common/chrome_result_codes.h" | 32 #include "chrome/common/chrome_result_codes.h" |
29 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/env_vars.h" | 34 #include "chrome/common/env_vars.h" |
31 #include "chrome/installer/util/google_update_constants.h" | 35 #include "chrome/installer/util/google_update_constants.h" |
32 #include "chrome/installer/util/google_update_settings.h" | 36 #include "chrome/installer/util/google_update_settings.h" |
33 #include "chrome/installer/util/install_util.h" | 37 #include "chrome/installer/util/install_util.h" |
34 #include "chrome/installer/util/util_constants.h" | 38 #include "chrome/installer/util/util_constants.h" |
| 39 #include "components/browser_watcher/watcher_client_win.h" |
| 40 #include "components/browser_watcher/watcher_main_win.h" |
35 #include "components/crash/app/breakpad_win.h" | 41 #include "components/crash/app/breakpad_win.h" |
36 #include "components/crash/app/crash_reporter_client.h" | 42 #include "components/crash/app/crash_reporter_client.h" |
37 #include "components/metrics/client_info.h" | 43 #include "components/metrics/client_info.h" |
38 #include "content/public/app/startup_helper_win.h" | 44 #include "content/public/app/startup_helper_win.h" |
39 #include "sandbox/win/src/sandbox.h" | 45 #include "sandbox/win/src/sandbox.h" |
40 | 46 |
41 namespace { | 47 namespace { |
42 // The entry point signature of chrome.dll. | 48 // The entry point signature of chrome.dll. |
43 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); | 49 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); |
44 | 50 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 // current directory to support run-what-you-compile and other development | 234 // current directory to support run-what-you-compile and other development |
229 // scenarios. | 235 // scenarios. |
230 // If that fails then we look at the version resource in the current | 236 // If that fails then we look at the version resource in the current |
231 // module. This is the expected path for chrome.exe browser instances in an | 237 // module. This is the expected path for chrome.exe browser instances in an |
232 // installed build. | 238 // installed build. |
233 HMODULE MainDllLoader::Load(base::string16* version, | 239 HMODULE MainDllLoader::Load(base::string16* version, |
234 base::string16* out_file) { | 240 base::string16* out_file) { |
235 const base::string16 executable_dir(GetExecutablePath()); | 241 const base::string16 executable_dir(GetExecutablePath()); |
236 *out_file = executable_dir; | 242 *out_file = executable_dir; |
237 | 243 |
238 const wchar_t* dll_name = metro_mode_ ? | 244 const wchar_t* dll_name = NULL; |
239 installer::kChromeMetroDll : | 245 if (metro_mode_) |
240 #if !defined(CHROME_MULTIPLE_DLL) | 246 dll_name = installer::kChromeMetroDll; |
241 installer::kChromeDll; | 247 |
| 248 if (process_type_ == "service" || process_type_.empty()) { |
| 249 dll_name = installer::kChromeDll; |
| 250 } else if (process_type_ == "watcher") { |
| 251 dll_name = browser_watcher::kWatcherDll; |
| 252 } else { |
| 253 #if defined(CHROME_MULTIPLE_DLL) |
| 254 dll_name = installer::kChromeChildDll; |
242 #else | 255 #else |
243 (process_type_ == "service") || process_type_.empty() ? | 256 dll_name = installer::kChromeDll; |
244 installer::kChromeDll : | |
245 installer::kChromeChildDll; | |
246 #endif | 257 #endif |
| 258 } |
| 259 |
247 const bool pre_read = !metro_mode_; | 260 const bool pre_read = !metro_mode_; |
248 HMODULE dll = LoadModuleWithDirectory(out_file, dll_name, pre_read); | 261 HMODULE dll = LoadModuleWithDirectory(out_file, dll_name, pre_read); |
249 if (!dll) { | 262 if (!dll) { |
250 base::string16 version_string(GetCurrentModuleVersion()); | 263 base::string16 version_string(GetCurrentModuleVersion()); |
251 if (version_string.empty()) { | 264 if (version_string.empty()) { |
252 LOG(ERROR) << "No valid Chrome version found"; | 265 LOG(ERROR) << "No valid Chrome version found"; |
253 return NULL; | 266 return NULL; |
254 } | 267 } |
255 *out_file = executable_dir; | 268 *out_file = executable_dir; |
256 *version = version_string; | 269 *version = version_string; |
(...skipping 22 matching lines...) Expand all Loading... |
279 if (metro_mode_) { | 292 if (metro_mode_) { |
280 HMODULE metro_dll = Load(&version, &file); | 293 HMODULE metro_dll = Load(&version, &file); |
281 if (!metro_dll) | 294 if (!metro_dll) |
282 return chrome::RESULT_CODE_MISSING_DATA; | 295 return chrome::RESULT_CODE_MISSING_DATA; |
283 | 296 |
284 InitMetro chrome_metro_main = | 297 InitMetro chrome_metro_main = |
285 reinterpret_cast<InitMetro>(::GetProcAddress(metro_dll, "InitMetro")); | 298 reinterpret_cast<InitMetro>(::GetProcAddress(metro_dll, "InitMetro")); |
286 return chrome_metro_main(); | 299 return chrome_metro_main(); |
287 } | 300 } |
288 | 301 |
| 302 if (process_type_ == "watcher") { |
| 303 HMODULE watcher_dll = Load(&version, &file); |
| 304 if (watcher_dll) { |
| 305 browser_watcher::WatcherMainFunction watcher_main = |
| 306 reinterpret_cast<browser_watcher::WatcherMainFunction>( |
| 307 ::GetProcAddress(watcher_dll, |
| 308 browser_watcher::kWatcherDLLEntrypoint)); |
| 309 |
| 310 watcher_main(chrome::kBrowserExitCodesRegistryPath); |
| 311 |
| 312 return content::RESULT_CODE_NORMAL_EXIT; |
| 313 } else { |
| 314 return chrome::RESULT_CODE_MISSING_DATA; |
| 315 } |
| 316 } |
| 317 |
289 // Initialize the sandbox services. | 318 // Initialize the sandbox services. |
290 sandbox::SandboxInterfaceInfo sandbox_info = {0}; | 319 sandbox::SandboxInterfaceInfo sandbox_info = {0}; |
291 content::InitializeSandboxInfo(&sandbox_info); | 320 content::InitializeSandboxInfo(&sandbox_info); |
292 | 321 |
293 crash_reporter::SetCrashReporterClient(g_chrome_crash_client.Pointer()); | 322 crash_reporter::SetCrashReporterClient(g_chrome_crash_client.Pointer()); |
294 bool exit_now = true; | 323 bool exit_now = true; |
295 if (process_type_.empty()) { | 324 if (process_type_.empty()) { |
296 if (breakpad::ShowRestartDialogIfCrashed(&exit_now)) { | 325 if (breakpad::ShowRestartDialogIfCrashed(&exit_now)) { |
297 // We restarted because of a previous crash. Ask user if we should | 326 // We restarted because of a previous crash. Ask user if we should |
298 // Relaunch. Only for the browser process. See crbug.com/132119. | 327 // Relaunch. Only for the browser process. See crbug.com/132119. |
299 if (exit_now) | 328 if (exit_now) |
300 return content::RESULT_CODE_NORMAL_EXIT; | 329 return content::RESULT_CODE_NORMAL_EXIT; |
301 } | 330 } |
302 } | 331 } |
303 breakpad::InitCrashReporter(process_type_); | 332 breakpad::InitCrashReporter(process_type_); |
304 | 333 |
305 dll_ = Load(&version, &file); | 334 dll_ = Load(&version, &file); |
306 if (!dll_) | 335 if (!dll_) |
307 return chrome::RESULT_CODE_MISSING_DATA; | 336 return chrome::RESULT_CODE_MISSING_DATA; |
308 | 337 |
309 scoped_ptr<base::Environment> env(base::Environment::Create()); | 338 scoped_ptr<base::Environment> env(base::Environment::Create()); |
310 env->SetVar(chrome::kChromeVersionEnvVar, base::WideToUTF8(version)); | 339 env->SetVar(chrome::kChromeVersionEnvVar, base::WideToUTF8(version)); |
311 | 340 |
312 OnBeforeLaunch(file); | 341 OnBeforeLaunch(process_type_, file); |
313 DLL_MAIN chrome_main = | 342 DLL_MAIN chrome_main = |
314 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll_, "ChromeMain")); | 343 reinterpret_cast<DLL_MAIN>(::GetProcAddress(dll_, "ChromeMain")); |
315 int rc = chrome_main(instance, &sandbox_info); | 344 int rc = chrome_main(instance, &sandbox_info); |
316 return OnBeforeExit(rc, file); | 345 return OnBeforeExit(rc, file); |
317 } | 346 } |
318 | 347 |
319 void MainDllLoader::RelaunchChromeBrowserWithNewCommandLineIfNeeded() { | 348 void MainDllLoader::RelaunchChromeBrowserWithNewCommandLineIfNeeded() { |
320 if (!dll_) | 349 if (!dll_) |
321 return; | 350 return; |
322 | 351 |
323 RelaunchChromeBrowserWithNewCommandLineIfNeededFunc relaunch_function = | 352 RelaunchChromeBrowserWithNewCommandLineIfNeededFunc relaunch_function = |
324 reinterpret_cast<RelaunchChromeBrowserWithNewCommandLineIfNeededFunc>( | 353 reinterpret_cast<RelaunchChromeBrowserWithNewCommandLineIfNeededFunc>( |
325 ::GetProcAddress(dll_, | 354 ::GetProcAddress(dll_, |
326 "RelaunchChromeBrowserWithNewCommandLineIfNeeded")); | 355 "RelaunchChromeBrowserWithNewCommandLineIfNeeded")); |
327 if (!relaunch_function) { | 356 if (!relaunch_function) { |
328 LOG(ERROR) << "Could not find exported function " | 357 LOG(ERROR) << "Could not find exported function " |
329 << "RelaunchChromeBrowserWithNewCommandLineIfNeeded"; | 358 << "RelaunchChromeBrowserWithNewCommandLineIfNeeded"; |
330 } else { | 359 } else { |
331 relaunch_function(); | 360 relaunch_function(); |
332 } | 361 } |
333 } | 362 } |
334 | 363 |
335 //============================================================================= | 364 //============================================================================= |
336 | 365 |
337 class ChromeDllLoader : public MainDllLoader { | 366 class ChromeDllLoader : public MainDllLoader { |
338 protected: | 367 protected: |
339 virtual void OnBeforeLaunch(const base::string16& dll_path) { | 368 virtual void OnBeforeLaunch(const std::string& process_type, |
| 369 const base::string16& dll_path) OVERRIDE; |
| 370 virtual int OnBeforeExit(int return_code, |
| 371 const base::string16& dll_path) OVERRIDE; |
| 372 }; |
| 373 |
| 374 void ChromeDllLoader::OnBeforeLaunch(const std::string& process_type, |
| 375 const base::string16& dll_path) { |
| 376 if (process_type.empty()) { |
340 RecordDidRun(dll_path); | 377 RecordDidRun(dll_path); |
| 378 |
| 379 // Launch the watcher process if stats collection consent has been granted. |
| 380 if (g_chrome_crash_client.Get().GetCollectStatsConsent()) { |
| 381 wchar_t exe_path[MAX_PATH]; |
| 382 ::GetModuleFileNameW(NULL, exe_path, MAX_PATH); |
| 383 |
| 384 base::CommandLine cmd_line = base::CommandLine(base::FilePath(exe_path)); |
| 385 cmd_line.AppendSwitchASCII(switches::kProcessType, "watcher"); |
| 386 browser_watcher::WatcherClient watcher_client(cmd_line); |
| 387 |
| 388 watcher_client.LaunchWatcher(); |
| 389 } |
341 } | 390 } |
| 391 } |
342 | 392 |
343 virtual int OnBeforeExit(int return_code, const base::string16& dll_path) { | 393 int ChromeDllLoader::OnBeforeExit(int return_code, |
| 394 const base::string16& dll_path) { |
344 // NORMAL_EXIT_CANCEL is used for experiments when the user cancels | 395 // NORMAL_EXIT_CANCEL is used for experiments when the user cancels |
345 // so we need to reset the did_run signal so omaha does not count | 396 // so we need to reset the did_run signal so omaha does not count |
346 // this run as active usage. | 397 // this run as active usage. |
347 if (chrome::RESULT_CODE_NORMAL_EXIT_CANCEL == return_code) { | 398 if (chrome::RESULT_CODE_NORMAL_EXIT_CANCEL == return_code) { |
348 ClearDidRun(dll_path); | 399 ClearDidRun(dll_path); |
349 } | 400 } |
350 return return_code; | 401 return return_code; |
351 } | 402 } |
352 }; | |
353 | 403 |
354 //============================================================================= | 404 //============================================================================= |
355 | 405 |
356 class ChromiumDllLoader : public MainDllLoader { | 406 class ChromiumDllLoader : public MainDllLoader { |
357 protected: | 407 protected: |
358 virtual void OnBeforeLaunch(const base::string16& dll_path) override { | 408 virtual void OnBeforeLaunch(const std::string& process_type, |
| 409 const base::string16& dll_path) override { |
359 } | 410 } |
360 virtual int OnBeforeExit(int return_code, | 411 virtual int OnBeforeExit(int return_code, |
361 const base::string16& dll_path) override { | 412 const base::string16& dll_path) override { |
362 return return_code; | 413 return return_code; |
363 } | 414 } |
364 }; | 415 }; |
365 | 416 |
366 MainDllLoader* MakeMainDllLoader() { | 417 MainDllLoader* MakeMainDllLoader() { |
367 #if defined(GOOGLE_CHROME_BUILD) | 418 #if defined(GOOGLE_CHROME_BUILD) |
368 return new ChromeDllLoader(); | 419 return new ChromeDllLoader(); |
369 #else | 420 #else |
370 return new ChromiumDllLoader(); | 421 return new ChromiumDllLoader(); |
371 #endif | 422 #endif |
372 } | 423 } |
OLD | NEW |