[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 2 | // 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] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 8 | #include <shlwapi.h> |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 9 | #include <shobjidl.h> |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include <propkey.h> // Needs to come after shobjidl.h. |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 11 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 14 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 17 | #include "base/metrics/field_trial.h" |
| 18 | #include "base/metrics/histogram_macros.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 19 | #include "base/path_service.h" |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 20 | #include "base/process/launch.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 22 | #include "base/strings/string_util.h" |
| 23 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 24 | #include "base/strings/utf_string_conversions.h" |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 25 | #include "base/timer/timer.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 26 | #include "base/win/registry.h" |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 27 | #include "base/win/scoped_comptr.h" |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 28 | #include "base/win/scoped_propvariant.h" |
[email protected] | f1024e2 | 2012-09-12 07:14:55 | [diff] [blame] | 29 | #include "base/win/shortcut.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 30 | #include "base/win/windows_version.h" |
[email protected] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 31 | #include "chrome/browser/policy/policy_path_parser.h" |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 32 | #include "chrome/browser/ui/startup/startup_browser_creator.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 33 | #include "chrome/browser/web_applications/web_app.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 34 | #include "chrome/common/chrome_constants.h" |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 35 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 36 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 37 | #include "chrome/installer/setup/setup_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 38 | #include "chrome/installer/util/browser_distribution.h" |
| 39 | #include "chrome/installer/util/create_reg_key_work_item.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 40 | #include "chrome/installer/util/install_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 41 | #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" |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 46 | #include "components/variations/variations_associated_data.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 47 | #include "content/public/browser/browser_thread.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 48 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 49 | using content::BrowserThread; |
| 50 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 53 | const wchar_t kAppListAppNameSuffix[] = L"AppList"; |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 54 | |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 55 | const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault"; |
| 56 | const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default"; |
| 57 | const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default"; |
| 58 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 59 | // Helper function for ShellIntegration::GetAppId to generates profile id |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 60 | // from profile path. "profile_id" is composed of sanitized basenames of |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 61 | // user data dir and profile dir joined by a ".". |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 62 | base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 63 | // Return empty string if profile_path is empty |
| 64 | if (profile_path.empty()) |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 65 | return base::string16(); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 66 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 67 | base::FilePath default_user_data_dir; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 68 | // 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] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 72 | profile_path.BaseName().value() == |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 73 | base::ASCIIToUTF16(chrome::kInitialProfile)) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 74 | return base::string16(); |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 75 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 76 | |
| 77 | // Get joined basenames of user data dir and profile. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 78 | base::string16 basenames = profile_path.DirName().BaseName().value() + |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 79 | L"." + profile_path.BaseName().value(); |
| 80 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 81 | base::string16 profile_id; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 82 | profile_id.reserve(basenames.size()); |
| 83 | |
| 84 | // Generate profile_id from sanitized basenames. |
| 85 | for (size_t i = 0; i < basenames.length(); ++i) { |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 86 | if (base::IsAsciiAlpha(basenames[i]) || |
| 87 | base::IsAsciiDigit(basenames[i]) || |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 88 | basenames[i] == L'.') |
| 89 | profile_id += basenames[i]; |
| 90 | } |
| 91 | |
| 92 | return profile_id; |
| 93 | } |
| 94 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 95 | base::string16 GetAppListAppName() { |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 96 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 97 | base::string16 app_name(dist->GetBaseAppId()); |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 98 | app_name.append(kAppListAppNameSuffix); |
| 99 | return app_name; |
| 100 | } |
| 101 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 102 | // Gets expected app id for given Chrome (based on |command_line| and |
| 103 | // |is_per_user_install|). |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 104 | base::string16 GetExpectedAppId(const base::CommandLine& command_line, |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 105 | bool is_per_user_install) { |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 106 | 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] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 111 | // Adjust with any policy that overrides any other way to set the path. |
| 112 | policy::path_parser::CheckUserDataDirPolicy(&user_data_dir); |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 113 | DCHECK(!user_data_dir.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 114 | |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 115 | base::FilePath profile_subdir; |
| 116 | if (command_line.HasSwitch(switches::kProfileDirectory)) { |
| 117 | profile_subdir = |
| 118 | command_line.GetSwitchValuePath(switches::kProfileDirectory); |
| 119 | } else { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 120 | profile_subdir = |
| 121 | base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile)); |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 122 | } |
| 123 | DCHECK(!profile_subdir.empty()); |
| 124 | |
| 125 | base::FilePath profile_path = user_data_dir.Append(profile_subdir); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 126 | base::string16 app_name; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 127 | if (command_line.HasSwitch(switches::kApp)) { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 128 | app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 129 | GURL(command_line.GetSwitchValueASCII(switches::kApp)))); |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 130 | } else if (command_line.HasSwitch(switches::kAppId)) { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 131 | app_name = base::UTF8ToUTF16( |
| 132 | web_app::GenerateApplicationNameFromExtensionId( |
| 133 | command_line.GetSwitchValueASCII(switches::kAppId))); |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 134 | } else if (command_line.HasSwitch(switches::kShowAppList)) { |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 135 | app_name = GetAppListAppName(); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 136 | } else { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 137 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 138 | app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 139 | } |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 140 | DCHECK(!app_name.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 141 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 142 | return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 145 | void MigrateChromiumShortcutsCallback() { |
| 146 | // This should run on the file thread. |
thestig | 00844cea | 2015-09-08 21:44:52 | [diff] [blame] | 147 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 148 | |
| 149 | // Get full path of chrome. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 150 | base::FilePath chrome_exe; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 151 | 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] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 160 | base::DIR_TASKBAR_PINS, |
| 161 | NULL |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 162 | }, { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 163 | base::DIR_USER_DESKTOP, |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 164 | 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 175 | base::FilePath path; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 176 | 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 | |
gab | 9d20474 | 2015-08-05 16:20:08 | [diff] [blame] | 184 | bool check_dual_mode = kLocations[i].location_id == base::DIR_START_MENU || |
| 185 | kLocations[i].location_id == base::DIR_TASKBAR_PINS; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 186 | ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path, |
| 187 | check_dual_mode); |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 191 | // 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. |
| 194 | base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) { |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 195 | base::string16 url_scheme = base::ASCIIToUTF16(url.scheme()); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 196 | // Don't attempt to query protocol association on an empty string. |
[email protected] | 6e84d37 | 2014-05-29 23:36:39 | [diff] [blame] | 197 | if (url_scheme.empty()) |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 198 | return base::string16(); |
| 199 | |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 200 | // 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] | 6e84d37 | 2014-05-29 23:36:39 | [diff] [blame] | 208 | url_scheme.c_str(), |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 209 | 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 | |
| 219 | base::string16 GetAppForProtocolUsingRegistry(const GURL& url) { |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 220 | const base::string16 cmd_key_path = |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 221 | base::ASCIIToUTF16(url.scheme() + "\\shell\\open\\command"); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 222 | base::win::RegKey cmd_key(HKEY_CLASSES_ROOT, |
| 223 | cmd_key_path.c_str(), |
| 224 | KEY_READ); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 225 | base::string16 application_to_launch; |
| 226 | if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) { |
benwells | e84d839 | 2015-08-18 05:23:58 | [diff] [blame] | 227 | const base::string16 url_spec = |
| 228 | base::ASCIIToUTF16(url.possibly_invalid_spec()); |
brettw | e6dae46 | 2015-06-24 20:54:45 | [diff] [blame] | 229 | base::ReplaceSubstringsAfterOffset(&application_to_launch, |
| 230 | 0, |
| 231 | L"%1", |
benwells | e84d839 | 2015-08-18 05:23:58 | [diff] [blame] | 232 | url_spec); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 233 | return application_to_launch; |
| 234 | } |
| 235 | return base::string16(); |
| 236 | } |
| 237 | |
| 238 | |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 239 | ShellIntegration::DefaultWebClientState |
| 240 | GetDefaultWebClientStateFromShellUtilDefaultState( |
| 241 | ShellUtil::DefaultState default_state) { |
| 242 | switch (default_state) { |
| 243 | case ShellUtil::NOT_DEFAULT: |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 244 | return ShellIntegration::NOT_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 245 | case ShellUtil::IS_DEFAULT: |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 246 | return ShellIntegration::IS_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 247 | default: |
| 248 | DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 249 | return ShellIntegration::UNKNOWN_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 253 | // Resets the default browser choice for the current user. |
| 254 | void 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. |
| 273 | bool 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] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 288 | } // namespace |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 289 | |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 290 | // static |
| 291 | bool ShellIntegration::IsSetAsDefaultAsynchronous() { |
| 292 | return base::win::GetVersion() >= base::win::VERSION_WIN10 && |
| 293 | IsAsyncSetAsDefaultEnabled(); |
| 294 | } |
| 295 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 296 | bool ShellIntegration::SetAsDefaultBrowser() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 297 | base::FilePath chrome_exe; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 298 | 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] | bf6117c7e | 2010-12-01 06:00:25 | [diff] [blame] | 304 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 305 | if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, |
| 306 | true /* elevate_if_not_admin */)) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 307 | LOG(ERROR) << "Chrome could not be set as default browser."; |
| 308 | return false; |
| 309 | } |
| 310 | |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 311 | VLOG(1) << "Chrome registered as default browser."; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 315 | bool 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] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 332 | bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) { |
| 333 | if (protocol.empty()) |
| 334 | return false; |
| 335 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 336 | base::FilePath chrome_exe; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 337 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 338 | LOG(ERROR) << "Error getting app exe path"; |
| 339 | return false; |
| 340 | } |
| 341 | |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 342 | base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 343 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 344 | if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe, |
| 345 | wprotocol)) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 346 | 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] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 355 | bool ShellIntegration::SetAsDefaultProtocolClientInteractive( |
| 356 | const std::string& protocol) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 357 | base::FilePath chrome_exe; |
[email protected] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 358 | 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] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 364 | base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 365 | if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe, |
| 366 | wprotocol)) { |
[email protected] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 367 | 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] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 375 | ShellIntegration::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; |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 381 | if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 382 | return SET_DEFAULT_UNATTENDED; |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 383 | if (IsSetAsDefaultAsynchronous()) |
| 384 | return SET_DEFAULT_ASYNCHRONOUS; |
| 385 | return SET_DEFAULT_INTERACTIVE; |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 386 | } |
| 387 | |
pmonette | 868ca64 | 2015-09-02 14:34:02 | [diff] [blame] | 388 | bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() { |
| 389 | return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 390 | } |
| 391 | |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 392 | base::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] | 42dc940 | 2013-01-30 07:54:20 | [diff] [blame] | 399 | } |
| 400 | |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 401 | ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() { |
| 402 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
| 403 | ShellUtil::GetChromeDefaultState()); |
| 404 | } |
| 405 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 406 | // 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. |
| 417 | bool ShellIntegration::IsFirefoxDefaultBrowser() { |
| 418 | bool ff_default = false; |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 419 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 420 | base::string16 app_cmd; |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 421 | base::win::RegKey key(HKEY_CURRENT_USER, |
| 422 | ShellUtil::kRegVistaUrlPrefs, KEY_READ); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 423 | if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 424 | app_cmd == L"FirefoxURL") |
| 425 | ff_default = true; |
| 426 | } else { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 427 | base::string16 key_path(L"http"); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 428 | key_path.append(ShellUtil::kRegShellOpen); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 429 | base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 430 | base::string16 app_cmd; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 431 | if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | cb1f4ac | 2014-08-07 16:55:42 | [diff] [blame] | 432 | base::string16::npos != |
brettw | fce8d19 | 2015-08-10 19:07:51 | [diff] [blame] | 433 | base::ToLowerASCII(app_cmd).find(L"firefox")) |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 434 | ff_default = true; |
| 435 | } |
| 436 | return ff_default; |
| 437 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 438 | |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 439 | ShellIntegration::DefaultWebClientState |
| 440 | ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) { |
| 441 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
| 442 | ShellUtil::GetChromeDefaultProtocolClientState( |
| 443 | base::UTF8ToUTF16(protocol))); |
| 444 | } |
| 445 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 446 | base::string16 ShellIntegration::GetAppModelIdForProfile( |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 447 | const base::string16& app_name, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 448 | const base::FilePath& profile_path) { |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 449 | std::vector<base::string16> components; |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 450 | components.push_back(app_name); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 451 | const base::string16 profile_id(GetProfileIdFromPath(profile_path)); |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 452 | if (!profile_id.empty()) |
| 453 | components.push_back(profile_id); |
| 454 | return ShellUtil::BuildAppModelId(components); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 457 | base::string16 ShellIntegration::GetChromiumModelIdForProfile( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 458 | const base::FilePath& profile_path) { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 459 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 460 | base::FilePath chrome_exe; |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 461 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 462 | NOTREACHED(); |
| 463 | return dist->GetBaseAppId(); |
| 464 | } |
| 465 | return GetAppModelIdForProfile( |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 466 | ShellUtil::GetBrowserModelId(dist, |
| 467 | InstallUtil::IsPerUserInstall(chrome_exe)), |
[email protected] | 78679969 | 2012-09-26 14:16:48 | [diff] [blame] | 468 | profile_path); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 469 | } |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 470 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 471 | base::string16 ShellIntegration::GetAppListAppModelIdForProfile( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 472 | const base::FilePath& profile_path) { |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 473 | return GetAppModelIdForProfile(GetAppListAppName(), profile_path); |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 476 | void ShellIntegration::MigrateChromiumShortcuts() { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 477 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 478 | return; |
| 479 | |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 480 | // 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] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 485 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 486 | base::Bind(&MigrateChromiumShortcutsCallback), |
| 487 | base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds)); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 488 | } |
[email protected] | 43903b8 | 2012-06-01 05:26:23 | [diff] [blame] | 489 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 490 | int ShellIntegration::MigrateShortcutsInPathInternal( |
| 491 | const base::FilePath& chrome_exe, |
| 492 | const base::FilePath& path, |
| 493 | bool check_dual_mode) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 494 | DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7); |
| 495 | |
| 496 | // Enumerate all pinned shortcuts in the given path directly. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 497 | base::FileEnumerator shortcuts_enum( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 498 | path, false, // not recursive |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 499 | base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk")); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 500 | |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 501 | bool is_per_user_install = InstallUtil::IsPerUserInstall(chrome_exe); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 502 | |
| 503 | int shortcuts_migrated = 0; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 504 | base::FilePath target_path; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 505 | base::string16 arguments; |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 506 | base::win::ScopedPropVariant propvariant; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 507 | for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 508 | 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 | } |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 515 | base::CommandLine command_line( |
| 516 | base::CommandLine::FromString(base::StringPrintf( |
| 517 | L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str()))); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 518 | |
| 519 | // Get the expected AppId for this Chrome shortcut. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 520 | base::string16 expected_app_id( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 521 | 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)) || |
dcheng | 81762e0 | 2014-11-21 21:22:28 | [diff] [blame] | 530 | FAILED(persist_file.QueryFrom(shell_link.get())) || |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 531 | 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] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 542 | propvariant.Reset(); |
dcheng | 81762e0 | 2014-11-21 21:22:28 | [diff] [blame] | 543 | if (FAILED(property_store.QueryFrom(shell_link.get())) || |
| 544 | property_store->GetValue(PKEY_AppUserModel_ID, propvariant.Receive()) != |
| 545 | S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 546 | // When in doubt, prefer not updating the shortcut. |
| 547 | NOTREACHED(); |
| 548 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 549 | } else { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 550 | 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] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 557 | if (expected_app_id != base::string16(propvariant.get().pwszVal)) |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 558 | updated_properties.set_app_id(expected_app_id); |
| 559 | break; |
| 560 | default: |
| 561 | NOTREACHED(); |
| 562 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 566 | // Only set dual mode if the expected app id is the default app id. |
| 567 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 568 | base::string16 default_chromium_model_id( |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 569 | ShellUtil::GetBrowserModelId(dist, is_per_user_install)); |
| 570 | if (check_dual_mode && expected_app_id == default_chromium_model_id) { |
gab | a1b29082 | 2015-07-27 16:34:26 | [diff] [blame] | 571 | const bool dual_mode_desired = |
| 572 | InstallUtil::ShouldInstallMetroProperties(); |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 573 | propvariant.Reset(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 574 | if (property_store->GetValue(PKEY_AppUserModel_IsDualMode, |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 575 | propvariant.Receive()) != S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 576 | // When in doubt, prefer to not update the shortcut. |
| 577 | NOTREACHED(); |
| 578 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 579 | } else { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 580 | switch (propvariant.get().vt) { |
| 581 | case VT_EMPTY: |
gab | a1b29082 | 2015-07-27 16:34:26 | [diff] [blame] | 582 | // 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] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 586 | break; |
| 587 | case VT_BOOL: |
gab | a1b29082 | 2015-07-27 16:34:26 | [diff] [blame] | 588 | // 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] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 591 | break; |
| 592 | default: |
| 593 | NOTREACHED(); |
| 594 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 595 | } |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 596 | } |
| 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 613 | base::FilePath ShellIntegration::GetStartMenuShortcut( |
| 614 | const base::FilePath& chrome_exe) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 615 | static const int kFolderIds[] = { |
| 616 | base::DIR_COMMON_START_MENU, |
| 617 | base::DIR_START_MENU, |
| 618 | }; |
| 619 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 620 | base::string16 shortcut_name( |
[email protected] | 16fcc0d | 2013-08-14 04:22:13 | [diff] [blame] | 621 | dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 622 | base::FilePath shortcut; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 623 | |
| 624 | // Check both the common and the per-user Start Menu folders for system-level |
| 625 | // installs. |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 626 | size_t folder = InstallUtil::IsPerUserInstall(chrome_exe) ? 1 : 0; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 627 | for (; folder < arraysize(kFolderIds); ++folder) { |
| 628 | if (!PathService::Get(kFolderIds[folder], &shortcut)) { |
| 629 | NOTREACHED(); |
| 630 | continue; |
| 631 | } |
| 632 | |
[email protected] | 126f462 | 2012-10-05 01:34:05 | [diff] [blame] | 633 | shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + |
| 634 | installer::kLnkExt); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 635 | if (base::PathExists(shortcut)) |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 636 | return shortcut; |
| 637 | } |
| 638 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 639 | return base::FilePath(); |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 640 | } |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame^] | 641 | |
| 642 | // static |
| 643 | bool ShellIntegration::DefaultWebClientWorker::ShouldReportAttemptResults() { |
| 644 | return base::win::GetVersion() >= base::win::VERSION_WIN10 && |
| 645 | IsSetAsDefaultAsynchronous(); |
| 646 | } |
| 647 | |
| 648 | bool 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 | |
| 696 | void 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 |
| 705 | bool 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 | } |