blob: eb37324f9cffbe739807145e57f87db8148a290c [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]89d43832013-06-29 20:25:2026#include "chrome/browser/policy/policy_path_parser.h"
[email protected]c9bb06f42010-01-13 23:53:4827#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3128#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1529#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4830#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0231#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3132#include "chrome/installer/util/browser_distribution.h"
33#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2734#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3135#include "chrome/installer/util/set_reg_value_work_item.h"
36#include "chrome/installer/util/shell_util.h"
37#include "chrome/installer/util/util_constants.h"
38#include "chrome/installer/util/work_item.h"
39#include "chrome/installer/util/work_item_list.h"
[email protected]c38831a12011-10-28 12:44:4940#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3141
[email protected]631bb742011-11-02 11:29:3942using content::BrowserThread;
43
[email protected]12f520c2010-01-06 18:11:1544namespace {
45
[email protected]da1ffc32013-02-15 21:22:5646const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5247
[email protected]12f520c2010-01-06 18:11:1548// Helper function for ShellIntegration::GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1449// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1550// user data dir and profile dir joined by a ".".
[email protected]650b2d52013-02-10 03:41:4551string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1552 // Return empty string if profile_path is empty
53 if (profile_path.empty())
[email protected]b6b72222012-02-11 02:04:1354 return string16();
[email protected]12f520c2010-01-06 18:11:1555
[email protected]650b2d52013-02-10 03:41:4556 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1557 // Return empty string if profile_path is in default user data
58 // dir and is the default profile.
59 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
60 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4861 profile_path.BaseName().value() ==
[email protected]d778d6e2011-08-12 09:47:0562 ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]b6b72222012-02-11 02:04:1363 return string16();
[email protected]162b5992011-03-15 19:40:4864 }
[email protected]12f520c2010-01-06 18:11:1565
66 // Get joined basenames of user data dir and profile.
[email protected]b6b72222012-02-11 02:04:1367 string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1568 L"." + profile_path.BaseName().value();
69
[email protected]b6b72222012-02-11 02:04:1370 string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1571 profile_id.reserve(basenames.size());
72
73 // Generate profile_id from sanitized basenames.
74 for (size_t i = 0; i < basenames.length(); ++i) {
75 if (IsAsciiAlpha(basenames[i]) ||
76 IsAsciiDigit(basenames[i]) ||
77 basenames[i] == L'.')
78 profile_id += basenames[i];
79 }
80
81 return profile_id;
82}
83
[email protected]da1ffc32013-02-15 21:22:5684string16 GetAppListAppName() {
85 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
86 string16 app_name(dist->GetBaseAppId());
87 app_name.append(kAppListAppNameSuffix);
88 return app_name;
89}
90
[email protected]8ea8f1ef2013-01-06 18:39:0391// Gets expected app id for given Chrome (based on |command_line| and
92// |is_per_user_install|).
93string16 GetExpectedAppId(const CommandLine& command_line,
94 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:5495 base::FilePath user_data_dir;
96 if (command_line.HasSwitch(switches::kUserDataDir))
97 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
98 else
99 chrome::GetDefaultUserDataDirectory(&user_data_dir);
[email protected]89d43832013-06-29 20:25:20100 // Adjust with any policy that overrides any other way to set the path.
101 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
[email protected]b39e32e2013-04-24 08:55:54102 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:48103
[email protected]b39e32e2013-04-24 08:55:54104 base::FilePath profile_subdir;
105 if (command_line.HasSwitch(switches::kProfileDirectory)) {
106 profile_subdir =
107 command_line.GetSwitchValuePath(switches::kProfileDirectory);
108 } else {
109 profile_subdir = base::FilePath(ASCIIToUTF16(chrome::kInitialProfile));
110 }
111 DCHECK(!profile_subdir.empty());
112
113 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]b6b72222012-02-11 02:04:13114 string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48115 if (command_line.HasSwitch(switches::kApp)) {
[email protected]b6b72222012-02-11 02:04:13116 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51117 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14118 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]b6b72222012-02-11 02:04:13119 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId(
[email protected]2f1c09d2011-01-14 14:58:14120 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52121 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56122 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48123 } else {
[email protected]a0448002012-06-19 04:32:10124 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03125 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48126 }
[email protected]b39e32e2013-04-24 08:55:54127 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48128
[email protected]8ea8f1ef2013-01-06 18:39:03129 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48130}
131
[email protected]3a3e72c2011-11-29 02:59:38132void MigrateChromiumShortcutsCallback() {
133 // This should run on the file thread.
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
135
136 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45137 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38138 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
139 return;
140
141 // Locations to check for shortcuts migration.
142 static const struct {
143 int location_id;
144 const wchar_t* sub_dir;
145 } kLocations[] = {
146 {
[email protected]e5f9d822012-11-06 22:27:01147 base::DIR_TASKBAR_PINS,
148 NULL
[email protected]3a3e72c2011-11-29 02:59:38149 }, {
[email protected]dea1d7d2012-09-20 16:24:52150 base::DIR_USER_DESKTOP,
[email protected]3a3e72c2011-11-29 02:59:38151 NULL
152 }, {
153 base::DIR_START_MENU,
154 NULL
155 }, {
156 base::DIR_APP_DATA,
157 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
158 }
159 };
160
161 for (int i = 0; i < arraysize(kLocations); ++i) {
[email protected]650b2d52013-02-10 03:41:45162 base::FilePath path;
[email protected]3a3e72c2011-11-29 02:59:38163 if (!PathService::Get(kLocations[i].location_id, &path)) {
164 NOTREACHED();
165 continue;
166 }
167
168 if (kLocations[i].sub_dir)
169 path = path.Append(kLocations[i].sub_dir);
170
[email protected]8ea8f1ef2013-01-06 18:39:03171 bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
172 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
173 check_dual_mode);
[email protected]3a3e72c2011-11-29 02:59:38174 }
175}
176
[email protected]eb63da72012-10-15 20:39:48177ShellIntegration::DefaultWebClientState
178 GetDefaultWebClientStateFromShellUtilDefaultState(
179 ShellUtil::DefaultState default_state) {
180 switch (default_state) {
181 case ShellUtil::NOT_DEFAULT:
[email protected]89886652012-12-11 18:09:07182 return ShellIntegration::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48183 case ShellUtil::IS_DEFAULT:
[email protected]89886652012-12-11 18:09:07184 return ShellIntegration::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48185 default:
186 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
[email protected]89886652012-12-11 18:09:07187 return ShellIntegration::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48188 }
189}
190
[email protected]3a3e72c2011-11-29 02:59:38191} // namespace
[email protected]12f520c2010-01-06 18:11:15192
[email protected]bd046bd42012-06-08 05:07:32193ShellIntegration::DefaultWebClientSetPermission
194 ShellIntegration::CanSetAsDefaultBrowser() {
195 if (!BrowserDistribution::GetDistribution()->CanSetAsDefault())
196 return SET_DEFAULT_NOT_ALLOWED;
197
[email protected]9fcf07f2012-06-18 16:13:51198 if (ShellUtil::CanMakeChromeDefaultUnattended())
[email protected]bd046bd42012-06-08 05:07:32199 return SET_DEFAULT_UNATTENDED;
[email protected]9fcf07f2012-06-18 16:13:51200 else
201 return SET_DEFAULT_INTERACTIVE;
[email protected]a01481b2011-07-15 04:30:02202}
203
[email protected]d24c4012009-07-28 01:57:31204bool ShellIntegration::SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45205 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31206 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
207 LOG(ERROR) << "Error getting app exe path";
208 return false;
209 }
210
211 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25212 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
213 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
[email protected]b9696482010-11-30 23:56:18214 chrome_exe.value(), true)) {
[email protected]d24c4012009-07-28 01:57:31215 LOG(ERROR) << "Chrome could not be set as default browser.";
216 return false;
217 }
218
[email protected]8e96e502010-10-21 20:57:12219 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31220 return true;
221}
222
[email protected]4468a5b2011-05-26 07:48:02223bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
224 if (protocol.empty())
225 return false;
226
[email protected]650b2d52013-02-10 03:41:45227 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02228 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
229 LOG(ERROR) << "Error getting app exe path";
230 return false;
231 }
232
[email protected]ee9d89d2012-09-25 18:21:03233 string16 wprotocol(UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02234 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
235 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
236 wprotocol)) {
237 LOG(ERROR) << "Chrome could not be set as default handler for "
238 << protocol << ".";
239 return false;
240 }
241
242 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
243 return true;
244}
245
[email protected]bd046bd42012-06-08 05:07:32246bool ShellIntegration::SetAsDefaultBrowserInteractive() {
[email protected]650b2d52013-02-10 03:41:45247 base::FilePath chrome_exe;
[email protected]bd046bd42012-06-08 05:07:32248 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
249 NOTREACHED() << "Error getting app exe path";
250 return false;
251 }
252
253 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
254 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) {
255 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
256 return false;
257 }
258
[email protected]ee9d89d2012-09-25 18:21:03259 VLOG(1) << "Set-default-browser Windows UI completed.";
260 return true;
261}
262
263bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
264 const std::string& protocol) {
[email protected]650b2d52013-02-10 03:41:45265 base::FilePath chrome_exe;
[email protected]ee9d89d2012-09-25 18:21:03266 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
267 NOTREACHED() << "Error getting app exe path";
268 return false;
269 }
270
271 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
272 string16 wprotocol(UTF8ToUTF16(protocol));
273 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
274 dist, chrome_exe.value(), wprotocol)) {
275 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
276 return false;
277 }
278
279 VLOG(1) << "Set-default-client Windows UI completed.";
[email protected]bd046bd42012-06-08 05:07:32280 return true;
281}
282
[email protected]89886652012-12-11 18:09:07283ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
[email protected]eb63da72012-10-15 20:39:48284 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52285 ShellUtil::GetChromeDefaultState());
[email protected]4468a5b2011-05-26 07:48:02286}
287
288ShellIntegration::DefaultWebClientState
289 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
[email protected]eb63da72012-10-15 20:39:48290 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52291 ShellUtil::GetChromeDefaultProtocolClientState(UTF8ToUTF16(protocol)));
[email protected]d24c4012009-07-28 01:57:31292}
293
[email protected]42dc9402013-01-30 07:54:20294std::string ShellIntegration::GetApplicationForProtocol(const GURL& url) {
295 // TODO(calamity): this will be implemented when external_protocol_dialog is
296 // refactored on windows.
297 NOTREACHED();
298 return std::string();
299}
300
[email protected]d24c4012009-07-28 01:57:31301// There is no reliable way to say which browser is default on a machine (each
302// browser can have some of the protocols/shortcuts). So we look for only HTTP
303// protocol handler. Even this handler is located at different places in
304// registry on XP and Vista:
305// - HKCR\http\shell\open\command (XP)
306// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
307// http\UserChoice (Vista)
308// This method checks if Firefox is defualt browser by checking these
309// locations and returns true if Firefox traces are found there. In case of
310// error (or if Firefox is not found)it returns the default value which
311// is false.
312bool ShellIntegration::IsFirefoxDefaultBrowser() {
313 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15314 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]b6b72222012-02-11 02:04:13315 string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54316 base::win::RegKey key(HKEY_CURRENT_USER,
317 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46318 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31319 app_cmd == L"FirefoxURL")
320 ff_default = true;
321 } else {
[email protected]b6b72222012-02-11 02:04:13322 string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31323 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54324 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]b6b72222012-02-11 02:04:13325 string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46326 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]b6b72222012-02-11 02:04:13327 string16::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31328 ff_default = true;
329 }
330 return ff_default;
331}
[email protected]12f520c2010-01-06 18:11:15332
[email protected]a0448002012-06-19 04:32:10333string16 ShellIntegration::GetAppModelIdForProfile(
334 const string16& app_name,
[email protected]650b2d52013-02-10 03:41:45335 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10336 std::vector<string16> components;
337 components.push_back(app_name);
338 const string16 profile_id(GetProfileIdFromPath(profile_path));
339 if (!profile_id.empty())
340 components.push_back(profile_id);
341 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15342}
343
[email protected]a0448002012-06-19 04:32:10344string16 ShellIntegration::GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45345 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10346 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45347 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10348 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
349 NOTREACHED();
350 return dist->GetBaseAppId();
351 }
352 return GetAppModelIdForProfile(
[email protected]786799692012-09-26 14:16:48353 ShellUtil::GetBrowserModelId(
354 dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())),
355 profile_path);
[email protected]12f520c2010-01-06 18:11:15356}
[email protected]c9bb06f42010-01-13 23:53:48357
[email protected]99002fd2012-11-06 04:35:52358string16 ShellIntegration::GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45359 const base::FilePath& profile_path) {
[email protected]da1ffc32013-02-15 21:22:56360 return ShellIntegration::GetAppModelIdForProfile(
361 GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52362}
363
[email protected]9c91fc22012-11-20 04:13:49364string16 ShellIntegration::GetChromiumIconLocation() {
365 // Determine the path to chrome.exe. If we can't determine what that is,
366 // we have bigger fish to fry...
[email protected]650b2d52013-02-10 03:41:45367 base::FilePath chrome_exe;
[email protected]9c91fc22012-11-20 04:13:49368 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
[email protected]2de2d622011-10-21 22:07:37369 NOTREACHED();
370 return string16();
371 }
372
[email protected]9c91fc22012-11-20 04:13:49373 return ShellUtil::FormatIconLocation(
374 chrome_exe.value(),
[email protected]2de2d622011-10-21 22:07:37375 BrowserDistribution::GetDistribution()->GetIconIndex());
[email protected]2de2d622011-10-21 22:07:37376}
377
[email protected]c9bb06f42010-01-13 23:53:48378void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15379 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48380 return;
381
[email protected]81fcf952012-11-26 22:25:14382 // This needs to happen eventually (e.g. so that the appid is fixed and the
383 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
384 // urgent and shouldn't delay Chrome startup.
385 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
386 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38387 BrowserThread::FILE, FROM_HERE,
[email protected]81fcf952012-11-26 22:25:14388 base::Bind(&MigrateChromiumShortcutsCallback),
389 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48390}
[email protected]43903b82012-06-01 05:26:23391
[email protected]650b2d52013-02-10 03:41:45392int ShellIntegration::MigrateShortcutsInPathInternal(
393 const base::FilePath& chrome_exe,
394 const base::FilePath& path,
395 bool check_dual_mode) {
[email protected]8ea8f1ef2013-01-06 18:39:03396 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
397
398 // Enumerate all pinned shortcuts in the given path directly.
[email protected]25a4c1c2013-06-08 04:53:36399 base::FileEnumerator shortcuts_enum(
[email protected]8ea8f1ef2013-01-06 18:39:03400 path, false, // not recursive
[email protected]25a4c1c2013-06-08 04:53:36401 base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
[email protected]8ea8f1ef2013-01-06 18:39:03402
403 bool is_per_user_install =
404 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
405
406 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45407 base::FilePath target_path;
[email protected]8ea8f1ef2013-01-06 18:39:03408 string16 arguments;
[email protected]07983302013-01-21 19:41:44409 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45410 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03411 shortcut = shortcuts_enum.Next()) {
412 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
413 // it is fixed to work with FilePaths with spaces.
414 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
415 chrome_exe != target_path) {
416 continue;
417 }
418 CommandLine command_line(CommandLine::FromString(base::StringPrintf(
419 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
420
421 // Get the expected AppId for this Chrome shortcut.
422 string16 expected_app_id(
423 GetExpectedAppId(command_line, is_per_user_install));
424 if (expected_app_id.empty())
425 continue;
426
427 // Load the shortcut.
428 base::win::ScopedComPtr<IShellLink> shell_link;
429 base::win::ScopedComPtr<IPersistFile> persist_file;
430 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
431 CLSCTX_INPROC_SERVER)) ||
432 FAILED(persist_file.QueryFrom(shell_link)) ||
433 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
434 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
435 continue;
436 }
437
438 // Any properties that need to be updated on the shortcut will be stored in
439 // |updated_properties|.
440 base::win::ShortcutProperties updated_properties;
441
442 // Validate the existing app id for the shortcut.
443 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44444 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03445 if (FAILED(property_store.QueryFrom(shell_link)) ||
[email protected]07983302013-01-21 19:41:44446 property_store->GetValue(PKEY_AppUserModel_ID,
447 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03448 // When in doubt, prefer not updating the shortcut.
449 NOTREACHED();
450 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03451 } else {
[email protected]07983302013-01-21 19:41:44452 switch (propvariant.get().vt) {
453 case VT_EMPTY:
454 // If there is no app_id set, set our app_id if one is expected.
455 if (!expected_app_id.empty())
456 updated_properties.set_app_id(expected_app_id);
457 break;
458 case VT_LPWSTR:
459 if (expected_app_id != string16(propvariant.get().pwszVal))
460 updated_properties.set_app_id(expected_app_id);
461 break;
462 default:
463 NOTREACHED();
464 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03465 }
466 }
467
[email protected]b39e32e2013-04-24 08:55:54468 // Only set dual mode if the expected app id is the default app id.
469 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
470 string16 default_chromium_model_id(
471 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
472 if (check_dual_mode && expected_app_id == default_chromium_model_id) {
[email protected]07983302013-01-21 19:41:44473 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03474 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44475 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03476 // When in doubt, prefer to not update the shortcut.
477 NOTREACHED();
478 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03479 } else {
[email protected]07983302013-01-21 19:41:44480 switch (propvariant.get().vt) {
481 case VT_EMPTY:
482 // If dual_mode is not set at all, make sure it gets set to true.
483 updated_properties.set_dual_mode(true);
484 break;
485 case VT_BOOL:
486 // If it is set to false, make sure it gets set to true as well.
487 if (!propvariant.get().boolVal)
488 updated_properties.set_dual_mode(true);
489 break;
490 default:
491 NOTREACHED();
492 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03493 }
[email protected]8ea8f1ef2013-01-06 18:39:03494 }
495 }
496
497 persist_file.Release();
498 shell_link.Release();
499
500 // Update the shortcut if some of its properties need to be updated.
501 if (updated_properties.options &&
502 base::win::CreateOrUpdateShortcutLink(
503 shortcut, updated_properties,
504 base::win::SHORTCUT_UPDATE_EXISTING)) {
505 ++shortcuts_migrated;
506 }
507 }
508 return shortcuts_migrated;
509}
510
[email protected]650b2d52013-02-10 03:41:45511base::FilePath ShellIntegration::GetStartMenuShortcut(
512 const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27513 static const int kFolderIds[] = {
514 base::DIR_COMMON_START_MENU,
515 base::DIR_START_MENU,
516 };
517 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
518 string16 shortcut_name(dist->GetAppShortCutName());
[email protected]650b2d52013-02-10 03:41:45519 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27520
521 // Check both the common and the per-user Start Menu folders for system-level
522 // installs.
523 size_t folder =
524 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
525 for (; folder < arraysize(kFolderIds); ++folder) {
526 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
527 NOTREACHED();
528 continue;
529 }
530
[email protected]126f4622012-10-05 01:34:05531 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
532 installer::kLnkExt);
[email protected]7567484142013-07-11 17:36:07533 if (base::PathExists(shortcut))
[email protected]3f69d6e612012-08-03 18:52:27534 return shortcut;
535 }
536
[email protected]650b2d52013-02-10 03:41:45537 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27538}