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