blob: 8184442e5e811d01eff6f6bdfbd6636e7ce91fa2 [file] [log] [blame]
[email protected]b6b72222012-02-11 02:04:131// Copyright (c) 2012 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>
[email protected]d24c4012009-07-28 01:57:3110
[email protected]3a3e72c2011-11-29 02:59:3811#include "base/bind.h"
[email protected]d24c4012009-07-28 01:57:3112#include "base/command_line.h"
13#include "base/file_util.h"
[email protected]25a4c1c2013-06-08 04:53:3614#include "base/files/file_enumerator.h"
[email protected]d24c4012009-07-28 01:57:3115#include "base/message_loop.h"
16#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4117#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1718#include "base/strings/string_util.h"
19#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0820#include "base/strings/utf_string_conversions.h"
[email protected]2d6503982010-10-17 04:41:5421#include "base/win/registry.h"
[email protected]8ee65ba2011-04-12 20:53:2322#include "base/win/scoped_comptr.h"
[email protected]07983302013-01-21 19:41:4423#include "base/win/scoped_propvariant.h"
[email protected]f1024e22012-09-12 07:14:5524#include "base/win/shortcut.h"
[email protected]935aa542010-10-15 01:59:1525#include "base/win/windows_version.h"
[email protected]c9bb06f42010-01-13 23:53:4826#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3127#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1528#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4829#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0230#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3131#include "chrome/installer/util/browser_distribution.h"
32#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2733#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3134#include "chrome/installer/util/set_reg_value_work_item.h"
35#include "chrome/installer/util/shell_util.h"
36#include "chrome/installer/util/util_constants.h"
37#include "chrome/installer/util/work_item.h"
38#include "chrome/installer/util/work_item_list.h"
[email protected]c38831a12011-10-28 12:44:4939#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3140
[email protected]631bb742011-11-02 11:29:3941using content::BrowserThread;
42
[email protected]12f520c2010-01-06 18:11:1543namespace {
44
[email protected]da1ffc32013-02-15 21:22:5645const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5246
[email protected]12f520c2010-01-06 18:11:1547// Helper function for ShellIntegration::GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1448// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1549// user data dir and profile dir joined by a ".".
[email protected]650b2d52013-02-10 03:41:4550string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1551 // Return empty string if profile_path is empty
52 if (profile_path.empty())
[email protected]b6b72222012-02-11 02:04:1353 return string16();
[email protected]12f520c2010-01-06 18:11:1554
[email protected]650b2d52013-02-10 03:41:4555 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1556 // Return empty string if profile_path is in default user data
57 // dir and is the default profile.
58 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
59 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4860 profile_path.BaseName().value() ==
[email protected]d778d6e2011-08-12 09:47:0561 ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]b6b72222012-02-11 02:04:1362 return string16();
[email protected]162b5992011-03-15 19:40:4863 }
[email protected]12f520c2010-01-06 18:11:1564
65 // Get joined basenames of user data dir and profile.
[email protected]b6b72222012-02-11 02:04:1366 string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1567 L"." + profile_path.BaseName().value();
68
[email protected]b6b72222012-02-11 02:04:1369 string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1570 profile_id.reserve(basenames.size());
71
72 // Generate profile_id from sanitized basenames.
73 for (size_t i = 0; i < basenames.length(); ++i) {
74 if (IsAsciiAlpha(basenames[i]) ||
75 IsAsciiDigit(basenames[i]) ||
76 basenames[i] == L'.')
77 profile_id += basenames[i];
78 }
79
80 return profile_id;
81}
82
[email protected]da1ffc32013-02-15 21:22:5683string16 GetAppListAppName() {
84 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
85 string16 app_name(dist->GetBaseAppId());
86 app_name.append(kAppListAppNameSuffix);
87 return app_name;
88}
89
[email protected]8ea8f1ef2013-01-06 18:39:0390// Gets expected app id for given Chrome (based on |command_line| and
91// |is_per_user_install|).
92string16 GetExpectedAppId(const CommandLine& command_line,
93 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:5494 base::FilePath user_data_dir;
95 if (command_line.HasSwitch(switches::kUserDataDir))
96 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
97 else
98 chrome::GetDefaultUserDataDirectory(&user_data_dir);
99 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:48100
[email protected]b39e32e2013-04-24 08:55:54101 base::FilePath profile_subdir;
102 if (command_line.HasSwitch(switches::kProfileDirectory)) {
103 profile_subdir =
104 command_line.GetSwitchValuePath(switches::kProfileDirectory);
105 } else {
106 profile_subdir = base::FilePath(ASCIIToUTF16(chrome::kInitialProfile));
107 }
108 DCHECK(!profile_subdir.empty());
109
110 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]b6b72222012-02-11 02:04:13111 string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48112 if (command_line.HasSwitch(switches::kApp)) {
[email protected]b6b72222012-02-11 02:04:13113 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51114 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14115 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]b6b72222012-02-11 02:04:13116 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId(
[email protected]2f1c09d2011-01-14 14:58:14117 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52118 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56119 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48120 } else {
[email protected]a0448002012-06-19 04:32:10121 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03122 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48123 }
[email protected]b39e32e2013-04-24 08:55:54124 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48125
[email protected]8ea8f1ef2013-01-06 18:39:03126 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48127}
128
[email protected]3a3e72c2011-11-29 02:59:38129void MigrateChromiumShortcutsCallback() {
130 // This should run on the file thread.
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
132
133 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45134 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38135 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
136 return;
137
138 // Locations to check for shortcuts migration.
139 static const struct {
140 int location_id;
141 const wchar_t* sub_dir;
142 } kLocations[] = {
143 {
[email protected]e5f9d822012-11-06 22:27:01144 base::DIR_TASKBAR_PINS,
145 NULL
[email protected]3a3e72c2011-11-29 02:59:38146 }, {
[email protected]dea1d7d2012-09-20 16:24:52147 base::DIR_USER_DESKTOP,
[email protected]3a3e72c2011-11-29 02:59:38148 NULL
149 }, {
150 base::DIR_START_MENU,
151 NULL
152 }, {
153 base::DIR_APP_DATA,
154 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
155 }
156 };
157
158 for (int i = 0; i < arraysize(kLocations); ++i) {
[email protected]650b2d52013-02-10 03:41:45159 base::FilePath path;
[email protected]3a3e72c2011-11-29 02:59:38160 if (!PathService::Get(kLocations[i].location_id, &path)) {
161 NOTREACHED();
162 continue;
163 }
164
165 if (kLocations[i].sub_dir)
166 path = path.Append(kLocations[i].sub_dir);
167
[email protected]8ea8f1ef2013-01-06 18:39:03168 bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
169 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
170 check_dual_mode);
[email protected]3a3e72c2011-11-29 02:59:38171 }
172}
173
[email protected]eb63da72012-10-15 20:39:48174ShellIntegration::DefaultWebClientState
175 GetDefaultWebClientStateFromShellUtilDefaultState(
176 ShellUtil::DefaultState default_state) {
177 switch (default_state) {
178 case ShellUtil::NOT_DEFAULT:
[email protected]89886652012-12-11 18:09:07179 return ShellIntegration::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48180 case ShellUtil::IS_DEFAULT:
[email protected]89886652012-12-11 18:09:07181 return ShellIntegration::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48182 default:
183 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
[email protected]89886652012-12-11 18:09:07184 return ShellIntegration::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48185 }
186}
187
[email protected]3a3e72c2011-11-29 02:59:38188} // namespace
[email protected]12f520c2010-01-06 18:11:15189
[email protected]bd046bd42012-06-08 05:07:32190ShellIntegration::DefaultWebClientSetPermission
191 ShellIntegration::CanSetAsDefaultBrowser() {
192 if (!BrowserDistribution::GetDistribution()->CanSetAsDefault())
193 return SET_DEFAULT_NOT_ALLOWED;
194
[email protected]9fcf07f2012-06-18 16:13:51195 if (ShellUtil::CanMakeChromeDefaultUnattended())
[email protected]bd046bd42012-06-08 05:07:32196 return SET_DEFAULT_UNATTENDED;
[email protected]9fcf07f2012-06-18 16:13:51197 else
198 return SET_DEFAULT_INTERACTIVE;
[email protected]a01481b2011-07-15 04:30:02199}
200
[email protected]d24c4012009-07-28 01:57:31201bool ShellIntegration::SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45202 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31203 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
204 LOG(ERROR) << "Error getting app exe path";
205 return false;
206 }
207
208 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25209 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
210 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
[email protected]b9696482010-11-30 23:56:18211 chrome_exe.value(), true)) {
[email protected]d24c4012009-07-28 01:57:31212 LOG(ERROR) << "Chrome could not be set as default browser.";
213 return false;
214 }
215
[email protected]8e96e502010-10-21 20:57:12216 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31217 return true;
218}
219
[email protected]4468a5b2011-05-26 07:48:02220bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
221 if (protocol.empty())
222 return false;
223
[email protected]650b2d52013-02-10 03:41:45224 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02225 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
226 LOG(ERROR) << "Error getting app exe path";
227 return false;
228 }
229
[email protected]ee9d89d2012-09-25 18:21:03230 string16 wprotocol(UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02231 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
232 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
233 wprotocol)) {
234 LOG(ERROR) << "Chrome could not be set as default handler for "
235 << protocol << ".";
236 return false;
237 }
238
239 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
240 return true;
241}
242
[email protected]bd046bd42012-06-08 05:07:32243bool ShellIntegration::SetAsDefaultBrowserInteractive() {
[email protected]650b2d52013-02-10 03:41:45244 base::FilePath chrome_exe;
[email protected]bd046bd42012-06-08 05:07:32245 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
246 NOTREACHED() << "Error getting app exe path";
247 return false;
248 }
249
250 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
251 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) {
252 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
253 return false;
254 }
255
[email protected]ee9d89d2012-09-25 18:21:03256 VLOG(1) << "Set-default-browser Windows UI completed.";
257 return true;
258}
259
260bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
261 const std::string& protocol) {
[email protected]650b2d52013-02-10 03:41:45262 base::FilePath chrome_exe;
[email protected]ee9d89d2012-09-25 18:21:03263 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
264 NOTREACHED() << "Error getting app exe path";
265 return false;
266 }
267
268 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
269 string16 wprotocol(UTF8ToUTF16(protocol));
270 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
271 dist, chrome_exe.value(), wprotocol)) {
272 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
273 return false;
274 }
275
276 VLOG(1) << "Set-default-client Windows UI completed.";
[email protected]bd046bd42012-06-08 05:07:32277 return true;
278}
279
[email protected]89886652012-12-11 18:09:07280ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
[email protected]eb63da72012-10-15 20:39:48281 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52282 ShellUtil::GetChromeDefaultState());
[email protected]4468a5b2011-05-26 07:48:02283}
284
285ShellIntegration::DefaultWebClientState
286 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
[email protected]eb63da72012-10-15 20:39:48287 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52288 ShellUtil::GetChromeDefaultProtocolClientState(UTF8ToUTF16(protocol)));
[email protected]d24c4012009-07-28 01:57:31289}
290
[email protected]42dc9402013-01-30 07:54:20291std::string ShellIntegration::GetApplicationForProtocol(const GURL& url) {
292 // TODO(calamity): this will be implemented when external_protocol_dialog is
293 // refactored on windows.
294 NOTREACHED();
295 return std::string();
296}
297
[email protected]d24c4012009-07-28 01:57:31298// There is no reliable way to say which browser is default on a machine (each
299// browser can have some of the protocols/shortcuts). So we look for only HTTP
300// protocol handler. Even this handler is located at different places in
301// registry on XP and Vista:
302// - HKCR\http\shell\open\command (XP)
303// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
304// http\UserChoice (Vista)
305// This method checks if Firefox is defualt browser by checking these
306// locations and returns true if Firefox traces are found there. In case of
307// error (or if Firefox is not found)it returns the default value which
308// is false.
309bool ShellIntegration::IsFirefoxDefaultBrowser() {
310 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15311 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]b6b72222012-02-11 02:04:13312 string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54313 base::win::RegKey key(HKEY_CURRENT_USER,
314 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46315 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31316 app_cmd == L"FirefoxURL")
317 ff_default = true;
318 } else {
[email protected]b6b72222012-02-11 02:04:13319 string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31320 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54321 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]b6b72222012-02-11 02:04:13322 string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46323 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]b6b72222012-02-11 02:04:13324 string16::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31325 ff_default = true;
326 }
327 return ff_default;
328}
[email protected]12f520c2010-01-06 18:11:15329
[email protected]a0448002012-06-19 04:32:10330string16 ShellIntegration::GetAppModelIdForProfile(
331 const string16& app_name,
[email protected]650b2d52013-02-10 03:41:45332 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10333 std::vector<string16> components;
334 components.push_back(app_name);
335 const string16 profile_id(GetProfileIdFromPath(profile_path));
336 if (!profile_id.empty())
337 components.push_back(profile_id);
338 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15339}
340
[email protected]a0448002012-06-19 04:32:10341string16 ShellIntegration::GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45342 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10343 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45344 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10345 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
346 NOTREACHED();
347 return dist->GetBaseAppId();
348 }
349 return GetAppModelIdForProfile(
[email protected]786799692012-09-26 14:16:48350 ShellUtil::GetBrowserModelId(
351 dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())),
352 profile_path);
[email protected]12f520c2010-01-06 18:11:15353}
[email protected]c9bb06f42010-01-13 23:53:48354
[email protected]99002fd2012-11-06 04:35:52355string16 ShellIntegration::GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45356 const base::FilePath& profile_path) {
[email protected]da1ffc32013-02-15 21:22:56357 return ShellIntegration::GetAppModelIdForProfile(
358 GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52359}
360
[email protected]9c91fc22012-11-20 04:13:49361string16 ShellIntegration::GetChromiumIconLocation() {
362 // Determine the path to chrome.exe. If we can't determine what that is,
363 // we have bigger fish to fry...
[email protected]650b2d52013-02-10 03:41:45364 base::FilePath chrome_exe;
[email protected]9c91fc22012-11-20 04:13:49365 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
[email protected]2de2d622011-10-21 22:07:37366 NOTREACHED();
367 return string16();
368 }
369
[email protected]9c91fc22012-11-20 04:13:49370 return ShellUtil::FormatIconLocation(
371 chrome_exe.value(),
[email protected]2de2d622011-10-21 22:07:37372 BrowserDistribution::GetDistribution()->GetIconIndex());
[email protected]2de2d622011-10-21 22:07:37373}
374
[email protected]c9bb06f42010-01-13 23:53:48375void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15376 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48377 return;
378
[email protected]81fcf952012-11-26 22:25:14379 // This needs to happen eventually (e.g. so that the appid is fixed and the
380 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
381 // urgent and shouldn't delay Chrome startup.
382 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
383 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38384 BrowserThread::FILE, FROM_HERE,
[email protected]81fcf952012-11-26 22:25:14385 base::Bind(&MigrateChromiumShortcutsCallback),
386 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48387}
[email protected]43903b82012-06-01 05:26:23388
[email protected]650b2d52013-02-10 03:41:45389int ShellIntegration::MigrateShortcutsInPathInternal(
390 const base::FilePath& chrome_exe,
391 const base::FilePath& path,
392 bool check_dual_mode) {
[email protected]8ea8f1ef2013-01-06 18:39:03393 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
394
395 // Enumerate all pinned shortcuts in the given path directly.
[email protected]25a4c1c2013-06-08 04:53:36396 base::FileEnumerator shortcuts_enum(
[email protected]8ea8f1ef2013-01-06 18:39:03397 path, false, // not recursive
[email protected]25a4c1c2013-06-08 04:53:36398 base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
[email protected]8ea8f1ef2013-01-06 18:39:03399
400 bool is_per_user_install =
401 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
402
403 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45404 base::FilePath target_path;
[email protected]8ea8f1ef2013-01-06 18:39:03405 string16 arguments;
[email protected]07983302013-01-21 19:41:44406 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45407 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03408 shortcut = shortcuts_enum.Next()) {
409 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
410 // it is fixed to work with FilePaths with spaces.
411 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
412 chrome_exe != target_path) {
413 continue;
414 }
415 CommandLine command_line(CommandLine::FromString(base::StringPrintf(
416 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
417
418 // Get the expected AppId for this Chrome shortcut.
419 string16 expected_app_id(
420 GetExpectedAppId(command_line, is_per_user_install));
421 if (expected_app_id.empty())
422 continue;
423
424 // Load the shortcut.
425 base::win::ScopedComPtr<IShellLink> shell_link;
426 base::win::ScopedComPtr<IPersistFile> persist_file;
427 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
428 CLSCTX_INPROC_SERVER)) ||
429 FAILED(persist_file.QueryFrom(shell_link)) ||
430 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
431 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
432 continue;
433 }
434
435 // Any properties that need to be updated on the shortcut will be stored in
436 // |updated_properties|.
437 base::win::ShortcutProperties updated_properties;
438
439 // Validate the existing app id for the shortcut.
440 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44441 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03442 if (FAILED(property_store.QueryFrom(shell_link)) ||
[email protected]07983302013-01-21 19:41:44443 property_store->GetValue(PKEY_AppUserModel_ID,
444 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03445 // When in doubt, prefer not updating the shortcut.
446 NOTREACHED();
447 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03448 } else {
[email protected]07983302013-01-21 19:41:44449 switch (propvariant.get().vt) {
450 case VT_EMPTY:
451 // If there is no app_id set, set our app_id if one is expected.
452 if (!expected_app_id.empty())
453 updated_properties.set_app_id(expected_app_id);
454 break;
455 case VT_LPWSTR:
456 if (expected_app_id != string16(propvariant.get().pwszVal))
457 updated_properties.set_app_id(expected_app_id);
458 break;
459 default:
460 NOTREACHED();
461 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03462 }
463 }
464
[email protected]b39e32e2013-04-24 08:55:54465 // Only set dual mode if the expected app id is the default app id.
466 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
467 string16 default_chromium_model_id(
468 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
469 if (check_dual_mode && expected_app_id == default_chromium_model_id) {
[email protected]07983302013-01-21 19:41:44470 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03471 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44472 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03473 // When in doubt, prefer to not update the shortcut.
474 NOTREACHED();
475 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03476 } else {
[email protected]07983302013-01-21 19:41:44477 switch (propvariant.get().vt) {
478 case VT_EMPTY:
479 // If dual_mode is not set at all, make sure it gets set to true.
480 updated_properties.set_dual_mode(true);
481 break;
482 case VT_BOOL:
483 // If it is set to false, make sure it gets set to true as well.
484 if (!propvariant.get().boolVal)
485 updated_properties.set_dual_mode(true);
486 break;
487 default:
488 NOTREACHED();
489 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03490 }
[email protected]8ea8f1ef2013-01-06 18:39:03491 }
492 }
493
494 persist_file.Release();
495 shell_link.Release();
496
497 // Update the shortcut if some of its properties need to be updated.
498 if (updated_properties.options &&
499 base::win::CreateOrUpdateShortcutLink(
500 shortcut, updated_properties,
501 base::win::SHORTCUT_UPDATE_EXISTING)) {
502 ++shortcuts_migrated;
503 }
504 }
505 return shortcuts_migrated;
506}
507
[email protected]650b2d52013-02-10 03:41:45508base::FilePath ShellIntegration::GetStartMenuShortcut(
509 const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27510 static const int kFolderIds[] = {
511 base::DIR_COMMON_START_MENU,
512 base::DIR_START_MENU,
513 };
514 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
515 string16 shortcut_name(dist->GetAppShortCutName());
[email protected]650b2d52013-02-10 03:41:45516 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27517
518 // Check both the common and the per-user Start Menu folders for system-level
519 // installs.
520 size_t folder =
521 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
522 for (; folder < arraysize(kFolderIds); ++folder) {
523 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
524 NOTREACHED();
525 continue;
526 }
527
[email protected]126f4622012-10-05 01:34:05528 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
529 installer::kLnkExt);
[email protected]3f69d6e612012-08-03 18:52:27530 if (file_util::PathExists(shortcut))
531 return shortcut;
532 }
533
[email protected]650b2d52013-02-10 03:41:45534 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27535}