mandoline: Allow running the aura/views UI on android.
[chromium-blink-merge.git] / chrome / app / chrome_exe_main_win.cc
blob1ef4082306931f59e57cc7da9fcaec418013d911
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 <malloc.h>
7 #include <shellscalingapi.h>
8 #include <tchar.h>
10 #include <string>
12 #include "base/at_exit.h"
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/win/windows_version.h"
17 #include "chrome/app/client_util.h"
18 #include "chrome/browser/chrome_process_finder_win.h"
19 #include "chrome/browser/policy/policy_path_parser.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/startup_metric_utils/startup_metric_utils.h"
24 #include "content/public/common/result_codes.h"
25 #include "ui/gfx/win/dpi.h"
27 namespace {
28 // List of switches that it's safe to rendezvous early with. Fast start should
29 // not be done if a command line contains a switch not in this set.
30 // Note this is currently stored as a list of two because it's probably faster
31 // to iterate over this small array than building a map for constant time
32 // lookups.
33 const char* const kFastStartSwitches[] = {
34 switches::kProfileDirectory,
35 switches::kShowAppList,
38 bool IsFastStartSwitch(const std::string& command_line_switch) {
39 for (size_t i = 0; i < arraysize(kFastStartSwitches); ++i) {
40 if (command_line_switch == kFastStartSwitches[i])
41 return true;
43 return false;
46 bool ContainsNonFastStartFlag(const base::CommandLine& command_line) {
47 const base::CommandLine::SwitchMap& switches = command_line.GetSwitches();
48 if (switches.size() > arraysize(kFastStartSwitches))
49 return true;
50 for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
51 it != switches.end(); ++it) {
52 if (!IsFastStartSwitch(it->first))
53 return true;
55 return false;
58 bool AttemptFastNotify(const base::CommandLine& command_line) {
59 if (ContainsNonFastStartFlag(command_line))
60 return false;
62 base::FilePath user_data_dir;
63 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir))
64 return false;
65 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
67 HWND chrome = chrome::FindRunningChromeWindow(user_data_dir);
68 if (!chrome)
69 return false;
70 return chrome::AttemptToNotifyRunningChrome(chrome, true) ==
71 chrome::NOTIFY_SUCCESS;
74 // Win8.1 supports monitor-specific DPI scaling.
75 bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
76 typedef HRESULT(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS);
77 SetProcessDpiAwarenessPtr set_process_dpi_awareness_func =
78 reinterpret_cast<SetProcessDpiAwarenessPtr>(
79 GetProcAddress(GetModuleHandleA("user32.dll"),
80 "SetProcessDpiAwarenessInternal"));
81 if (set_process_dpi_awareness_func) {
82 HRESULT hr = set_process_dpi_awareness_func(value);
83 if (SUCCEEDED(hr)) {
84 VLOG(1) << "SetProcessDpiAwareness succeeded.";
85 return true;
86 } else if (hr == E_ACCESSDENIED) {
87 LOG(ERROR) << "Access denied error from SetProcessDpiAwareness. "
88 "Function called twice, or manifest was used.";
91 return false;
94 // This function works for Windows Vista through Win8. Win8.1 must use
95 // SetProcessDpiAwareness[Wrapper].
96 BOOL SetProcessDPIAwareWrapper() {
97 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
98 SetProcessDPIAwarePtr set_process_dpi_aware_func =
99 reinterpret_cast<SetProcessDPIAwarePtr>(
100 GetProcAddress(GetModuleHandleA("user32.dll"),
101 "SetProcessDPIAware"));
102 return set_process_dpi_aware_func &&
103 set_process_dpi_aware_func();
106 void EnableHighDPISupport() {
107 if (!SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) {
108 SetProcessDPIAwareWrapper();
112 void SwitchToLFHeap() {
113 // Only needed on XP but harmless on other Windows flavors.
114 auto crt_heap = _get_heap_handle();
115 ULONG enable_LFH = 2;
116 if (HeapSetInformation(reinterpret_cast<HANDLE>(crt_heap),
117 HeapCompatibilityInformation,
118 &enable_LFH, sizeof(enable_LFH))) {
119 VLOG(1) << "Low fragmentation heap enabled.";
123 } // namespace
125 #if !defined(ADDRESS_SANITIZER)
126 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
127 #else
128 // The AddressSanitizer build should be a console program as it prints out stuff
129 // on stderr.
130 int main() {
131 HINSTANCE instance = GetModuleHandle(NULL);
132 #endif
133 SwitchToLFHeap();
135 startup_metric_utils::RecordExeMainEntryTime();
137 // Signal Chrome Elf that Chrome has begun to start.
138 SignalChromeElf();
140 // Initialize the commandline singleton from the environment.
141 base::CommandLine::Init(0, NULL);
142 // The exit manager is in charge of calling the dtors of singletons.
143 base::AtExitManager exit_manager;
145 // We don't want to set DPI awareness on pre-Win7 because we don't support
146 // DirectWrite there. GDI fonts are kerned very badly, so better to leave
147 // DPI-unaware and at effective 1.0. See also ShouldUseDirectWrite().
148 if (base::win::GetVersion() >= base::win::VERSION_WIN7)
149 EnableHighDPISupport();
151 if (AttemptFastNotify(*base::CommandLine::ForCurrentProcess()))
152 return 0;
154 // Load and launch the chrome dll. *Everything* happens inside.
155 VLOG(1) << "About to load main DLL.";
156 MainDllLoader* loader = MakeMainDllLoader();
157 int rc = loader->Launch(instance);
158 loader->RelaunchChromeBrowserWithNewCommandLineIfNeeded();
159 delete loader;
160 return rc;