blob: 856225259bd2563f4716cc2a6a5b6baecbf5e06d [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]5c9587c2008-12-09 21:20:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1152b7e2009-09-14 03:26:035#include "chrome/browser/browser_main.h"
[email protected]5c9587c2008-12-09 21:20:166#include "chrome/browser/browser_main_win.h"
7
[email protected]71b73f02011-04-06 15:57:298#include <windows.h>
[email protected]e0785902011-05-19 23:34:179#include <shellapi.h>
[email protected]4a0765a2009-05-08 23:12:2510
[email protected]c83dd912010-04-06 18:50:5111#include <algorithm>
12
[email protected]5c9587c2008-12-09 21:20:1613#include "base/command_line.h"
[email protected]ae0f0772010-08-13 04:54:1014#include "base/environment.h"
[email protected]7cf1b6ce2010-03-20 06:37:0115#include "base/i18n/rtl.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]5c9587c2008-12-09 21:20:1617#include "base/path_service.h"
[email protected]e0785902011-05-19 23:34:1718#include "base/scoped_native_library.h"
[email protected]fd59f822011-05-12 18:07:1819#include "base/time.h"
[email protected]be1ce6a72010-08-03 14:35:2220#include "base/utf_string_conversions.h"
[email protected]935aa542010-10-15 01:59:1521#include "base/win/windows_version.h"
[email protected]ecb924c2011-03-17 00:34:0922#include "base/win/wrapped_window_proc.h"
[email protected]a3abd5572011-04-15 02:09:3323#include "chrome/browser/browser_util_win.h"
[email protected]820735792010-07-29 23:40:0124#include "chrome/browser/first_run/first_run.h"
[email protected]cd1adc22009-01-16 01:29:2225#include "chrome/browser/metrics/metrics_service.h"
[email protected]71b73f02011-04-06 15:57:2926#include "chrome/browser/ui/browser_list.h"
[email protected]9e790bd2011-01-10 23:48:5427#include "chrome/browser/ui/views/uninstall_view.h"
[email protected]ecb924c2011-03-17 00:34:0928#include "chrome/common/chrome_constants.h"
[email protected]5c9587c2008-12-09 21:20:1629#include "chrome/common/chrome_switches.h"
30#include "chrome/common/env_vars.h"
[email protected]bf6117c7e2010-12-01 06:00:2531#include "chrome/installer/util/browser_distribution.h"
[email protected]fd59f822011-05-12 18:07:1832#include "chrome/installer/util/google_update_settings.h"
[email protected]5c9587c2008-12-09 21:20:1633#include "chrome/installer/util/helper.h"
34#include "chrome/installer/util/install_util.h"
35#include "chrome/installer/util/shell_util.h"
[email protected]415c2cd2011-03-11 21:56:1136#include "content/common/main_function_params.h"
[email protected]4dd57932011-03-17 06:06:1237#include "content/common/result_codes.h"
[email protected]e0785902011-05-19 23:34:1738#include "crypto/nss_util.h"
[email protected]34ac8f32009-02-22 23:03:2739#include "grit/chromium_strings.h"
40#include "grit/generated_resources.h"
[email protected]f8abf722010-07-07 19:46:2441#include "net/base/winsock_init.h"
[email protected]2380f372011-02-23 21:35:1942#include "net/socket/client_socket_factory.h"
[email protected]c051a1b2011-01-21 23:30:1743#include "ui/base/l10n/l10n_util.h"
44#include "ui/base/l10n/l10n_util_win.h"
[email protected]75b68052011-02-03 06:01:1645#include "ui/base/message_box_win.h"
[email protected]148d1052009-07-31 22:53:3746#include "views/focus/accelerator_handler.h"
[email protected]2362e4f2009-05-08 00:34:0547#include "views/window/window.h"
[email protected]5c9587c2008-12-09 21:20:1648
[email protected]0fd23af2011-02-20 06:33:0449namespace {
[email protected]ecb924c2011-03-17 00:34:0950
[email protected]a08ba822011-02-20 07:45:4751typedef HRESULT (STDAPICALLTYPE* RegisterApplicationRestartProc)(
[email protected]0fd23af2011-02-20 06:33:0452 const wchar_t* command_line,
53 DWORD flags);
[email protected]ecb924c2011-03-17 00:34:0954
55void InitializeWindowProcExceptions() {
56 // Get the breakpad pointer from chrome.exe
57 base::win::WinProcExceptionFilter exception_filter =
58 reinterpret_cast<base::win::WinProcExceptionFilter>(
59 ::GetProcAddress(::GetModuleHandle(
60 chrome::kBrowserProcessExecutableName),
61 "CrashForException"));
62 exception_filter = base::win::SetWinProcExceptionFilter(exception_filter);
63 DCHECK(!exception_filter);
64}
65
[email protected]fd59f822011-05-12 18:07:1866// BrowserUsageUpdater --------------------------------------------------------
67// This class' job is to update the registry 'dr' value every 24 hours
68// that way google update can accurately track browser usage without
69// undercounting users that do not close chrome for long periods of time.
70class BrowserUsageUpdater : public Task {
71 public:
72 virtual ~BrowserUsageUpdater() {}
73
74 virtual void Run() OVERRIDE {
75 if (UpdateUsageRegKey())
76 Track();
77 }
78
79 static void Track() {
80 BrowserThread::PostDelayedTask(
81 BrowserThread::FILE,
82 FROM_HERE, new BrowserUsageUpdater,
83 base::TimeDelta::FromHours(24).InMillisecondsRoundedUp());
84 }
85
86 private:
87 bool UpdateUsageRegKey() {
88 FilePath module_dir;
89 if (!PathService::Get(base::DIR_MODULE, &module_dir))
90 return false;
91 bool system_level =
92 !InstallUtil::IsPerUserInstall(module_dir.value().c_str());
93 return GoogleUpdateSettings::UpdateDidRunState(true, system_level);
94 }
95};
96
[email protected]0fd23af2011-02-20 06:33:0497} // namespace
98
[email protected]3b6aa8b62009-09-15 21:36:1199void DidEndMainMessageLoop() {
[email protected]b443cb042009-12-15 22:05:09100 OleUninitialize();
[email protected]1152b7e2009-09-14 03:26:03101}
102
103void RecordBreakpadStatusUMA(MetricsService* metrics) {
[email protected]c83dd912010-04-06 18:50:51104 DWORD len = ::GetEnvironmentVariableW(
105 ASCIIToWide(env_vars::kNoOOBreakpad).c_str() , NULL, 0);
[email protected]1152b7e2009-09-14 03:26:03106 metrics->RecordBreakpadRegistration((len == 0));
107 metrics->RecordBreakpadHasDebugger(TRUE == ::IsDebuggerPresent());
108}
109
[email protected]34f73fb2010-03-24 20:50:34110void WarnAboutMinimumSystemRequirements() {
[email protected]935aa542010-10-15 01:59:15111 if (base::win::GetVersion() < base::win::VERSION_XP) {
[email protected]34f73fb2010-03-24 20:50:34112 // Display a warning message if the user is running chrome on Windows 2000.
[email protected]0f26d7b2011-01-05 19:10:44113 const string16 text =
114 l10n_util::GetStringUTF16(IDS_UNSUPPORTED_OS_PRE_WIN_XP);
115 const string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]75b68052011-02-03 06:01:16116 ui::MessageBox(NULL, text, caption, MB_OK | MB_ICONWARNING | MB_TOPMOST);
[email protected]5c9587c2008-12-09 21:20:16117 }
[email protected]5c9587c2008-12-09 21:20:16118}
119
[email protected]45d72762011-04-15 18:58:20120void RecordBrowserStartupTime() {
121 // Calculate the time that has elapsed from our own process creation.
122 FILETIME creation_time = {};
123 FILETIME ignore = {};
124 ::GetProcessTimes(::GetCurrentProcess(), &creation_time, &ignore, &ignore,
125 &ignore);
126
127 base::TimeDelta elapsed_from_startup =
128 base::Time::Now() - base::Time::FromFileTime(creation_time);
129
130 // Record the time to present in a histogram.
131 UMA_HISTOGRAM_MEDIUM_TIMES("Startup.BrowserMessageLoopStartTime",
132 elapsed_from_startup);
133}
134
[email protected]4df8786f2009-04-17 13:24:57135int AskForUninstallConfirmation() {
136 int ret = ResultCodes::NORMAL_EXIT;
[email protected]a173cc92009-08-20 23:26:37137 views::Window::CreateChromeWindow(NULL, gfx::Rect(),
138 new UninstallView(ret))->Show();
[email protected]047f6222009-07-29 23:34:56139 views::AcceleratorHandler accelerator_handler;
140 MessageLoopForUI::current()->Run(&accelerator_handler);
[email protected]4df8786f2009-04-17 13:24:57141 return ret;
[email protected]5c9587c2008-12-09 21:20:16142}
143
[email protected]53c38d232009-02-13 20:52:18144void ShowCloseBrowserFirstMessageBox() {
[email protected]0f26d7b2011-01-05 19:10:44145 const string16 text = l10n_util::GetStringUTF16(IDS_UNINSTALL_CLOSE_APP);
146 const string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]53c38d232009-02-13 20:52:18147 const UINT flags = MB_OK | MB_ICONWARNING | MB_TOPMOST;
[email protected]75b68052011-02-03 06:01:16148 ui::MessageBox(NULL, text, caption, flags);
[email protected]53c38d232009-02-13 20:52:18149}
150
151int DoUninstallTasks(bool chrome_still_running) {
[email protected]a173cc92009-08-20 23:26:37152 // We want to show a warning to user (and exit) if Chrome is already running
153 // *before* we show the uninstall confirmation dialog box. But while the
154 // uninstall confirmation dialog is up, user might start Chrome, so we
155 // check once again after user acknowledges Uninstall dialog.
[email protected]53c38d232009-02-13 20:52:18156 if (chrome_still_running) {
157 ShowCloseBrowserFirstMessageBox();
158 return ResultCodes::UNINSTALL_CHROME_ALIVE;
159 }
[email protected]4df8786f2009-04-17 13:24:57160 int ret = AskForUninstallConfirmation();
[email protected]a3abd5572011-04-15 02:09:33161 if (browser_util::IsBrowserAlreadyRunning()) {
[email protected]a173cc92009-08-20 23:26:37162 ShowCloseBrowserFirstMessageBox();
163 return ResultCodes::UNINSTALL_CHROME_ALIVE;
164 }
165
[email protected]4df8786f2009-04-17 13:24:57166 if (ret != ResultCodes::UNINSTALL_USER_CANCEL) {
167 // The following actions are just best effort.
[email protected]8e96e502010-10-21 20:57:12168 VLOG(1) << "Executing uninstall actions";
[email protected]4df8786f2009-04-17 13:24:57169 if (!FirstRun::RemoveSentinel())
[email protected]8e96e502010-10-21 20:57:12170 VLOG(1) << "Failed to delete sentinel file.";
[email protected]4df8786f2009-04-17 13:24:57171 // We want to remove user level shortcuts and we only care about the ones
172 // created by us and not by the installer so |alternate| is false.
[email protected]bf6117c7e2010-12-01 06:00:25173 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
174 if (!ShellUtil::RemoveChromeDesktopShortcut(dist, ShellUtil::CURRENT_USER,
175 false))
[email protected]8e96e502010-10-21 20:57:12176 VLOG(1) << "Failed to delete desktop shortcut.";
[email protected]bf6117c7e2010-12-01 06:00:25177 if (!ShellUtil::RemoveChromeQuickLaunchShortcut(dist,
178 ShellUtil::CURRENT_USER))
[email protected]8e96e502010-10-21 20:57:12179 VLOG(1) << "Failed to delete quick launch shortcut.";
[email protected]4df8786f2009-04-17 13:24:57180 }
[email protected]5c9587c2008-12-09 21:20:16181 return ret;
182}
183
184// Prepares the localized strings that are going to be displayed to
185// the user if the browser process dies. These strings are stored in the
186// environment block so they are accessible in the early stages of the
187// chrome executable's lifetime.
[email protected]0fd23af2011-02-20 06:33:04188void PrepareRestartOnCrashEnviroment(const CommandLine& parsed_command_line) {
[email protected]5c9587c2008-12-09 21:20:16189 // Clear this var so child processes don't show the dialog by default.
[email protected]ae0f0772010-08-13 04:54:10190 scoped_ptr<base::Environment> env(base::Environment::Create());
191 env->UnSetVar(env_vars::kShowRestart);
[email protected]5c9587c2008-12-09 21:20:16192
193 // For non-interactive tests we don't restart on crash.
[email protected]ae0f0772010-08-13 04:54:10194 if (env->HasVar(env_vars::kHeadless))
[email protected]5c9587c2008-12-09 21:20:16195 return;
196
197 // If the known command-line test options are used we don't create the
198 // environment block which means we don't get the restart dialog.
199 if (parsed_command_line.HasSwitch(switches::kBrowserCrashTest) ||
200 parsed_command_line.HasSwitch(switches::kBrowserAssertTest) ||
201 parsed_command_line.HasSwitch(switches::kNoErrorDialogs))
202 return;
203
204 // The encoding we use for the info is "title|context|direction" where
205 // direction is either env_vars::kRtlLocale or env_vars::kLtrLocale depending
206 // on the current locale.
[email protected]6baca1a2010-08-14 00:17:05207 string16 dlg_strings(l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_TITLE));
208 dlg_strings.push_back('|');
[email protected]1c53ffde12010-08-17 22:40:31209 string16 adjusted_string(
210 l10n_util::GetStringUTF16(IDS_CRASH_RECOVERY_CONTENT));
[email protected]c32d31e2010-11-24 07:27:42211 base::i18n::AdjustStringForLocaleDirection(&adjusted_string);
[email protected]6baca1a2010-08-14 00:17:05212 dlg_strings.append(adjusted_string);
213 dlg_strings.push_back('|');
214 dlg_strings.append(ASCIIToUTF16(
215 base::i18n::IsRTL() ? env_vars::kRtlLocale : env_vars::kLtrLocale));
[email protected]5c9587c2008-12-09 21:20:16216
[email protected]6baca1a2010-08-14 00:17:05217 env->SetVar(env_vars::kRestartInfo, UTF16ToUTF8(dlg_strings));
[email protected]5c9587c2008-12-09 21:20:16218}
219
[email protected]0fd23af2011-02-20 06:33:04220bool RegisterApplicationRestart(const CommandLine& parsed_command_line) {
[email protected]0fd23af2011-02-20 06:33:04221 DCHECK(base::win::GetVersion() >= base::win::VERSION_VISTA);
222 base::ScopedNativeLibrary library(FilePath(L"kernel32.dll"));
223 // Get the function pointer for RegisterApplicationRestart.
[email protected]a08ba822011-02-20 07:45:47224 RegisterApplicationRestartProc register_application_restart =
225 static_cast<RegisterApplicationRestartProc>(
226 library.GetFunctionPointer("RegisterApplicationRestart"));
[email protected]0fd23af2011-02-20 06:33:04227 if (!register_application_restart)
228 return false;
229
230 // The Windows Restart Manager expects a string of command line flags only,
231 // without the program.
232 CommandLine command_line(CommandLine::NO_PROGRAM);
[email protected]a40ca4302011-05-14 01:10:24233 command_line.AppendArguments(parsed_command_line, false);
[email protected]0fd23af2011-02-20 06:33:04234 // Ensure restore last session is set.
235 if (!command_line.HasSwitch(switches::kRestoreLastSession))
236 command_line.AppendSwitch(switches::kRestoreLastSession);
[email protected]0fd23af2011-02-20 06:33:04237
238 // Restart Chrome if the computer is restarted as the result of an update.
239 // This could be extended to handle crashes, hangs, and patches.
[email protected]9dc12406b2011-03-03 15:50:30240 HRESULT hr = register_application_restart(
241 command_line.command_line_string().c_str(),
[email protected]0fd23af2011-02-20 06:33:04242 RESTART_NO_CRASH | RESTART_NO_HANG | RESTART_NO_PATCH);
243 DCHECK(SUCCEEDED(hr)) << "RegisterApplicationRestart failed.";
244 return SUCCEEDED(hr);
245}
246
[email protected]5c9587c2008-12-09 21:20:16247// This method handles the --hide-icons and --show-icons command line options
248// for chrome that get triggered by Windows from registry entries
249// HideIconsCommand & ShowIconsCommand. Chrome doesn't support hide icons
250// functionality so we just ask the users if they want to uninstall Chrome.
[email protected]0fd23af2011-02-20 06:33:04251int HandleIconsCommands(const CommandLine& parsed_command_line) {
[email protected]5c9587c2008-12-09 21:20:16252 if (parsed_command_line.HasSwitch(switches::kHideIcons)) {
[email protected]0f26d7b2011-01-05 19:10:44253 string16 cp_applet;
[email protected]935aa542010-10-15 01:59:15254 base::win::Version version = base::win::GetVersion();
255 if (version >= base::win::VERSION_VISTA) {
[email protected]5c9587c2008-12-09 21:20:16256 cp_applet.assign(L"Programs and Features"); // Windows Vista and later.
[email protected]935aa542010-10-15 01:59:15257 } else if (version >= base::win::VERSION_XP) {
[email protected]5c9587c2008-12-09 21:20:16258 cp_applet.assign(L"Add/Remove Programs"); // Windows XP.
259 } else {
260 return ResultCodes::UNSUPPORTED_PARAM; // Not supported
261 }
262
[email protected]0f26d7b2011-01-05 19:10:44263 const string16 msg =
264 l10n_util::GetStringFUTF16(IDS_HIDE_ICONS_NOT_SUPPORTED, cp_applet);
265 const string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]5c9587c2008-12-09 21:20:16266 const UINT flags = MB_OKCANCEL | MB_ICONWARNING | MB_TOPMOST;
[email protected]75b68052011-02-03 06:01:16267 if (IDOK == ui::MessageBox(NULL, msg, caption, flags))
[email protected]5c9587c2008-12-09 21:20:16268 ShellExecute(NULL, NULL, L"appwiz.cpl", NULL, NULL, SW_SHOWNORMAL);
269 return ResultCodes::NORMAL_EXIT; // Exit as we are not launching browser.
270 }
271 // We don't hide icons so we shouldn't do anything special to show them
272 return ResultCodes::UNSUPPORTED_PARAM;
273}
274
275// Check if there is any machine level Chrome installed on the current
276// machine. If yes and the current Chrome process is user level, we do not
277// allow the user level Chrome to run. So we notify the user and uninstall
278// user level Chrome.
279bool CheckMachineLevelInstall() {
[email protected]bf6117c7e2010-12-01 06:00:25280 // TODO(tommi): Check if using the default distribution is always the right
281 // thing to do.
282 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]1db1b492010-12-16 19:03:37283 scoped_ptr<Version> version(InstallUtil::GetChromeVersion(dist, true));
[email protected]5c9587c2008-12-09 21:20:16284 if (version.get()) {
[email protected]b9696482010-11-30 23:56:18285 FilePath exe_path;
286 PathService::Get(base::DIR_EXE, &exe_path);
287 std::wstring exe = exe_path.value();
[email protected]bf6117c7e2010-12-01 06:00:25288 FilePath user_exe_path(installer::GetChromeInstallPath(false, dist));
289 if (FilePath::CompareEqualIgnoreCase(exe, user_exe_path.value())) {
[email protected]0f26d7b2011-01-05 19:10:44290 const string16 text =
291 l10n_util::GetStringUTF16(IDS_MACHINE_LEVEL_INSTALL_CONFLICT);
292 const string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
[email protected]5c9587c2008-12-09 21:20:16293 const UINT flags = MB_OK | MB_ICONERROR | MB_TOPMOST;
[email protected]75b68052011-02-03 06:01:16294 ui::MessageBox(NULL, text, caption, flags);
[email protected]e5fbd4962011-02-11 16:30:43295 CommandLine uninstall_cmd(
296 InstallUtil::GetChromeUninstallCmd(false, dist->GetType()));
297 if (!uninstall_cmd.GetProgram().empty()) {
[email protected]74ca0442010-12-15 14:44:50298 uninstall_cmd.AppendSwitch(installer::switches::kForceUninstall);
[email protected]e6124ad52010-11-15 04:17:52299 uninstall_cmd.AppendSwitch(
[email protected]74ca0442010-12-15 14:44:50300 installer::switches::kDoNotRemoveSharedItems);
[email protected]5c9587c2008-12-09 21:20:16301 base::LaunchApp(uninstall_cmd, false, false, NULL);
302 }
303 return true;
304 }
305 }
306 return false;
307}
[email protected]f8abf722010-07-07 19:46:24308
309// BrowserMainPartsWin ---------------------------------------------------------
310
311class BrowserMainPartsWin : public BrowserMainParts {
312 public:
313 explicit BrowserMainPartsWin(const MainFunctionParams& parameters)
314 : BrowserMainParts(parameters) {}
315
[email protected]1fec64352010-07-27 13:55:21316 protected:
[email protected]f8abf722010-07-07 19:46:24317 virtual void PreEarlyInitialization() {
318 // Initialize Winsock.
319 net::EnsureWinsockInit();
320 }
[email protected]1fec64352010-07-27 13:55:21321
322 virtual void PreMainMessageLoopStart() {
323 OleInitialize(NULL);
[email protected]337ca072010-11-16 15:28:45324
325 // If we're running tests (ui_task is non-null), then the ResourceBundle
326 // has already been initialized.
327 if (!parameters().ui_task) {
328 // Override the configured locale with the user's preferred UI language.
329 l10n_util::OverrideLocaleWithUILanguageList();
[email protected]ecb924c2011-03-17 00:34:09330
331 // Make sure that we know how to handle exceptions from the message loop.
332 InitializeWindowProcExceptions();
[email protected]337ca072010-11-16 15:28:45333 }
[email protected]1fec64352010-07-27 13:55:21334 }
[email protected]d7dbe28c2010-07-29 04:33:47335
[email protected]fd59f822011-05-12 18:07:18336 virtual void PostMainMessageLoopStart() OVERRIDE {
337 MessageLoop::current()->PostDelayedTask(
338 FROM_HERE,
339 NewRunnableFunction(&BrowserUsageUpdater::Track),
340 base::TimeDelta::FromSeconds(30).InMillisecondsRoundedUp());
341 }
342
[email protected]d7dbe28c2010-07-29 04:33:47343 private:
344 virtual void InitializeSSL() {
345 // Use NSS for SSL by default.
[email protected]2380f372011-02-23 21:35:19346 // The default client socket factory uses NSS for SSL by default on
347 // Windows.
348 if (parsed_command_line().HasSwitch(switches::kUseSystemSSL)) {
349 net::ClientSocketFactory::UseSystemSSL();
350 } else {
[email protected]d7dbe28c2010-07-29 04:33:47351 // We want to be sure to init NSPR on the main thread.
[email protected]4b559b4d2011-04-14 17:37:14352 crypto::EnsureNSPRInit();
[email protected]d7dbe28c2010-07-29 04:33:47353 }
354 }
[email protected]f8abf722010-07-07 19:46:24355};
356
357// static
358BrowserMainParts* BrowserMainParts::CreateBrowserMainParts(
359 const MainFunctionParams& parameters) {
360 return new BrowserMainPartsWin(parameters);
361}