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
3 // found in the LICENSE file.
10 #include "base/at_exit.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "chrome/app/chrome_breakpad_client.h"
15 #include "chrome/app/client_util.h"
16 #include "chrome/app/metro_driver_win.h"
17 #include "chrome/browser/chrome_process_finder_win.h"
18 #include "chrome/browser/policy/policy_path_parser.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths_internal.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome_elf/chrome_elf_main.h"
23 #include "components/breakpad/app/breakpad_client.h"
24 #include "components/breakpad/app/breakpad_win.h"
25 #include "components/startup_metric_utils/startup_metric_utils.h"
26 #include "content/public/app/startup_helper_win.h"
27 #include "content/public/common/result_codes.h"
28 #include "sandbox/win/src/sandbox_factory.h"
29 #include "ui/gfx/win/dpi.h"
33 base::LazyInstance
<chrome::ChromeBreakpadClient
>::Leaky
34 g_chrome_breakpad_client
= LAZY_INSTANCE_INITIALIZER
;
36 void CheckSafeModeLaunch() {
37 unsigned short k1
= ::GetAsyncKeyState(VK_CONTROL
);
38 unsigned short k2
= ::GetAsyncKeyState(VK_MENU
);
39 const unsigned short kPressedMask
= 0x8000;
40 if ((k1
& kPressedMask
) && (k2
& kPressedMask
))
41 ::SetEnvironmentVariableA(chrome::kSafeModeEnvVar
, "1");
44 int RunChrome(HINSTANCE instance
) {
45 breakpad::SetBreakpadClient(g_chrome_breakpad_client
.Pointer());
47 CheckSafeModeLaunch();
50 // We restarted because of a previous crash. Ask user if we should relaunch.
51 // Only show this for the browser process. See crbug.com/132119.
52 const std::string process_type
=
53 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
54 switches::kProcessType
);
55 if (process_type
.empty()) {
56 if (breakpad::ShowRestartDialogIfCrashed(&exit_now
)) {
58 return content::RESULT_CODE_NORMAL_EXIT
;
62 // Initialize the sandbox services.
63 sandbox::SandboxInterfaceInfo sandbox_info
= {0};
64 content::InitializeSandboxInfo(&sandbox_info
);
66 // Load and launch the chrome dll. *Everything* happens inside.
67 MainDllLoader
* loader
= MakeMainDllLoader();
68 int rc
= loader
->Launch(instance
, &sandbox_info
);
69 loader
->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
74 // List of switches that it's safe to rendezvous early with. Fast start should
75 // not be done if a command line contains a switch not in this set.
76 // Note this is currently stored as a list of two because it's probably faster
77 // to iterate over this small array than building a map for constant time
79 const char* const kFastStartSwitches
[] = {
80 switches::kProfileDirectory
,
81 switches::kShowAppList
,
84 bool IsFastStartSwitch(const std::string
& command_line_switch
) {
85 for (size_t i
= 0; i
< arraysize(kFastStartSwitches
); ++i
) {
86 if (command_line_switch
== kFastStartSwitches
[i
])
92 bool ContainsNonFastStartFlag(const CommandLine
& command_line
) {
93 const CommandLine::SwitchMap
& switches
= command_line
.GetSwitches();
94 if (switches
.size() > arraysize(kFastStartSwitches
))
96 for (CommandLine::SwitchMap::const_iterator it
= switches
.begin();
97 it
!= switches
.end(); ++it
) {
98 if (!IsFastStartSwitch(it
->first
))
104 bool AttemptFastNotify(const CommandLine
& command_line
) {
105 if (ContainsNonFastStartFlag(command_line
))
108 base::FilePath user_data_dir
;
109 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir
))
111 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
113 HWND chrome
= chrome::FindRunningChromeWindow(user_data_dir
);
116 return chrome::AttemptToNotifyRunningChrome(chrome
, true) ==
117 chrome::NOTIFY_SUCCESS
;
122 int APIENTRY
wWinMain(HINSTANCE instance
, HINSTANCE prev
, wchar_t*, int) {
123 startup_metric_utils::RecordExeMainEntryTime();
124 // Initialize the commandline singleton from the environment.
125 CommandLine::Init(0, NULL
);
126 // The exit manager is in charge of calling the dtors of singletons.
127 base::AtExitManager exit_manager
;
129 gfx::EnableHighDPISupport();
131 if (AttemptFastNotify(*CommandLine::ForCurrentProcess()))
134 // Signal Chrome Elf that Chrome has begun to start.
137 MetroDriver metro_driver
;
138 if (metro_driver
.in_metro_mode())
139 return metro_driver
.RunInMetro(instance
, &RunChrome
);
140 // Not in metro mode, proceed as normal.
141 return RunChrome(instance
);