[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 1 | // Copyright (c) 2011 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> |
| 10 | #include <propvarutil.h> |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 11 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 12 | #include "base/command_line.h" |
| 13 | #include "base/file_util.h" |
| 14 | #include "base/message_loop.h" |
| 15 | #include "base/path_service.h" |
[email protected] | 2de2d62 | 2011-10-21 22:07:37 | [diff] [blame^] | 16 | #include "base/string_number_conversions.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 33272e1f0 | 2011-08-17 00:22:07 | [diff] [blame] | 18 | #include "base/stringprintf.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 19 | #include "base/task.h" |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 20 | #include "base/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] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 23 | #include "base/win/windows_version.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 24 | #include "chrome/browser/web_applications/web_app.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 25 | #include "chrome/common/chrome_constants.h" |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 26 | #include "chrome/common/chrome_paths.h" |
| 27 | #include "chrome/common/chrome_paths_internal.h" |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 28 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 29 | #include "chrome/installer/setup/setup_util.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 30 | #include "chrome/installer/util/browser_distribution.h" |
| 31 | #include "chrome/installer/util/create_reg_key_work_item.h" |
| 32 | #include "chrome/installer/util/set_reg_value_work_item.h" |
| 33 | #include "chrome/installer/util/shell_util.h" |
| 34 | #include "chrome/installer/util/util_constants.h" |
| 35 | #include "chrome/installer/util/work_item.h" |
| 36 | #include "chrome/installer/util/work_item_list.h" |
[email protected] | 1625ffd | 2011-03-01 17:51:50 | [diff] [blame] | 37 | #include "content/browser/browser_thread.h" |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 38 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | // Helper function for ShellIntegration::GetAppId to generates profile id |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 42 | // from profile path. "profile_id" is composed of sanitized basenames of |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 43 | // user data dir and profile dir joined by a ".". |
| 44 | std::wstring GetProfileIdFromPath(const FilePath& profile_path) { |
| 45 | // Return empty string if profile_path is empty |
| 46 | if (profile_path.empty()) |
[email protected] | 810a52ef | 2010-01-08 01:22:15 | [diff] [blame] | 47 | return std::wstring(); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 48 | |
| 49 | FilePath default_user_data_dir; |
| 50 | // Return empty string if profile_path is in default user data |
| 51 | // dir and is the default profile. |
| 52 | if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) && |
| 53 | profile_path.DirName() == default_user_data_dir && |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 54 | profile_path.BaseName().value() == |
[email protected] | d778d6e | 2011-08-12 09:47:05 | [diff] [blame] | 55 | ASCIIToUTF16(chrome::kInitialProfile)) { |
[email protected] | 810a52ef | 2010-01-08 01:22:15 | [diff] [blame] | 56 | return std::wstring(); |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 57 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 58 | |
| 59 | // Get joined basenames of user data dir and profile. |
| 60 | std::wstring basenames = profile_path.DirName().BaseName().value() + |
| 61 | L"." + profile_path.BaseName().value(); |
| 62 | |
| 63 | std::wstring profile_id; |
| 64 | profile_id.reserve(basenames.size()); |
| 65 | |
| 66 | // Generate profile_id from sanitized basenames. |
| 67 | for (size_t i = 0; i < basenames.length(); ++i) { |
| 68 | if (IsAsciiAlpha(basenames[i]) || |
| 69 | IsAsciiDigit(basenames[i]) || |
| 70 | basenames[i] == L'.') |
| 71 | profile_id += basenames[i]; |
| 72 | } |
| 73 | |
| 74 | return profile_id; |
| 75 | } |
| 76 | |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 77 | // Migrates existing chromium shortucts for Win7. |
| 78 | class MigrateChromiumShortcutsTask : public Task { |
| 79 | public: |
| 80 | MigrateChromiumShortcutsTask() { } |
| 81 | |
| 82 | virtual void Run(); |
| 83 | |
| 84 | private: |
| 85 | void MigrateWin7Shortcuts(); |
| 86 | void MigrateWin7ShortcutsInPath(const FilePath& path) const; |
| 87 | |
| 88 | // Get expected app id for given chrome shortcut. |
| 89 | // Returns true if the shortcut point to chrome and expected app id is |
| 90 | // successfully derived. |
| 91 | bool GetExpectedAppId(IShellLink* shell_link, |
| 92 | std::wstring* expected_app_id) const; |
| 93 | |
| 94 | // Get app id associated with given shortcut. |
| 95 | bool GetShortcutAppId(IShellLink* shell_link, std::wstring* app_id) const; |
| 96 | |
| 97 | FilePath chrome_exe_; |
| 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(MigrateChromiumShortcutsTask); |
| 100 | }; |
| 101 | |
| 102 | void MigrateChromiumShortcutsTask::Run() { |
| 103 | // This should run on the file thread. |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 104 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 105 | |
| 106 | MigrateWin7Shortcuts(); |
| 107 | } |
| 108 | |
| 109 | void MigrateChromiumShortcutsTask::MigrateWin7Shortcuts() { |
| 110 | // Get full path of chrome. |
| 111 | if (!PathService::Get(base::FILE_EXE, &chrome_exe_)) |
| 112 | return; |
| 113 | |
| 114 | // Locations to check for shortcuts migration. |
| 115 | static const struct { |
| 116 | int location_id; |
| 117 | const wchar_t* sub_dir; |
| 118 | } kLocations[] = { |
| 119 | { |
| 120 | base::DIR_APP_DATA, |
| 121 | L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar" |
| 122 | }, { |
| 123 | chrome::DIR_USER_DESKTOP, |
| 124 | NULL |
| 125 | }, { |
| 126 | base::DIR_START_MENU, |
| 127 | NULL |
| 128 | }, { |
| 129 | base::DIR_APP_DATA, |
| 130 | L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu" |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | for (int i = 0; i < arraysize(kLocations); ++i) { |
| 135 | FilePath path; |
| 136 | if (!PathService::Get(kLocations[i].location_id, &path)) { |
| 137 | NOTREACHED(); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if (kLocations[i].sub_dir) |
| 142 | path = path.Append(kLocations[i].sub_dir); |
| 143 | |
| 144 | MigrateWin7ShortcutsInPath(path); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void MigrateChromiumShortcutsTask::MigrateWin7ShortcutsInPath( |
| 149 | const FilePath& path) const { |
| 150 | // Enumerate all pinned shortcuts in the given path directly. |
| 151 | file_util::FileEnumerator shortcuts_enum( |
| 152 | path, |
| 153 | false, // not recursive |
| 154 | file_util::FileEnumerator::FILES, |
| 155 | FILE_PATH_LITERAL("*.lnk")); |
| 156 | |
| 157 | for (FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty(); |
| 158 | shortcut = shortcuts_enum.Next()) { |
| 159 | // Load the shortcut. |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 160 | base::win::ScopedComPtr<IShellLink> shell_link; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 161 | if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, |
| 162 | NULL, |
| 163 | CLSCTX_INPROC_SERVER))) { |
| 164 | NOTREACHED(); |
| 165 | return; |
| 166 | } |
| 167 | |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 168 | base::win::ScopedComPtr<IPersistFile> persist_file; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 169 | if (FAILED(persist_file.QueryFrom(shell_link)) || |
| 170 | FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) { |
| 171 | NOTREACHED(); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | // Get expected app id from shortcut. |
| 176 | std::wstring expected_app_id; |
| 177 | if (!GetExpectedAppId(shell_link, &expected_app_id) || |
| 178 | expected_app_id.empty()) |
| 179 | continue; |
| 180 | |
| 181 | // Get existing app id from shortcut if any. |
| 182 | std::wstring existing_app_id; |
| 183 | GetShortcutAppId(shell_link, &existing_app_id); |
| 184 | |
| 185 | if (expected_app_id != existing_app_id) { |
| 186 | file_util::UpdateShortcutLink(NULL, |
| 187 | shortcut.value().c_str(), |
| 188 | NULL, |
| 189 | NULL, |
| 190 | NULL, |
| 191 | NULL, |
| 192 | 0, |
| 193 | expected_app_id.c_str()); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | bool MigrateChromiumShortcutsTask::GetExpectedAppId( |
| 199 | IShellLink* shell_link, |
| 200 | std::wstring* expected_app_id) const { |
| 201 | DCHECK(shell_link); |
| 202 | DCHECK(expected_app_id); |
| 203 | |
| 204 | expected_app_id->clear(); |
| 205 | |
| 206 | // Check if the shortcut points to chrome_exe. |
| 207 | std::wstring source; |
| 208 | if (FAILED(shell_link->GetPath(WriteInto(&source, MAX_PATH), |
| 209 | MAX_PATH, |
| 210 | NULL, |
| 211 | SLGP_RAWPATH)) || |
| 212 | lstrcmpi(chrome_exe_.value().c_str(), source.c_str())) |
| 213 | return false; |
| 214 | |
| 215 | std::wstring arguments; |
| 216 | if (FAILED(shell_link->GetArguments(WriteInto(&arguments, MAX_PATH), |
| 217 | MAX_PATH))) |
| 218 | return false; |
| 219 | |
| 220 | // Get expected app id from shortcut command line. |
[email protected] | 33272e1f0 | 2011-08-17 00:22:07 | [diff] [blame] | 221 | CommandLine command_line = CommandLine::FromString(base::StringPrintf( |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 222 | L"\"%ls\" %ls", source.c_str(), arguments.c_str())); |
| 223 | |
| 224 | FilePath profile_path; |
| 225 | if (command_line.HasSwitch(switches::kUserDataDir)) { |
[email protected] | 660e428f | 2010-08-04 01:23:00 | [diff] [blame] | 226 | profile_path = |
[email protected] | 162b599 | 2011-03-15 19:40:48 | [diff] [blame] | 227 | command_line.GetSwitchValuePath(switches::kUserDataDir).AppendASCII( |
[email protected] | d778d6e | 2011-08-12 09:47:05 | [diff] [blame] | 228 | chrome::kInitialProfile); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | std::wstring app_name; |
| 232 | if (command_line.HasSwitch(switches::kApp)) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 233 | app_name = UTF8ToWide(web_app::GenerateApplicationNameFromURL( |
| 234 | GURL(command_line.GetSwitchValueASCII(switches::kApp)))); |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 235 | } else if (command_line.HasSwitch(switches::kAppId)) { |
| 236 | app_name = UTF8ToWide(web_app::GenerateApplicationNameFromExtensionId( |
| 237 | command_line.GetSwitchValueASCII(switches::kAppId))); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 238 | } else { |
[email protected] | 1caa9261 | 2010-06-11 00:13:56 | [diff] [blame] | 239 | app_name = BrowserDistribution::GetDistribution()->GetBrowserAppId(); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 1caa9261 | 2010-06-11 00:13:56 | [diff] [blame] | 242 | expected_app_id->assign(ShellIntegration::GetAppId(app_name, profile_path)); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | |
| 246 | bool MigrateChromiumShortcutsTask::GetShortcutAppId( |
| 247 | IShellLink* shell_link, |
| 248 | std::wstring* app_id) const { |
| 249 | DCHECK(shell_link); |
| 250 | DCHECK(app_id); |
| 251 | |
| 252 | app_id->clear(); |
| 253 | |
[email protected] | 8ee65ba | 2011-04-12 20:53:23 | [diff] [blame] | 254 | base::win::ScopedComPtr<IPropertyStore> property_store; |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 255 | if (FAILED(property_store.QueryFrom(shell_link))) |
| 256 | return false; |
| 257 | |
| 258 | PROPVARIANT appid_value; |
| 259 | PropVariantInit(&appid_value); |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 260 | if (FAILED(property_store->GetValue(PKEY_AppUserModel_ID, |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 261 | &appid_value))) |
| 262 | return false; |
| 263 | |
| 264 | if (appid_value.vt == VT_LPWSTR || |
| 265 | appid_value.vt == VT_BSTR) |
| 266 | app_id->assign(appid_value.pwszVal); |
| 267 | |
| 268 | PropVariantClear(&appid_value); |
| 269 | return true; |
| 270 | } |
| 271 | |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 272 | }; |
| 273 | |
[email protected] | a01481b | 2011-07-15 04:30:02 | [diff] [blame] | 274 | bool ShellIntegration::CanSetAsDefaultBrowser() { |
| 275 | return BrowserDistribution::GetDistribution()->CanSetAsDefault(); |
| 276 | } |
| 277 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 278 | bool ShellIntegration::SetAsDefaultBrowser() { |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 279 | FilePath chrome_exe; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 280 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 281 | LOG(ERROR) << "Error getting app exe path"; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // From UI currently we only allow setting default browser for current user. |
[email protected] | bf6117c7e | 2010-12-01 06:00:25 | [diff] [blame] | 286 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 287 | if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 288 | chrome_exe.value(), true)) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 289 | LOG(ERROR) << "Chrome could not be set as default browser."; |
| 290 | return false; |
| 291 | } |
| 292 | |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 293 | VLOG(1) << "Chrome registered as default browser."; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 297 | bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) { |
| 298 | if (protocol.empty()) |
| 299 | return false; |
| 300 | |
| 301 | FilePath chrome_exe; |
| 302 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 303 | LOG(ERROR) << "Error getting app exe path"; |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | std::wstring wprotocol = UTF8ToWide(protocol); |
| 308 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 309 | if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(), |
| 310 | wprotocol)) { |
| 311 | LOG(ERROR) << "Chrome could not be set as default handler for " |
| 312 | << protocol << "."; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | VLOG(1) << "Chrome registered as default handler for " << protocol << "."; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | ShellIntegration::DefaultWebClientState ShellIntegration::IsDefaultBrowser() { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 321 | // First determine the app path. If we can't determine what that is, we have |
| 322 | // bigger fish to fry... |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 323 | FilePath app_path; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 324 | if (!PathService::Get(base::FILE_EXE, &app_path)) { |
| 325 | LOG(ERROR) << "Error getting app exe path"; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 326 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 327 | } |
| 328 | // When we check for default browser we don't necessarily want to count file |
| 329 | // type handlers and icons as having changed the default browser status, |
| 330 | // since the user may have changed their shell settings to cause HTML files |
| 331 | // to open with a text editor for example. We also don't want to aggressively |
| 332 | // claim FTP, since the user may have a separate FTP client. It is an open |
| 333 | // question as to how to "heal" these settings. Perhaps the user should just |
| 334 | // re-run the installer or run with the --set-default-browser command line |
| 335 | // flag. There is doubtless some other key we can hook into to cause "Repair" |
| 336 | // to show up in Add/Remove programs for us. |
| 337 | const std::wstring kChromeProtocols[] = {L"http", L"https"}; |
| 338 | |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 339 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 340 | base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR; |
| 341 | HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration, |
| 342 | NULL, CLSCTX_INPROC); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 343 | if (!SUCCEEDED(hr)) |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 344 | return NOT_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 345 | |
| 346 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 347 | std::wstring app_name = dist->GetApplicationName(); |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 348 | // If a user specific default browser entry exists, we check for that |
| 349 | // app name being default. If not, then default browser is just called |
| 350 | // Google Chrome or Chromium so we do not append suffix to app name. |
| 351 | std::wstring suffix; |
[email protected] | bf6117c7e | 2010-12-01 06:00:25 | [diff] [blame] | 352 | if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 353 | app_name += suffix; |
| 354 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 355 | for (int i = 0; i < _countof(kChromeProtocols); i++) { |
| 356 | BOOL result = TRUE; |
| 357 | hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL, |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 358 | AL_EFFECTIVE, app_name.c_str(), &result); |
[email protected] | fe989f18 | 2009-11-25 22:54:00 | [diff] [blame] | 359 | if (!SUCCEEDED(hr) || result == FALSE) { |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 360 | return NOT_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 361 | } |
| 362 | } |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 363 | } else { |
| 364 | std::wstring short_app_path; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 365 | DWORD get_path_result = GetShortPathName(app_path.value().c_str(), |
| 366 | WriteInto(&short_app_path, MAX_PATH), MAX_PATH); |
| 367 | if (!get_path_result || get_path_result > MAX_PATH) { |
| 368 | LOG(ERROR) << "GetShortPathName error in IsDefaultBrowser."; |
| 369 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 370 | } |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 371 | |
| 372 | // open command for protocol associations |
| 373 | for (int i = 0; i < _countof(kChromeProtocols); i++) { |
| 374 | // Check in HKEY_CLASSES_ROOT that is the result of merge between |
| 375 | // HKLM and HKCU |
| 376 | HKEY root_key = HKEY_CLASSES_ROOT; |
| 377 | // Check <protocol>\shell\open\command |
| 378 | std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 379 | base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 380 | std::wstring value; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 381 | if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 382 | return NOT_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 383 | // Need to normalize path in case it's been munged. |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 384 | CommandLine command_line = CommandLine::FromString(value); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 385 | std::wstring short_path; |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 386 | get_path_result = GetShortPathName( |
| 387 | command_line.GetProgram().value().c_str(), |
| 388 | WriteInto(&short_path, MAX_PATH), MAX_PATH); |
| 389 | if (!get_path_result || get_path_result > MAX_PATH) { |
| 390 | LOG(ERROR) << "GetShortPathName error in IsDefaultBrowser."; |
| 391 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 392 | } |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 393 | if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 394 | return NOT_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 395 | } |
| 396 | } |
[email protected] | 4468a5b | 2011-05-26 07:48:02 | [diff] [blame] | 397 | return IS_DEFAULT_WEB_CLIENT; |
| 398 | } |
| 399 | |
| 400 | ShellIntegration::DefaultWebClientState |
| 401 | ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) { |
| 402 | if (protocol.empty()) |
| 403 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 404 | |
| 405 | // Determine the app path. If we can't determine what that is, we have |
| 406 | // bigger fish to fry... |
| 407 | FilePath app_path; |
| 408 | if (!PathService::Get(base::FILE_EXE, &app_path)) { |
| 409 | LOG(ERROR) << "Error getting app exe path"; |
| 410 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 411 | } |
| 412 | |
| 413 | std::wstring wprotocol = UTF8ToWide(protocol); |
| 414 | |
| 415 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 416 | base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR; |
| 417 | HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration, |
| 418 | NULL, CLSCTX_INPROC); |
| 419 | if (!SUCCEEDED(hr)) |
| 420 | return NOT_DEFAULT_WEB_CLIENT; |
| 421 | |
| 422 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 423 | std::wstring app_name = dist->GetApplicationName(); |
| 424 | // If a user specific default browser entry exists, we check for that |
| 425 | // app name being default. If not, then default browser is just called |
| 426 | // Google Chrome or Chromium so we do not append suffix to app name. |
| 427 | std::wstring suffix; |
| 428 | if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) |
| 429 | app_name += suffix; |
| 430 | |
| 431 | BOOL result = TRUE; |
| 432 | hr = pAAR->QueryAppIsDefault(wprotocol.c_str(), AT_URLPROTOCOL, |
| 433 | AL_EFFECTIVE, app_name.c_str(), &result); |
| 434 | if (!SUCCEEDED(hr) || result == FALSE) { |
| 435 | return NOT_DEFAULT_WEB_CLIENT; |
| 436 | } |
| 437 | } else { |
| 438 | std::wstring short_app_path; |
| 439 | DWORD get_path_result = GetShortPathName(app_path.value().c_str(), |
| 440 | WriteInto(&short_app_path, MAX_PATH), MAX_PATH); |
| 441 | if (!get_path_result || get_path_result > MAX_PATH) { |
| 442 | LOG(ERROR) << "GetShortPathName error in IsDefaultProtocolClient."; |
| 443 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 444 | } |
| 445 | |
| 446 | // open command for protocol associations |
| 447 | // Check in HKEY_CLASSES_ROOT that is the result of merge between |
| 448 | // HKLM and HKCU |
| 449 | HKEY root_key = HKEY_CLASSES_ROOT; |
| 450 | // Check <protocol>\shell\open\command |
| 451 | std::wstring key_path(wprotocol + ShellUtil::kRegShellOpen); |
| 452 | base::win::RegKey key(root_key, key_path.c_str(), KEY_READ); |
| 453 | std::wstring value; |
| 454 | if (!key.Valid() || (key.ReadValue(L"", &value) != ERROR_SUCCESS)) |
| 455 | return NOT_DEFAULT_WEB_CLIENT; |
| 456 | // Need to normalize path in case it's been munged. |
| 457 | CommandLine command_line = CommandLine::FromString(value); |
| 458 | std::wstring short_path; |
| 459 | get_path_result = GetShortPathName( |
| 460 | command_line.GetProgram().value().c_str(), |
| 461 | WriteInto(&short_path, MAX_PATH), MAX_PATH); |
| 462 | if (!get_path_result || get_path_result > MAX_PATH) { |
| 463 | LOG(ERROR) << "GetShortPathName error in IsDefaultProtocolClient."; |
| 464 | return UNKNOWN_DEFAULT_WEB_CLIENT; |
| 465 | } |
| 466 | if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path)) |
| 467 | return NOT_DEFAULT_WEB_CLIENT; |
| 468 | } |
| 469 | return IS_DEFAULT_WEB_CLIENT; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // There is no reliable way to say which browser is default on a machine (each |
| 473 | // browser can have some of the protocols/shortcuts). So we look for only HTTP |
| 474 | // protocol handler. Even this handler is located at different places in |
| 475 | // registry on XP and Vista: |
| 476 | // - HKCR\http\shell\open\command (XP) |
| 477 | // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ |
| 478 | // http\UserChoice (Vista) |
| 479 | // This method checks if Firefox is defualt browser by checking these |
| 480 | // locations and returns true if Firefox traces are found there. In case of |
| 481 | // error (or if Firefox is not found)it returns the default value which |
| 482 | // is false. |
| 483 | bool ShellIntegration::IsFirefoxDefaultBrowser() { |
| 484 | bool ff_default = false; |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 485 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 486 | std::wstring app_cmd; |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 487 | base::win::RegKey key(HKEY_CURRENT_USER, |
| 488 | ShellUtil::kRegVistaUrlPrefs, KEY_READ); |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 489 | if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 490 | app_cmd == L"FirefoxURL") |
| 491 | ff_default = true; |
| 492 | } else { |
| 493 | std::wstring key_path(L"http"); |
| 494 | key_path.append(ShellUtil::kRegShellOpen); |
[email protected] | 2d650398 | 2010-10-17 04:41:54 | [diff] [blame] | 495 | base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 496 | std::wstring app_cmd; |
[email protected] | e06f4d5 | 2011-01-19 07:28:46 | [diff] [blame] | 497 | if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) && |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 498 | std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox")) |
| 499 | ff_default = true; |
| 500 | } |
| 501 | return ff_default; |
| 502 | } |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 503 | |
[email protected] | 1caa9261 | 2010-06-11 00:13:56 | [diff] [blame] | 504 | std::wstring ShellIntegration::GetAppId(const std::wstring& app_name, |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 505 | const FilePath& profile_path) { |
| 506 | std::wstring app_id(app_name); |
| 507 | |
| 508 | std::wstring profile_id(GetProfileIdFromPath(profile_path)); |
| 509 | if (!profile_id.empty()) { |
| 510 | app_id += L"."; |
| 511 | app_id += profile_id; |
| 512 | } |
| 513 | |
| 514 | // App id should be less than 128 chars. |
| 515 | DCHECK(app_id.length() < 128); |
| 516 | return app_id; |
| 517 | } |
| 518 | |
| 519 | std::wstring ShellIntegration::GetChromiumAppId(const FilePath& profile_path) { |
[email protected] | 1caa9261 | 2010-06-11 00:13:56 | [diff] [blame] | 520 | return GetAppId(BrowserDistribution::GetDistribution()->GetBrowserAppId(), |
| 521 | profile_path); |
[email protected] | 12f520c | 2010-01-06 18:11:15 | [diff] [blame] | 522 | } |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 523 | |
[email protected] | 2de2d62 | 2011-10-21 22:07:37 | [diff] [blame^] | 524 | string16 ShellIntegration::GetChromiumIconPath() { |
| 525 | // Determine the app path. If we can't determine what that is, we have |
| 526 | // bigger fish to fry... |
| 527 | FilePath app_path; |
| 528 | if (!PathService::Get(base::FILE_EXE, &app_path)) { |
| 529 | NOTREACHED(); |
| 530 | return string16(); |
| 531 | } |
| 532 | |
| 533 | string16 icon_path(app_path.value()); |
| 534 | icon_path.push_back(','); |
| 535 | icon_path += base::IntToString16( |
| 536 | BrowserDistribution::GetDistribution()->GetIconIndex()); |
| 537 | return icon_path; |
| 538 | } |
| 539 | |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 540 | void ShellIntegration::MigrateChromiumShortcuts() { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 541 | if (base::win::GetVersion() < base::win::VERSION_WIN7) |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 542 | return; |
| 543 | |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 544 | BrowserThread::PostTask( |
| 545 | BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask()); |
[email protected] | c9bb06f4 | 2010-01-13 23:53:48 | [diff] [blame] | 546 | } |