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 "chrome/app/client_util.h"
14 #include "chrome/browser/chrome_process_finder_win.h"
15 #include "chrome/browser/policy/policy_path_parser.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_paths_internal.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome_elf/chrome_elf_main.h"
20 #include "components/startup_metric_utils/startup_metric_utils.h"
21 #include "content/public/common/result_codes.h"
22 #include "ui/gfx/win/dpi.h"
26 void CheckSafeModeLaunch() {
27 unsigned short k1
= ::GetAsyncKeyState(VK_CONTROL
);
28 unsigned short k2
= ::GetAsyncKeyState(VK_MENU
);
29 const unsigned short kPressedMask
= 0x8000;
30 if ((k1
& kPressedMask
) && (k2
& kPressedMask
))
31 ::SetEnvironmentVariableA(chrome::kSafeModeEnvVar
, "1");
34 // List of switches that it's safe to rendezvous early with. Fast start should
35 // not be done if a command line contains a switch not in this set.
36 // Note this is currently stored as a list of two because it's probably faster
37 // to iterate over this small array than building a map for constant time
39 const char* const kFastStartSwitches
[] = {
40 switches::kProfileDirectory
,
41 switches::kShowAppList
,
44 bool IsFastStartSwitch(const std::string
& command_line_switch
) {
45 for (size_t i
= 0; i
< arraysize(kFastStartSwitches
); ++i
) {
46 if (command_line_switch
== kFastStartSwitches
[i
])
52 bool ContainsNonFastStartFlag(const CommandLine
& command_line
) {
53 const CommandLine::SwitchMap
& switches
= command_line
.GetSwitches();
54 if (switches
.size() > arraysize(kFastStartSwitches
))
56 for (CommandLine::SwitchMap::const_iterator it
= switches
.begin();
57 it
!= switches
.end(); ++it
) {
58 if (!IsFastStartSwitch(it
->first
))
64 bool AttemptFastNotify(const CommandLine
& command_line
) {
65 if (ContainsNonFastStartFlag(command_line
))
68 base::FilePath user_data_dir
;
69 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir
))
71 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
73 HWND chrome
= chrome::FindRunningChromeWindow(user_data_dir
);
76 return chrome::AttemptToNotifyRunningChrome(chrome
, true) ==
77 chrome::NOTIFY_SUCCESS
;
82 int APIENTRY
wWinMain(HINSTANCE instance
, HINSTANCE prev
, wchar_t*, int) {
83 startup_metric_utils::RecordExeMainEntryTime();
85 // Signal Chrome Elf that Chrome has begun to start.
88 // Initialize the commandline singleton from the environment.
89 CommandLine::Init(0, NULL
);
90 // The exit manager is in charge of calling the dtors of singletons.
91 base::AtExitManager exit_manager
;
93 gfx::EnableHighDPISupport();
95 if (AttemptFastNotify(*CommandLine::ForCurrentProcess()))
98 CheckSafeModeLaunch();
100 // Load and launch the chrome dll. *Everything* happens inside.
101 MainDllLoader
* loader
= MakeMainDllLoader();
102 int rc
= loader
->Launch(instance
);
103 loader
->RelaunchChromeBrowserWithNewCommandLineIfNeeded();