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