Add python coverage module to third_party
[chromium-blink-merge.git] / chrome / app / chrome_exe_main_win.cc
bloba3e4e5e2eacad79aa51bdc8700afb434a16dca8b
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.
5 #include <windows.h>
6 #include <tchar.h>
8 #include <string>
10 #include "base/at_exit.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/win/windows_version.h"
15 #include "chrome/app/client_util.h"
16 #include "chrome/browser/chrome_process_finder_win.h"
17 #include "chrome/browser/policy/policy_path_parser.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_paths_internal.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome_elf/chrome_elf_main.h"
22 #include "components/startup_metric_utils/startup_metric_utils.h"
23 #include "content/public/common/result_codes.h"
24 #include "ui/gfx/win/dpi.h"
26 namespace {
28 void CheckSafeModeLaunch() {
29 unsigned short k1 = ::GetAsyncKeyState(VK_CONTROL);
30 unsigned short k2 = ::GetAsyncKeyState(VK_MENU);
31 const unsigned short kPressedMask = 0x8000;
32 if ((k1 & kPressedMask) && (k2 & kPressedMask))
33 ::SetEnvironmentVariableA(chrome::kSafeModeEnvVar, "1");
36 // List of switches that it's safe to rendezvous early with. Fast start should
37 // not be done if a command line contains a switch not in this set.
38 // Note this is currently stored as a list of two because it's probably faster
39 // to iterate over this small array than building a map for constant time
40 // lookups.
41 const char* const kFastStartSwitches[] = {
42 switches::kProfileDirectory,
43 switches::kShowAppList,
46 bool IsFastStartSwitch(const std::string& command_line_switch) {
47 for (size_t i = 0; i < arraysize(kFastStartSwitches); ++i) {
48 if (command_line_switch == kFastStartSwitches[i])
49 return true;
51 return false;
54 bool ContainsNonFastStartFlag(const CommandLine& command_line) {
55 const CommandLine::SwitchMap& switches = command_line.GetSwitches();
56 if (switches.size() > arraysize(kFastStartSwitches))
57 return true;
58 for (CommandLine::SwitchMap::const_iterator it = switches.begin();
59 it != switches.end(); ++it) {
60 if (!IsFastStartSwitch(it->first))
61 return true;
63 return false;
66 bool AttemptFastNotify(const CommandLine& command_line) {
67 if (ContainsNonFastStartFlag(command_line))
68 return false;
70 base::FilePath user_data_dir;
71 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir))
72 return false;
73 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
75 HWND chrome = chrome::FindRunningChromeWindow(user_data_dir);
76 if (!chrome)
77 return false;
78 return chrome::AttemptToNotifyRunningChrome(chrome, true) ==
79 chrome::NOTIFY_SUCCESS;
82 // Duplicated from Win8.1 SDK ShellScalingApi.h
83 typedef enum PROCESS_DPI_AWARENESS {
84 PROCESS_DPI_UNAWARE = 0,
85 PROCESS_SYSTEM_DPI_AWARE = 1,
86 PROCESS_PER_MONITOR_DPI_AWARE = 2
87 } PROCESS_DPI_AWARENESS;
89 typedef enum MONITOR_DPI_TYPE {
90 MDT_EFFECTIVE_DPI = 0,
91 MDT_ANGULAR_DPI = 1,
92 MDT_RAW_DPI = 2,
93 MDT_DEFAULT = MDT_EFFECTIVE_DPI
94 } MONITOR_DPI_TYPE;
96 // Win8.1 supports monitor-specific DPI scaling.
97 bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
98 typedef HRESULT(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS);
99 SetProcessDpiAwarenessPtr set_process_dpi_awareness_func =
100 reinterpret_cast<SetProcessDpiAwarenessPtr>(
101 GetProcAddress(GetModuleHandleA("user32.dll"),
102 "SetProcessDpiAwarenessInternal"));
103 if (set_process_dpi_awareness_func) {
104 HRESULT hr = set_process_dpi_awareness_func(value);
105 if (SUCCEEDED(hr)) {
106 VLOG(1) << "SetProcessDpiAwareness succeeded.";
107 return true;
108 } else if (hr == E_ACCESSDENIED) {
109 LOG(ERROR) << "Access denied error from SetProcessDpiAwareness. "
110 "Function called twice, or manifest was used.";
113 return false;
116 // This function works for Windows Vista through Win8. Win8.1 must use
117 // SetProcessDpiAwareness[Wrapper]
118 BOOL SetProcessDPIAwareWrapper() {
119 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
120 SetProcessDPIAwarePtr set_process_dpi_aware_func =
121 reinterpret_cast<SetProcessDPIAwarePtr>(
122 GetProcAddress(GetModuleHandleA("user32.dll"),
123 "SetProcessDPIAware"));
124 return set_process_dpi_aware_func &&
125 set_process_dpi_aware_func();
129 void EnableHighDPISupport() {
130 if (!SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) {
131 SetProcessDPIAwareWrapper();
135 } // namespace
137 #if !defined(ADDRESS_SANITIZER)
138 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
139 #else
140 // The AddressSanitizer build should be a console program as it prints out stuff
141 // on stderr.
142 int main() {
143 HINSTANCE instance = GetModuleHandle(NULL);
144 #endif
145 startup_metric_utils::RecordExeMainEntryTime();
147 // Signal Chrome Elf that Chrome has begun to start.
148 SignalChromeElf();
150 // Initialize the commandline singleton from the environment.
151 CommandLine::Init(0, NULL);
152 // The exit manager is in charge of calling the dtors of singletons.
153 base::AtExitManager exit_manager;
155 // We don't want to set DPI awareness on Vista because we don't support
156 // DirectWrite there. GDI fonts are kerned very badly, so better to leave
157 // DPI-unaware and at effective 1.0. See also ShouldUseDirectWrite().
158 if (base::win::GetVersion() > base::win::VERSION_VISTA)
159 EnableHighDPISupport();
161 if (AttemptFastNotify(*CommandLine::ForCurrentProcess()))
162 return 0;
164 CheckSafeModeLaunch();
166 // Load and launch the chrome dll. *Everything* happens inside.
167 MainDllLoader* loader = MakeMainDllLoader();
168 int rc = loader->Launch(instance);
169 loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
170 delete loader;
171 return rc;