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