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