Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chrome_browser_main_win.cc
blob3ff1f1ef44d8540b7648239d081c6a98c92f4de2
1 // Copyright (c) 2012 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 "chrome/browser/chrome_browser_main_win.h"
7 #include <windows.h>
8 #include <shellapi.h>
10 #include <algorithm>
12 #include "base/command_line.h"
13 #include "base/environment.h"
14 #include "base/files/file_path.h"
15 #include "base/i18n/rtl.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/path_service.h"
18 #include "base/scoped_native_library.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/win/metro.h"
22 #include "base/win/windows_version.h"
23 #include "base/win/wrapped_window_proc.h"
24 #include "chrome/browser/browser_util_win.h"
25 #include "chrome/browser/chrome_elf_init_win.h"
26 #include "chrome/browser/first_run/first_run.h"
27 #include "chrome/browser/install_verification/win/install_verification.h"
28 #include "chrome/browser/profiles/profile_info_cache.h"
29 #include "chrome/browser/profiles/profile_shortcut_manager.h"
30 #include "chrome/browser/shell_integration.h"
31 #include "chrome/browser/ui/simple_message_box.h"
32 #include "chrome/browser/ui/uninstall_browser_prompt.h"
33 #include "chrome/common/chrome_constants.h"
34 #include "chrome/common/chrome_result_codes.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/chrome_version_info.h"
37 #include "chrome/common/env_vars.h"
38 #include "chrome/installer/launcher_support/chrome_launcher_support.h"
39 #include "chrome/installer/util/browser_distribution.h"
40 #include "chrome/installer/util/helper.h"
41 #include "chrome/installer/util/install_util.h"
42 #include "chrome/installer/util/l10n_string_util.h"
43 #include "chrome/installer/util/shell_util.h"
44 #include "content/public/browser/browser_thread.h"
45 #include "content/public/common/main_function_params.h"
46 #include "grit/app_locale_settings.h"
47 #include "grit/chromium_strings.h"
48 #include "grit/generated_resources.h"
49 #include "installer_util_strings/installer_util_strings.h"
50 #include "ui/base/cursor/cursor_loader_win.h"
51 #include "ui/base/l10n/l10n_util.h"
52 #include "ui/base/l10n/l10n_util_win.h"
53 #include "ui/base/ui_base_switches.h"
54 #include "ui/base/win/message_box_win.h"
55 #include "ui/gfx/platform_font_win.h"
56 #include "ui/gfx/switches.h"
58 namespace {
60 typedef HRESULT (STDAPICALLTYPE* RegisterApplicationRestartProc)(
61 const wchar_t* command_line,
62 DWORD flags);
64 void InitializeWindowProcExceptions() {
65 // Get the breakpad pointer from chrome.exe
66 base::win::WinProcExceptionFilter exception_filter =
67 reinterpret_cast<base::win::WinProcExceptionFilter>(
68 ::GetProcAddress(::GetModuleHandle(
69 chrome::kBrowserProcessExecutableName),
70 "CrashForException"));
71 exception_filter = base::win::SetWinProcExceptionFilter(exception_filter);
72 DCHECK(!exception_filter);
75 // gfx::Font callbacks
76 void AdjustUIFont(LOGFONT* logfont) {
77 l10n_util::AdjustUIFont(logfont);
80 int GetMinimumFontSize() {
81 int min_font_size;
82 base::StringToInt(l10n_util::GetStringUTF16(IDS_MINIMUM_UI_FONT_SIZE),
83 &min_font_size);
84 return min_font_size;
87 class TranslationDelegate : public installer::TranslationDelegate {
88 public:
89 virtual base::string16 GetLocalizedString(int installer_string_id) OVERRIDE;
92 bool IsSafeModeStart() {
93 return ::GetEnvironmentVariableA(chrome::kSafeModeEnvVar, NULL, 0) != 0;
96 } // namespace
98 void ShowCloseBrowserFirstMessageBox() {
99 int message_id = IDS_UNINSTALL_CLOSE_APP;
100 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
101 (ShellIntegration::GetDefaultBrowser() == ShellIntegration::IS_DEFAULT)) {
102 message_id = IDS_UNINSTALL_CLOSE_APP_IMMERSIVE;
104 chrome::ShowMessageBox(NULL,
105 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
106 l10n_util::GetStringUTF16(message_id),
107 chrome::MESSAGE_BOX_TYPE_WARNING);
110 int DoUninstallTasks(bool chrome_still_running) {
111 // We want to show a warning to user (and exit) if Chrome is already running
112 // *before* we show the uninstall confirmation dialog box. But while the
113 // uninstall confirmation dialog is up, user might start Chrome, so we
114 // check once again after user acknowledges Uninstall dialog.
115 if (chrome_still_running) {
116 ShowCloseBrowserFirstMessageBox();
117 return chrome::RESULT_CODE_UNINSTALL_CHROME_ALIVE;
119 int result = chrome::ShowUninstallBrowserPrompt(
120 !chrome_launcher_support::IsAppLauncherPresent());
121 // Don't offer to delete the profile if the App Launcher is also installed.
122 if (browser_util::IsBrowserAlreadyRunning()) {
123 ShowCloseBrowserFirstMessageBox();
124 return chrome::RESULT_CODE_UNINSTALL_CHROME_ALIVE;
127 if (result != chrome::RESULT_CODE_UNINSTALL_USER_CANCEL) {
128 // The following actions are just best effort.
129 // TODO(gab): Look into removing this code which is now redundant with the
130 // work done by setup.exe on uninstall.
131 VLOG(1) << "Executing uninstall actions";
132 base::FilePath chrome_exe;
133 if (PathService::Get(base::FILE_EXE, &chrome_exe)) {
134 ShellUtil::ShortcutLocation user_shortcut_locations[] = {
135 ShellUtil::SHORTCUT_LOCATION_DESKTOP,
136 ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH,
137 ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR,
138 ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR,
140 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
141 for (size_t i = 0; i < arraysize(user_shortcut_locations); ++i) {
142 if (!ShellUtil::RemoveShortcuts(user_shortcut_locations[i], dist,
143 ShellUtil::CURRENT_USER, chrome_exe)) {
144 VLOG(1) << "Failed to delete shortcut at location "
145 << user_shortcut_locations[i];
148 } else {
149 NOTREACHED();
152 return result;
155 void MaybeEnableHighResolutionTimeEverywhere() {
156 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
157 bool user_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
158 switches::kEnableHighResolutionTime);
159 if (user_enabled || channel == chrome::VersionInfo::CHANNEL_CANARY) {
160 bool is_enabled = base::TimeTicks::SetNowIsHighResNowIfSupported();
161 if (is_enabled && !user_enabled) {
162 // Ensure that all of the renderers will enable it too.
163 CommandLine::ForCurrentProcess()->AppendSwitch(
164 switches::kEnableHighResolutionTime);
169 // ChromeBrowserMainPartsWin ---------------------------------------------------
171 ChromeBrowserMainPartsWin::ChromeBrowserMainPartsWin(
172 const content::MainFunctionParams& parameters)
173 : ChromeBrowserMainParts(parameters) {
174 MaybeEnableHighResolutionTimeEverywhere();
175 if (base::win::IsMetroProcess()) {
176 typedef const wchar_t* (*GetMetroSwitches)(void);
177 GetMetroSwitches metro_switches_proc = reinterpret_cast<GetMetroSwitches>(
178 GetProcAddress(base::win::GetMetroModule(),
179 "GetMetroCommandLineSwitches"));
180 if (metro_switches_proc) {
181 base::string16 metro_switches = (*metro_switches_proc)();
182 if (!metro_switches.empty()) {
183 CommandLine extra_switches(CommandLine::NO_PROGRAM);
184 extra_switches.ParseFromString(metro_switches);
185 CommandLine::ForCurrentProcess()->AppendArguments(extra_switches,
186 false);
192 ChromeBrowserMainPartsWin::~ChromeBrowserMainPartsWin() {
195 void ChromeBrowserMainPartsWin::ToolkitInitialized() {
196 ChromeBrowserMainParts::ToolkitInitialized();
197 gfx::PlatformFontWin::adjust_font_callback = &AdjustUIFont;
198 gfx::PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
199 #if defined(USE_AURA)
200 ui::CursorLoaderWin::SetCursorResourceModule(chrome::kBrowserResourcesDll);
201 #endif
204 void ChromeBrowserMainPartsWin::PreMainMessageLoopStart() {
205 // installer_util references strings that are normally compiled into
206 // setup.exe. In Chrome, these strings are in the locale files.
207 SetupInstallerUtilStrings();
209 ChromeBrowserMainParts::PreMainMessageLoopStart();
210 if (!parameters().ui_task) {
211 // Make sure that we know how to handle exceptions from the message loop.
212 InitializeWindowProcExceptions();
216 int ChromeBrowserMainPartsWin::PreCreateThreads() {
217 int rv = ChromeBrowserMainParts::PreCreateThreads();
219 if (IsSafeModeStart()) {
220 // TODO(cpu): disable other troublesome features for safe mode.
221 CommandLine::ForCurrentProcess()->AppendSwitch(
222 switches::kDisableGpu);
223 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
224 switches::kHighDPISupport, "0");
226 // TODO(viettrungluu): why don't we run this earlier?
227 if (!parsed_command_line().HasSwitch(switches::kNoErrorDialogs) &&
228 base::win::GetVersion() < base::win::VERSION_XP) {
229 chrome::ShowMessageBox(NULL,
230 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
231 l10n_util::GetStringUTF16(IDS_UNSUPPORTED_OS_PRE_WIN_XP),
232 chrome::MESSAGE_BOX_TYPE_WARNING);
235 return rv;
238 void ChromeBrowserMainPartsWin::ShowMissingLocaleMessageBox() {
239 ui::MessageBox(NULL,
240 base::ASCIIToUTF16(chrome_browser::kMissingLocaleDataMessage),
241 base::ASCIIToUTF16(chrome_browser::kMissingLocaleDataTitle),
242 MB_OK | MB_ICONERROR | MB_TOPMOST);
245 void ChromeBrowserMainPartsWin::PostBrowserStart() {
246 ChromeBrowserMainParts::PostBrowserStart();
248 // Set up a task to verify installed modules in the current process. Use a
249 // delay to reduce the impact on startup time.
250 content::BrowserThread::GetMessageLoopProxyForThread(
251 content::BrowserThread::UI)->PostDelayedTask(
252 FROM_HERE,
253 base::Bind(&VerifyInstallation),
254 base::TimeDelta::FromSeconds(45));
256 InitializeChromeElf();
259 // static
260 void ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment(
261 const CommandLine& parsed_command_line) {
262 // Clear this var so child processes don't show the dialog by default.
263 scoped_ptr<base::Environment> env(base::Environment::Create());
264 env->UnSetVar(env_vars::kShowRestart);
266 // For non-interactive tests we don't restart on crash.
267 if (env->HasVar(env_vars::kHeadless))
268 return;
270 // If the known command-line test options are used we don't create the
271 // environment block which means we don't get the restart dialog.
272 if (parsed_command_line.HasSwitch(switches::kBrowserCrashTest) ||
273 parsed_command_line.HasSwitch(switches::kBrowserAssertTest) ||
274 parsed_command_line.HasSwitch(switches::kNoErrorDialogs))
275 return;
277 // The encoding we use for the info is "title|context|direction" where
278 // direction is either env_vars::kRtlLocale or env_vars::kLtrLocale depending
279 // on the current locale.
280 base::string16 dlg_strings(l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE));
281 dlg_strings.push_back('|');
282 base::string16 adjusted_string(
283 l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_CONTENT));
284 base::i18n::AdjustStringForLocaleDirection(&adjusted_string);
285 dlg_strings.append(adjusted_string);
286 dlg_strings.push_back('|');
287 dlg_strings.append(base::ASCIIToUTF16(
288 base::i18n::IsRTL() ? env_vars::kRtlLocale : env_vars::kLtrLocale));
290 env->SetVar(env_vars::kRestartInfo, base::UTF16ToUTF8(dlg_strings));
293 // static
294 void ChromeBrowserMainPartsWin::RegisterApplicationRestart(
295 const CommandLine& parsed_command_line) {
296 DCHECK(base::win::GetVersion() >= base::win::VERSION_VISTA);
297 base::ScopedNativeLibrary library(base::FilePath(L"kernel32.dll"));
298 // Get the function pointer for RegisterApplicationRestart.
299 RegisterApplicationRestartProc register_application_restart =
300 reinterpret_cast<RegisterApplicationRestartProc>(
301 library.GetFunctionPointer("RegisterApplicationRestart"));
302 if (!register_application_restart) {
303 LOG(WARNING) << "Cannot find RegisterApplicationRestart in kernel32.dll";
304 return;
306 // The Windows Restart Manager expects a string of command line flags only,
307 // without the program.
308 CommandLine command_line(CommandLine::NO_PROGRAM);
309 command_line.AppendArguments(parsed_command_line, false);
310 if (!command_line.HasSwitch(switches::kRestoreLastSession))
311 command_line.AppendSwitch(switches::kRestoreLastSession);
313 // Restart Chrome if the computer is restarted as the result of an update.
314 // This could be extended to handle crashes, hangs, and patches.
315 HRESULT hr = register_application_restart(
316 command_line.GetCommandLineString().c_str(),
317 RESTART_NO_CRASH | RESTART_NO_HANG | RESTART_NO_PATCH);
318 if (FAILED(hr)) {
319 if (hr == E_INVALIDARG) {
320 LOG(WARNING) << "Command line too long for RegisterApplicationRestart";
321 } else {
322 NOTREACHED() << "RegisterApplicationRestart failed. hr: " << hr <<
323 ", command_line: " << command_line.GetCommandLineString();
328 // static
329 int ChromeBrowserMainPartsWin::HandleIconsCommands(
330 const CommandLine& parsed_command_line) {
331 if (parsed_command_line.HasSwitch(switches::kHideIcons)) {
332 base::string16 cp_applet;
333 base::win::Version version = base::win::GetVersion();
334 if (version >= base::win::VERSION_VISTA) {
335 cp_applet.assign(L"Programs and Features"); // Windows Vista and later.
336 } else if (version >= base::win::VERSION_XP) {
337 cp_applet.assign(L"Add/Remove Programs"); // Windows XP.
338 } else {
339 return chrome::RESULT_CODE_UNSUPPORTED_PARAM; // Not supported
342 const base::string16 msg =
343 l10n_util::GetStringFUTF16(IDS_HIDE_ICONS_NOT_SUPPORTED, cp_applet);
344 const base::string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
345 const UINT flags = MB_OKCANCEL | MB_ICONWARNING | MB_TOPMOST;
346 if (IDOK == ui::MessageBox(NULL, msg, caption, flags))
347 ShellExecute(NULL, NULL, L"appwiz.cpl", NULL, NULL, SW_SHOWNORMAL);
349 // Exit as we are not launching the browser.
350 return content::RESULT_CODE_NORMAL_EXIT;
352 // We don't hide icons so we shouldn't do anything special to show them
353 return chrome::RESULT_CODE_UNSUPPORTED_PARAM;
356 // static
357 bool ChromeBrowserMainPartsWin::CheckMachineLevelInstall() {
358 // TODO(tommi): Check if using the default distribution is always the right
359 // thing to do.
360 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
361 Version version;
362 InstallUtil::GetChromeVersion(dist, true, &version);
363 if (version.IsValid()) {
364 base::FilePath exe_path;
365 PathService::Get(base::DIR_EXE, &exe_path);
366 std::wstring exe = exe_path.value();
367 base::FilePath user_exe_path(installer::GetChromeInstallPath(false, dist));
368 if (base::FilePath::CompareEqualIgnoreCase(exe, user_exe_path.value())) {
369 bool is_metro = base::win::IsMetroProcess();
370 if (!is_metro) {
371 // The dialog cannot be shown in Win8 Metro as doing so hangs Chrome on
372 // an invisible dialog.
373 // TODO (gab): Get rid of this dialog altogether and auto-launch
374 // system-level Chrome instead.
375 const base::string16 text =
376 l10n_util::GetStringUTF16(IDS_MACHINE_LEVEL_INSTALL_CONFLICT);
377 const base::string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
378 const UINT flags = MB_OK | MB_ICONERROR | MB_TOPMOST;
379 ui::MessageBox(NULL, text, caption, flags);
381 CommandLine uninstall_cmd(
382 InstallUtil::GetChromeUninstallCmd(false, dist->GetType()));
383 if (!uninstall_cmd.GetProgram().empty()) {
384 uninstall_cmd.AppendSwitch(installer::switches::kSelfDestruct);
385 uninstall_cmd.AppendSwitch(installer::switches::kForceUninstall);
386 uninstall_cmd.AppendSwitch(
387 installer::switches::kDoNotRemoveSharedItems);
389 // Trigger Active Setup for the system-level Chrome to make sure
390 // per-user shortcuts to the system-level Chrome are created. Skip this
391 // if the system-level Chrome will undergo first run anyway, as Active
392 // Setup is triggered on system-level Chrome's first run.
393 // TODO(gab): Instead of having callers of Active Setup think about
394 // other callers, have Active Setup itself register when it ran and
395 // no-op otherwise (http://crbug.com/346843).
396 if (!first_run::IsChromeFirstRun())
397 uninstall_cmd.AppendSwitch(installer::switches::kTriggerActiveSetup);
399 const base::FilePath setup_exe(uninstall_cmd.GetProgram());
400 const base::string16 params(uninstall_cmd.GetArgumentsString());
402 SHELLEXECUTEINFO sei = { sizeof(sei) };
403 sei.fMask = SEE_MASK_NOASYNC;
404 sei.nShow = SW_SHOWNORMAL;
405 sei.lpFile = setup_exe.value().c_str();
406 sei.lpParameters = params.c_str();
407 // On Windows 8 SEE_MASK_FLAG_LOG_USAGE is necessary to guarantee we
408 // flip to the Desktop when launching.
409 if (is_metro)
410 sei.fMask |= SEE_MASK_FLAG_LOG_USAGE;
412 if (!::ShellExecuteEx(&sei))
413 DPCHECK(false);
415 return true;
418 return false;
421 base::string16 TranslationDelegate::GetLocalizedString(
422 int installer_string_id) {
423 int resource_id = 0;
424 switch (installer_string_id) {
425 // HANDLE_STRING is used by the DO_INSTALLER_STRING_MAPPING macro which is in
426 // the generated header installer_util_strings.h.
427 #define HANDLE_STRING(base_id, chrome_id) \
428 case base_id: \
429 resource_id = chrome_id; \
430 break;
431 DO_INSTALLER_STRING_MAPPING
432 #undef HANDLE_STRING
433 default:
434 NOTREACHED();
436 if (resource_id)
437 return l10n_util::GetStringUTF16(resource_id);
438 return base::string16();
441 // static
442 void ChromeBrowserMainPartsWin::SetupInstallerUtilStrings() {
443 CR_DEFINE_STATIC_LOCAL(TranslationDelegate, delegate, ());
444 installer::SetTranslationDelegate(&delegate);