blob: 137823b795e9bde44a48c41d6dc5242652394233 [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]caa05352014-03-01 00:43:058#include <shlwapi.h>
[email protected]d24c4012009-07-28 01:57:319#include <shobjidl.h>
[email protected]935aa542010-10-15 01:59:1510#include <propkey.h>
[email protected]d24c4012009-07-28 01:57:3111
[email protected]3a3e72c2011-11-29 02:59:3812#include "base/bind.h"
[email protected]d24c4012009-07-28 01:57:3113#include "base/command_line.h"
14#include "base/file_util.h"
[email protected]25a4c1c2013-06-08 04:53:3615#include "base/files/file_enumerator.h"
[email protected]fa1e0e12013-07-18 00:10:1416#include "base/message_loop/message_loop.h"
[email protected]d24c4012009-07-28 01:57:3117#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4118#include "base/strings/string_number_conversions.h"
[email protected]24a555b62013-06-10 22:01:1719#include "base/strings/string_util.h"
20#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0821#include "base/strings/utf_string_conversions.h"
[email protected]2d6503982010-10-17 04:41:5422#include "base/win/registry.h"
[email protected]8ee65ba2011-04-12 20:53:2323#include "base/win/scoped_comptr.h"
[email protected]07983302013-01-21 19:41:4424#include "base/win/scoped_propvariant.h"
[email protected]f1024e22012-09-12 07:14:5525#include "base/win/shortcut.h"
[email protected]935aa542010-10-15 01:59:1526#include "base/win/windows_version.h"
[email protected]89d43832013-06-29 20:25:2027#include "chrome/browser/policy/policy_path_parser.h"
[email protected]c9bb06f42010-01-13 23:53:4828#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3129#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1530#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4831#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0232#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3133#include "chrome/installer/util/browser_distribution.h"
34#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2735#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3136#include "chrome/installer/util/set_reg_value_work_item.h"
37#include "chrome/installer/util/shell_util.h"
38#include "chrome/installer/util/util_constants.h"
39#include "chrome/installer/util/work_item.h"
40#include "chrome/installer/util/work_item_list.h"
[email protected]c38831a12011-10-28 12:44:4941#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3142
[email protected]631bb742011-11-02 11:29:3943using content::BrowserThread;
44
[email protected]12f520c2010-01-06 18:11:1545namespace {
46
[email protected]da1ffc32013-02-15 21:22:5647const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5248
[email protected]12f520c2010-01-06 18:11:1549// Helper function for ShellIntegration::GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1450// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1551// user data dir and profile dir joined by a ".".
[email protected]6a72a632013-12-12 22:22:0052base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1553 // Return empty string if profile_path is empty
54 if (profile_path.empty())
[email protected]0085863a2013-12-06 21:19:0355 return base::string16();
[email protected]12f520c2010-01-06 18:11:1556
[email protected]650b2d52013-02-10 03:41:4557 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1558 // Return empty string if profile_path is in default user data
59 // dir and is the default profile.
60 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
61 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4862 profile_path.BaseName().value() ==
[email protected]f911df52013-12-24 23:24:2363 base::ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]0085863a2013-12-06 21:19:0364 return base::string16();
[email protected]162b5992011-03-15 19:40:4865 }
[email protected]12f520c2010-01-06 18:11:1566
67 // Get joined basenames of user data dir and profile.
[email protected]0085863a2013-12-06 21:19:0368 base::string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1569 L"." + profile_path.BaseName().value();
70
[email protected]0085863a2013-12-06 21:19:0371 base::string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1572 profile_id.reserve(basenames.size());
73
74 // Generate profile_id from sanitized basenames.
75 for (size_t i = 0; i < basenames.length(); ++i) {
76 if (IsAsciiAlpha(basenames[i]) ||
77 IsAsciiDigit(basenames[i]) ||
78 basenames[i] == L'.')
79 profile_id += basenames[i];
80 }
81
82 return profile_id;
83}
84
[email protected]6a72a632013-12-12 22:22:0085base::string16 GetAppListAppName() {
[email protected]da1ffc32013-02-15 21:22:5686 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:0387 base::string16 app_name(dist->GetBaseAppId());
[email protected]da1ffc32013-02-15 21:22:5688 app_name.append(kAppListAppNameSuffix);
89 return app_name;
90}
91
[email protected]8ea8f1ef2013-01-06 18:39:0392// Gets expected app id for given Chrome (based on |command_line| and
93// |is_per_user_install|).
[email protected]6a72a632013-12-12 22:22:0094base::string16 GetExpectedAppId(const CommandLine& command_line,
95 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:5496 base::FilePath user_data_dir;
97 if (command_line.HasSwitch(switches::kUserDataDir))
98 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
99 else
100 chrome::GetDefaultUserDataDirectory(&user_data_dir);
[email protected]89d43832013-06-29 20:25:20101 // Adjust with any policy that overrides any other way to set the path.
102 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
[email protected]b39e32e2013-04-24 08:55:54103 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:48104
[email protected]b39e32e2013-04-24 08:55:54105 base::FilePath profile_subdir;
106 if (command_line.HasSwitch(switches::kProfileDirectory)) {
107 profile_subdir =
108 command_line.GetSwitchValuePath(switches::kProfileDirectory);
109 } else {
[email protected]f911df52013-12-24 23:24:23110 profile_subdir =
111 base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile));
[email protected]b39e32e2013-04-24 08:55:54112 }
113 DCHECK(!profile_subdir.empty());
114
115 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]0085863a2013-12-06 21:19:03116 base::string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48117 if (command_line.HasSwitch(switches::kApp)) {
[email protected]f911df52013-12-24 23:24:23118 app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51119 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14120 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]f911df52013-12-24 23:24:23121 app_name = base::UTF8ToUTF16(
122 web_app::GenerateApplicationNameFromExtensionId(
123 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52124 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56125 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48126 } else {
[email protected]a0448002012-06-19 04:32:10127 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03128 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48129 }
[email protected]b39e32e2013-04-24 08:55:54130 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48131
[email protected]8ea8f1ef2013-01-06 18:39:03132 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48133}
134
[email protected]3a3e72c2011-11-29 02:59:38135void MigrateChromiumShortcutsCallback() {
136 // This should run on the file thread.
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
138
139 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45140 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38141 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
142 return;
143
144 // Locations to check for shortcuts migration.
145 static const struct {
146 int location_id;
147 const wchar_t* sub_dir;
148 } kLocations[] = {
149 {
[email protected]e5f9d822012-11-06 22:27:01150 base::DIR_TASKBAR_PINS,
151 NULL
[email protected]3a3e72c2011-11-29 02:59:38152 }, {
[email protected]dea1d7d2012-09-20 16:24:52153 base::DIR_USER_DESKTOP,
[email protected]3a3e72c2011-11-29 02:59:38154 NULL
155 }, {
156 base::DIR_START_MENU,
157 NULL
158 }, {
159 base::DIR_APP_DATA,
160 L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
161 }
162 };
163
164 for (int i = 0; i < arraysize(kLocations); ++i) {
[email protected]650b2d52013-02-10 03:41:45165 base::FilePath path;
[email protected]3a3e72c2011-11-29 02:59:38166 if (!PathService::Get(kLocations[i].location_id, &path)) {
167 NOTREACHED();
168 continue;
169 }
170
171 if (kLocations[i].sub_dir)
172 path = path.Append(kLocations[i].sub_dir);
173
[email protected]8ea8f1ef2013-01-06 18:39:03174 bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
175 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
176 check_dual_mode);
[email protected]3a3e72c2011-11-29 02:59:38177 }
178}
179
[email protected]caa05352014-03-01 00:43:05180// Windows 8 introduced a new protocol->executable binding system which cannot
181// be retrieved in the HKCR registry subkey method implemented below. We call
182// AssocQueryString with the new Win8-only flag ASSOCF_IS_PROTOCOL instead.
183base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) {
[email protected]6e84d372014-05-29 23:36:39184 base::string16 url_scheme = base::ASCIIToWide(url.scheme());
[email protected]caa05352014-03-01 00:43:05185 // Don't attempt to query protocol association on an empty string.
[email protected]6e84d372014-05-29 23:36:39186 if (url_scheme.empty())
[email protected]caa05352014-03-01 00:43:05187 return base::string16();
188
[email protected]caa05352014-03-01 00:43:05189 // Query AssocQueryString for a human-readable description of the program
190 // that will be invoked given the provided URL spec. This is used only to
191 // populate the external protocol dialog box the user sees when invoking
192 // an unknown external protocol.
193 wchar_t out_buffer[1024];
194 DWORD buffer_size = arraysize(out_buffer);
195 HRESULT hr = AssocQueryString(ASSOCF_IS_PROTOCOL,
196 ASSOCSTR_FRIENDLYAPPNAME,
[email protected]6e84d372014-05-29 23:36:39197 url_scheme.c_str(),
[email protected]caa05352014-03-01 00:43:05198 NULL,
199 out_buffer,
200 &buffer_size);
201 if (FAILED(hr)) {
202 DLOG(WARNING) << "AssocQueryString failed!";
203 return base::string16();
204 }
205 return base::string16(out_buffer);
206}
207
208base::string16 GetAppForProtocolUsingRegistry(const GURL& url) {
209 base::string16 url_spec = base::ASCIIToWide(url.possibly_invalid_spec());
210 const base::string16 cmd_key_path =
211 base::ASCIIToWide(url.scheme() + "\\shell\\open\\command");
212 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT,
213 cmd_key_path.c_str(),
214 KEY_READ);
215 size_t split_offset = url_spec.find(L':');
216 if (split_offset == base::string16::npos)
217 return base::string16();
218 const base::string16 parameters = url_spec.substr(split_offset + 1,
219 url_spec.length() - 1);
220 base::string16 application_to_launch;
221 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) {
222 ReplaceSubstringsAfterOffset(&application_to_launch,
223 0,
224 L"%1",
225 parameters);
226 return application_to_launch;
227 }
228 return base::string16();
229}
230
231
[email protected]eb63da72012-10-15 20:39:48232ShellIntegration::DefaultWebClientState
233 GetDefaultWebClientStateFromShellUtilDefaultState(
234 ShellUtil::DefaultState default_state) {
235 switch (default_state) {
236 case ShellUtil::NOT_DEFAULT:
[email protected]89886652012-12-11 18:09:07237 return ShellIntegration::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48238 case ShellUtil::IS_DEFAULT:
[email protected]89886652012-12-11 18:09:07239 return ShellIntegration::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48240 default:
241 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
[email protected]89886652012-12-11 18:09:07242 return ShellIntegration::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48243 }
244}
245
[email protected]3a3e72c2011-11-29 02:59:38246} // namespace
[email protected]12f520c2010-01-06 18:11:15247
[email protected]bd046bd42012-06-08 05:07:32248ShellIntegration::DefaultWebClientSetPermission
249 ShellIntegration::CanSetAsDefaultBrowser() {
[email protected]ee5396f602013-10-23 18:52:54250 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
251 if (distribution->GetDefaultBrowserControlPolicy() !=
252 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
[email protected]bd046bd42012-06-08 05:07:32253 return SET_DEFAULT_NOT_ALLOWED;
254
[email protected]9fcf07f2012-06-18 16:13:51255 if (ShellUtil::CanMakeChromeDefaultUnattended())
[email protected]bd046bd42012-06-08 05:07:32256 return SET_DEFAULT_UNATTENDED;
[email protected]9fcf07f2012-06-18 16:13:51257 else
258 return SET_DEFAULT_INTERACTIVE;
[email protected]a01481b2011-07-15 04:30:02259}
260
[email protected]d24c4012009-07-28 01:57:31261bool ShellIntegration::SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45262 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31263 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
264 LOG(ERROR) << "Error getting app exe path";
265 return false;
266 }
267
268 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25269 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
270 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
[email protected]b9696482010-11-30 23:56:18271 chrome_exe.value(), true)) {
[email protected]d24c4012009-07-28 01:57:31272 LOG(ERROR) << "Chrome could not be set as default browser.";
273 return false;
274 }
275
[email protected]8e96e502010-10-21 20:57:12276 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31277 return true;
278}
279
[email protected]4468a5b2011-05-26 07:48:02280bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
281 if (protocol.empty())
282 return false;
283
[email protected]650b2d52013-02-10 03:41:45284 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02285 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
286 LOG(ERROR) << "Error getting app exe path";
287 return false;
288 }
289
[email protected]f911df52013-12-24 23:24:23290 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02291 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
292 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
293 wprotocol)) {
294 LOG(ERROR) << "Chrome could not be set as default handler for "
295 << protocol << ".";
296 return false;
297 }
298
299 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
300 return true;
301}
302
[email protected]bd046bd42012-06-08 05:07:32303bool ShellIntegration::SetAsDefaultBrowserInteractive() {
[email protected]650b2d52013-02-10 03:41:45304 base::FilePath chrome_exe;
[email protected]bd046bd42012-06-08 05:07:32305 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
306 NOTREACHED() << "Error getting app exe path";
307 return false;
308 }
309
310 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
311 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) {
312 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
313 return false;
314 }
315
[email protected]ee9d89d2012-09-25 18:21:03316 VLOG(1) << "Set-default-browser Windows UI completed.";
317 return true;
318}
319
320bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
321 const std::string& protocol) {
[email protected]650b2d52013-02-10 03:41:45322 base::FilePath chrome_exe;
[email protected]ee9d89d2012-09-25 18:21:03323 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
324 NOTREACHED() << "Error getting app exe path";
325 return false;
326 }
327
328 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]f911df52013-12-24 23:24:23329 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
[email protected]ee9d89d2012-09-25 18:21:03330 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
331 dist, chrome_exe.value(), wprotocol)) {
332 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
333 return false;
334 }
335
336 VLOG(1) << "Set-default-client Windows UI completed.";
[email protected]bd046bd42012-06-08 05:07:32337 return true;
338}
339
[email protected]89886652012-12-11 18:09:07340ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
[email protected]eb63da72012-10-15 20:39:48341 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]8ad11c552012-10-16 17:40:52342 ShellUtil::GetChromeDefaultState());
[email protected]4468a5b2011-05-26 07:48:02343}
344
345ShellIntegration::DefaultWebClientState
346 ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
[email protected]eb63da72012-10-15 20:39:48347 return GetDefaultWebClientStateFromShellUtilDefaultState(
[email protected]f911df52013-12-24 23:24:23348 ShellUtil::GetChromeDefaultProtocolClientState(
349 base::UTF8ToUTF16(protocol)));
[email protected]d24c4012009-07-28 01:57:31350}
351
[email protected]caa05352014-03-01 00:43:05352base::string16 ShellIntegration::GetApplicationNameForProtocol(
353 const GURL& url) {
354 // Windows 8 or above requires a new protocol association query.
355 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
356 return GetAppForProtocolUsingAssocQuery(url);
357 else
358 return GetAppForProtocolUsingRegistry(url);
[email protected]42dc9402013-01-30 07:54:20359}
360
[email protected]d24c4012009-07-28 01:57:31361// There is no reliable way to say which browser is default on a machine (each
362// browser can have some of the protocols/shortcuts). So we look for only HTTP
363// protocol handler. Even this handler is located at different places in
364// registry on XP and Vista:
365// - HKCR\http\shell\open\command (XP)
366// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
367// http\UserChoice (Vista)
368// This method checks if Firefox is defualt browser by checking these
369// locations and returns true if Firefox traces are found there. In case of
370// error (or if Firefox is not found)it returns the default value which
371// is false.
372bool ShellIntegration::IsFirefoxDefaultBrowser() {
373 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15374 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]0085863a2013-12-06 21:19:03375 base::string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54376 base::win::RegKey key(HKEY_CURRENT_USER,
377 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46378 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31379 app_cmd == L"FirefoxURL")
380 ff_default = true;
381 } else {
[email protected]0085863a2013-12-06 21:19:03382 base::string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31383 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54384 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]0085863a2013-12-06 21:19:03385 base::string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46386 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]0085863a2013-12-06 21:19:03387 base::string16::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31388 ff_default = true;
389 }
390 return ff_default;
391}
[email protected]12f520c2010-01-06 18:11:15392
[email protected]6a72a632013-12-12 22:22:00393base::string16 ShellIntegration::GetAppModelIdForProfile(
[email protected]0085863a2013-12-06 21:19:03394 const base::string16& app_name,
[email protected]650b2d52013-02-10 03:41:45395 const base::FilePath& profile_path) {
[email protected]d2065e062013-12-12 23:49:52396 std::vector<base::string16> components;
[email protected]a0448002012-06-19 04:32:10397 components.push_back(app_name);
[email protected]0085863a2013-12-06 21:19:03398 const base::string16 profile_id(GetProfileIdFromPath(profile_path));
[email protected]a0448002012-06-19 04:32:10399 if (!profile_id.empty())
400 components.push_back(profile_id);
401 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15402}
403
[email protected]6a72a632013-12-12 22:22:00404base::string16 ShellIntegration::GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45405 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10406 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45407 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10408 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
409 NOTREACHED();
410 return dist->GetBaseAppId();
411 }
412 return GetAppModelIdForProfile(
[email protected]786799692012-09-26 14:16:48413 ShellUtil::GetBrowserModelId(
414 dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())),
415 profile_path);
[email protected]12f520c2010-01-06 18:11:15416}
[email protected]c9bb06f42010-01-13 23:53:48417
[email protected]6a72a632013-12-12 22:22:00418base::string16 ShellIntegration::GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45419 const base::FilePath& profile_path) {
[email protected]da1ffc32013-02-15 21:22:56420 return ShellIntegration::GetAppModelIdForProfile(
421 GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52422}
423
[email protected]c9bb06f42010-01-13 23:53:48424void ShellIntegration::MigrateChromiumShortcuts() {
[email protected]935aa542010-10-15 01:59:15425 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48426 return;
427
[email protected]81fcf952012-11-26 22:25:14428 // This needs to happen eventually (e.g. so that the appid is fixed and the
429 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
430 // urgent and shouldn't delay Chrome startup.
431 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
432 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38433 BrowserThread::FILE, FROM_HERE,
[email protected]81fcf952012-11-26 22:25:14434 base::Bind(&MigrateChromiumShortcutsCallback),
435 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48436}
[email protected]43903b82012-06-01 05:26:23437
[email protected]650b2d52013-02-10 03:41:45438int ShellIntegration::MigrateShortcutsInPathInternal(
439 const base::FilePath& chrome_exe,
440 const base::FilePath& path,
441 bool check_dual_mode) {
[email protected]8ea8f1ef2013-01-06 18:39:03442 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
443
444 // Enumerate all pinned shortcuts in the given path directly.
[email protected]25a4c1c2013-06-08 04:53:36445 base::FileEnumerator shortcuts_enum(
[email protected]8ea8f1ef2013-01-06 18:39:03446 path, false, // not recursive
[email protected]25a4c1c2013-06-08 04:53:36447 base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
[email protected]8ea8f1ef2013-01-06 18:39:03448
449 bool is_per_user_install =
450 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
451
452 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45453 base::FilePath target_path;
[email protected]0085863a2013-12-06 21:19:03454 base::string16 arguments;
[email protected]07983302013-01-21 19:41:44455 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45456 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03457 shortcut = shortcuts_enum.Next()) {
458 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
459 // it is fixed to work with FilePaths with spaces.
460 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
461 chrome_exe != target_path) {
462 continue;
463 }
464 CommandLine command_line(CommandLine::FromString(base::StringPrintf(
465 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
466
467 // Get the expected AppId for this Chrome shortcut.
[email protected]0085863a2013-12-06 21:19:03468 base::string16 expected_app_id(
[email protected]8ea8f1ef2013-01-06 18:39:03469 GetExpectedAppId(command_line, is_per_user_install));
470 if (expected_app_id.empty())
471 continue;
472
473 // Load the shortcut.
474 base::win::ScopedComPtr<IShellLink> shell_link;
475 base::win::ScopedComPtr<IPersistFile> persist_file;
476 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
477 CLSCTX_INPROC_SERVER)) ||
478 FAILED(persist_file.QueryFrom(shell_link)) ||
479 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
480 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
481 continue;
482 }
483
484 // Any properties that need to be updated on the shortcut will be stored in
485 // |updated_properties|.
486 base::win::ShortcutProperties updated_properties;
487
488 // Validate the existing app id for the shortcut.
489 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44490 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03491 if (FAILED(property_store.QueryFrom(shell_link)) ||
[email protected]07983302013-01-21 19:41:44492 property_store->GetValue(PKEY_AppUserModel_ID,
493 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03494 // When in doubt, prefer not updating the shortcut.
495 NOTREACHED();
496 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03497 } else {
[email protected]07983302013-01-21 19:41:44498 switch (propvariant.get().vt) {
499 case VT_EMPTY:
500 // If there is no app_id set, set our app_id if one is expected.
501 if (!expected_app_id.empty())
502 updated_properties.set_app_id(expected_app_id);
503 break;
504 case VT_LPWSTR:
[email protected]0085863a2013-12-06 21:19:03505 if (expected_app_id != base::string16(propvariant.get().pwszVal))
[email protected]07983302013-01-21 19:41:44506 updated_properties.set_app_id(expected_app_id);
507 break;
508 default:
509 NOTREACHED();
510 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03511 }
512 }
513
[email protected]b39e32e2013-04-24 08:55:54514 // Only set dual mode if the expected app id is the default app id.
515 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03516 base::string16 default_chromium_model_id(
[email protected]b39e32e2013-04-24 08:55:54517 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
518 if (check_dual_mode && expected_app_id == default_chromium_model_id) {
[email protected]07983302013-01-21 19:41:44519 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03520 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44521 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03522 // When in doubt, prefer to not update the shortcut.
523 NOTREACHED();
524 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03525 } else {
[email protected]07983302013-01-21 19:41:44526 switch (propvariant.get().vt) {
527 case VT_EMPTY:
528 // If dual_mode is not set at all, make sure it gets set to true.
529 updated_properties.set_dual_mode(true);
530 break;
531 case VT_BOOL:
532 // If it is set to false, make sure it gets set to true as well.
533 if (!propvariant.get().boolVal)
534 updated_properties.set_dual_mode(true);
535 break;
536 default:
537 NOTREACHED();
538 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03539 }
[email protected]8ea8f1ef2013-01-06 18:39:03540 }
541 }
542
543 persist_file.Release();
544 shell_link.Release();
545
546 // Update the shortcut if some of its properties need to be updated.
547 if (updated_properties.options &&
548 base::win::CreateOrUpdateShortcutLink(
549 shortcut, updated_properties,
550 base::win::SHORTCUT_UPDATE_EXISTING)) {
551 ++shortcuts_migrated;
552 }
553 }
554 return shortcuts_migrated;
555}
556
[email protected]650b2d52013-02-10 03:41:45557base::FilePath ShellIntegration::GetStartMenuShortcut(
558 const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27559 static const int kFolderIds[] = {
560 base::DIR_COMMON_START_MENU,
561 base::DIR_START_MENU,
562 };
563 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03564 base::string16 shortcut_name(
[email protected]16fcc0d2013-08-14 04:22:13565 dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME));
[email protected]650b2d52013-02-10 03:41:45566 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27567
568 // Check both the common and the per-user Start Menu folders for system-level
569 // installs.
570 size_t folder =
571 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
572 for (; folder < arraysize(kFolderIds); ++folder) {
573 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
574 NOTREACHED();
575 continue;
576 }
577
[email protected]126f4622012-10-05 01:34:05578 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
579 installer::kLnkExt);
[email protected]7567484142013-07-11 17:36:07580 if (base::PathExists(shortcut))
[email protected]3f69d6e612012-08-03 18:52:27581 return shortcut;
582 }
583
[email protected]650b2d52013-02-10 03:41:45584 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27585}