blob: 2d53d288d87dbb63848a6fcb46ddac629cacfb18 [file] [log] [blame]
[email protected]810a52ef2010-01-08 01:22:151// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]d24c4012009-07-28 01:57:312// 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]d24c4012009-07-28 01:57:318#include <shobjidl.h>
[email protected]935aa542010-10-15 01:59:159#include <propkey.h>
10#include <propvarutil.h>
[email protected]d24c4012009-07-28 01:57:3111
[email protected]d24c4012009-07-28 01:57:3112#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]c9bb06f42010-01-13 23:53:4816#include "base/scoped_comptr_win.h"
[email protected]d24c4012009-07-28 01:57:3117#include "base/string_util.h"
18#include "base/task.h"
[email protected]57ecc4b2010-08-11 03:02:5119#include "base/utf_string_conversions.h"
[email protected]d24c4012009-07-28 01:57:3120#include "base/win_util.h"
[email protected]2d6503982010-10-17 04:41:5421#include "base/win/registry.h"
[email protected]935aa542010-10-15 01:59:1522#include "base/win/windows_version.h"
[email protected]017a7a112010-10-12 16:38:2723#include "chrome/browser/browser_thread.h"
[email protected]c9bb06f42010-01-13 23:53:4824#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3125#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1526#include "chrome/common/chrome_paths.h"
27#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4828#include "chrome/common/chrome_switches.h"
[email protected]d24c4012009-07-28 01:57:3129#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"
36
[email protected]12f520c2010-01-06 18:11:1537namespace {
38
39// Helper function for ShellIntegration::GetAppId to generates profile id
40// from profile path. "profile_id" is composed of sanitized basenames of
41// user data dir and profile dir joined by a ".".
42std::wstring GetProfileIdFromPath(const FilePath& profile_path) {
43 // Return empty string if profile_path is empty
44 if (profile_path.empty())
[email protected]810a52ef2010-01-08 01:22:1545 return std::wstring();
[email protected]12f520c2010-01-06 18:11:1546
47 FilePath default_user_data_dir;
48 // Return empty string if profile_path is in default user data
49 // dir and is the default profile.
50 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
51 profile_path.DirName() == default_user_data_dir &&
52 profile_path.BaseName().value() == chrome::kNotSignedInProfile)
[email protected]810a52ef2010-01-08 01:22:1553 return std::wstring();
[email protected]12f520c2010-01-06 18:11:1554
55 // Get joined basenames of user data dir and profile.
56 std::wstring basenames = profile_path.DirName().BaseName().value() +
57 L"." + profile_path.BaseName().value();
58
59 std::wstring profile_id;
60 profile_id.reserve(basenames.size());
61
62 // Generate profile_id from sanitized basenames.
63 for (size_t i = 0; i < basenames.length(); ++i) {
64 if (IsAsciiAlpha(basenames[i]) ||
65 IsAsciiDigit(basenames[i]) ||
66 basenames[i] == L'.')
67 profile_id += basenames[i];
68 }
69
70 return profile_id;
71}
72
[email protected]c9bb06f42010-01-13 23:53:4873// Migrates existing chromium shortucts for Win7.
74class MigrateChromiumShortcutsTask : public Task {
75 public:
76 MigrateChromiumShortcutsTask() { }
77
78 virtual void Run();
79
80 private:
81 void MigrateWin7Shortcuts();
82 void MigrateWin7ShortcutsInPath(const FilePath& path) const;
83
84 // Get expected app id for given chrome shortcut.
85 // Returns true if the shortcut point to chrome and expected app id is
86 // successfully derived.
87 bool GetExpectedAppId(IShellLink* shell_link,
88 std::wstring* expected_app_id) const;
89
90 // Get app id associated with given shortcut.
91 bool GetShortcutAppId(IShellLink* shell_link, std::wstring* app_id) const;
92
93 FilePath chrome_exe_;
94
95 DISALLOW_COPY_AND_ASSIGN(MigrateChromiumShortcutsTask);
96};
97
98void MigrateChromiumShortcutsTask::Run() {
99 // This should run on the file thread.
[email protected]0c7d74f2010-10-11 11:55:26100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]c9bb06f42010-01-13 23:53:48101
102 MigrateWin7Shortcuts();
103}
104
105void MigrateChromiumShortcutsTask::MigrateWin7Shortcuts() {
106 // Get full path of chrome.
107 if (!PathService::Get(base::FILE_EXE, &chrome_exe_))
108 return;
109
110 // Locations to check for shortcuts migration.
111 static const struct {
112 int location_id;
113 const wchar_t* sub_dir;
114 } kLocations[] = {
115 {
116 base::DIR_APP_DATA,
117 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar"
118 }, {
119 chrome::DIR_USER_DESKTOP,
120 NULL
121 }, {
122 base::DIR_START_MENU,
123 NULL
124 }, {
125 base::DIR_APP_DATA,
126 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
127 }
128 };
129
130 for (int i = 0; i < arraysize(kLocations); ++i) {
131 FilePath path;
132 if (!PathService::Get(kLocations[i].location_id, &path)) {
133 NOTREACHED();
134 continue;
135 }
136
137 if (kLocations[i].sub_dir)
138 path = path.Append(kLocations[i].sub_dir);
139
140 MigrateWin7ShortcutsInPath(path);
141 }
142}
143
144void MigrateChromiumShortcutsTask::MigrateWin7ShortcutsInPath(
145 const FilePath& path) const {
146 // Enumerate all pinned shortcuts in the given path directly.
147 file_util::FileEnumerator shortcuts_enum(
148 path,
149 false, // not recursive
150 file_util::FileEnumerator::FILES,
151 FILE_PATH_LITERAL("*.lnk"));
152
153 for (FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
154 shortcut = shortcuts_enum.Next()) {
155 // Load the shortcut.
156 ScopedComPtr<IShellLink> shell_link;
157 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink,
158 NULL,
159 CLSCTX_INPROC_SERVER))) {
160 NOTREACHED();
161 return;
162 }
163
164 ScopedComPtr<IPersistFile> persist_file;
165 if (FAILED(persist_file.QueryFrom(shell_link)) ||
166 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
167 NOTREACHED();
168 return;
169 }
170
171 // Get expected app id from shortcut.
172 std::wstring expected_app_id;
173 if (!GetExpectedAppId(shell_link, &expected_app_id) ||
174 expected_app_id.empty())
175 continue;
176
177 // Get existing app id from shortcut if any.
178 std::wstring existing_app_id;
179 GetShortcutAppId(shell_link, &existing_app_id);
180
181 if (expected_app_id != existing_app_id) {
182 file_util::UpdateShortcutLink(NULL,
183 shortcut.value().c_str(),
184 NULL,
185 NULL,
186 NULL,
187 NULL,
188 0,
189 expected_app_id.c_str());
190 }
191 }
192}
193
194bool MigrateChromiumShortcutsTask::GetExpectedAppId(
195 IShellLink* shell_link,
196 std::wstring* expected_app_id) const {
197 DCHECK(shell_link);
198 DCHECK(expected_app_id);
199
200 expected_app_id->clear();
201
202 // Check if the shortcut points to chrome_exe.
203 std::wstring source;
204 if (FAILED(shell_link->GetPath(WriteInto(&source, MAX_PATH),
205 MAX_PATH,
206 NULL,
207 SLGP_RAWPATH)) ||
208 lstrcmpi(chrome_exe_.value().c_str(), source.c_str()))
209 return false;
210
211 std::wstring arguments;
212 if (FAILED(shell_link->GetArguments(WriteInto(&arguments, MAX_PATH),
213 MAX_PATH)))
214 return false;
215
216 // Get expected app id from shortcut command line.
217 CommandLine command_line = CommandLine::FromString(StringPrintf(
218 L"\"%ls\" %ls", source.c_str(), arguments.c_str()));
219
220 FilePath profile_path;
221 if (command_line.HasSwitch(switches::kUserDataDir)) {
[email protected]660e428f2010-08-04 01:23:00222 profile_path =
223 command_line.GetSwitchValuePath(switches::kUserDataDir).Append(
224 chrome::kNotSignedInProfile);
[email protected]c9bb06f42010-01-13 23:53:48225 }
226
227 std::wstring app_name;
228 if (command_line.HasSwitch(switches::kApp)) {
[email protected]57ecc4b2010-08-11 03:02:51229 app_name = UTF8ToWide(web_app::GenerateApplicationNameFromURL(
230 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]c9bb06f42010-01-13 23:53:48231 } else {
[email protected]1caa92612010-06-11 00:13:56232 app_name = BrowserDistribution::GetDistribution()->GetBrowserAppId();
[email protected]c9bb06f42010-01-13 23:53:48233 }
234
[email protected]1caa92612010-06-11 00:13:56235 expected_app_id->assign(ShellIntegration::GetAppId(app_name, profile_path));
[email protected]c9bb06f42010-01-13 23:53:48236 return true;
237}
238
239bool MigrateChromiumShortcutsTask::GetShortcutAppId(
240 IShellLink* shell_link,
241 std::wstring* app_id) const {
242 DCHECK(shell_link);
243 DCHECK(app_id);
244
245 app_id->clear();
246
247 ScopedComPtr<IPropertyStore> property_store;
248 if (FAILED(property_store.QueryFrom(shell_link)))
249 return false;
250
251 PROPVARIANT appid_value;
252 PropVariantInit(&appid_value);
[email protected]935aa542010-10-15 01:59:15253 if (FAILED(property_store->GetValue(PKEY_AppUserModel_ID,
[email protected]c9bb06f42010-01-13 23:53:48254 &appid_value)))
255 return false;
256
257 if (appid_value.vt == VT_LPWSTR ||
258 appid_value.vt == VT_BSTR)
259 app_id->assign(appid_value.pwszVal);
260
261 PropVariantClear(&appid_value);
262 return true;
263}
264
[email protected]12f520c2010-01-06 18:11:15265};
266
[email protected]d24c4012009-07-28 01:57:31267bool ShellIntegration::SetAsDefaultBrowser() {
268 std::wstring chrome_exe;
269 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
270 LOG(ERROR) << "Error getting app exe path";
271 return false;
272 }
273
274 // From UI currently we only allow setting default browser for current user.
275 if (!ShellUtil::MakeChromeDefault(ShellUtil::CURRENT_USER,
276 chrome_exe, true)) {
277 LOG(ERROR) << "Chrome could not be set as default browser.";
278 return false;
279 }
280
281 LOG(INFO) << "Chrome registered as default browser.";
282 return true;
283}
284
[email protected]264f74d12009-09-04 23:39:58285ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() {
[email protected]d24c4012009-07-28 01:57:31286 // First determine the app path. If we can't determine what that is, we have
287 // bigger fish to fry...
288 std::wstring app_path;
289 if (!PathService::Get(base::FILE_EXE, &app_path)) {
290 LOG(ERROR) << "Error getting app exe path";
[email protected]264f74d12009-09-04 23:39:58291 return UNKNOWN_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31292 }
293 // When we check for default browser we don't necessarily want to count file
294 // type handlers and icons as having changed the default browser status,
295 // since the user may have changed their shell settings to cause HTML files
296 // to open with a text editor for example. We also don't want to aggressively
297 // claim FTP, since the user may have a separate FTP client. It is an open
298 // question as to how to "heal" these settings. Perhaps the user should just
299 // re-run the installer or run with the --set-default-browser command line
300 // flag. There is doubtless some other key we can hook into to cause "Repair"
301 // to show up in Add/Remove programs for us.
302 const std::wstring kChromeProtocols[] = {L"http", L"https"};
303
[email protected]935aa542010-10-15 01:59:15304 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]d24c4012009-07-28 01:57:31305 IApplicationAssociationRegistration* pAAR;
306 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
307 NULL, CLSCTX_INPROC, __uuidof(IApplicationAssociationRegistration),
308 (void**)&pAAR);
309 if (!SUCCEEDED(hr))
[email protected]fe989f182009-11-25 22:54:00310 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31311
312 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
313 std::wstring app_name = dist->GetApplicationName();
[email protected]b31844b2009-08-14 23:46:21314 // If a user specific default browser entry exists, we check for that
315 // app name being default. If not, then default browser is just called
316 // Google Chrome or Chromium so we do not append suffix to app name.
317 std::wstring suffix;
318 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(&suffix))
319 app_name += suffix;
320
[email protected]d24c4012009-07-28 01:57:31321 for (int i = 0; i < _countof(kChromeProtocols); i++) {
322 BOOL result = TRUE;
323 hr = pAAR->QueryAppIsDefault(kChromeProtocols[i].c_str(), AT_URLPROTOCOL,
[email protected]b31844b2009-08-14 23:46:21324 AL_EFFECTIVE, app_name.c_str(), &result);
[email protected]fe989f182009-11-25 22:54:00325 if (!SUCCEEDED(hr) || result == FALSE) {
[email protected]264f74d12009-09-04 23:39:58326 pAAR->Release();
327 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31328 }
329 }
330 pAAR->Release();
331 } else {
332 std::wstring short_app_path;
333 GetShortPathName(app_path.c_str(), WriteInto(&short_app_path, MAX_PATH),
334 MAX_PATH);
335
336 // open command for protocol associations
337 for (int i = 0; i < _countof(kChromeProtocols); i++) {
338 // Check in HKEY_CLASSES_ROOT that is the result of merge between
339 // HKLM and HKCU
340 HKEY root_key = HKEY_CLASSES_ROOT;
341 // Check <protocol>\shell\open\command
342 std::wstring key_path(kChromeProtocols[i] + ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54343 base::win::RegKey key(root_key, key_path.c_str(), KEY_READ);
[email protected]d24c4012009-07-28 01:57:31344 std::wstring value;
345 if (!key.Valid() || !key.ReadValue(L"", &value))
[email protected]fe989f182009-11-25 22:54:00346 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31347 // Need to normalize path in case it's been munged.
[email protected]51343d5a2009-10-26 22:39:33348 CommandLine command_line = CommandLine::FromString(value);
[email protected]d24c4012009-07-28 01:57:31349 std::wstring short_path;
[email protected]fc8edf52010-10-14 20:30:45350 GetShortPathName(command_line.GetProgram().value().c_str(),
[email protected]d24c4012009-07-28 01:57:31351 WriteInto(&short_path, MAX_PATH), MAX_PATH);
[email protected]eccb9d12009-10-28 05:40:09352 if (!FilePath::CompareEqualIgnoreCase(short_path, short_app_path))
[email protected]264f74d12009-09-04 23:39:58353 return NOT_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31354 }
355 }
[email protected]264f74d12009-09-04 23:39:58356 return IS_DEFAULT_BROWSER;
[email protected]d24c4012009-07-28 01:57:31357}
358
359// There is no reliable way to say which browser is default on a machine (each
360// browser can have some of the protocols/shortcuts). So we look for only HTTP
361// protocol handler. Even this handler is located at different places in
362// registry on XP and Vista:
363// - HKCR\http\shell\open\command (XP)
364// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
365// http\UserChoice (Vista)
366// This method checks if Firefox is defualt browser by checking these
367// locations and returns true if Firefox traces are found there. In case of
368// error (or if Firefox is not found)it returns the default value which
369// is false.
370bool ShellIntegration::IsFirefoxDefaultBrowser() {
371 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15372 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]d24c4012009-07-28 01:57:31373 std::wstring app_cmd;
[email protected]2d6503982010-10-17 04:41:54374 base::win::RegKey key(HKEY_CURRENT_USER,
375 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]d24c4012009-07-28 01:57:31376 if (key.Valid() && key.ReadValue(L"Progid", &app_cmd) &&
377 app_cmd == L"FirefoxURL")
378 ff_default = true;
379 } else {
380 std::wstring key_path(L"http");
381 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54382 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]d24c4012009-07-28 01:57:31383 std::wstring app_cmd;
384 if (key.Valid() && key.ReadValue(L"", &app_cmd) &&
385 std::wstring::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
386 ff_default = true;
387 }
388 return ff_default;
389}
[email protected]12f520c2010-01-06 18:11:15390
[email protected]1caa92612010-06-11 00:13:56391std::wstring ShellIntegration::GetAppId(const std::wstring& app_name,
[email protected]12f520c2010-01-06 18:11:15392 const FilePath& profile_path) {
393 std::wstring app_id(app_name);
394
395 std::wstring profile_id(GetProfileIdFromPath(profile_path));
396 if (!profile_id.empty()) {
397 app_id += L".";
398 app_id += profile_id;
399 }
400
401 // App id should be less than 128 chars.
402 DCHECK(app_id.length() < 128);
403 return app_id;
404}
405
406std::wstring ShellIntegration::GetChromiumAppId(const FilePath& profile_path) {
[email protected]1caa92612010-06-11 00:13:56407 return GetAppId(BrowserDistribution::GetDistribution()->GetBrowserAppId(),
408 profile_path);
[email protected]12f520c2010-01-06 18:11:15409}
[email protected]c9bb06f42010-01-13 23:53:48410
411void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15412 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48413 return;
414
[email protected]0c7d74f2010-10-11 11:55:26415 BrowserThread::PostTask(
416 BrowserThread::FILE, FROM_HERE, new MigrateChromiumShortcutsTask());
[email protected]c9bb06f42010-01-13 23:53:48417}