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.
7 #include <shellscalingapi.h>
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/time/time.h"
17 #include "base/win/windows_version.h"
18 #include "chrome/app/client_util.h"
19 #include "chrome/browser/chrome_process_finder_win.h"
20 #include "chrome/browser/policy/policy_path_parser.h"
21 #include "chrome/common/chrome_paths_internal.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome_elf/chrome_elf_main.h"
24 #include "components/startup_metric_utils/startup_metric_utils.h"
25 #include "content/public/common/result_codes.h"
26 #include "ui/gfx/win/dpi.h"
29 // List of switches that it's safe to rendezvous early with. Fast start should
30 // not be done if a command line contains a switch not in this set.
31 // Note this is currently stored as a list of two because it's probably faster
32 // to iterate over this small array than building a map for constant time
34 const char* const kFastStartSwitches
[] = {
35 switches::kProfileDirectory
,
36 switches::kShowAppList
,
39 bool IsFastStartSwitch(const std::string
& command_line_switch
) {
40 for (size_t i
= 0; i
< arraysize(kFastStartSwitches
); ++i
) {
41 if (command_line_switch
== kFastStartSwitches
[i
])
47 bool ContainsNonFastStartFlag(const base::CommandLine
& command_line
) {
48 const base::CommandLine::SwitchMap
& switches
= command_line
.GetSwitches();
49 if (switches
.size() > arraysize(kFastStartSwitches
))
51 for (base::CommandLine::SwitchMap::const_iterator it
= switches
.begin();
52 it
!= switches
.end(); ++it
) {
53 if (!IsFastStartSwitch(it
->first
))
59 bool AttemptFastNotify(const base::CommandLine
& command_line
) {
60 if (ContainsNonFastStartFlag(command_line
))
63 base::FilePath user_data_dir
;
64 if (!chrome::GetDefaultUserDataDirectory(&user_data_dir
))
66 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir
);
68 HWND chrome
= chrome::FindRunningChromeWindow(user_data_dir
);
71 return chrome::AttemptToNotifyRunningChrome(chrome
, true) ==
72 chrome::NOTIFY_SUCCESS
;
75 // Win8.1 supports monitor-specific DPI scaling.
76 bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value
) {
77 typedef HRESULT(WINAPI
*SetProcessDpiAwarenessPtr
)(PROCESS_DPI_AWARENESS
);
78 SetProcessDpiAwarenessPtr set_process_dpi_awareness_func
=
79 reinterpret_cast<SetProcessDpiAwarenessPtr
>(
80 GetProcAddress(GetModuleHandleA("user32.dll"),
81 "SetProcessDpiAwarenessInternal"));
82 if (set_process_dpi_awareness_func
) {
83 HRESULT hr
= set_process_dpi_awareness_func(value
);
85 VLOG(1) << "SetProcessDpiAwareness succeeded.";
87 } else if (hr
== E_ACCESSDENIED
) {
88 LOG(ERROR
) << "Access denied error from SetProcessDpiAwareness. "
89 "Function called twice, or manifest was used.";
95 // This function works for Windows Vista through Win8. Win8.1 must use
96 // SetProcessDpiAwareness[Wrapper].
97 BOOL
SetProcessDPIAwareWrapper() {
98 typedef BOOL(WINAPI
*SetProcessDPIAwarePtr
)(VOID
);
99 SetProcessDPIAwarePtr set_process_dpi_aware_func
=
100 reinterpret_cast<SetProcessDPIAwarePtr
>(
101 GetProcAddress(GetModuleHandleA("user32.dll"),
102 "SetProcessDPIAware"));
103 return set_process_dpi_aware_func
&&
104 set_process_dpi_aware_func();
107 void EnableHighDPISupport() {
108 if (!SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE
)) {
109 SetProcessDPIAwareWrapper();
113 void SwitchToLFHeap() {
114 // Only needed on XP but harmless on other Windows flavors.
115 auto crt_heap
= _get_heap_handle();
116 ULONG enable_LFH
= 2;
117 if (HeapSetInformation(reinterpret_cast<HANDLE
>(crt_heap
),
118 HeapCompatibilityInformation
,
119 &enable_LFH
, sizeof(enable_LFH
))) {
120 VLOG(1) << "Low fragmentation heap enabled.";
126 #if !defined(ADDRESS_SANITIZER)
127 int APIENTRY
wWinMain(HINSTANCE instance
, HINSTANCE prev
, wchar_t*, int) {
129 // The AddressSanitizer build should be a console program as it prints out stuff
132 HINSTANCE instance
= GetModuleHandle(NULL
);
136 startup_metric_utils::RecordExeMainEntryPointTime(base::Time::Now());
138 // Signal Chrome Elf that Chrome has begun to start.
141 // Initialize the commandline singleton from the environment.
142 base::CommandLine::Init(0, NULL
);
143 // The exit manager is in charge of calling the dtors of singletons.
144 base::AtExitManager exit_manager
;
146 // We don't want to set DPI awareness on pre-Win7 because we don't support
147 // DirectWrite there. GDI fonts are kerned very badly, so better to leave
148 // DPI-unaware and at effective 1.0. See also ShouldUseDirectWrite().
149 if (base::win::GetVersion() >= base::win::VERSION_WIN7
)
150 EnableHighDPISupport();
152 if (AttemptFastNotify(*base::CommandLine::ForCurrentProcess()))
155 // Load and launch the chrome dll. *Everything* happens inside.
156 VLOG(1) << "About to load main DLL.";
157 MainDllLoader
* loader
= MakeMainDllLoader();
158 int rc
= loader
->Launch(instance
);
159 loader
->RelaunchChromeBrowserWithNewCommandLineIfNeeded();