blob: 4089dc47a12765898c4a3413352e26cabed14faf [file] [log] [blame]
[email protected]b6b72222012-02-11 02:04:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d24c4012009-07-28 01:57:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/shell_integration.h"
6
7#include <windows.h>
[email protected]caa05352014-03-01 00:43:058#include <shlwapi.h>
[email protected]d24c4012009-07-28 01:57:319#include <shobjidl.h>
thestig18dfb7a52014-08-26 10:44:0410#include <propkey.h> // Needs to come after shobjidl.h.
[email protected]d24c4012009-07-28 01:57:3111
[email protected]3a3e72c2011-11-29 02:59:3812#include "base/bind.h"
[email protected]d24c4012009-07-28 01:57:3113#include "base/command_line.h"
[email protected]25a4c1c2013-06-08 04:53:3614#include "base/files/file_enumerator.h"
thestig18dfb7a52014-08-26 10:44:0415#include "base/files/file_util.h"
[email protected]fa1e0e12013-07-18 00:10:1416#include "base/message_loop/message_loop.h"
pmonettef89ac7c72015-10-06 03:22:0117#include "base/metrics/field_trial.h"
18#include "base/metrics/histogram_macros.h"
[email protected]d24c4012009-07-28 01:57:3119#include "base/path_service.h"
pmonettef89ac7c72015-10-06 03:22:0120#include "base/process/launch.h"
[email protected]3ea1b182013-02-08 22:38:4121#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1722#include "base/strings/string_util.h"
23#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0824#include "base/strings/utf_string_conversions.h"
pmonettef89ac7c72015-10-06 03:22:0125#include "base/timer/timer.h"
[email protected]2d6503982010-10-17 04:41:5426#include "base/win/registry.h"
[email protected]8ee65ba2011-04-12 20:53:2327#include "base/win/scoped_comptr.h"
[email protected]07983302013-01-21 19:41:4428#include "base/win/scoped_propvariant.h"
[email protected]f1024e22012-09-12 07:14:5529#include "base/win/shortcut.h"
[email protected]935aa542010-10-15 01:59:1530#include "base/win/windows_version.h"
[email protected]89d43832013-06-29 20:25:2031#include "chrome/browser/policy/policy_path_parser.h"
pmonettef89ac7c72015-10-06 03:22:0132#include "chrome/browser/ui/startup/startup_browser_creator.h"
[email protected]c9bb06f42010-01-13 23:53:4833#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3134#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1535#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4836#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0237#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3138#include "chrome/installer/util/browser_distribution.h"
39#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2740#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3141#include "chrome/installer/util/set_reg_value_work_item.h"
42#include "chrome/installer/util/shell_util.h"
43#include "chrome/installer/util/util_constants.h"
44#include "chrome/installer/util/work_item.h"
45#include "chrome/installer/util/work_item_list.h"
pmonettef89ac7c72015-10-06 03:22:0146#include "components/variations/variations_associated_data.h"
[email protected]c38831a12011-10-28 12:44:4947#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3148
[email protected]631bb742011-11-02 11:29:3949using content::BrowserThread;
50
[email protected]12f520c2010-01-06 18:11:1551namespace {
52
[email protected]da1ffc32013-02-15 21:22:5653const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5254
pmonettef89ac7c72015-10-06 03:22:0155const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault";
56const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default";
57const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default";
58
[email protected]12f520c2010-01-06 18:11:1559// Helper function for ShellIntegration::GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1460// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1561// user data dir and profile dir joined by a ".".
[email protected]6a72a632013-12-12 22:22:0062base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1563 // Return empty string if profile_path is empty
64 if (profile_path.empty())
[email protected]0085863a2013-12-06 21:19:0365 return base::string16();
[email protected]12f520c2010-01-06 18:11:1566
[email protected]650b2d52013-02-10 03:41:4567 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1568 // Return empty string if profile_path is in default user data
69 // dir and is the default profile.
70 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
71 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4872 profile_path.BaseName().value() ==
[email protected]f911df52013-12-24 23:24:2373 base::ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]0085863a2013-12-06 21:19:0374 return base::string16();
[email protected]162b5992011-03-15 19:40:4875 }
[email protected]12f520c2010-01-06 18:11:1576
77 // Get joined basenames of user data dir and profile.
[email protected]0085863a2013-12-06 21:19:0378 base::string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1579 L"." + profile_path.BaseName().value();
80
[email protected]0085863a2013-12-06 21:19:0381 base::string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1582 profile_id.reserve(basenames.size());
83
84 // Generate profile_id from sanitized basenames.
85 for (size_t i = 0; i < basenames.length(); ++i) {
brettwb3413062015-06-24 00:39:0286 if (base::IsAsciiAlpha(basenames[i]) ||
87 base::IsAsciiDigit(basenames[i]) ||
[email protected]12f520c2010-01-06 18:11:1588 basenames[i] == L'.')
89 profile_id += basenames[i];
90 }
91
92 return profile_id;
93}
94
[email protected]6a72a632013-12-12 22:22:0095base::string16 GetAppListAppName() {
[email protected]da1ffc32013-02-15 21:22:5696 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:0397 base::string16 app_name(dist->GetBaseAppId());
[email protected]da1ffc32013-02-15 21:22:5698 app_name.append(kAppListAppNameSuffix);
99 return app_name;
100}
101
[email protected]8ea8f1ef2013-01-06 18:39:03102// Gets expected app id for given Chrome (based on |command_line| and
103// |is_per_user_install|).
avi556c05022014-12-22 23:31:43104base::string16 GetExpectedAppId(const base::CommandLine& command_line,
[email protected]6a72a632013-12-12 22:22:00105 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:54106 base::FilePath user_data_dir;
107 if (command_line.HasSwitch(switches::kUserDataDir))
108 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
109 else
110 chrome::GetDefaultUserDataDirectory(&user_data_dir);
[email protected]89d43832013-06-29 20:25:20111 // Adjust with any policy that overrides any other way to set the path.
112 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
[email protected]b39e32e2013-04-24 08:55:54113 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:48114
[email protected]b39e32e2013-04-24 08:55:54115 base::FilePath profile_subdir;
116 if (command_line.HasSwitch(switches::kProfileDirectory)) {
117 profile_subdir =
118 command_line.GetSwitchValuePath(switches::kProfileDirectory);
119 } else {
[email protected]f911df52013-12-24 23:24:23120 profile_subdir =
121 base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile));
[email protected]b39e32e2013-04-24 08:55:54122 }
123 DCHECK(!profile_subdir.empty());
124
125 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]0085863a2013-12-06 21:19:03126 base::string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48127 if (command_line.HasSwitch(switches::kApp)) {
[email protected]f911df52013-12-24 23:24:23128 app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51129 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14130 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]f911df52013-12-24 23:24:23131 app_name = base::UTF8ToUTF16(
132 web_app::GenerateApplicationNameFromExtensionId(
133 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52134 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56135 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48136 } else {
[email protected]a0448002012-06-19 04:32:10137 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03138 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48139 }
[email protected]b39e32e2013-04-24 08:55:54140 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48141
[email protected]8ea8f1ef2013-01-06 18:39:03142 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48143}
144
[email protected]3a3e72c2011-11-29 02:59:38145void MigrateChromiumShortcutsCallback() {
146 // This should run on the file thread.
thestig00844cea2015-09-08 21:44:52147 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
[email protected]3a3e72c2011-11-29 02:59:38148
149 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45150 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38151 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
152 return;
153
154 // Locations to check for shortcuts migration.
155 static const struct {
156 int location_id;
157 const wchar_t* sub_dir;
158 } kLocations[] = {
159 {
[email protected]e5f9d822012-11-06 22:27:01160 base::DIR_TASKBAR_PINS,
161 NULL
[email protected]3a3e72c2011-11-29 02:59:38162 }, {
[email protected]dea1d7d2012-09-20 16:24:52163 base::DIR_USER_DESKTOP,
[email protected]3a3e72c2011-11-29 02:59:38164 NULL
165 }, {
166 base::DIR_START_MENU,
167 NULL
168 }, {
169 base::DIR_APP_DATA,
170 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
171 }
172 };
173
174 for (int i = 0; i < arraysize(kLocations); ++i) {
[email protected]650b2d52013-02-10 03:41:45175 base::FilePath path;
[email protected]3a3e72c2011-11-29 02:59:38176 if (!PathService::Get(kLocations[i].location_id, &path)) {
177 NOTREACHED();
178 continue;
179 }
180
181 if (kLocations[i].sub_dir)
182 path = path.Append(kLocations[i].sub_dir);
183
gab9d204742015-08-05 16:20:08184 bool check_dual_mode = kLocations[i].location_id == base::DIR_START_MENU ||
185 kLocations[i].location_id == base::DIR_TASKBAR_PINS;
[email protected]8ea8f1ef2013-01-06 18:39:03186 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
187 check_dual_mode);
[email protected]3a3e72c2011-11-29 02:59:38188 }
189}
190
[email protected]caa05352014-03-01 00:43:05191// Windows 8 introduced a new protocol->executable binding system which cannot
192// be retrieved in the HKCR registry subkey method implemented below. We call
193// AssocQueryString with the new Win8-only flag ASSOCF_IS_PROTOCOL instead.
194base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) {
thestig11bf74d2014-11-24 20:14:42195 base::string16 url_scheme = base::ASCIIToUTF16(url.scheme());
[email protected]caa05352014-03-01 00:43:05196 // Don't attempt to query protocol association on an empty string.
[email protected]6e84d372014-05-29 23:36:39197 if (url_scheme.empty())
[email protected]caa05352014-03-01 00:43:05198 return base::string16();
199
[email protected]caa05352014-03-01 00:43:05200 // Query AssocQueryString for a human-readable description of the program
201 // that will be invoked given the provided URL spec. This is used only to
202 // populate the external protocol dialog box the user sees when invoking
203 // an unknown external protocol.
204 wchar_t out_buffer[1024];
205 DWORD buffer_size = arraysize(out_buffer);
206 HRESULT hr = AssocQueryString(ASSOCF_IS_PROTOCOL,
207 ASSOCSTR_FRIENDLYAPPNAME,
[email protected]6e84d372014-05-29 23:36:39208 url_scheme.c_str(),
[email protected]caa05352014-03-01 00:43:05209 NULL,
210 out_buffer,
211 &buffer_size);
212 if (FAILED(hr)) {
213 DLOG(WARNING) << "AssocQueryString failed!";
214 return base::string16();
215 }
216 return base::string16(out_buffer);
217}
218
219base::string16 GetAppForProtocolUsingRegistry(const GURL& url) {
[email protected]caa05352014-03-01 00:43:05220 const base::string16 cmd_key_path =
thestig11bf74d2014-11-24 20:14:42221 base::ASCIIToUTF16(url.scheme() + "\\shell\\open\\command");
[email protected]caa05352014-03-01 00:43:05222 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT,
223 cmd_key_path.c_str(),
224 KEY_READ);
[email protected]caa05352014-03-01 00:43:05225 base::string16 application_to_launch;
226 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) {
benwellse84d8392015-08-18 05:23:58227 const base::string16 url_spec =
228 base::ASCIIToUTF16(url.possibly_invalid_spec());
brettwe6dae462015-06-24 20:54:45229 base::ReplaceSubstringsAfterOffset(&application_to_launch,
230 0,
231 L"%1",
benwellse84d8392015-08-18 05:23:58232 url_spec);
[email protected]caa05352014-03-01 00:43:05233 return application_to_launch;
234 }
235 return base::string16();
236}
237
238
[email protected]eb63da72012-10-15 20:39:48239ShellIntegration::DefaultWebClientState
240 GetDefaultWebClientStateFromShellUtilDefaultState(
241 ShellUtil::DefaultState default_state) {
242 switch (default_state) {
243 case ShellUtil::NOT_DEFAULT:
[email protected]89886652012-12-11 18:09:07244 return ShellIntegration::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48245 case ShellUtil::IS_DEFAULT:
[email protected]89886652012-12-11 18:09:07246 return ShellIntegration::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48247 default:
248 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
[email protected]89886652012-12-11 18:09:07249 return ShellIntegration::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48250 }
251}
252
pmonettef89ac7c72015-10-06 03:22:01253// Resets the default browser choice for the current user.
254void ResetDefaultBrowser() {
255 static const wchar_t* const kUrlAssociationKeyFormats[] = {
256 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
257 L"%ls\\UserChoice",
258 L"SOFTWARE\\Microsoft\\Windows\\Roaming\\OpenWith\\UrlAssociations\\"
259 L"%ls\\UserChoice"};
260 static const wchar_t* const kProtocols[] = {L"http", L"https"};
261
262 for (const wchar_t* format : kUrlAssociationKeyFormats) {
263 for (const wchar_t* protocol : kProtocols) {
264 base::win::RegKey registry_key(
265 HKEY_CURRENT_USER, base::StringPrintf(format, protocol).c_str(),
266 KEY_SET_VALUE);
267 registry_key.DeleteValue(L"Hash");
268 }
269 }
270}
271
272// Returns true if the AsyncSetAsDefault field trial is activated.
273bool IsAsyncSetAsDefaultEnabled() {
274 using base::CommandLine;
275
276 // Note: It's important to query the field trial state first, to ensure that
277 // UMA reports the correct group.
278 const std::string group_name =
279 base::FieldTrialList::FindFullName("AsyncSetAsDefault");
280 if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableAsyncSetAsDefault))
281 return false;
282 if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableAsyncSetAsDefault))
283 return true;
284
285 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE);
286}
287
[email protected]3a3e72c2011-11-29 02:59:38288} // namespace
[email protected]12f520c2010-01-06 18:11:15289
pmonettef89ac7c72015-10-06 03:22:01290// static
291bool ShellIntegration::IsSetAsDefaultAsynchronous() {
292 return base::win::GetVersion() >= base::win::VERSION_WIN10 &&
293 IsAsyncSetAsDefaultEnabled();
294}
295
[email protected]d24c4012009-07-28 01:57:31296bool ShellIntegration::SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45297 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31298 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
299 LOG(ERROR) << "Error getting app exe path";
300 return false;
301 }
302
303 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25304 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
grte76ca2852014-12-05 16:42:10305 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe,
306 true /* elevate_if_not_admin */)) {
[email protected]d24c4012009-07-28 01:57:31307 LOG(ERROR) << "Chrome could not be set as default browser.";
308 return false;
309 }
310
[email protected]8e96e502010-10-21 20:57:12311 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31312 return true;
313}
314
pmonette034a03d92015-10-02 21:04:27315bool ShellIntegration::SetAsDefaultBrowserInteractive() {
316 base::FilePath chrome_exe;
317 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
318 NOTREACHED() << "Error getting app exe path";
319 return false;
320 }
321
322 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
323 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) {
324 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
325 return false;
326 }
327
328 VLOG(1) << "Set-default-browser Windows UI completed.";
329 return true;
330}
331
[email protected]4468a5b2011-05-26 07:48:02332bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
333 if (protocol.empty())
334 return false;
335
[email protected]650b2d52013-02-10 03:41:45336 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02337 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
338 LOG(ERROR) << "Error getting app exe path";
339 return false;
340 }
341
[email protected]f911df52013-12-24 23:24:23342 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02343 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
grte76ca2852014-12-05 16:42:10344 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe,
345 wprotocol)) {
[email protected]4468a5b2011-05-26 07:48:02346 LOG(ERROR) << "Chrome could not be set as default handler for "
347 << protocol << ".";
348 return false;
349 }
350
351 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
352 return true;
353}
354
[email protected]ee9d89d2012-09-25 18:21:03355bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
356 const std::string& protocol) {
[email protected]650b2d52013-02-10 03:41:45357 base::FilePath chrome_exe;
[email protected]ee9d89d2012-09-25 18:21:03358 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
359 NOTREACHED() << "Error getting app exe path";
360 return false;
361 }
362
363 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]f911df52013-12-24 23:24:23364 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
grte76ca2852014-12-05 16:42:10365 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe,
366 wprotocol)) {
[email protected]ee9d89d2012-09-25 18:21:03367 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
368 return false;
369 }
370
371 VLOG(1) << "Set-default-client Windows UI completed.";
[email protected]bd046bd42012-06-08 05:07:32372 return true;
373}
374
pmonette034a03d92015-10-02 21:04:27375ShellIntegration::DefaultWebClientSetPermission
376 ShellIntegration::CanSetAsDefaultBrowser() {
377 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
378 if (distribution->GetDefaultBrowserControlPolicy() !=
379 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
380 return SET_DEFAULT_NOT_ALLOWED;
pmonette034a03d92015-10-02 21:04:27381 if (ShellUtil::CanMakeChromeDefaultUnattended())
382 return SET_DEFAULT_UNATTENDED;
pmonettef89ac7c72015-10-06 03:22:01383 if (IsSetAsDefaultAsynchronous())
384 return SET_DEFAULT_ASYNCHRONOUS;
385 return SET_DEFAULT_INTERACTIVE;
pmonette034a03d92015-10-02 21:04:27386}
387
pmonette868ca642015-09-02 14:34:02388bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() {
389 return base::win::GetVersion() < base::win::VERSION_WIN8;
390}
391
[email protected]caa05352014-03-01 00:43:05392base::string16 ShellIntegration::GetApplicationNameForProtocol(
393 const GURL& url) {
394 // Windows 8 or above requires a new protocol association query.
395 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
396 return GetAppForProtocolUsingAssocQuery(url);
397 else
398 return GetAppForProtocolUsingRegistry(url);
[email protected]42dc9402013-01-30 07:54:20399}
400
pmonette034a03d92015-10-02 21:04:27401ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
402 return GetDefaultWebClientStateFromShellUtilDefaultState(
403 ShellUtil::GetChromeDefaultState());
404}
405
[email protected]d24c4012009-07-28 01:57:31406// There is no reliable way to say which browser is default on a machine (each
407// browser can have some of the protocols/shortcuts). So we look for only HTTP
408// protocol handler. Even this handler is located at different places in
409// registry on XP and Vista:
410// - HKCR\http\shell\open\command (XP)
411// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
412// http\UserChoice (Vista)
413// This method checks if Firefox is defualt browser by checking these
414// locations and returns true if Firefox traces are found there. In case of
415// error (or if Firefox is not found)it returns the default value which
416// is false.
417bool ShellIntegration::IsFirefoxDefaultBrowser() {
418 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15419 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]0085863a2013-12-06 21:19:03420 base::string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54421 base::win::RegKey key(HKEY_CURRENT_USER,
422 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46423 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31424 app_cmd == L"FirefoxURL")
425 ff_default = true;
426 } else {
[email protected]0085863a2013-12-06 21:19:03427 base::string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31428 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54429 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]0085863a2013-12-06 21:19:03430 base::string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46431 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]cb1f4ac2014-08-07 16:55:42432 base::string16::npos !=
brettwfce8d192015-08-10 19:07:51433 base::ToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31434 ff_default = true;
435 }
436 return ff_default;
437}
[email protected]12f520c2010-01-06 18:11:15438
pmonette034a03d92015-10-02 21:04:27439ShellIntegration::DefaultWebClientState
440 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
441 return GetDefaultWebClientStateFromShellUtilDefaultState(
442 ShellUtil::GetChromeDefaultProtocolClientState(
443 base::UTF8ToUTF16(protocol)));
444}
445
[email protected]6a72a632013-12-12 22:22:00446base::string16 ShellIntegration::GetAppModelIdForProfile(
[email protected]0085863a2013-12-06 21:19:03447 const base::string16& app_name,
[email protected]650b2d52013-02-10 03:41:45448 const base::FilePath& profile_path) {
[email protected]d2065e062013-12-12 23:49:52449 std::vector<base::string16> components;
[email protected]a0448002012-06-19 04:32:10450 components.push_back(app_name);
[email protected]0085863a2013-12-06 21:19:03451 const base::string16 profile_id(GetProfileIdFromPath(profile_path));
[email protected]a0448002012-06-19 04:32:10452 if (!profile_id.empty())
453 components.push_back(profile_id);
454 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15455}
456
[email protected]6a72a632013-12-12 22:22:00457base::string16 ShellIntegration::GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45458 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10459 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45460 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10461 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
462 NOTREACHED();
463 return dist->GetBaseAppId();
464 }
465 return GetAppModelIdForProfile(
grte76ca2852014-12-05 16:42:10466 ShellUtil::GetBrowserModelId(dist,
467 InstallUtil::IsPerUserInstall(chrome_exe)),
[email protected]786799692012-09-26 14:16:48468 profile_path);
[email protected]12f520c2010-01-06 18:11:15469}
[email protected]c9bb06f42010-01-13 23:53:48470
[email protected]6a72a632013-12-12 22:22:00471base::string16 ShellIntegration::GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45472 const base::FilePath& profile_path) {
pmonettef89ac7c72015-10-06 03:22:01473 return GetAppModelIdForProfile(GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52474}
475
[email protected]c9bb06f42010-01-13 23:53:48476void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15477 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48478 return;
479
[email protected]81fcf952012-11-26 22:25:14480 // This needs to happen eventually (e.g. so that the appid is fixed and the
481 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
482 // urgent and shouldn't delay Chrome startup.
483 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
484 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38485 BrowserThread::FILE, FROM_HERE,
[email protected]81fcf952012-11-26 22:25:14486 base::Bind(&MigrateChromiumShortcutsCallback),
487 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48488}
[email protected]43903b82012-06-01 05:26:23489
[email protected]650b2d52013-02-10 03:41:45490int ShellIntegration::MigrateShortcutsInPathInternal(
491 const base::FilePath& chrome_exe,
492 const base::FilePath& path,
493 bool check_dual_mode) {
[email protected]8ea8f1ef2013-01-06 18:39:03494 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
495
496 // Enumerate all pinned shortcuts in the given path directly.
[email protected]25a4c1c2013-06-08 04:53:36497 base::FileEnumerator shortcuts_enum(
[email protected]8ea8f1ef2013-01-06 18:39:03498 path, false, // not recursive
[email protected]25a4c1c2013-06-08 04:53:36499 base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
[email protected]8ea8f1ef2013-01-06 18:39:03500
grte76ca2852014-12-05 16:42:10501 bool is_per_user_install = InstallUtil::IsPerUserInstall(chrome_exe);
[email protected]8ea8f1ef2013-01-06 18:39:03502
503 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45504 base::FilePath target_path;
[email protected]0085863a2013-12-06 21:19:03505 base::string16 arguments;
[email protected]07983302013-01-21 19:41:44506 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45507 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03508 shortcut = shortcuts_enum.Next()) {
509 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
510 // it is fixed to work with FilePaths with spaces.
511 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
512 chrome_exe != target_path) {
513 continue;
514 }
avi556c05022014-12-22 23:31:43515 base::CommandLine command_line(
516 base::CommandLine::FromString(base::StringPrintf(
517 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
[email protected]8ea8f1ef2013-01-06 18:39:03518
519 // Get the expected AppId for this Chrome shortcut.
[email protected]0085863a2013-12-06 21:19:03520 base::string16 expected_app_id(
[email protected]8ea8f1ef2013-01-06 18:39:03521 GetExpectedAppId(command_line, is_per_user_install));
522 if (expected_app_id.empty())
523 continue;
524
525 // Load the shortcut.
526 base::win::ScopedComPtr<IShellLink> shell_link;
527 base::win::ScopedComPtr<IPersistFile> persist_file;
528 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
529 CLSCTX_INPROC_SERVER)) ||
dcheng81762e02014-11-21 21:22:28530 FAILED(persist_file.QueryFrom(shell_link.get())) ||
[email protected]8ea8f1ef2013-01-06 18:39:03531 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
532 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
533 continue;
534 }
535
536 // Any properties that need to be updated on the shortcut will be stored in
537 // |updated_properties|.
538 base::win::ShortcutProperties updated_properties;
539
540 // Validate the existing app id for the shortcut.
541 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44542 propvariant.Reset();
dcheng81762e02014-11-21 21:22:28543 if (FAILED(property_store.QueryFrom(shell_link.get())) ||
544 property_store->GetValue(PKEY_AppUserModel_ID, propvariant.Receive()) !=
545 S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03546 // When in doubt, prefer not updating the shortcut.
547 NOTREACHED();
548 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03549 } else {
[email protected]07983302013-01-21 19:41:44550 switch (propvariant.get().vt) {
551 case VT_EMPTY:
552 // If there is no app_id set, set our app_id if one is expected.
553 if (!expected_app_id.empty())
554 updated_properties.set_app_id(expected_app_id);
555 break;
556 case VT_LPWSTR:
[email protected]0085863a2013-12-06 21:19:03557 if (expected_app_id != base::string16(propvariant.get().pwszVal))
[email protected]07983302013-01-21 19:41:44558 updated_properties.set_app_id(expected_app_id);
559 break;
560 default:
561 NOTREACHED();
562 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03563 }
564 }
565
[email protected]b39e32e2013-04-24 08:55:54566 // Only set dual mode if the expected app id is the default app id.
567 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03568 base::string16 default_chromium_model_id(
[email protected]b39e32e2013-04-24 08:55:54569 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
570 if (check_dual_mode && expected_app_id == default_chromium_model_id) {
gaba1b290822015-07-27 16:34:26571 const bool dual_mode_desired =
572 InstallUtil::ShouldInstallMetroProperties();
[email protected]07983302013-01-21 19:41:44573 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03574 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44575 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03576 // When in doubt, prefer to not update the shortcut.
577 NOTREACHED();
578 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03579 } else {
[email protected]07983302013-01-21 19:41:44580 switch (propvariant.get().vt) {
581 case VT_EMPTY:
gaba1b290822015-07-27 16:34:26582 // If dual_mode is not set at all, make sure it gets set to true if
583 // desired.
584 if (dual_mode_desired)
585 updated_properties.set_dual_mode(true);
[email protected]07983302013-01-21 19:41:44586 break;
587 case VT_BOOL:
gaba1b290822015-07-27 16:34:26588 // Make sure dual_mode is set as desired.
589 if ((!!propvariant.get().boolVal) != dual_mode_desired)
590 updated_properties.set_dual_mode(dual_mode_desired);
[email protected]07983302013-01-21 19:41:44591 break;
592 default:
593 NOTREACHED();
594 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03595 }
[email protected]8ea8f1ef2013-01-06 18:39:03596 }
597 }
598
599 persist_file.Release();
600 shell_link.Release();
601
602 // Update the shortcut if some of its properties need to be updated.
603 if (updated_properties.options &&
604 base::win::CreateOrUpdateShortcutLink(
605 shortcut, updated_properties,
606 base::win::SHORTCUT_UPDATE_EXISTING)) {
607 ++shortcuts_migrated;
608 }
609 }
610 return shortcuts_migrated;
611}
612
[email protected]650b2d52013-02-10 03:41:45613base::FilePath ShellIntegration::GetStartMenuShortcut(
614 const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27615 static const int kFolderIds[] = {
616 base::DIR_COMMON_START_MENU,
617 base::DIR_START_MENU,
618 };
619 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03620 base::string16 shortcut_name(
[email protected]16fcc0d2013-08-14 04:22:13621 dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME));
[email protected]650b2d52013-02-10 03:41:45622 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27623
624 // Check both the common and the per-user Start Menu folders for system-level
625 // installs.
grte76ca2852014-12-05 16:42:10626 size_t folder = InstallUtil::IsPerUserInstall(chrome_exe) ? 1 : 0;
[email protected]3f69d6e612012-08-03 18:52:27627 for (; folder < arraysize(kFolderIds); ++folder) {
628 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
629 NOTREACHED();
630 continue;
631 }
632
[email protected]126f4622012-10-05 01:34:05633 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
634 installer::kLnkExt);
[email protected]7567484142013-07-11 17:36:07635 if (base::PathExists(shortcut))
[email protected]3f69d6e612012-08-03 18:52:27636 return shortcut;
637 }
638
[email protected]650b2d52013-02-10 03:41:45639 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27640}
pmonettef89ac7c72015-10-06 03:22:01641
642// static
643bool ShellIntegration::DefaultWebClientWorker::ShouldReportAttemptResults() {
644 return base::win::GetVersion() >= base::win::VERSION_WIN10 &&
645 IsSetAsDefaultAsynchronous();
646}
647
648bool ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() {
649 DCHECK_CURRENTLY_ON(BrowserThread::UI);
650
651 if (!IsSetAsDefaultAsynchronous())
652 return false;
653
654 // On Windows 10+, there is no official way to prompt the user to set a
655 // default browser. This is the workaround:
656 // 1. Unregister the default browser.
657 // 2. Open "How to make Chrome my default browser" link with openwith.exe.
658 // 3. Windows will prompt the user with "How would you link to open this?".
659 // 4. If Chrome is selected, we intercept the attempt to open the URL and
660 // instead call OnSetAsDefaultAttemptComplete(), passing true to indicate
661 // success.
662 // 5. If Chrome is not selected, the url is opened in the selected browser.
663 // After a certain amount of time, we notify the observer that the
664 // process failed.
665
666 if (!StartupBrowserCreator::SetDefaultBrowserCallback(
667 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this,
668 AttemptResult::SUCCESS))) {
669 // Another worker is currently processing. Note that this will still cause
670 // SetAsDefaultBrowserAsynchronous() to be invoked in SetAsDefault() but
671 // the other worker will happily intercept the attempt.
672 BrowserThread::PostTask(
673 BrowserThread::UI, FROM_HERE,
674 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this,
675 AttemptResult::OTHER_WORKER));
676 return false;
677 }
678
679 // Start the timer.
680 if (!async_timer_)
681 async_timer_.reset(new base::OneShotTimer());
682 std::string value = variations::GetVariationParamValue(
683 kAsyncSetAsDefaultExperimentName, "TimerDuration");
684 int seconds = 0;
685 if (!value.empty())
686 base::StringToInt(value, &seconds);
687 if (!seconds)
688 seconds = 120; // Default value of 2 minutes.
689 async_timer_->Start(
690 FROM_HERE, base::TimeDelta::FromSeconds(seconds),
691 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this,
692 AttemptResult::FAILURE));
693 return true;
694}
695
696void ShellIntegration::DefaultBrowserWorker::FinalizeSetAsDefault() {
697 DCHECK_CURRENTLY_ON(BrowserThread::UI);
698 DCHECK(set_as_default_initialized());
699
700 async_timer_.reset();
701 StartupBrowserCreator::ClearDefaultBrowserCallback();
702}
703
704// static
705bool ShellIntegration::DefaultBrowserWorker::SetAsDefaultBrowserAsynchronous() {
706 DCHECK(IsSetAsDefaultAsynchronous());
707
708 ResetDefaultBrowser();
709
710 base::CommandLine cmdline(base::FilePath(L"openwith.exe"));
711 cmdline.AppendArgNative(StartupBrowserCreator::GetDefaultBrowserUrl());
712 return base::LaunchProcess(cmdline, base::LaunchOptions()).IsValid();
713}