blob: 6f1957a74f78536cee3f142d7f167773b7f65c64 [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
47bool ShellIntegration::IsDefaultBrowser() {
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";
53 return false;
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))
72 return false;
73
74 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
75 std::wstring app_name = dist->GetApplicationName();
76 std::wstring app_name_with_suffix;
77 ShellUtil::GetUserSpecificDefaultBrowserSuffix(&app_name_with_suffix);
78 app_name_with_suffix = app_name + app_name_with_suffix;
79 for (int i = 0; i < _countof(kChromeProtocols); i++) {
80 BOOL result = TRUE;
81 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
82 AL_EFFECTIVE, app_name_with_suffix.c_str(), &result);
83 if (!SUCCEEDED(hr) || (result == FALSE)) {
84 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(),
85 AT_URLPROTOCOL, AL_EFFECTIVE, app_name.c_str(), &result);
86 if (!SUCCEEDED(hr) || (result == FALSE)) {
87 pAAR->Release();
88 return false;
89 }
90 }
91 }
92 pAAR->Release();
93 } else {
94 std::wstring short_app_path;
95 GetShortPathName(app_path.c_str(), WriteInto(&short_app_path, MAX_PATH),
96 MAX_PATH);
97
98 // open command for protocol associations
99 for (int i = 0; i < _countof(kChromeProtocols); i++) {
100 // Check in HKEY_CLASSES_ROOT that is the result of merge between
101 // HKLM and HKCU
102 HKEY root_key = HKEY_CLASSES_ROOT;
103 // Check <protocol>\shell\open\command
104 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen);
105 RegKey key(root_key, key_path.c_str(), KEY_READ);
106 std::wstring value;
107 if (!key.Valid() || !key.ReadValue(L"", &value))
108 return false;
109 // Need to normalize path in case it's been munged.
110 CommandLine command_line(L"");
111 command_line.ParseFromString(value);
112 std::wstring short_path;
113 GetShortPathName(command_line.program().c_str(),
114 WriteInto(&short_path, MAX_PATH), MAX_PATH);
115 if ((short_path.size() != short_app_path.size()) ||
116 (!std::equal(short_path.begin(),
117 short_path.end(),
118 short_app_path.begin(),
119 CaseInsensitiveCompare<wchar_t>())))
120 return false;
121 }
122 }
123 return true;
124}
125
126// There is no reliable way to say which browser is default on a machine (each
127// browser can have some of the protocols/shortcuts). So we look for only HTTP
128// protocol handler. Even this handler is located at different places in
129// registry on XP and Vista:
130// - HKCR\http\shell\open\command (XP)
131// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
132// http\UserChoice (Vista)
133// This method checks if Firefox is defualt browser by checking these
134// locations and returns true if Firefox traces are found there. In case of
135// error (or if Firefox is not found)it returns the default value which
136// is false.
137bool ShellIntegration::IsFirefoxDefaultBrowser() {
138 bool ff_default = false;
139 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) {
140 std::wstring app_cmd;
141 RegKey key(HKEY_CURRENT_USER, ShellUtil::kRegVistaUrlPrefs, KEY_READ);
142 if (key.Valid() && key.ReadValue(L"Progid", &app_cmd) &&
143 app_cmd == L"FirefoxURL")
144 ff_default = true;
145 } else {
146 std::wstring key_path(L"http");
147 key_path.append(ShellUtil::kRegShellOpen);
148 RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
149 std::wstring app_cmd;
150 if (key.Valid() && key.ReadValue(L"", &app_cmd) &&
151 std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
152 ff_default = true;
153 }
154 return ff_default;
155}