[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 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 5 | #include "chrome/browser/shell_integration_win.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 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. |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 13 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 16 | #include "base/bind.h" |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 17 | #include "base/callback.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 18 | #include "base/command_line.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 19 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 21 | #include "base/macros.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 22 | #include "base/memory/ptr_util.h" |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 23 | #include "base/memory/weak_ptr.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 24 | #include "base/message_loop/message_loop.h" |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 25 | #include "base/metrics/histogram_macros.h" |
grt | c291eea | 2016-05-26 15:38:48 | [diff] [blame] | 26 | #include "base/metrics/user_metrics.h" |
| 27 | #include "base/metrics/user_metrics_action.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 28 | #include "base/path_service.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 29 | #include "base/strings/string_util.h" |
| 30 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 31 | #include "base/strings/utf_string_conversions.h" |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 32 | #include "base/time/time.h" |
| 33 | #include "base/timer/timer.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 34 | #include "base/win/registry.h" |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 35 | #include "base/win/scoped_comptr.h" |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 36 | #include "base/win/scoped_propvariant.h" |
[email protected] | f1024e2 | 2012-09-12 07:14:55 | [diff] [blame] | 37 | #include "base/win/shortcut.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 38 | #include "base/win/windows_version.h" |
[email protected] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 39 | #include "chrome/browser/policy/policy_path_parser.h" |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 40 | #include "chrome/browser/shell_integration.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 41 | #include "chrome/browser/web_applications/web_app.h" |
grt | c291eea | 2016-05-26 15:38:48 | [diff] [blame] | 42 | #include "chrome/browser/win/settings_app_monitor.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 43 | #include "chrome/common/chrome_constants.h" |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 44 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 45 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 46 | #include "chrome/installer/setup/setup_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 47 | #include "chrome/installer/util/browser_distribution.h" |
| 48 | #include "chrome/installer/util/create_reg_key_work_item.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 49 | #include "chrome/installer/util/install_util.h" |
pmonette | 0c40087a | 2016-04-21 00:05:16 | [diff] [blame] | 50 | #include "chrome/installer/util/scoped_user_protocol_entry.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 51 | #include "chrome/installer/util/set_reg_value_work_item.h" |
| 52 | #include "chrome/installer/util/shell_util.h" |
| 53 | #include "chrome/installer/util/util_constants.h" |
| 54 | #include "chrome/installer/util/work_item.h" |
| 55 | #include "chrome/installer/util/work_item_list.h" |
pmonette | 2b1dbee | 2016-01-08 20:18:58 | [diff] [blame] | 56 | #include "components/variations/variations_associated_data.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 57 | #include "content/public/browser/browser_thread.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 58 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 59 | using content::BrowserThread; |
| 60 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 61 | namespace shell_integration { |
| 62 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 63 | namespace { |
| 64 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 65 | // Helper function for GetAppId to generates profile id |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 66 | // from profile path. "profile_id" is composed of sanitized basenames of |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 67 | // user data dir and profile dir joined by a ".". |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 68 | base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 69 | // Return empty string if profile_path is empty |
| 70 | if (profile_path.empty()) |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 71 | return base::string16(); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 72 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 73 | base::FilePath default_user_data_dir; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 74 | // Return empty string if profile_path is in default user data |
| 75 | // dir and is the default profile. |
| 76 | if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) && |
| 77 | profile_path.DirName() == default_user_data_dir && |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 78 | profile_path.BaseName().value() == |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 79 | base::ASCIIToUTF16(chrome::kInitialProfile)) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 80 | return base::string16(); |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 81 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 82 | |
| 83 | // Get joined basenames of user data dir and profile. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 84 | base::string16 basenames = profile_path.DirName().BaseName().value() + |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 85 | L"." + profile_path.BaseName().value(); |
| 86 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 87 | base::string16 profile_id; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 88 | profile_id.reserve(basenames.size()); |
| 89 | |
| 90 | // Generate profile_id from sanitized basenames. |
| 91 | for (size_t i = 0; i < basenames.length(); ++i) { |
brettw | b341306 | 2015-06-24 00:39:02 | [diff] [blame] | 92 | if (base::IsAsciiAlpha(basenames[i]) || |
| 93 | base::IsAsciiDigit(basenames[i]) || |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 94 | basenames[i] == L'.') |
| 95 | profile_id += basenames[i]; |
| 96 | } |
| 97 | |
| 98 | return profile_id; |
| 99 | } |
| 100 | |
tapted | c2c690ab | 2016-05-25 02:49:03 | [diff] [blame] | 101 | base::string16 GetAppListAppName() { |
| 102 | static const base::char16 kAppListAppNameSuffix[] = L"AppList"; |
| 103 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 104 | base::string16 app_name(dist->GetBaseAppId()); |
| 105 | app_name.append(kAppListAppNameSuffix); |
| 106 | return app_name; |
| 107 | } |
| 108 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 109 | // Gets expected app id for given Chrome (based on |command_line| and |
| 110 | // |is_per_user_install|). |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 111 | base::string16 GetExpectedAppId(const base::CommandLine& command_line, |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame] | 112 | bool is_per_user_install) { |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 113 | base::FilePath user_data_dir; |
| 114 | if (command_line.HasSwitch(switches::kUserDataDir)) |
| 115 | user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir); |
| 116 | else |
| 117 | chrome::GetDefaultUserDataDirectory(&user_data_dir); |
[email protected] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 118 | // Adjust with any policy that overrides any other way to set the path. |
| 119 | policy::path_parser::CheckUserDataDirPolicy(&user_data_dir); |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 120 | DCHECK(!user_data_dir.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 121 | |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 122 | base::FilePath profile_subdir; |
| 123 | if (command_line.HasSwitch(switches::kProfileDirectory)) { |
| 124 | profile_subdir = |
| 125 | command_line.GetSwitchValuePath(switches::kProfileDirectory); |
| 126 | } else { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 127 | profile_subdir = |
| 128 | base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile)); |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 129 | } |
| 130 | DCHECK(!profile_subdir.empty()); |
| 131 | |
| 132 | base::FilePath profile_path = user_data_dir.Append(profile_subdir); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 133 | base::string16 app_name; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 134 | if (command_line.HasSwitch(switches::kApp)) { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 135 | app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 136 | GURL(command_line.GetSwitchValueASCII(switches::kApp)))); |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 137 | } else if (command_line.HasSwitch(switches::kAppId)) { |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 138 | app_name = base::UTF8ToUTF16( |
| 139 | web_app::GenerateApplicationNameFromExtensionId( |
| 140 | command_line.GetSwitchValueASCII(switches::kAppId))); |
tapted | c2c690ab | 2016-05-25 02:49:03 | [diff] [blame] | 141 | } else if (command_line.HasSwitch(switches::kShowAppList)) { |
| 142 | app_name = GetAppListAppName(); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 143 | } else { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 144 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 145 | app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 146 | } |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 147 | DCHECK(!app_name.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 148 | |
pmonette | 9e4c1a8 | 2016-04-14 18:15:30 | [diff] [blame] | 149 | return win::GetAppModelIdForProfile(app_name, profile_path); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 150 | } |
| 151 | |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 152 | void MigrateTaskbarPinsCallback() { |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 153 | // This should run on the file thread. |
thestig | 00844cea | 2015-09-08 21:44:52 | [diff] [blame] | 154 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 155 | |
| 156 | // Get full path of chrome. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 157 | base::FilePath chrome_exe; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 158 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 159 | return; |
| 160 | |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 161 | base::FilePath pins_path; |
| 162 | if (!PathService::Get(base::DIR_TASKBAR_PINS, &pins_path)) { |
| 163 | NOTREACHED(); |
| 164 | return; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 165 | } |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 166 | |
pmonette | 9e4c1a8 | 2016-04-14 18:15:30 | [diff] [blame] | 167 | win::MigrateShortcutsInPathInternal(chrome_exe, pins_path); |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 170 | // Windows 8 introduced a new protocol->executable binding system which cannot |
| 171 | // be retrieved in the HKCR registry subkey method implemented below. We call |
| 172 | // AssocQueryString with the new Win8-only flag ASSOCF_IS_PROTOCOL instead. |
| 173 | base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) { |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 174 | base::string16 url_scheme = base::ASCIIToUTF16(url.scheme()); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 175 | // Don't attempt to query protocol association on an empty string. |
[email protected] | 6e84d37 | 2014-05-29 23:36:39 | [diff] [blame] | 176 | if (url_scheme.empty()) |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 177 | return base::string16(); |
| 178 | |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 179 | // Query AssocQueryString for a human-readable description of the program |
| 180 | // that will be invoked given the provided URL spec. This is used only to |
| 181 | // populate the external protocol dialog box the user sees when invoking |
| 182 | // an unknown external protocol. |
| 183 | wchar_t out_buffer[1024]; |
| 184 | DWORD buffer_size = arraysize(out_buffer); |
| 185 | HRESULT hr = AssocQueryString(ASSOCF_IS_PROTOCOL, |
| 186 | ASSOCSTR_FRIENDLYAPPNAME, |
[email protected] | 6e84d37 | 2014-05-29 23:36:39 | [diff] [blame] | 187 | url_scheme.c_str(), |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 188 | NULL, |
| 189 | out_buffer, |
| 190 | &buffer_size); |
| 191 | if (FAILED(hr)) { |
| 192 | DLOG(WARNING) << "AssocQueryString failed!"; |
| 193 | return base::string16(); |
| 194 | } |
| 195 | return base::string16(out_buffer); |
| 196 | } |
| 197 | |
| 198 | base::string16 GetAppForProtocolUsingRegistry(const GURL& url) { |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 199 | const base::string16 cmd_key_path = |
thestig | 11bf74d | 2014-11-24 20:14:42 | [diff] [blame] | 200 | base::ASCIIToUTF16(url.scheme() + "\\shell\\open\\command"); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 201 | base::win::RegKey cmd_key(HKEY_CLASSES_ROOT, |
| 202 | cmd_key_path.c_str(), |
| 203 | KEY_READ); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 204 | base::string16 application_to_launch; |
| 205 | if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) { |
benwells | e84d839 | 2015-08-18 05:23:58 | [diff] [blame] | 206 | const base::string16 url_spec = |
| 207 | base::ASCIIToUTF16(url.possibly_invalid_spec()); |
brettw | e6dae46 | 2015-06-24 20:54:45 | [diff] [blame] | 208 | base::ReplaceSubstringsAfterOffset(&application_to_launch, |
| 209 | 0, |
| 210 | L"%1", |
benwells | e84d839 | 2015-08-18 05:23:58 | [diff] [blame] | 211 | url_spec); |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 212 | return application_to_launch; |
| 213 | } |
| 214 | return base::string16(); |
| 215 | } |
| 216 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 217 | DefaultWebClientState GetDefaultWebClientStateFromShellUtilDefaultState( |
| 218 | ShellUtil::DefaultState default_state) { |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 219 | switch (default_state) { |
| 220 | case ShellUtil::NOT_DEFAULT: |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 221 | return DefaultWebClientState::NOT_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 222 | case ShellUtil::IS_DEFAULT: |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 223 | return DefaultWebClientState::IS_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 224 | default: |
| 225 | DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 226 | return DefaultWebClientState::UNKNOWN_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
grt | c291eea | 2016-05-26 15:38:48 | [diff] [blame] | 230 | // A recorder of user actions in the Windows Settings app. |
| 231 | class DefaultBrowserActionRecorder : public win::SettingsAppMonitor::Delegate { |
| 232 | public: |
| 233 | // Creates the recorder and the monitor that drives it. |continuation| will be |
| 234 | // run once the monitor's initialization completes (regardless of success or |
| 235 | // failure). |
| 236 | explicit DefaultBrowserActionRecorder(base::Closure continuation) |
| 237 | : continuation_(std::move(continuation)), settings_app_monitor_(this) {} |
| 238 | |
| 239 | private: |
| 240 | // win::SettingsAppMonitor::Delegate: |
| 241 | void OnInitialized(HRESULT result) override { |
| 242 | UMA_HISTOGRAM_BOOLEAN("SettingsAppMonitor.InitializationResult", |
| 243 | SUCCEEDED(result)); |
| 244 | if (SUCCEEDED(result)) { |
| 245 | base::RecordAction( |
| 246 | base::UserMetricsAction("SettingsAppMonitor.Initialized")); |
| 247 | } |
| 248 | continuation_.Run(); |
| 249 | continuation_ = base::Closure(); |
| 250 | } |
| 251 | |
| 252 | void OnAppFocused() override { |
| 253 | base::RecordAction( |
| 254 | base::UserMetricsAction("SettingsAppMonitor.AppFocused")); |
| 255 | } |
| 256 | |
| 257 | void OnChooserInvoked() override { |
| 258 | base::RecordAction( |
| 259 | base::UserMetricsAction("SettingsAppMonitor.ChooserInvoked")); |
| 260 | } |
| 261 | |
| 262 | void OnBrowserChosen(const base::string16& browser_name) override { |
| 263 | if (browser_name == |
| 264 | BrowserDistribution::GetDistribution()->GetDisplayName()) { |
| 265 | base::RecordAction( |
| 266 | base::UserMetricsAction("SettingsAppMonitor.ChromeBrowserChosen")); |
| 267 | } else { |
| 268 | base::RecordAction( |
| 269 | base::UserMetricsAction("SettingsAppMonitor.OtherBrowserChosen")); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // A closure to be run once initialization completes. |
| 274 | base::Closure continuation_; |
| 275 | |
| 276 | // Monitors user interaction with the Windows Settings app for the sake of |
| 277 | // reporting user actions. |
| 278 | win::SettingsAppMonitor settings_app_monitor_; |
| 279 | |
| 280 | DISALLOW_COPY_AND_ASSIGN(DefaultBrowserActionRecorder); |
| 281 | }; |
| 282 | |
| 283 | // A function bound up in a callback with a DefaultBrowserActionRecorder and |
| 284 | // a closure to keep the former alive until the time comes to run the latter. |
| 285 | void OnSettingsAppFinished( |
| 286 | std::unique_ptr<DefaultBrowserActionRecorder> recorder, |
| 287 | const base::Closure& on_finished_callback) { |
| 288 | recorder.reset(); |
| 289 | on_finished_callback.Run(); |
| 290 | } |
| 291 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 292 | // There is no way to make sure the user is done with the system settings, but a |
| 293 | // signal that the interaction is finished is needed for UMA. A timer of 2 |
| 294 | // minutes is used as a substitute. The registry keys for the protocol |
| 295 | // association with an app are also monitored to signal the end of the |
| 296 | // interaction early when it is clear that the user made a choice (e.g. http |
| 297 | // and https for default browser). |
| 298 | // |
| 299 | // This helper class manages both the timer and the registry watchers and makes |
| 300 | // sure the callback for the end of the settings interaction is only run once. |
| 301 | // This class also manages its own lifetime. |
| 302 | class OpenSystemSettingsHelper { |
| 303 | public: |
| 304 | // Begin the monitoring and will call |on_finished_callback| when done. |
| 305 | // Takes in a null-terminated array of |protocols| whose registry keys must be |
pmonette | 0c40087a | 2016-04-21 00:05:16 | [diff] [blame] | 306 | // watched. The array must contain at least one element. |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 307 | static void Begin(const wchar_t* const protocols[], |
| 308 | const base::Closure& on_finished_callback) { |
| 309 | new OpenSystemSettingsHelper(protocols, on_finished_callback); |
| 310 | } |
| 311 | |
| 312 | private: |
| 313 | // The reason the settings interaction concluded. Do not modify the ordering |
| 314 | // because it is used for UMA. |
| 315 | enum ConcludeReason { REGISTRY_WATCHER, TIMEOUT, NUM_CONCLUDE_REASON_TYPES }; |
| 316 | |
| 317 | OpenSystemSettingsHelper(const wchar_t* const protocols[], |
| 318 | const base::Closure& on_finished_callback) |
pmonette | 0c40087a | 2016-04-21 00:05:16 | [diff] [blame] | 319 | : scoped_user_protocol_entry_(protocols[0]), |
| 320 | on_finished_callback_(on_finished_callback), |
| 321 | weak_ptr_factory_(this) { |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 322 | static const wchar_t kUrlAssociationFormat[] = |
| 323 | L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" |
| 324 | L"%ls\\UserChoice"; |
| 325 | |
| 326 | // Remember the start time. |
| 327 | start_time_ = base::TimeTicks::Now(); |
| 328 | |
| 329 | for (const wchar_t* const* scan = &protocols[0]; *scan != nullptr; ++scan) { |
| 330 | AddRegistryKeyWatcher( |
| 331 | base::StringPrintf(kUrlAssociationFormat, *scan).c_str()); |
| 332 | } |
| 333 | // Only the watchers that were succesfully initialized are counted. |
| 334 | registry_watcher_count_ = registry_key_watchers_.size(); |
| 335 | |
| 336 | timer_.Start( |
| 337 | FROM_HERE, base::TimeDelta::FromMinutes(2), |
| 338 | base::Bind(&OpenSystemSettingsHelper::ConcludeInteraction, |
| 339 | weak_ptr_factory_.GetWeakPtr(), ConcludeReason::TIMEOUT)); |
| 340 | } |
| 341 | |
| 342 | // Called when a change is detected on one of the registry keys being watched. |
| 343 | // Note: All types of modification to the registry key will trigger this |
| 344 | // function even if the value change is the only one that matters. This |
| 345 | // is good enough for now. |
| 346 | void OnRegistryKeyChanged() { |
| 347 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 348 | |
| 349 | // Make sure all the registry watchers have fired. |
| 350 | if (--registry_watcher_count_ == 0) { |
| 351 | UMA_HISTOGRAM_MEDIUM_TIMES( |
| 352 | "DefaultBrowser.SettingsInteraction.RegistryWatcherDuration", |
| 353 | base::TimeTicks::Now() - start_time_); |
| 354 | |
| 355 | ConcludeInteraction(ConcludeReason::REGISTRY_WATCHER); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // Ends the monitoring with the system settings. Will call |
| 360 | // |on_finished_callback_| and then dispose of this class instance to make |
| 361 | // sure the callback won't get called subsequently. |
| 362 | void ConcludeInteraction(ConcludeReason conclude_reason) { |
| 363 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 364 | |
| 365 | UMA_HISTOGRAM_ENUMERATION( |
| 366 | "DefaultBrowser.SettingsInteraction.ConcludeReason", conclude_reason, |
| 367 | NUM_CONCLUDE_REASON_TYPES); |
| 368 | on_finished_callback_.Run(); |
| 369 | delete this; |
| 370 | } |
| 371 | |
| 372 | // Helper function to create a registry watcher for a given |key_path|. Do |
| 373 | // nothing on initialization failure. |
| 374 | void AddRegistryKeyWatcher(const wchar_t* key_path) { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 375 | auto reg_key = base::WrapUnique( |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 376 | new base::win::RegKey(HKEY_CURRENT_USER, key_path, KEY_NOTIFY)); |
| 377 | |
| 378 | if (reg_key->Valid() && |
| 379 | reg_key->StartWatching( |
| 380 | base::Bind(&OpenSystemSettingsHelper::OnRegistryKeyChanged, |
| 381 | weak_ptr_factory_.GetWeakPtr()))) { |
| 382 | registry_key_watchers_.push_back(std::move(reg_key)); |
| 383 | } |
| 384 | } |
| 385 | |
pmonette | 0c40087a | 2016-04-21 00:05:16 | [diff] [blame] | 386 | // This is needed to make sure that Windows displays an entry for the protocol |
| 387 | // inside the "Choose default apps by protocol" settings page. |
| 388 | ScopedUserProtocolEntry scoped_user_protocol_entry_; |
| 389 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 390 | // The function to call when the interaction with the system settings is |
| 391 | // finished. |
| 392 | base::Closure on_finished_callback_; |
| 393 | |
| 394 | // The number of time the registry key watchers must fire. |
| 395 | int registry_watcher_count_ = 0; |
| 396 | |
| 397 | // There can be multiple registry key watchers as some settings modify |
| 398 | // multiple protocol associations. e.g. Changing the default browser modifies |
| 399 | // the http and https associations. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 400 | std::vector<std::unique_ptr<base::win::RegKey>> registry_key_watchers_; |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 401 | |
| 402 | base::OneShotTimer timer_; |
| 403 | |
| 404 | // Records the time it takes for the final registry watcher to get signaled. |
| 405 | base::TimeTicks start_time_; |
| 406 | |
| 407 | // Weak ptrs are used to bind this class to the callbacks of the timer and the |
| 408 | // registry watcher. This makes it possible to self-delete after one of the |
| 409 | // callbacks is executed to cancel the remaining ones. |
| 410 | base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_; |
| 411 | |
| 412 | DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper); |
| 413 | }; |
| 414 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 415 | } // namespace |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 416 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 417 | bool SetAsDefaultBrowser() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 418 | base::FilePath chrome_exe; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 419 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 420 | LOG(ERROR) << "Error getting app exe path"; |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | // From UI currently we only allow setting default browser for current user. |
[email protected] | bf6117c7e | 2010-12-01 06:00:25 | [diff] [blame] | 425 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 426 | if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, |
| 427 | true /* elevate_if_not_admin */)) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 428 | LOG(ERROR) << "Chrome could not be set as default browser."; |
| 429 | return false; |
| 430 | } |
| 431 | |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 432 | VLOG(1) << "Chrome registered as default browser."; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 433 | return true; |
| 434 | } |
| 435 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 436 | bool SetAsDefaultProtocolClient(const std::string& protocol) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 437 | if (protocol.empty()) |
| 438 | return false; |
| 439 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 440 | base::FilePath chrome_exe; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 441 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 442 | LOG(ERROR) << "Error getting app exe path"; |
| 443 | return false; |
| 444 | } |
| 445 | |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 446 | base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 447 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 448 | if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe, |
| 449 | wprotocol)) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 450 | LOG(ERROR) << "Chrome could not be set as default handler for " |
| 451 | << protocol << "."; |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | VLOG(1) << "Chrome registered as default handler for " << protocol << "."; |
| 456 | return true; |
| 457 | } |
| 458 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 459 | DefaultWebClientSetPermission GetDefaultWebClientSetPermission() { |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 460 | BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 461 | if (distribution->GetDefaultBrowserControlPolicy() != |
| 462 | BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
| 463 | return SET_DEFAULT_NOT_ALLOWED; |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 464 | if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 465 | return SET_DEFAULT_UNATTENDED; |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 466 | // Windows 8 and 10 both introduced a new way to set the default web client |
| 467 | // which require user interaction. |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame] | 468 | return SET_DEFAULT_INTERACTIVE; |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 469 | } |
| 470 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 471 | bool IsElevationNeededForSettingDefaultProtocolClient() { |
pmonette | 868ca64 | 2015-09-02 14:34:02 | [diff] [blame] | 472 | return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 473 | } |
| 474 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 475 | base::string16 GetApplicationNameForProtocol(const GURL& url) { |
[email protected] | caa0535 | 2014-03-01 00:43:05 | [diff] [blame] | 476 | // Windows 8 or above requires a new protocol association query. |
| 477 | if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
| 478 | return GetAppForProtocolUsingAssocQuery(url); |
| 479 | else |
| 480 | return GetAppForProtocolUsingRegistry(url); |
[email protected] | 42dc940 | 2013-01-30 07:54:20 | [diff] [blame] | 481 | } |
| 482 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 483 | DefaultWebClientState GetDefaultBrowser() { |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 484 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
| 485 | ShellUtil::GetChromeDefaultState()); |
| 486 | } |
| 487 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 488 | // There is no reliable way to say which browser is default on a machine (each |
| 489 | // browser can have some of the protocols/shortcuts). So we look for only HTTP |
| 490 | // protocol handler. Even this handler is located at different places in |
| 491 | // registry on XP and Vista: |
| 492 | // - HKCR\http\shell\open\command (XP) |
| 493 | // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ |
| 494 | // http\UserChoice (Vista) |
| 495 | // This method checks if Firefox is defualt browser by checking these |
| 496 | // locations and returns true if Firefox traces are found there. In case of |
| 497 | // error (or if Firefox is not found)it returns the default value which |
| 498 | // is false. |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 499 | bool IsFirefoxDefaultBrowser() { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 500 | bool ff_default = false; |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 501 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 502 | base::string16 app_cmd; |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 503 | base::win::RegKey key(HKEY_CURRENT_USER, |
| 504 | ShellUtil::kRegVistaUrlPrefs, KEY_READ); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 505 | if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 506 | app_cmd == L"FirefoxURL") |
| 507 | ff_default = true; |
| 508 | } else { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 509 | base::string16 key_path(L"http"); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 510 | key_path.append(ShellUtil::kRegShellOpen); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 511 | base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 512 | base::string16 app_cmd; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 513 | if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | cb1f4ac | 2014-08-07 16:55:42 | [diff] [blame] | 514 | base::string16::npos != |
brettw | fce8d19 | 2015-08-10 19:07:51 | [diff] [blame] | 515 | base::ToLowerASCII(app_cmd).find(L"firefox")) |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 516 | ff_default = true; |
| 517 | } |
| 518 | return ff_default; |
| 519 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 520 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 521 | DefaultWebClientState IsDefaultProtocolClient(const std::string& protocol) { |
pmonette | 034a03d9 | 2015-10-02 21:04:27 | [diff] [blame] | 522 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
| 523 | ShellUtil::GetChromeDefaultProtocolClientState( |
| 524 | base::UTF8ToUTF16(protocol))); |
| 525 | } |
| 526 | |
pmonette | 9e4c1a8 | 2016-04-14 18:15:30 | [diff] [blame] | 527 | namespace win { |
| 528 | |
| 529 | bool SetAsDefaultBrowserUsingIntentPicker() { |
| 530 | base::FilePath chrome_exe; |
| 531 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 532 | NOTREACHED() << "Error getting app exe path"; |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 537 | if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) { |
| 538 | LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | VLOG(1) << "Set-default-browser Windows UI completed."; |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | void SetAsDefaultBrowserUsingSystemSettings( |
| 547 | const base::Closure& on_finished_callback) { |
| 548 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 549 | |
| 550 | base::FilePath chrome_exe; |
| 551 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 552 | NOTREACHED() << "Error getting app exe path"; |
| 553 | on_finished_callback.Run(); |
| 554 | return; |
| 555 | } |
| 556 | |
grt | c291eea | 2016-05-26 15:38:48 | [diff] [blame] | 557 | // Create an action recorder that will open the settings app once it has |
| 558 | // initialized. |
| 559 | std::unique_ptr<DefaultBrowserActionRecorder> recorder( |
| 560 | new DefaultBrowserActionRecorder(base::Bind( |
| 561 | base::IgnoreResult(&ShellUtil::ShowMakeChromeDefaultSystemUI), |
| 562 | base::Unretained(BrowserDistribution::GetDistribution()), |
| 563 | chrome_exe))); |
pmonette | 9e4c1a8 | 2016-04-14 18:15:30 | [diff] [blame] | 564 | |
grt | c291eea | 2016-05-26 15:38:48 | [diff] [blame] | 565 | // The helper manages its own lifetime. Bind the action recorder |
| 566 | // into the finished callback to keep it alive throughout the |
| 567 | // interaction. |
| 568 | static const wchar_t* const kProtocols[] = {L"http", L"https", nullptr}; |
| 569 | OpenSystemSettingsHelper::Begin( |
| 570 | kProtocols, base::Bind(&OnSettingsAppFinished, base::Passed(&recorder), |
| 571 | on_finished_callback)); |
pmonette | 9e4c1a8 | 2016-04-14 18:15:30 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | bool SetAsDefaultProtocolClientUsingIntentPicker(const std::string& protocol) { |
| 575 | base::FilePath chrome_exe; |
| 576 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 577 | NOTREACHED() << "Error getting app exe path"; |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 582 | base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
| 583 | if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe, |
| 584 | wprotocol)) { |
| 585 | LOG(ERROR) << "Failed to launch the set-default-client Windows UI."; |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | VLOG(1) << "Set-default-client Windows UI completed."; |
| 590 | return true; |
| 591 | } |
| 592 | |
pmonette | 0c40087a | 2016-04-21 00:05:16 | [diff] [blame] | 593 | void SetAsDefaultProtocolClientUsingSystemSettings( |
| 594 | const std::string& protocol, |
| 595 | const base::Closure& on_finished_callback) { |
| 596 | DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 597 | |
| 598 | base::FilePath chrome_exe; |
| 599 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 600 | NOTREACHED() << "Error getting app exe path"; |
| 601 | on_finished_callback.Run(); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // The helper manages its own lifetime. |
| 606 | base::string16 wprotocol(base::UTF8ToUTF16(protocol)); |
| 607 | const wchar_t* const kProtocols[] = {wprotocol.c_str(), nullptr}; |
| 608 | OpenSystemSettingsHelper::Begin(kProtocols, on_finished_callback); |
| 609 | |
| 610 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 611 | ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe, |
| 612 | wprotocol); |
| 613 | } |
| 614 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 615 | base::string16 GetAppModelIdForProfile(const base::string16& app_name, |
| 616 | const base::FilePath& profile_path) { |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 617 | std::vector<base::string16> components; |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 618 | components.push_back(app_name); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 619 | const base::string16 profile_id(GetProfileIdFromPath(profile_path)); |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 620 | if (!profile_id.empty()) |
| 621 | components.push_back(profile_id); |
| 622 | return ShellUtil::BuildAppModelId(components); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 623 | } |
| 624 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 625 | base::string16 GetChromiumModelIdForProfile( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 626 | const base::FilePath& profile_path) { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 627 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 628 | base::FilePath chrome_exe; |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 629 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 630 | NOTREACHED(); |
| 631 | return dist->GetBaseAppId(); |
| 632 | } |
| 633 | return GetAppModelIdForProfile( |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 634 | ShellUtil::GetBrowserModelId(dist, |
| 635 | InstallUtil::IsPerUserInstall(chrome_exe)), |
[email protected] | 78679969 | 2012-09-26 14:16:48 | [diff] [blame] | 636 | profile_path); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 637 | } |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 638 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 639 | void MigrateTaskbarPins() { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 640 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 641 | return; |
| 642 | |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 643 | // This needs to happen eventually (e.g. so that the appid is fixed and the |
| 644 | // run-time Chrome icon is merged with the taskbar shortcut), but this is not |
| 645 | // urgent and shouldn't delay Chrome startup. |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 646 | static const int64_t kMigrateTaskbarPinsDelaySeconds = 15; |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 647 | BrowserThread::PostDelayedTask( |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 648 | BrowserThread::FILE, FROM_HERE, |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 649 | base::Bind(&MigrateTaskbarPinsCallback), |
| 650 | base::TimeDelta::FromSeconds(kMigrateTaskbarPinsDelaySeconds)); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 651 | } |
[email protected] | 43903b8 | 2012-06-01 05:26:23 | [diff] [blame] | 652 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 653 | int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe, |
| 654 | const base::FilePath& path) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 655 | DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7); |
| 656 | |
| 657 | // Enumerate all pinned shortcuts in the given path directly. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 658 | base::FileEnumerator shortcuts_enum( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 659 | path, false, // not recursive |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 660 | base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk")); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 661 | |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 662 | bool is_per_user_install = InstallUtil::IsPerUserInstall(chrome_exe); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 663 | |
| 664 | int shortcuts_migrated = 0; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 665 | base::FilePath target_path; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 666 | base::string16 arguments; |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 667 | base::win::ScopedPropVariant propvariant; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 668 | for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 669 | shortcut = shortcuts_enum.Next()) { |
| 670 | // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once |
| 671 | // it is fixed to work with FilePaths with spaces. |
| 672 | if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) || |
| 673 | chrome_exe != target_path) { |
| 674 | continue; |
| 675 | } |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 676 | base::CommandLine command_line( |
| 677 | base::CommandLine::FromString(base::StringPrintf( |
| 678 | L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str()))); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 679 | |
| 680 | // Get the expected AppId for this Chrome shortcut. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 681 | base::string16 expected_app_id( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 682 | GetExpectedAppId(command_line, is_per_user_install)); |
| 683 | if (expected_app_id.empty()) |
| 684 | continue; |
| 685 | |
| 686 | // Load the shortcut. |
| 687 | base::win::ScopedComPtr<IShellLink> shell_link; |
| 688 | base::win::ScopedComPtr<IPersistFile> persist_file; |
| 689 | if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL, |
| 690 | CLSCTX_INPROC_SERVER)) || |
dcheng | 81762e0 | 2014-11-21 21:22:28 | [diff] [blame] | 691 | FAILED(persist_file.QueryFrom(shell_link.get())) || |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 692 | FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) { |
| 693 | DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value(); |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | // Any properties that need to be updated on the shortcut will be stored in |
| 698 | // |updated_properties|. |
| 699 | base::win::ShortcutProperties updated_properties; |
| 700 | |
| 701 | // Validate the existing app id for the shortcut. |
| 702 | base::win::ScopedComPtr<IPropertyStore> property_store; |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 703 | propvariant.Reset(); |
dcheng | 81762e0 | 2014-11-21 21:22:28 | [diff] [blame] | 704 | if (FAILED(property_store.QueryFrom(shell_link.get())) || |
| 705 | property_store->GetValue(PKEY_AppUserModel_ID, propvariant.Receive()) != |
| 706 | S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 707 | // When in doubt, prefer not updating the shortcut. |
| 708 | NOTREACHED(); |
| 709 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 710 | } else { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 711 | switch (propvariant.get().vt) { |
| 712 | case VT_EMPTY: |
| 713 | // If there is no app_id set, set our app_id if one is expected. |
| 714 | if (!expected_app_id.empty()) |
| 715 | updated_properties.set_app_id(expected_app_id); |
| 716 | break; |
| 717 | case VT_LPWSTR: |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 718 | if (expected_app_id != base::string16(propvariant.get().pwszVal)) |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 719 | updated_properties.set_app_id(expected_app_id); |
| 720 | break; |
| 721 | default: |
| 722 | NOTREACHED(); |
| 723 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 727 | // Clear dual_mode property from any shortcuts that previously had it (it |
| 728 | // was only ever installed on shortcuts with the |
| 729 | // |default_chromium_model_id|). |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 730 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 731 | base::string16 default_chromium_model_id( |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 732 | ShellUtil::GetBrowserModelId(dist, is_per_user_install)); |
gab | 88257b6 | 2016-01-15 03:19:19 | [diff] [blame] | 733 | if (expected_app_id == default_chromium_model_id) { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 734 | propvariant.Reset(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 735 | if (property_store->GetValue(PKEY_AppUserModel_IsDualMode, |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 736 | propvariant.Receive()) != S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 737 | // When in doubt, prefer to not update the shortcut. |
| 738 | NOTREACHED(); |
| 739 | continue; |
gab | 38d242d4d | 2016-01-14 20:38:12 | [diff] [blame] | 740 | } |
| 741 | if (propvariant.get().vt == VT_BOOL && |
| 742 | !!propvariant.get().boolVal) { |
| 743 | updated_properties.set_dual_mode(false); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | persist_file.Release(); |
| 748 | shell_link.Release(); |
| 749 | |
| 750 | // Update the shortcut if some of its properties need to be updated. |
| 751 | if (updated_properties.options && |
| 752 | base::win::CreateOrUpdateShortcutLink( |
| 753 | shortcut, updated_properties, |
| 754 | base::win::SHORTCUT_UPDATE_EXISTING)) { |
| 755 | ++shortcuts_migrated; |
| 756 | } |
| 757 | } |
| 758 | return shortcuts_migrated; |
| 759 | } |
| 760 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 761 | base::FilePath GetStartMenuShortcut(const base::FilePath& chrome_exe) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 762 | static const int kFolderIds[] = { |
| 763 | base::DIR_COMMON_START_MENU, |
| 764 | base::DIR_START_MENU, |
| 765 | }; |
| 766 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
tapted | d7ef82ae | 2016-05-13 01:10:19 | [diff] [blame] | 767 | const base::string16 shortcut_name(dist->GetShortcutName() + |
| 768 | installer::kLnkExt); |
grt | 2be0cd08 | 2015-11-12 15:32:21 | [diff] [blame] | 769 | base::FilePath programs_folder; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 770 | base::FilePath shortcut; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 771 | |
| 772 | // Check both the common and the per-user Start Menu folders for system-level |
| 773 | // installs. |
grt | e76ca285 | 2014-12-05 16:42:10 | [diff] [blame] | 774 | size_t folder = InstallUtil::IsPerUserInstall(chrome_exe) ? 1 : 0; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 775 | for (; folder < arraysize(kFolderIds); ++folder) { |
grt | 2be0cd08 | 2015-11-12 15:32:21 | [diff] [blame] | 776 | if (!PathService::Get(kFolderIds[folder], &programs_folder)) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 777 | NOTREACHED(); |
| 778 | continue; |
| 779 | } |
| 780 | |
grt | 2be0cd08 | 2015-11-12 15:32:21 | [diff] [blame] | 781 | shortcut = programs_folder.Append(shortcut_name); |
| 782 | if (base::PathExists(shortcut)) |
| 783 | return shortcut; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 784 | } |
| 785 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 786 | return base::FilePath(); |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 787 | } |
pmonette | f89ac7c7 | 2015-10-06 03:22:01 | [diff] [blame] | 788 | |
pmonette | 32a5cfb4 | 2016-04-11 22:04:44 | [diff] [blame] | 789 | } // namespace win |
| 790 | |
pmonette | 9fa59e88 | 2016-02-10 00:12:19 | [diff] [blame] | 791 | } // namespace shell_integration |