blob: cebc421754a34c1930961d6dc49da1ff48e7ae23 [file] [log] [blame]
[email protected]d24c4012009-07-28 01:57:311// 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
29bool 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]264f74d12009-09-04 23:39:5847ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() {
[email protected]d24c4012009-07-28 01:57:3148 // 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]264f74d12009-09-04 23:39:5853 return UNKNOWN_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:3154 }
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]264f74d12009-09-04 23:39:5872 return UNKNOWN_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:3173
74 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
75 std::wstring app_name = dist->GetApplicationName();
[email protected]b31844b2009-08-14 23:46:2176 // 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]d24c4012009-07-28 01:57:3183 for (int i = 0; i < _countof(kChromeProtocols); i++) {
84 BOOL result = TRUE;
85 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
[email protected]b31844b2009-08-14 23:46:2186 AL_EFFECTIVE, app_name.c_str(), &result);
[email protected]264f74d12009-09-04 23:39:5887 if (!SUCCEEDED(hr)) {
[email protected]b31844b2009-08-14 23:46:2188 pAAR->Release();
[email protected]264f74d12009-09-04 23:39:5889 return UNKNOWN_DEFAULT_BROWSER;
90 }
91 if (result == FALSE) {
92 pAAR->Release();
93 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:3194 }
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]264f74d12009-09-04 23:39:58112 return UNKNOWN_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31113 // 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]264f74d12009-09-04 23:39:58124 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31125 }
126 }
[email protected]264f74d12009-09-04 23:39:58127 return IS_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31128}
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.
141bool 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}