blob: fb1bd643b7d061084fed2ec030ee51c53bc17a58 [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"
14#include "base/message_loop.h"
15#include "base/path_service.h"
[email protected]d24c4012009-07-28 01:57:3116#include "base/string_util.h"
[email protected]33272e1f02011-08-17 00:22:0717#include "base/stringprintf.h"
[email protected]3ea1b182013-02-08 22:38:4118#include "base/strings/string_number_conversions.h"
[email protected]57ecc4b2010-08-11 03:02:5119#include "base/utf_string_conversions.h"
[email protected]2d6503982010-10-17 04:41:5420#include "base/win/registry.h"
[email protected]8ee65ba2011-04-12 20:53:2321#include "base/win/scoped_comptr.h"
[email protected]07983302013-01-21 19:41:4422#include "base/win/scoped_propvariant.h"
[email protected]f1024e22012-09-12 07:14:5523#include "base/win/shortcut.h"
[email protected]935aa542010-10-15 01:59:1524#include "base/win/windows_version.h"
[email protected]c9bb06f42010-01-13 23:53:4825#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3126#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1527#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4828#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0229#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3130#include "chrome/installer/util/browser_distribution.h"
31#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2732#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3133#include "chrome/installer/util/set_reg_value_work_item.h"
34#include "chrome/installer/util/shell_util.h"
35#include "chrome/installer/util/util_constants.h"
36#include "chrome/installer/util/work_item.h"
37#include "chrome/installer/util/work_item_list.h"
[email protected]c38831a12011-10-28 12:44:4938#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3139
[email protected]631bb742011-11-02 11:29:3940using content::BrowserThread;
41
[email protected]12f520c2010-01-06 18:11:1542namespace {
43
[email protected]da1ffc32013-02-15 21:22:5644const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5245
[email protected]12f520c2010-01-06 18:11:1546// Helper function for ShellIntegration::GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1447// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1548// user data dir and profile dir joined by a ".".
[email protected]650b2d52013-02-10 03:41:4549string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1550 // Return empty string if profile_path is empty
51 if (profile_path.empty())
[email protected]b6b72222012-02-11 02:04:1352 return string16();
[email protected]12f520c2010-01-06 18:11:1553
[email protected]650b2d52013-02-10 03:41:4554 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1555 // Return empty string if profile_path is in default user data
56 // dir and is the default profile.
57 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
58 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4859 profile_path.BaseName().value() ==
[email protected]d778d6e2011-08-12 09:47:0560 ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]b6b72222012-02-11 02:04:1361 return string16();
[email protected]162b5992011-03-15 19:40:4862 }
[email protected]12f520c2010-01-06 18:11:1563
64 // Get joined basenames of user data dir and profile.
[email protected]b6b72222012-02-11 02:04:1365 string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1566 L"." + profile_path.BaseName().value();
67
[email protected]b6b72222012-02-11 02:04:1368 string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1569 profile_id.reserve(basenames.size());
70
71 // Generate profile_id from sanitized basenames.
72 for (size_t i = 0; i < basenames.length(); ++i) {
73 if (IsAsciiAlpha(basenames[i]) ||
74 IsAsciiDigit(basenames[i]) ||
75 basenames[i] == L'.')
76 profile_id += basenames[i];
77 }
78
79 return profile_id;
80}
81
[email protected]da1ffc32013-02-15 21:22:5682string16 GetAppListAppName() {
83 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
84 string16 app_name(dist->GetBaseAppId());
85 app_name.append(kAppListAppNameSuffix);
86 return app_name;
87}
88
[email protected]8ea8f1ef2013-01-06 18:39:0389// Gets expected app id for given Chrome (based on |command_line| and
90// |is_per_user_install|).
91string16 GetExpectedAppId(const CommandLine& command_line,
92 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:5493 base::FilePath user_data_dir;
94 if (command_line.HasSwitch(switches::kUserDataDir))
95 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
96 else
97 chrome::GetDefaultUserDataDirectory(&user_data_dir);
98 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:4899
[email protected]b39e32e2013-04-24 08:55:54100 base::FilePath profile_subdir;
101 if (command_line.HasSwitch(switches::kProfileDirectory)) {
102 profile_subdir =
103 command_line.GetSwitchValuePath(switches::kProfileDirectory);
104 } else {
105 profile_subdir = base::FilePath(ASCIIToUTF16(chrome::kInitialProfile));
106 }
107 DCHECK(!profile_subdir.empty());
108
109 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]b6b72222012-02-11 02:04:13110 string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48111 if (command_line.HasSwitch(switches::kApp)) {
[email protected]b6b72222012-02-11 02:04:13112 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51113 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14114 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]b6b72222012-02-11 02:04:13115 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId(
[email protected]2f1c09d2011-01-14 14:58:14116 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52117 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56118 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48119 } else {
[email protected]a0448002012-06-19 04:32:10120 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03121 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48122 }
[email protected]b39e32e2013-04-24 08:55:54123 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48124
[email protected]8ea8f1ef2013-01-06 18:39:03125 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48126}
127
[email protected]3a3e72c2011-11-29 02:59:38128void MigrateChromiumShortcutsCallback() {
129 // This should run on the file thread.
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
131
132 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45133 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38134 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
135 return;
136
137 // Locations to check for shortcuts migration.
138 static const struct {
139 int location_id;
140 const wchar_t* sub_dir;
141 } kLocations[] = {
142 {
[email protected]e5f9d822012-11-06 22:27:01143 base::DIR_TASKBAR_PINS,
144 NULL
[email protected]3a3e72c2011-11-29 02:59:38145 }, {
[email protected]dea1d7d2012-09-20 16:24:52146 base::DIR_USER_DESKTOP,
[email protected]3a3e72c2011-11-29 02:59:38147 NULL
148 }, {
149 base::DIR_START_MENU,
150 NULL
151 }, {
152 base::DIR_APP_DATA,
153 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
154 }
155 };
156
157 for (int i = 0; i < arraysize(kLocations); ++i) {
[email protected]650b2d52013-02-10 03:41:45158 base::FilePath path;
[email protected]3a3e72c2011-11-29 02:59:38159 if (!PathService::Get(kLocations[i].location_id, &path)) {
160 NOTREACHED();
161 continue;
162 }
163
164 if (kLocations[i].sub_dir)
165 path = path.Append(kLocations[i].sub_dir);
166
[email protected]8ea8f1ef2013-01-06 18:39:03167 bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
168 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
169 check_dual_mode);
[email protected]3a3e72c2011-11-29 02:59:38170 }
171}
172
[email protected]eb63da72012-10-15 20:39:48173ShellIntegration::DefaultWebClientState
174 GetDefaultWebClientStateFromShellUtilDefaultState(
175 ShellUtil::DefaultState default_state) {
176 switch (default_state) {
177 case ShellUtil::NOT_DEFAULT:
[email protected]89886652012-12-11 18:09:07178 return ShellIntegration::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48179 case ShellUtil::IS_DEFAULT:
[email protected]89886652012-12-11 18:09:07180 return ShellIntegration::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48181 default:
182 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
[email protected]89886652012-12-11 18:09:07183 return ShellIntegration::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48184 }
185}
186
[email protected]3a3e72c2011-11-29 02:59:38187} // namespace
[email protected]12f520c2010-01-06 18:11:15188
[email protected]bd046bd42012-06-08 05:07:32189ShellIntegration::DefaultWebClientSetPermission
190 ShellIntegration::CanSetAsDefaultBrowser() {
191 if (!BrowserDistribution::GetDistribution()->CanSetAsDefault())
192 return SET_DEFAULT_NOT_ALLOWED;
193
[email protected]9fcf07f2012-06-18 16:13:51194 if (ShellUtil::CanMakeChromeDefaultUnattended())
[email protected]bd046bd42012-06-08 05:07:32195 return SET_DEFAULT_UNATTENDED;
[email protected]9fcf07f2012-06-18 16:13:51196 else
197 return SET_DEFAULT_INTERACTIVE;
[email protected]a01481b2011-07-15 04:30:02198}
199
[email protected]d24c4012009-07-28 01:57:31200bool ShellIntegration::SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45201 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31202 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
203 LOG(ERROR) << "Error getting app exe path";
204 return false;
205 }
206
207 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25208 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
209 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
[email protected]b9696482010-11-30 23:56:18210 chrome_exe.value(), true)) {
[email protected]d24c4012009-07-28 01:57:31211 LOG(ERROR) << "Chrome could not be set as default browser.";
212 return false;
213 }
214
[email protected]8e96e502010-10-21 20:57:12215 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31216 return true;
217}
218
[email protected]4468a5b2011-05-26 07:48:02219bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
220 if (protocol.empty())
221 return false;
222
[email protected]650b2d52013-02-10 03:41:45223 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02224 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
225 LOG(ERROR) << "Error getting app exe path";
226 return false;
227 }
228
[email protected]ee9d89d2012-09-25 18:21:03229 string16 wprotocol(UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02230 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
231 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
232 wprotocol)) {
233 LOG(ERROR) << "Chrome could not be set as default handler for "
234 << protocol << ".";
235 return false;
236 }
237
238 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
239 return true;
240}
241
[email protected]bd046bd42012-06-08 05:07:32242bool ShellIntegration::SetAsDefaultBrowserInteractive() {
[email protected]650b2d52013-02-10 03:41:45243 base::FilePath chrome_exe;
[email protected]bd046bd42012-06-08 05:07:32244 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
245 NOTREACHED() << "Error getting app exe path";
246 return false;
247 }
248
249 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
250 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) {
251 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
252 return false;
253 }
254
[email protected]ee9d89d2012-09-25 18:21:03255 VLOG(1) << "Set-default-browser Windows UI completed.";
256 return true;
257}
258
259bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
260 const std::string& protocol) {
[email protected]650b2d52013-02-10 03:41:45261 base::FilePath chrome_exe;
[email protected]ee9d89d2012-09-25 18:21:03262 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
263 NOTREACHED() << "Error getting app exe path";
264 return false;
265 }
266
267 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
268 string16 wprotocol(UTF8ToUTF16(protocol));
269 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
270 dist, chrome_exe.value(), wprotocol)) {
271 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
272 return false;
273 }
274
275 VLOG(1) << "Set-default-client Windows UI completed.";
[email protected]bd046bd42012-06-08 05:07:32276 return true;
277}
278
[email protected]89886652012-12-11 18:09:07279ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
[email protected]eb63da72012-10-15 20:39:48280 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52281 ShellUtil::GetChromeDefaultState());
[email protected]4468a5b2011-05-26 07:48:02282}
283
284ShellIntegration::DefaultWebClientState
285 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
[email protected]eb63da72012-10-15 20:39:48286 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52287 ShellUtil::GetChromeDefaultProtocolClientState(UTF8ToUTF16(protocol)));
[email protected]d24c4012009-07-28 01:57:31288}
289
[email protected]42dc9402013-01-30 07:54:20290std::string ShellIntegration::GetApplicationForProtocol(const GURL& url) {
291 // TODO(calamity): this will be implemented when external_protocol_dialog is
292 // refactored on windows.
293 NOTREACHED();
294 return std::string();
295}
296
[email protected]d24c4012009-07-28 01:57:31297// There is no reliable way to say which browser is default on a machine (each
298// browser can have some of the protocols/shortcuts). So we look for only HTTP
299// protocol handler. Even this handler is located at different places in
300// registry on XP and Vista:
301// - HKCR\http\shell\open\command (XP)
302// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
303// http\UserChoice (Vista)
304// This method checks if Firefox is defualt browser by checking these
305// locations and returns true if Firefox traces are found there. In case of
306// error (or if Firefox is not found)it returns the default value which
307// is false.
308bool ShellIntegration::IsFirefoxDefaultBrowser() {
309 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15310 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]b6b72222012-02-11 02:04:13311 string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54312 base::win::RegKey key(HKEY_CURRENT_USER,
313 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46314 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31315 app_cmd == L"FirefoxURL")
316 ff_default = true;
317 } else {
[email protected]b6b72222012-02-11 02:04:13318 string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31319 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54320 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]b6b72222012-02-11 02:04:13321 string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46322 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]b6b72222012-02-11 02:04:13323 string16::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31324 ff_default = true;
325 }
326 return ff_default;
327}
[email protected]12f520c2010-01-06 18:11:15328
[email protected]a0448002012-06-19 04:32:10329string16 ShellIntegration::GetAppModelIdForProfile(
330 const string16& app_name,
[email protected]650b2d52013-02-10 03:41:45331 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10332 std::vector<string16> components;
333 components.push_back(app_name);
334 const string16 profile_id(GetProfileIdFromPath(profile_path));
335 if (!profile_id.empty())
336 components.push_back(profile_id);
337 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15338}
339
[email protected]a0448002012-06-19 04:32:10340string16 ShellIntegration::GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45341 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10342 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45343 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10344 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
345 NOTREACHED();
346 return dist->GetBaseAppId();
347 }
348 return GetAppModelIdForProfile(
[email protected]786799692012-09-26 14:16:48349 ShellUtil::GetBrowserModelId(
350 dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())),
351 profile_path);
[email protected]12f520c2010-01-06 18:11:15352}
[email protected]c9bb06f42010-01-13 23:53:48353
[email protected]99002fd2012-11-06 04:35:52354string16 ShellIntegration::GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45355 const base::FilePath& profile_path) {
[email protected]da1ffc32013-02-15 21:22:56356 return ShellIntegration::GetAppModelIdForProfile(
357 GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52358}
359
[email protected]9c91fc22012-11-20 04:13:49360string16 ShellIntegration::GetChromiumIconLocation() {
361 // Determine the path to chrome.exe. If we can't determine what that is,
362 // we have bigger fish to fry...
[email protected]650b2d52013-02-10 03:41:45363 base::FilePath chrome_exe;
[email protected]9c91fc22012-11-20 04:13:49364 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
[email protected]2de2d622011-10-21 22:07:37365 NOTREACHED();
366 return string16();
367 }
368
[email protected]9c91fc22012-11-20 04:13:49369 return ShellUtil::FormatIconLocation(
370 chrome_exe.value(),
[email protected]2de2d622011-10-21 22:07:37371 BrowserDistribution::GetDistribution()->GetIconIndex());
[email protected]2de2d622011-10-21 22:07:37372}
373
[email protected]c9bb06f42010-01-13 23:53:48374void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15375 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48376 return;
377
[email protected]81fcf952012-11-26 22:25:14378 // This needs to happen eventually (e.g. so that the appid is fixed and the
379 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
380 // urgent and shouldn't delay Chrome startup.
381 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
382 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38383 BrowserThread::FILE, FROM_HERE,
[email protected]81fcf952012-11-26 22:25:14384 base::Bind(&MigrateChromiumShortcutsCallback),
385 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48386}
[email protected]43903b82012-06-01 05:26:23387
[email protected]650b2d52013-02-10 03:41:45388int ShellIntegration::MigrateShortcutsInPathInternal(
389 const base::FilePath& chrome_exe,
390 const base::FilePath& path,
391 bool check_dual_mode) {
[email protected]8ea8f1ef2013-01-06 18:39:03392 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
393
394 // Enumerate all pinned shortcuts in the given path directly.
395 file_util::FileEnumerator shortcuts_enum(
396 path, false, // not recursive
397 file_util::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
398
399 bool is_per_user_install =
400 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
401
402 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45403 base::FilePath target_path;
[email protected]8ea8f1ef2013-01-06 18:39:03404 string16 arguments;
[email protected]07983302013-01-21 19:41:44405 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45406 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03407 shortcut = shortcuts_enum.Next()) {
408 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
409 // it is fixed to work with FilePaths with spaces.
410 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
411 chrome_exe != target_path) {
412 continue;
413 }
414 CommandLine command_line(CommandLine::FromString(base::StringPrintf(
415 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
416
417 // Get the expected AppId for this Chrome shortcut.
418 string16 expected_app_id(
419 GetExpectedAppId(command_line, is_per_user_install));
420 if (expected_app_id.empty())
421 continue;
422
423 // Load the shortcut.
424 base::win::ScopedComPtr<IShellLink> shell_link;
425 base::win::ScopedComPtr<IPersistFile> persist_file;
426 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
427 CLSCTX_INPROC_SERVER)) ||
428 FAILED(persist_file.QueryFrom(shell_link)) ||
429 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
430 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
431 continue;
432 }
433
434 // Any properties that need to be updated on the shortcut will be stored in
435 // |updated_properties|.
436 base::win::ShortcutProperties updated_properties;
437
438 // Validate the existing app id for the shortcut.
439 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44440 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03441 if (FAILED(property_store.QueryFrom(shell_link)) ||
[email protected]07983302013-01-21 19:41:44442 property_store->GetValue(PKEY_AppUserModel_ID,
443 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03444 // When in doubt, prefer not updating the shortcut.
445 NOTREACHED();
446 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03447 } else {
[email protected]07983302013-01-21 19:41:44448 switch (propvariant.get().vt) {
449 case VT_EMPTY:
450 // If there is no app_id set, set our app_id if one is expected.
451 if (!expected_app_id.empty())
452 updated_properties.set_app_id(expected_app_id);
453 break;
454 case VT_LPWSTR:
455 if (expected_app_id != string16(propvariant.get().pwszVal))
456 updated_properties.set_app_id(expected_app_id);
457 break;
458 default:
459 NOTREACHED();
460 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03461 }
462 }
463
[email protected]b39e32e2013-04-24 08:55:54464 // Only set dual mode if the expected app id is the default app id.
465 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
466 string16 default_chromium_model_id(
467 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
468 if (check_dual_mode && expected_app_id == default_chromium_model_id) {
[email protected]07983302013-01-21 19:41:44469 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03470 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44471 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03472 // When in doubt, prefer to not update the shortcut.
473 NOTREACHED();
474 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03475 } else {
[email protected]07983302013-01-21 19:41:44476 switch (propvariant.get().vt) {
477 case VT_EMPTY:
478 // If dual_mode is not set at all, make sure it gets set to true.
479 updated_properties.set_dual_mode(true);
480 break;
481 case VT_BOOL:
482 // If it is set to false, make sure it gets set to true as well.
483 if (!propvariant.get().boolVal)
484 updated_properties.set_dual_mode(true);
485 break;
486 default:
487 NOTREACHED();
488 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03489 }
[email protected]8ea8f1ef2013-01-06 18:39:03490 }
491 }
492
493 persist_file.Release();
494 shell_link.Release();
495
496 // Update the shortcut if some of its properties need to be updated.
497 if (updated_properties.options &&
498 base::win::CreateOrUpdateShortcutLink(
499 shortcut, updated_properties,
500 base::win::SHORTCUT_UPDATE_EXISTING)) {
501 ++shortcuts_migrated;
502 }
503 }
504 return shortcuts_migrated;
505}
506
[email protected]650b2d52013-02-10 03:41:45507base::FilePath ShellIntegration::GetStartMenuShortcut(
508 const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27509 static const int kFolderIds[] = {
510 base::DIR_COMMON_START_MENU,
511 base::DIR_START_MENU,
512 };
513 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
514 string16 shortcut_name(dist->GetAppShortCutName());
[email protected]650b2d52013-02-10 03:41:45515 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27516
517 // Check both the common and the per-user Start Menu folders for system-level
518 // installs.
519 size_t folder =
520 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
521 for (; folder < arraysize(kFolderIds); ++folder) {
522 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
523 NOTREACHED();
524 continue;
525 }
526
[email protected]126f4622012-10-05 01:34:05527 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
528 installer::kLnkExt);
[email protected]3f69d6e612012-08-03 18:52:27529 if (file_util::PathExists(shortcut))
530 return shortcut;
531 }
532
[email protected]650b2d52013-02-10 03:41:45533 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27534}