[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/shell_integration.h" |
| 6 | |
| 7 | #include <windows.h> |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 8 | #include <shobjidl.h> |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 9 | #include <propkey.h> |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 10 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 12 | #include "base/command_line.h" |
| 13 | #include "base/file_util.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 14 | #include "base/files/file_enumerator.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/strings/stringprintf.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 21 | #include "base/win/registry.h" |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 22 | #include "base/win/scoped_comptr.h" |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 23 | #include "base/win/scoped_propvariant.h" |
[email protected] | f1024e2 | 2012-09-12 07:14:55 | [diff] [blame] | 24 | #include "base/win/shortcut.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 25 | #include "base/win/windows_version.h" |
[email protected] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 26 | #include "chrome/browser/policy/policy_path_parser.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 27 | #include "chrome/browser/web_applications/web_app.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 28 | #include "chrome/common/chrome_constants.h" |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 29 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 30 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 31 | #include "chrome/installer/setup/setup_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 32 | #include "chrome/installer/util/browser_distribution.h" |
| 33 | #include "chrome/installer/util/create_reg_key_work_item.h" |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 34 | #include "chrome/installer/util/install_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 35 | #include "chrome/installer/util/set_reg_value_work_item.h" |
| 36 | #include "chrome/installer/util/shell_util.h" |
| 37 | #include "chrome/installer/util/util_constants.h" |
| 38 | #include "chrome/installer/util/work_item.h" |
| 39 | #include "chrome/installer/util/work_item_list.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 40 | #include "content/public/browser/browser_thread.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 41 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 42 | using content::BrowserThread; |
| 43 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 44 | namespace { |
| 45 | |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 46 | const wchar_t kAppListAppNameSuffix[] = L"AppList"; |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 47 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 48 | // Helper function for ShellIntegration::GetAppId to generates profile id |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 49 | // from profile path. "profile_id" is composed of sanitized basenames of |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 50 | // user data dir and profile dir joined by a ".". |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 51 | base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 52 | // Return empty string if profile_path is empty |
| 53 | if (profile_path.empty()) |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 54 | return base::string16(); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 55 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 56 | base::FilePath default_user_data_dir; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 57 | // Return empty string if profile_path is in default user data |
| 58 | // dir and is the default profile. |
| 59 | if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) && |
| 60 | profile_path.DirName() == default_user_data_dir && |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 61 | profile_path.BaseName().value() == |
[email protected] | d778d6e | 2011-08-12 09:47:05 | [diff] [blame] | 62 | ASCIIToUTF16(chrome::kInitialProfile)) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 63 | return base::string16(); |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 64 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 65 | |
| 66 | // Get joined basenames of user data dir and profile. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 67 | base::string16 basenames = profile_path.DirName().BaseName().value() + |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 68 | L"." + profile_path.BaseName().value(); |
| 69 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 70 | base::string16 profile_id; |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 71 | profile_id.reserve(basenames.size()); |
| 72 | |
| 73 | // Generate profile_id from sanitized basenames. |
| 74 | for (size_t i = 0; i < basenames.length(); ++i) { |
| 75 | if (IsAsciiAlpha(basenames[i]) || |
| 76 | IsAsciiDigit(basenames[i]) || |
| 77 | basenames[i] == L'.') |
| 78 | profile_id += basenames[i]; |
| 79 | } |
| 80 | |
| 81 | return profile_id; |
| 82 | } |
| 83 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 84 | base::string16 GetAppListAppName() { |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 85 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 86 | base::string16 app_name(dist->GetBaseAppId()); |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 87 | app_name.append(kAppListAppNameSuffix); |
| 88 | return app_name; |
| 89 | } |
| 90 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 91 | // Gets expected app id for given Chrome (based on |command_line| and |
| 92 | // |is_per_user_install|). |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 93 | base::string16 GetExpectedAppId(const CommandLine& command_line, |
| 94 | bool is_per_user_install) { |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 95 | base::FilePath user_data_dir; |
| 96 | if (command_line.HasSwitch(switches::kUserDataDir)) |
| 97 | user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir); |
| 98 | else |
| 99 | chrome::GetDefaultUserDataDirectory(&user_data_dir); |
[email protected] | 89d4383 | 2013-06-29 20:25:20 | [diff] [blame] | 100 | // Adjust with any policy that overrides any other way to set the path. |
| 101 | policy::path_parser::CheckUserDataDirPolicy(&user_data_dir); |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 102 | DCHECK(!user_data_dir.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 103 | |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 104 | base::FilePath profile_subdir; |
| 105 | if (command_line.HasSwitch(switches::kProfileDirectory)) { |
| 106 | profile_subdir = |
| 107 | command_line.GetSwitchValuePath(switches::kProfileDirectory); |
| 108 | } else { |
| 109 | profile_subdir = base::FilePath(ASCIIToUTF16(chrome::kInitialProfile)); |
| 110 | } |
| 111 | DCHECK(!profile_subdir.empty()); |
| 112 | |
| 113 | base::FilePath profile_path = user_data_dir.Append(profile_subdir); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 114 | base::string16 app_name; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 115 | if (command_line.HasSwitch(switches::kApp)) { |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 116 | app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 117 | GURL(command_line.GetSwitchValueASCII(switches::kApp)))); |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 118 | } else if (command_line.HasSwitch(switches::kAppId)) { |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 119 | app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId( |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 120 | command_line.GetSwitchValueASCII(switches::kAppId))); |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 121 | } else if (command_line.HasSwitch(switches::kShowAppList)) { |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 122 | app_name = GetAppListAppName(); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 123 | } else { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 124 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 125 | app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 126 | } |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 127 | DCHECK(!app_name.empty()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 128 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 129 | return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 132 | void MigrateChromiumShortcutsCallback() { |
| 133 | // This should run on the file thread. |
| 134 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 | |
| 136 | // Get full path of chrome. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 137 | base::FilePath chrome_exe; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 138 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 139 | return; |
| 140 | |
| 141 | // Locations to check for shortcuts migration. |
| 142 | static const struct { |
| 143 | int location_id; |
| 144 | const wchar_t* sub_dir; |
| 145 | } kLocations[] = { |
| 146 | { |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 147 | base::DIR_TASKBAR_PINS, |
| 148 | NULL |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 149 | }, { |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 150 | base::DIR_USER_DESKTOP, |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 151 | NULL |
| 152 | }, { |
| 153 | base::DIR_START_MENU, |
| 154 | NULL |
| 155 | }, { |
| 156 | base::DIR_APP_DATA, |
| 157 | L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu" |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | for (int i = 0; i < arraysize(kLocations); ++i) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 162 | base::FilePath path; |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 163 | if (!PathService::Get(kLocations[i].location_id, &path)) { |
| 164 | NOTREACHED(); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (kLocations[i].sub_dir) |
| 169 | path = path.Append(kLocations[i].sub_dir); |
| 170 | |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 171 | bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU); |
| 172 | ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path, |
| 173 | check_dual_mode); |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 177 | ShellIntegration::DefaultWebClientState |
| 178 | GetDefaultWebClientStateFromShellUtilDefaultState( |
| 179 | ShellUtil::DefaultState default_state) { |
| 180 | switch (default_state) { |
| 181 | case ShellUtil::NOT_DEFAULT: |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 182 | return ShellIntegration::NOT_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 183 | case ShellUtil::IS_DEFAULT: |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 184 | return ShellIntegration::IS_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 185 | default: |
| 186 | DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 187 | return ShellIntegration::UNKNOWN_DEFAULT; |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 191 | } // namespace |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 192 | |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 193 | ShellIntegration::DefaultWebClientSetPermission |
| 194 | ShellIntegration::CanSetAsDefaultBrowser() { |
[email protected] | ee5396f60 | 2013-10-23 18:52:54 | [diff] [blame] | 195 | BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 196 | if (distribution->GetDefaultBrowserControlPolicy() != |
| 197 | BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 198 | return SET_DEFAULT_NOT_ALLOWED; |
| 199 | |
[email protected] | 9fcf07f | 2012-06-18 16:13:51 | [diff] [blame] | 200 | if (ShellUtil::CanMakeChromeDefaultUnattended()) |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 201 | return SET_DEFAULT_UNATTENDED; |
[email protected] | 9fcf07f | 2012-06-18 16:13:51 | [diff] [blame] | 202 | else |
| 203 | return SET_DEFAULT_INTERACTIVE; |
[email protected] | a01481b | 2011-07-15 04:30:02 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 206 | bool ShellIntegration::SetAsDefaultBrowser() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 207 | base::FilePath chrome_exe; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 208 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 209 | LOG(ERROR) << "Error getting app exe path"; |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // From UI currently we only allow setting default browser for current user. |
[email protected] | bf6117c7e | 2010-12-01 06:00:25 | [diff] [blame] | 214 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 215 | if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 216 | chrome_exe.value(), true)) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 217 | LOG(ERROR) << "Chrome could not be set as default browser."; |
| 218 | return false; |
| 219 | } |
| 220 | |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 221 | VLOG(1) << "Chrome registered as default browser."; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 222 | return true; |
| 223 | } |
| 224 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 225 | bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) { |
| 226 | if (protocol.empty()) |
| 227 | return false; |
| 228 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 229 | base::FilePath chrome_exe; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 230 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 231 | LOG(ERROR) << "Error getting app exe path"; |
| 232 | return false; |
| 233 | } |
| 234 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 235 | base::string16 wprotocol(UTF8ToUTF16(protocol)); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 236 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 237 | if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(), |
| 238 | wprotocol)) { |
| 239 | LOG(ERROR) << "Chrome could not be set as default handler for " |
| 240 | << protocol << "."; |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | VLOG(1) << "Chrome registered as default handler for " << protocol << "."; |
| 245 | return true; |
| 246 | } |
| 247 | |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 248 | bool ShellIntegration::SetAsDefaultBrowserInteractive() { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 249 | base::FilePath chrome_exe; |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 250 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 251 | NOTREACHED() << "Error getting app exe path"; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 256 | if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) { |
| 257 | LOG(ERROR) << "Failed to launch the set-default-browser Windows UI."; |
| 258 | return false; |
| 259 | } |
| 260 | |
[email protected] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 261 | VLOG(1) << "Set-default-browser Windows UI completed."; |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | bool ShellIntegration::SetAsDefaultProtocolClientInteractive( |
| 266 | const std::string& protocol) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 267 | base::FilePath chrome_exe; |
[email protected] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 268 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 269 | NOTREACHED() << "Error getting app exe path"; |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 274 | base::string16 wprotocol(UTF8ToUTF16(protocol)); |
[email protected] | ee9d89d | 2012-09-25 18:21:03 | [diff] [blame] | 275 | if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI( |
| 276 | dist, chrome_exe.value(), wprotocol)) { |
| 277 | LOG(ERROR) << "Failed to launch the set-default-client Windows UI."; |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | VLOG(1) << "Set-default-client Windows UI completed."; |
[email protected] | bd046bd4 | 2012-06-08 05:07:32 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
[email protected] | 8988665 | 2012-12-11 18:09:07 | [diff] [blame] | 285 | ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() { |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 286 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
[email protected] | 8ad11c55 | 2012-10-16 17:40:52 | [diff] [blame] | 287 | ShellUtil::GetChromeDefaultState()); |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | ShellIntegration::DefaultWebClientState |
| 291 | ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) { |
[email protected] | eb63da7 | 2012-10-15 20:39:48 | [diff] [blame] | 292 | return GetDefaultWebClientStateFromShellUtilDefaultState( |
[email protected] | 8ad11c55 | 2012-10-16 17:40:52 | [diff] [blame] | 293 | ShellUtil::GetChromeDefaultProtocolClientState(UTF8ToUTF16(protocol))); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | 42dc940 | 2013-01-30 07:54:20 | [diff] [blame] | 296 | std::string ShellIntegration::GetApplicationForProtocol(const GURL& url) { |
| 297 | // TODO(calamity): this will be implemented when external_protocol_dialog is |
| 298 | // refactored on windows. |
| 299 | NOTREACHED(); |
| 300 | return std::string(); |
| 301 | } |
| 302 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 303 | // There is no reliable way to say which browser is default on a machine (each |
| 304 | // browser can have some of the protocols/shortcuts). So we look for only HTTP |
| 305 | // protocol handler. Even this handler is located at different places in |
| 306 | // registry on XP and Vista: |
| 307 | // - HKCR\http\shell\open\command (XP) |
| 308 | // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ |
| 309 | // http\UserChoice (Vista) |
| 310 | // This method checks if Firefox is defualt browser by checking these |
| 311 | // locations and returns true if Firefox traces are found there. In case of |
| 312 | // error (or if Firefox is not found)it returns the default value which |
| 313 | // is false. |
| 314 | bool ShellIntegration::IsFirefoxDefaultBrowser() { |
| 315 | bool ff_default = false; |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 316 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 317 | base::string16 app_cmd; |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 318 | base::win::RegKey key(HKEY_CURRENT_USER, |
| 319 | ShellUtil::kRegVistaUrlPrefs, KEY_READ); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 320 | if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 321 | app_cmd == L"FirefoxURL") |
| 322 | ff_default = true; |
| 323 | } else { |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 324 | base::string16 key_path(L"http"); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 325 | key_path.append(ShellUtil::kRegShellOpen); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 326 | base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 327 | base::string16 app_cmd; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 328 | if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 329 | base::string16::npos != StringToLowerASCII(app_cmd).find(L"firefox")) |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 330 | ff_default = true; |
| 331 | } |
| 332 | return ff_default; |
| 333 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 334 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 335 | base::string16 ShellIntegration::GetAppModelIdForProfile( |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 336 | const base::string16& app_name, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 337 | const base::FilePath& profile_path) { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 338 | std::vector<string16> components; |
| 339 | components.push_back(app_name); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 340 | const base::string16 profile_id(GetProfileIdFromPath(profile_path)); |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 341 | if (!profile_id.empty()) |
| 342 | components.push_back(profile_id); |
| 343 | return ShellUtil::BuildAppModelId(components); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 346 | base::string16 ShellIntegration::GetChromiumModelIdForProfile( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 347 | const base::FilePath& profile_path) { |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 348 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 349 | base::FilePath chrome_exe; |
[email protected] | a044800 | 2012-06-19 04:32:10 | [diff] [blame] | 350 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 351 | NOTREACHED(); |
| 352 | return dist->GetBaseAppId(); |
| 353 | } |
| 354 | return GetAppModelIdForProfile( |
[email protected] | 78679969 | 2012-09-26 14:16:48 | [diff] [blame] | 355 | ShellUtil::GetBrowserModelId( |
| 356 | dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())), |
| 357 | profile_path); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 358 | } |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 359 | |
[email protected] | 6a72a63 | 2013-12-12 22:22:00 | [diff] [blame^] | 360 | base::string16 ShellIntegration::GetAppListAppModelIdForProfile( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 361 | const base::FilePath& profile_path) { |
[email protected] | da1ffc3 | 2013-02-15 21:22:56 | [diff] [blame] | 362 | return ShellIntegration::GetAppModelIdForProfile( |
| 363 | GetAppListAppName(), profile_path); |
[email protected] | 99002fd | 2012-11-06 04:35:52 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 366 | void ShellIntegration::MigrateChromiumShortcuts() { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 367 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 368 | return; |
| 369 | |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 370 | // This needs to happen eventually (e.g. so that the appid is fixed and the |
| 371 | // run-time Chrome icon is merged with the taskbar shortcut), but this is not |
| 372 | // urgent and shouldn't delay Chrome startup. |
| 373 | static const int64 kMigrateChromiumShortcutsDelaySeconds = 15; |
| 374 | BrowserThread::PostDelayedTask( |
[email protected] | 3a3e72c | 2011-11-29 02:59:38 | [diff] [blame] | 375 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 81fcf95 | 2012-11-26 22:25:14 | [diff] [blame] | 376 | base::Bind(&MigrateChromiumShortcutsCallback), |
| 377 | base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds)); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 378 | } |
[email protected] | 43903b8 | 2012-06-01 05:26:23 | [diff] [blame] | 379 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 380 | int ShellIntegration::MigrateShortcutsInPathInternal( |
| 381 | const base::FilePath& chrome_exe, |
| 382 | const base::FilePath& path, |
| 383 | bool check_dual_mode) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 384 | DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7); |
| 385 | |
| 386 | // Enumerate all pinned shortcuts in the given path directly. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 387 | base::FileEnumerator shortcuts_enum( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 388 | path, false, // not recursive |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 389 | base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk")); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 390 | |
| 391 | bool is_per_user_install = |
| 392 | InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()); |
| 393 | |
| 394 | int shortcuts_migrated = 0; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 395 | base::FilePath target_path; |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 396 | base::string16 arguments; |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 397 | base::win::ScopedPropVariant propvariant; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 398 | for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 399 | shortcut = shortcuts_enum.Next()) { |
| 400 | // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once |
| 401 | // it is fixed to work with FilePaths with spaces. |
| 402 | if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) || |
| 403 | chrome_exe != target_path) { |
| 404 | continue; |
| 405 | } |
| 406 | CommandLine command_line(CommandLine::FromString(base::StringPrintf( |
| 407 | L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str()))); |
| 408 | |
| 409 | // Get the expected AppId for this Chrome shortcut. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 410 | base::string16 expected_app_id( |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 411 | GetExpectedAppId(command_line, is_per_user_install)); |
| 412 | if (expected_app_id.empty()) |
| 413 | continue; |
| 414 | |
| 415 | // Load the shortcut. |
| 416 | base::win::ScopedComPtr<IShellLink> shell_link; |
| 417 | base::win::ScopedComPtr<IPersistFile> persist_file; |
| 418 | if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL, |
| 419 | CLSCTX_INPROC_SERVER)) || |
| 420 | FAILED(persist_file.QueryFrom(shell_link)) || |
| 421 | FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) { |
| 422 | DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value(); |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | // Any properties that need to be updated on the shortcut will be stored in |
| 427 | // |updated_properties|. |
| 428 | base::win::ShortcutProperties updated_properties; |
| 429 | |
| 430 | // Validate the existing app id for the shortcut. |
| 431 | base::win::ScopedComPtr<IPropertyStore> property_store; |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 432 | propvariant.Reset(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 433 | if (FAILED(property_store.QueryFrom(shell_link)) || |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 434 | property_store->GetValue(PKEY_AppUserModel_ID, |
| 435 | propvariant.Receive()) != S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 436 | // When in doubt, prefer not updating the shortcut. |
| 437 | NOTREACHED(); |
| 438 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 439 | } else { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 440 | switch (propvariant.get().vt) { |
| 441 | case VT_EMPTY: |
| 442 | // If there is no app_id set, set our app_id if one is expected. |
| 443 | if (!expected_app_id.empty()) |
| 444 | updated_properties.set_app_id(expected_app_id); |
| 445 | break; |
| 446 | case VT_LPWSTR: |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 447 | if (expected_app_id != base::string16(propvariant.get().pwszVal)) |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 448 | updated_properties.set_app_id(expected_app_id); |
| 449 | break; |
| 450 | default: |
| 451 | NOTREACHED(); |
| 452 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 456 | // Only set dual mode if the expected app id is the default app id. |
| 457 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 458 | base::string16 default_chromium_model_id( |
[email protected] | b39e32e | 2013-04-24 08:55:54 | [diff] [blame] | 459 | ShellUtil::GetBrowserModelId(dist, is_per_user_install)); |
| 460 | if (check_dual_mode && expected_app_id == default_chromium_model_id) { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 461 | propvariant.Reset(); |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 462 | if (property_store->GetValue(PKEY_AppUserModel_IsDualMode, |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 463 | propvariant.Receive()) != S_OK) { |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 464 | // When in doubt, prefer to not update the shortcut. |
| 465 | NOTREACHED(); |
| 466 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 467 | } else { |
[email protected] | 0798330 | 2013-01-21 19:41:44 | [diff] [blame] | 468 | switch (propvariant.get().vt) { |
| 469 | case VT_EMPTY: |
| 470 | // If dual_mode is not set at all, make sure it gets set to true. |
| 471 | updated_properties.set_dual_mode(true); |
| 472 | break; |
| 473 | case VT_BOOL: |
| 474 | // If it is set to false, make sure it gets set to true as well. |
| 475 | if (!propvariant.get().boolVal) |
| 476 | updated_properties.set_dual_mode(true); |
| 477 | break; |
| 478 | default: |
| 479 | NOTREACHED(); |
| 480 | continue; |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 481 | } |
[email protected] | 8ea8f1ef | 2013-01-06 18:39:03 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | persist_file.Release(); |
| 486 | shell_link.Release(); |
| 487 | |
| 488 | // Update the shortcut if some of its properties need to be updated. |
| 489 | if (updated_properties.options && |
| 490 | base::win::CreateOrUpdateShortcutLink( |
| 491 | shortcut, updated_properties, |
| 492 | base::win::SHORTCUT_UPDATE_EXISTING)) { |
| 493 | ++shortcuts_migrated; |
| 494 | } |
| 495 | } |
| 496 | return shortcuts_migrated; |
| 497 | } |
| 498 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 499 | base::FilePath ShellIntegration::GetStartMenuShortcut( |
| 500 | const base::FilePath& chrome_exe) { |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 501 | static const int kFolderIds[] = { |
| 502 | base::DIR_COMMON_START_MENU, |
| 503 | base::DIR_START_MENU, |
| 504 | }; |
| 505 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 506 | base::string16 shortcut_name( |
[email protected] | 16fcc0d | 2013-08-14 04:22:13 | [diff] [blame] | 507 | dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 508 | base::FilePath shortcut; |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 509 | |
| 510 | // Check both the common and the per-user Start Menu folders for system-level |
| 511 | // installs. |
| 512 | size_t folder = |
| 513 | InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0; |
| 514 | for (; folder < arraysize(kFolderIds); ++folder) { |
| 515 | if (!PathService::Get(kFolderIds[folder], &shortcut)) { |
| 516 | NOTREACHED(); |
| 517 | continue; |
| 518 | } |
| 519 | |
[email protected] | 126f462 | 2012-10-05 01:34:05 | [diff] [blame] | 520 | shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + |
| 521 | installer::kLnkExt); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 522 | if (base::PathExists(shortcut)) |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 523 | return shortcut; |
| 524 | } |
| 525 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 526 | return base::FilePath(); |
[email protected] | 3f69d6e61 | 2012-08-03 18:52:27 | [diff] [blame] | 527 | } |