blob: b35ac181b7d88eed5c686015756a47fb8df6b4fc [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"
[email protected]12f520c2010-01-06 18:11:1521#include "chrome/common/chrome_paths.h"
22#include "chrome/common/chrome_paths_internal.h"
[email protected]d24c4012009-07-28 01:57:3123#include "chrome/installer/util/browser_distribution.h"
24#include "chrome/installer/util/create_reg_key_work_item.h"
25#include "chrome/installer/util/set_reg_value_work_item.h"
26#include "chrome/installer/util/shell_util.h"
27#include "chrome/installer/util/util_constants.h"
28#include "chrome/installer/util/work_item.h"
29#include "chrome/installer/util/work_item_list.h"
30
[email protected]12f520c2010-01-06 18:11:1531namespace {
32
33// Helper function for ShellIntegration::GetAppId to generates profile id
34// from profile path. "profile_id" is composed of sanitized basenames of
35// user data dir and profile dir joined by a ".".
36std::wstring GetProfileIdFromPath(const FilePath& profile_path) {
37 // Return empty string if profile_path is empty
38 if (profile_path.empty())
39 return EmptyWString();
40
41 FilePath default_user_data_dir;
42 // Return empty string if profile_path is in default user data
43 // dir and is the default profile.
44 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
45 profile_path.DirName() == default_user_data_dir &&
46 profile_path.BaseName().value() == chrome::kNotSignedInProfile)
47 return EmptyWString();
48
49 // Get joined basenames of user data dir and profile.
50 std::wstring basenames = profile_path.DirName().BaseName().value() +
51 L"." + profile_path.BaseName().value();
52
53 std::wstring profile_id;
54 profile_id.reserve(basenames.size());
55
56 // Generate profile_id from sanitized basenames.
57 for (size_t i = 0; i < basenames.length(); ++i) {
58 if (IsAsciiAlpha(basenames[i]) ||
59 IsAsciiDigit(basenames[i]) ||
60 basenames[i] == L'.')
61 profile_id += basenames[i];
62 }
63
64 return profile_id;
65}
66
67};
68
[email protected]d24c4012009-07-28 01:57:3169bool ShellIntegration::SetAsDefaultBrowser() {
70 std::wstring chrome_exe;
71 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
72 LOG(ERROR) << "Error getting app exe path";
73 return false;
74 }
75
76 // From UI currently we only allow setting default browser for current user.
77 if (!ShellUtil::MakeChromeDefault(ShellUtil::CURRENT_USER,
78 chrome_exe, true)) {
79 LOG(ERROR) << "Chrome could not be set as default browser.";
80 return false;
81 }
82
83 LOG(INFO) << "Chrome registered as default browser.";
84 return true;
85}
86
[email protected]264f74d12009-09-04 23:39:5887ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() {
[email protected]d24c4012009-07-28 01:57:3188 // First determine the app path. If we can't determine what that is, we have
89 // bigger fish to fry...
90 std::wstring app_path;
91 if (!PathService::Get(base::FILE_EXE, &app_path)) {
92 LOG(ERROR) << "Error getting app exe path";
[email protected]264f74d12009-09-04 23:39:5893 return UNKNOWN_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:3194 }
95 // When we check for default browser we don't necessarily want to count file
96 // type handlers and icons as having changed the default browser status,
97 // since the user may have changed their shell settings to cause HTML files
98 // to open with a text editor for example. We also don't want to aggressively
99 // claim FTP, since the user may have a separate FTP client. It is an open
100 // question as to how to "heal" these settings. Perhaps the user should just
101 // re-run the installer or run with the --set-default-browser command line
102 // flag. There is doubtless some other key we can hook into to cause "Repair"
103 // to show up in Add/Remove programs for us.
104 const std::wstring kChromeProtocols[] = {L"http", L"https"};
105
106 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) {
107 IApplicationAssociationRegistration* pAAR;
108 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
109 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration),
110 (void**)&pAAR);
111 if (!SUCCEEDED(hr))
[email protected]fe989f182009-11-25 22:54:00112 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31113
114 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
115 std::wstring app_name = dist->GetApplicationName();
[email protected]b31844b2009-08-14 23:46:21116 // If a user specific default browser entry exists, we check for that
117 // app name being default. If not, then default browser is just called
118 // Google Chrome or Chromium so we do not append suffix to app name.
119 std::wstring suffix;
120 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(&suffix))
121 app_name += suffix;
122
[email protected]d24c4012009-07-28 01:57:31123 for (int i = 0; i < _countof(kChromeProtocols); i++) {
124 BOOL result = TRUE;
125 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
[email protected]b31844b2009-08-14 23:46:21126 AL_EFFECTIVE, app_name.c_str(), &result);
[email protected]fe989f182009-11-25 22:54:00127 if (!SUCCEEDED(hr) || result == FALSE) {
[email protected]264f74d12009-09-04 23:39:58128 pAAR->Release();
129 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31130 }
131 }
132 pAAR->Release();
133 } else {
134 std::wstring short_app_path;
135 GetShortPathName(app_path.c_str(), WriteInto(&short_app_path, MAX_PATH),
136 MAX_PATH);
137
138 // open command for protocol associations
139 for (int i = 0; i < _countof(kChromeProtocols); i++) {
140 // Check in HKEY_CLASSES_ROOT that is the result of merge between
141 // HKLM and HKCU
142 HKEY root_key = HKEY_CLASSES_ROOT;
143 // Check <protocol>\shell\open\command
144 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen);
145 RegKey key(root_key, key_path.c_str(), KEY_READ);
146 std::wstring value;
147 if (!key.Valid() || !key.ReadValue(L"", &value))
[email protected]fe989f182009-11-25 22:54:00148 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31149 // Need to normalize path in case it's been munged.
[email protected]51343d5a2009-10-26 22:39:33150 CommandLine command_line = CommandLine::FromString(value);
[email protected]d24c4012009-07-28 01:57:31151 std::wstring short_path;
152 GetShortPathName(command_line.program().c_str(),
153 WriteInto(&short_path, MAX_PATH), MAX_PATH);
[email protected]eccb9d12009-10-28 05:40:09154 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
[email protected]264f74d12009-09-04 23:39:58155 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31156 }
157 }
[email protected]264f74d12009-09-04 23:39:58158 return IS_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31159}
160
161// There is no reliable way to say which browser is default on a machine (each
162// browser can have some of the protocols/shortcuts). So we look for only HTTP
163// protocol handler. Even this handler is located at different places in
164// registry on XP and Vista:
165// - HKCR\http\shell\open\command (XP)
166// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
167// http\UserChoice (Vista)
168// This method checks if Firefox is defualt browser by checking these
169// locations and returns true if Firefox traces are found there. In case of
170// error (or if Firefox is not found)it returns the default value which
171// is false.
172bool ShellIntegration::IsFirefoxDefaultBrowser() {
173 bool ff_default = false;
174 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) {
175 std::wstring app_cmd;
176 RegKey key(HKEY_CURRENT_USER, ShellUtil::kRegVistaUrlPrefs, KEY_READ);
177 if (key.Valid() && key.ReadValue(L"Progid", &app_cmd) &&
178 app_cmd == L"FirefoxURL")
179 ff_default = true;
180 } else {
181 std::wstring key_path(L"http");
182 key_path.append(ShellUtil::kRegShellOpen);
183 RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
184 std::wstring app_cmd;
185 if (key.Valid() && key.ReadValue(L"", &app_cmd) &&
186 std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
187 ff_default = true;
188 }
189 return ff_default;
190}
[email protected]12f520c2010-01-06 18:11:15191
192std::wstring ShellIntegration::GetAppId(const wchar_t* app_name,
193 const FilePath& profile_path) {
194 std::wstring app_id(app_name);
195
196 std::wstring profile_id(GetProfileIdFromPath(profile_path));
197 if (!profile_id.empty()) {
198 app_id += L".";
199 app_id += profile_id;
200 }
201
202 // App id should be less than 128 chars.
203 DCHECK(app_id.length() < 128);
204 return app_id;
205}
206
207std::wstring ShellIntegration::GetChromiumAppId(const FilePath& profile_path) {
208 return GetAppId(chrome::kBrowserAppID, profile_path);
209}