[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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> |
| 8 | #include <shlobj.h> |
| 9 | #include <shobjidl.h> |
| 10 | |
| 11 | #include "app/win_util.h" |
| 12 | #include "base/command_line.h" |
| 13 | #include "base/file_util.h" |
| 14 | #include "base/message_loop.h" |
| 15 | #include "base/path_service.h" |
| 16 | #include "base/registry.h" |
| 17 | #include "base/string_util.h" |
| 18 | #include "base/task.h" |
| 19 | #include "base/win_util.h" |
| 20 | #include "chrome/common/chrome_constants.h" |
| 21 | #include "chrome/installer/util/browser_distribution.h" |
| 22 | #include "chrome/installer/util/create_reg_key_work_item.h" |
| 23 | #include "chrome/installer/util/set_reg_value_work_item.h" |
| 24 | #include "chrome/installer/util/shell_util.h" |
| 25 | #include "chrome/installer/util/util_constants.h" |
| 26 | #include "chrome/installer/util/work_item.h" |
| 27 | #include "chrome/installer/util/work_item_list.h" |
| 28 | |
| 29 | bool ShellIntegration::SetAsDefaultBrowser() { |
| 30 | std::wstring chrome_exe; |
| 31 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 32 | LOG(ERROR) << "Error getting app exe path"; |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | // From UI currently we only allow setting default browser for current user. |
| 37 | if (!ShellUtil::MakeChromeDefault(ShellUtil::CURRENT_USER, |
| 38 | chrome_exe, true)) { |
| 39 | LOG(ERROR) << "Chrome could not be set as default browser."; |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | LOG(INFO) << "Chrome registered as default browser."; |
| 44 | return true; |
| 45 | } |
| 46 | |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 47 | ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() { |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 48 | // First determine the app path. If we can't determine what that is, we have |
| 49 | // bigger fish to fry... |
| 50 | std::wstring app_path; |
| 51 | if (!PathService::Get(base::FILE_EXE, &app_path)) { |
| 52 | LOG(ERROR) << "Error getting app exe path"; |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 53 | return UNKNOWN_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 54 | } |
| 55 | // When we check for default browser we don't necessarily want to count file |
| 56 | // type handlers and icons as having changed the default browser status, |
| 57 | // since the user may have changed their shell settings to cause HTML files |
| 58 | // to open with a text editor for example. We also don't want to aggressively |
| 59 | // claim FTP, since the user may have a separate FTP client. It is an open |
| 60 | // question as to how to "heal" these settings. Perhaps the user should just |
| 61 | // re-run the installer or run with the --set-default-browser command line |
| 62 | // flag. There is doubtless some other key we can hook into to cause "Repair" |
| 63 | // to show up in Add/Remove programs for us. |
| 64 | const std::wstring kChromeProtocols[] = {L"http", L"https"}; |
| 65 | |
| 66 | if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { |
| 67 | IApplicationAssociationRegistration* pAAR; |
| 68 | HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, |
| 69 | NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration), |
| 70 | (void**)&pAAR); |
| 71 | if (!SUCCEEDED(hr)) |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 72 | return UNKNOWN_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 73 | |
| 74 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 75 | std::wstring app_name = dist->GetApplicationName(); |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 76 | // If a user specific default browser entry exists, we check for that |
| 77 | // app name being default. If not, then default browser is just called |
| 78 | // Google Chrome or Chromium so we do not append suffix to app name. |
| 79 | std::wstring suffix; |
| 80 | if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(&suffix)) |
| 81 | app_name += suffix; |
| 82 | |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 83 | for (int i = 0; i < _countof(kChromeProtocols); i++) { |
| 84 | BOOL result = TRUE; |
| 85 | hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL, |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 86 | AL_EFFECTIVE, app_name.c_str(), &result); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 87 | if (!SUCCEEDED(hr)) { |
[email protected] | b31844b | 2009-08-14 23:46:21 | [diff] [blame] | 88 | pAAR->Release(); |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 89 | return UNKNOWN_DEFAULT_BROWSER; |
| 90 | } |
| 91 | if (result == FALSE) { |
| 92 | pAAR->Release(); |
| 93 | return NOT_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | pAAR->Release(); |
| 97 | } else { |
| 98 | std::wstring short_app_path; |
| 99 | GetShortPathName(app_path.c_str(), WriteInto(&short_app_path, MAX_PATH), |
| 100 | MAX_PATH); |
| 101 | |
| 102 | // open command for protocol associations |
| 103 | for (int i = 0; i < _countof(kChromeProtocols); i++) { |
| 104 | // Check in HKEY_CLASSES_ROOT that is the result of merge between |
| 105 | // HKLM and HKCU |
| 106 | HKEY root_key = HKEY_CLASSES_ROOT; |
| 107 | // Check <protocol>\shell\open\command |
| 108 | std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen); |
| 109 | RegKey key(root_key, key_path.c_str(), KEY_READ); |
| 110 | std::wstring value; |
| 111 | if (!key.Valid() || !key.ReadValue(L"", &value)) |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 112 | return UNKNOWN_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 113 | // Need to normalize path in case it's been munged. |
| 114 | CommandLine command_line(L""); |
| 115 | command_line.ParseFromString(value); |
| 116 | std::wstring short_path; |
| 117 | GetShortPathName(command_line.program().c_str(), |
| 118 | WriteInto(&short_path, MAX_PATH), MAX_PATH); |
| 119 | if ((short_path.size() != short_app_path.size()) || |
| 120 | (!std::equal(short_path.begin(), |
| 121 | short_path.end(), |
| 122 | short_app_path.begin(), |
| 123 | CaseInsensitiveCompare<wchar_t>()))) |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 124 | return NOT_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 125 | } |
| 126 | } |
[email protected] | 264f74d1 | 2009-09-04 23:39:58 | [diff] [blame^] | 127 | return IS_DEFAULT_BROWSER; |
[email protected] | d24c401 | 2009-07-28 01:57:31 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // There is no reliable way to say which browser is default on a machine (each |
| 131 | // browser can have some of the protocols/shortcuts). So we look for only HTTP |
| 132 | // protocol handler. Even this handler is located at different places in |
| 133 | // registry on XP and Vista: |
| 134 | // - HKCR\http\shell\open\command (XP) |
| 135 | // - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ |
| 136 | // http\UserChoice (Vista) |
| 137 | // This method checks if Firefox is defualt browser by checking these |
| 138 | // locations and returns true if Firefox traces are found there. In case of |
| 139 | // error (or if Firefox is not found)it returns the default value which |
| 140 | // is false. |
| 141 | bool ShellIntegration::IsFirefoxDefaultBrowser() { |
| 142 | bool ff_default = false; |
| 143 | if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) { |
| 144 | std::wstring app_cmd; |
| 145 | RegKey key(HKEY_CURRENT_USER, ShellUtil::kRegVistaUrlPrefs, KEY_READ); |
| 146 | if (key.Valid() && key.ReadValue(L"Progid", &app_cmd) && |
| 147 | app_cmd == L"FirefoxURL") |
| 148 | ff_default = true; |
| 149 | } else { |
| 150 | std::wstring key_path(L"http"); |
| 151 | key_path.append(ShellUtil::kRegShellOpen); |
| 152 | RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ); |
| 153 | std::wstring app_cmd; |
| 154 | if (key.Valid() && key.ReadValue(L"", &app_cmd) && |
| 155 | std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox")) |
| 156 | ff_default = true; |
| 157 | } |
| 158 | return ff_default; |
| 159 | } |