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/win/windows_version.h"
14 #include "chrome/app/client_util.h"
15 #include "chrome/browser/chrome_process_finder_win.h"
16 #include "chrome/browser/policy/policy_path_parser.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_paths_internal.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome_elf/chrome_elf_main.h"
21 #include "components/startup_metric_utils/startup_metric_utils.h"
22 #include "content/public/common/result_codes.h"
23 #include "ui/gfx/win/dpi.h"
27 void CheckSafeModeLaunch() {
28 unsigned short k1 = ::GetAsyncKeyState(VK_CONTROL);
29 unsigned short k2 = ::GetAsyncKeyState(VK_MENU);
30 const unsigned short kPressedMask = 0x8000;
31 if ((k1 & kPressedMask) && (k2 & kPressedMask))
32 ::SetEnvironmentVariableA(chrome::kSafeModeEnvVar, "1");
35 // List of switches that it's safe to rendezvous early with. Fast start should
36 // not be done if a command line contains a switch not in this set.
37 // Note this is currently stored as a list of two because it's probably faster
38 // to iterate over this small array than building a map for constant time
40 const char* const kFastStartSwitches[] = {
41 switches::kProfileDirectory,
42 switches::kShowAppList,
45 bool IsFastStartSwitch(const std::string& command_line_switch) {
46 for (size_t i = 0; i < arraysize(kFastStartSwitches); ++i) {
47 if (command_line_switch == kFastStartSwitches[i])
53 bool ContainsNonFastStartFlag(const CommandLine& command_line) {
54 const CommandLine::SwitchMap& switches = command_line.GetSwitches();
55 if (switches.size() > arraysize(kFastStartSwitches))
57 for (CommandLine::SwitchMap::const_iterator it = switches.begin();
58 it != switches.end(); ++it) {
59 if (!IsFastStartSwitch(it->first))
65 bool AttemptFastNotify(const CommandLine& command_line) {
66 if (ContainsNonFastStartFlag(command_line))
69 base::FilePath user_data_dir;
70 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir))
72 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
74 HWND chrome = chrome::FindRunningChromeWindow(user_data_dir);
77 return chrome::AttemptToNotifyRunningChrome(chrome, true) ==
78 chrome::NOTIFY_SUCCESS;
83 #if !defined(ADDRESS_SANITIZER)
84 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
86 // The AddressSanitizer build should be a console program as it prints out stuff
89 HINSTANCE instance = GetModuleHandle(NULL);
91 startup_metric_utils::RecordExeMainEntryTime();
93 // Signal Chrome Elf that Chrome has begun to start.
96 // Initialize the commandline singleton from the environment.
97 CommandLine::Init(0, NULL);
98 // The exit manager is in charge of calling the dtors of singletons.
99 base::AtExitManager exit_manager;
101 // We don't want to set DPI awareness on Vista because we don't support
102 // DirectWrite there. GDI fonts are kerned very badly, so better to leave
103 // DPI-unaware and at effective 1.0. See also ShouldUseDirectWrite().
104 if (base::win::GetVersion() > base::win::VERSION_VISTA)
105 gfx::EnableHighDPISupport();
107 if (AttemptFastNotify(*CommandLine::ForCurrentProcess()))
110 CheckSafeModeLaunch();
112 // Load and launch the chrome dll. *Everything* happens inside.
113 MainDllLoader* loader = MakeMainDllLoader();
114 int rc = loader->Launch(instance);
115 loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded();