blob: 9a92a9c438eacc3dd186c19701c4a74d41639d37 [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
pmonette32a5cfb42016-04-11 22:04:445#include "chrome/browser/shell_integration_win.h"
[email protected]d24c4012009-07-28 01:57:316
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>
thestig18dfb7a52014-08-26 10:44:0410#include <propkey.h> // Needs to come after shobjidl.h.
avi664c07b2015-12-26 02:18:3111#include <stddef.h>
12#include <stdint.h>
[email protected]d24c4012009-07-28 01:57:3113
pmonette32a5cfb42016-04-11 22:04:4414#include <vector>
15
[email protected]3a3e72c2011-11-29 02:59:3816#include "base/bind.h"
pmonette32a5cfb42016-04-11 22:04:4417#include "base/callback.h"
[email protected]d24c4012009-07-28 01:57:3118#include "base/command_line.h"
[email protected]25a4c1c2013-06-08 04:53:3619#include "base/files/file_enumerator.h"
thestig18dfb7a52014-08-26 10:44:0420#include "base/files/file_util.h"
avi664c07b2015-12-26 02:18:3121#include "base/macros.h"
pmonette32a5cfb42016-04-11 22:04:4422#include "base/memory/weak_ptr.h"
[email protected]fa1e0e12013-07-18 00:10:1423#include "base/message_loop/message_loop.h"
pmonette32a5cfb42016-04-11 22:04:4424#include "base/metrics/histogram_macros.h"
[email protected]d24c4012009-07-28 01:57:3125#include "base/path_service.h"
[email protected]24a555b62013-06-10 22:01:1726#include "base/strings/string_util.h"
27#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0828#include "base/strings/utf_string_conversions.h"
pmonette32a5cfb42016-04-11 22:04:4429#include "base/time/time.h"
30#include "base/timer/timer.h"
[email protected]2d6503982010-10-17 04:41:5431#include "base/win/registry.h"
[email protected]8ee65ba2011-04-12 20:53:2332#include "base/win/scoped_comptr.h"
[email protected]07983302013-01-21 19:41:4433#include "base/win/scoped_propvariant.h"
[email protected]f1024e22012-09-12 07:14:5534#include "base/win/shortcut.h"
[email protected]935aa542010-10-15 01:59:1535#include "base/win/windows_version.h"
[email protected]89d43832013-06-29 20:25:2036#include "chrome/browser/policy/policy_path_parser.h"
pmonette32a5cfb42016-04-11 22:04:4437#include "chrome/browser/shell_integration.h"
[email protected]c9bb06f42010-01-13 23:53:4838#include "chrome/browser/web_applications/web_app.h"
[email protected]d24c4012009-07-28 01:57:3139#include "chrome/common/chrome_constants.h"
[email protected]12f520c2010-01-06 18:11:1540#include "chrome/common/chrome_paths_internal.h"
[email protected]c9bb06f42010-01-13 23:53:4841#include "chrome/common/chrome_switches.h"
[email protected]4468a5b2011-05-26 07:48:0242#include "chrome/installer/setup/setup_util.h"
[email protected]d24c4012009-07-28 01:57:3143#include "chrome/installer/util/browser_distribution.h"
44#include "chrome/installer/util/create_reg_key_work_item.h"
[email protected]3f69d6e612012-08-03 18:52:2745#include "chrome/installer/util/install_util.h"
[email protected]d24c4012009-07-28 01:57:3146#include "chrome/installer/util/set_reg_value_work_item.h"
47#include "chrome/installer/util/shell_util.h"
48#include "chrome/installer/util/util_constants.h"
49#include "chrome/installer/util/work_item.h"
50#include "chrome/installer/util/work_item_list.h"
pmonette2b1dbee2016-01-08 20:18:5851#include "components/variations/variations_associated_data.h"
[email protected]c38831a12011-10-28 12:44:4952#include "content/public/browser/browser_thread.h"
[email protected]d24c4012009-07-28 01:57:3153
[email protected]631bb742011-11-02 11:29:3954using content::BrowserThread;
55
pmonette9fa59e882016-02-10 00:12:1956namespace shell_integration {
57
[email protected]12f520c2010-01-06 18:11:1558namespace {
59
[email protected]da1ffc32013-02-15 21:22:5660const wchar_t kAppListAppNameSuffix[] = L"AppList";
[email protected]99002fd2012-11-06 04:35:5261
pmonette9fa59e882016-02-10 00:12:1962// Helper function for GetAppId to generates profile id
[email protected]2f1c09d2011-01-14 14:58:1463// from profile path. "profile_id" is composed of sanitized basenames of
[email protected]12f520c2010-01-06 18:11:1564// user data dir and profile dir joined by a ".".
[email protected]6a72a632013-12-12 22:22:0065base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
[email protected]12f520c2010-01-06 18:11:1566 // Return empty string if profile_path is empty
67 if (profile_path.empty())
[email protected]0085863a2013-12-06 21:19:0368 return base::string16();
[email protected]12f520c2010-01-06 18:11:1569
[email protected]650b2d52013-02-10 03:41:4570 base::FilePath default_user_data_dir;
[email protected]12f520c2010-01-06 18:11:1571 // Return empty string if profile_path is in default user data
72 // dir and is the default profile.
73 if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
74 profile_path.DirName() == default_user_data_dir &&
[email protected]162b5992011-03-15 19:40:4875 profile_path.BaseName().value() ==
[email protected]f911df52013-12-24 23:24:2376 base::ASCIIToUTF16(chrome::kInitialProfile)) {
[email protected]0085863a2013-12-06 21:19:0377 return base::string16();
[email protected]162b5992011-03-15 19:40:4878 }
[email protected]12f520c2010-01-06 18:11:1579
80 // Get joined basenames of user data dir and profile.
[email protected]0085863a2013-12-06 21:19:0381 base::string16 basenames = profile_path.DirName().BaseName().value() +
[email protected]12f520c2010-01-06 18:11:1582 L"." + profile_path.BaseName().value();
83
[email protected]0085863a2013-12-06 21:19:0384 base::string16 profile_id;
[email protected]12f520c2010-01-06 18:11:1585 profile_id.reserve(basenames.size());
86
87 // Generate profile_id from sanitized basenames.
88 for (size_t i = 0; i < basenames.length(); ++i) {
brettwb3413062015-06-24 00:39:0289 if (base::IsAsciiAlpha(basenames[i]) ||
90 base::IsAsciiDigit(basenames[i]) ||
[email protected]12f520c2010-01-06 18:11:1591 basenames[i] == L'.')
92 profile_id += basenames[i];
93 }
94
95 return profile_id;
96}
97
[email protected]6a72a632013-12-12 22:22:0098base::string16 GetAppListAppName() {
[email protected]da1ffc32013-02-15 21:22:5699 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03100 base::string16 app_name(dist->GetBaseAppId());
[email protected]da1ffc32013-02-15 21:22:56101 app_name.append(kAppListAppNameSuffix);
102 return app_name;
103}
104
[email protected]8ea8f1ef2013-01-06 18:39:03105// Gets expected app id for given Chrome (based on |command_line| and
106// |is_per_user_install|).
avi556c05022014-12-22 23:31:43107base::string16 GetExpectedAppId(const base::CommandLine& command_line,
[email protected]6a72a632013-12-12 22:22:00108 bool is_per_user_install) {
[email protected]b39e32e2013-04-24 08:55:54109 base::FilePath user_data_dir;
110 if (command_line.HasSwitch(switches::kUserDataDir))
111 user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
112 else
113 chrome::GetDefaultUserDataDirectory(&user_data_dir);
[email protected]89d43832013-06-29 20:25:20114 // Adjust with any policy that overrides any other way to set the path.
115 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
[email protected]b39e32e2013-04-24 08:55:54116 DCHECK(!user_data_dir.empty());
[email protected]c9bb06f42010-01-13 23:53:48117
[email protected]b39e32e2013-04-24 08:55:54118 base::FilePath profile_subdir;
119 if (command_line.HasSwitch(switches::kProfileDirectory)) {
120 profile_subdir =
121 command_line.GetSwitchValuePath(switches::kProfileDirectory);
122 } else {
[email protected]f911df52013-12-24 23:24:23123 profile_subdir =
124 base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile));
[email protected]b39e32e2013-04-24 08:55:54125 }
126 DCHECK(!profile_subdir.empty());
127
128 base::FilePath profile_path = user_data_dir.Append(profile_subdir);
[email protected]0085863a2013-12-06 21:19:03129 base::string16 app_name;
[email protected]c9bb06f42010-01-13 23:53:48130 if (command_line.HasSwitch(switches::kApp)) {
[email protected]f911df52013-12-24 23:24:23131 app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
[email protected]57ecc4b2010-08-11 03:02:51132 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
[email protected]2f1c09d2011-01-14 14:58:14133 } else if (command_line.HasSwitch(switches::kAppId)) {
[email protected]f911df52013-12-24 23:24:23134 app_name = base::UTF8ToUTF16(
135 web_app::GenerateApplicationNameFromExtensionId(
136 command_line.GetSwitchValueASCII(switches::kAppId)));
[email protected]99002fd2012-11-06 04:35:52137 } else if (command_line.HasSwitch(switches::kShowAppList)) {
[email protected]da1ffc32013-02-15 21:22:56138 app_name = GetAppListAppName();
[email protected]c9bb06f42010-01-13 23:53:48139 } else {
[email protected]a0448002012-06-19 04:32:10140 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]8ea8f1ef2013-01-06 18:39:03141 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
[email protected]c9bb06f42010-01-13 23:53:48142 }
[email protected]b39e32e2013-04-24 08:55:54143 DCHECK(!app_name.empty());
[email protected]c9bb06f42010-01-13 23:53:48144
pmonette9fa59e882016-02-10 00:12:19145 return GetAppModelIdForProfile(app_name, profile_path);
[email protected]c9bb06f42010-01-13 23:53:48146}
147
gab88257b62016-01-15 03:19:19148void MigrateTaskbarPinsCallback() {
[email protected]3a3e72c2011-11-29 02:59:38149 // This should run on the file thread.
thestig00844cea2015-09-08 21:44:52150 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
[email protected]3a3e72c2011-11-29 02:59:38151
152 // Get full path of chrome.
[email protected]650b2d52013-02-10 03:41:45153 base::FilePath chrome_exe;
[email protected]3a3e72c2011-11-29 02:59:38154 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
155 return;
156
gab88257b62016-01-15 03:19:19157 base::FilePath pins_path;
158 if (!PathService::Get(base::DIR_TASKBAR_PINS, &pins_path)) {
159 NOTREACHED();
160 return;
[email protected]3a3e72c2011-11-29 02:59:38161 }
gab88257b62016-01-15 03:19:19162
pmonette9fa59e882016-02-10 00:12:19163 MigrateShortcutsInPathInternal(chrome_exe, pins_path);
[email protected]3a3e72c2011-11-29 02:59:38164}
165
[email protected]caa05352014-03-01 00:43:05166// Windows 8 introduced a new protocol->executable binding system which cannot
167// be retrieved in the HKCR registry subkey method implemented below. We call
168// AssocQueryString with the new Win8-only flag ASSOCF_IS_PROTOCOL instead.
169base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) {
thestig11bf74d2014-11-24 20:14:42170 base::string16 url_scheme = base::ASCIIToUTF16(url.scheme());
[email protected]caa05352014-03-01 00:43:05171 // Don't attempt to query protocol association on an empty string.
[email protected]6e84d372014-05-29 23:36:39172 if (url_scheme.empty())
[email protected]caa05352014-03-01 00:43:05173 return base::string16();
174
[email protected]caa05352014-03-01 00:43:05175 // Query AssocQueryString for a human-readable description of the program
176 // that will be invoked given the provided URL spec. This is used only to
177 // populate the external protocol dialog box the user sees when invoking
178 // an unknown external protocol.
179 wchar_t out_buffer[1024];
180 DWORD buffer_size = arraysize(out_buffer);
181 HRESULT hr = AssocQueryString(ASSOCF_IS_PROTOCOL,
182 ASSOCSTR_FRIENDLYAPPNAME,
[email protected]6e84d372014-05-29 23:36:39183 url_scheme.c_str(),
[email protected]caa05352014-03-01 00:43:05184 NULL,
185 out_buffer,
186 &buffer_size);
187 if (FAILED(hr)) {
188 DLOG(WARNING) << "AssocQueryString failed!";
189 return base::string16();
190 }
191 return base::string16(out_buffer);
192}
193
194base::string16 GetAppForProtocolUsingRegistry(const GURL& url) {
[email protected]caa05352014-03-01 00:43:05195 const base::string16 cmd_key_path =
thestig11bf74d2014-11-24 20:14:42196 base::ASCIIToUTF16(url.scheme() + "\\shell\\open\\command");
[email protected]caa05352014-03-01 00:43:05197 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT,
198 cmd_key_path.c_str(),
199 KEY_READ);
[email protected]caa05352014-03-01 00:43:05200 base::string16 application_to_launch;
201 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) {
benwellse84d8392015-08-18 05:23:58202 const base::string16 url_spec =
203 base::ASCIIToUTF16(url.possibly_invalid_spec());
brettwe6dae462015-06-24 20:54:45204 base::ReplaceSubstringsAfterOffset(&application_to_launch,
205 0,
206 L"%1",
benwellse84d8392015-08-18 05:23:58207 url_spec);
[email protected]caa05352014-03-01 00:43:05208 return application_to_launch;
209 }
210 return base::string16();
211}
212
pmonette9fa59e882016-02-10 00:12:19213DefaultWebClientState GetDefaultWebClientStateFromShellUtilDefaultState(
214 ShellUtil::DefaultState default_state) {
[email protected]eb63da72012-10-15 20:39:48215 switch (default_state) {
216 case ShellUtil::NOT_DEFAULT:
pmonette9fa59e882016-02-10 00:12:19217 return DefaultWebClientState::NOT_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48218 case ShellUtil::IS_DEFAULT:
pmonette9fa59e882016-02-10 00:12:19219 return DefaultWebClientState::IS_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48220 default:
221 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
pmonette9fa59e882016-02-10 00:12:19222 return DefaultWebClientState::UNKNOWN_DEFAULT;
[email protected]eb63da72012-10-15 20:39:48223 }
224}
225
pmonette32a5cfb42016-04-11 22:04:44226// There is no way to make sure the user is done with the system settings, but a
227// signal that the interaction is finished is needed for UMA. A timer of 2
228// minutes is used as a substitute. The registry keys for the protocol
229// association with an app are also monitored to signal the end of the
230// interaction early when it is clear that the user made a choice (e.g. http
231// and https for default browser).
232//
233// This helper class manages both the timer and the registry watchers and makes
234// sure the callback for the end of the settings interaction is only run once.
235// This class also manages its own lifetime.
236class OpenSystemSettingsHelper {
237 public:
238 // Begin the monitoring and will call |on_finished_callback| when done.
239 // Takes in a null-terminated array of |protocols| whose registry keys must be
240 // watched.
241 static void Begin(const wchar_t* const protocols[],
242 const base::Closure& on_finished_callback) {
243 new OpenSystemSettingsHelper(protocols, on_finished_callback);
244 }
245
246 private:
247 // The reason the settings interaction concluded. Do not modify the ordering
248 // because it is used for UMA.
249 enum ConcludeReason { REGISTRY_WATCHER, TIMEOUT, NUM_CONCLUDE_REASON_TYPES };
250
251 OpenSystemSettingsHelper(const wchar_t* const protocols[],
252 const base::Closure& on_finished_callback)
253 : on_finished_callback_(on_finished_callback), weak_ptr_factory_(this) {
254 static const wchar_t kUrlAssociationFormat[] =
255 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
256 L"%ls\\UserChoice";
257
258 // Remember the start time.
259 start_time_ = base::TimeTicks::Now();
260
261 for (const wchar_t* const* scan = &protocols[0]; *scan != nullptr; ++scan) {
262 AddRegistryKeyWatcher(
263 base::StringPrintf(kUrlAssociationFormat, *scan).c_str());
264 }
265 // Only the watchers that were succesfully initialized are counted.
266 registry_watcher_count_ = registry_key_watchers_.size();
267
268 timer_.Start(
269 FROM_HERE, base::TimeDelta::FromMinutes(2),
270 base::Bind(&OpenSystemSettingsHelper::ConcludeInteraction,
271 weak_ptr_factory_.GetWeakPtr(), ConcludeReason::TIMEOUT));
272 }
273
274 // Called when a change is detected on one of the registry keys being watched.
275 // Note: All types of modification to the registry key will trigger this
276 // function even if the value change is the only one that matters. This
277 // is good enough for now.
278 void OnRegistryKeyChanged() {
279 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
280
281 // Make sure all the registry watchers have fired.
282 if (--registry_watcher_count_ == 0) {
283 UMA_HISTOGRAM_MEDIUM_TIMES(
284 "DefaultBrowser.SettingsInteraction.RegistryWatcherDuration",
285 base::TimeTicks::Now() - start_time_);
286
287 ConcludeInteraction(ConcludeReason::REGISTRY_WATCHER);
288 }
289 }
290
291 // Ends the monitoring with the system settings. Will call
292 // |on_finished_callback_| and then dispose of this class instance to make
293 // sure the callback won't get called subsequently.
294 void ConcludeInteraction(ConcludeReason conclude_reason) {
295 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
296
297 UMA_HISTOGRAM_ENUMERATION(
298 "DefaultBrowser.SettingsInteraction.ConcludeReason", conclude_reason,
299 NUM_CONCLUDE_REASON_TYPES);
300 on_finished_callback_.Run();
301 delete this;
302 }
303
304 // Helper function to create a registry watcher for a given |key_path|. Do
305 // nothing on initialization failure.
306 void AddRegistryKeyWatcher(const wchar_t* key_path) {
307 auto reg_key = make_scoped_ptr(
308 new base::win::RegKey(HKEY_CURRENT_USER, key_path, KEY_NOTIFY));
309
310 if (reg_key->Valid() &&
311 reg_key->StartWatching(
312 base::Bind(&OpenSystemSettingsHelper::OnRegistryKeyChanged,
313 weak_ptr_factory_.GetWeakPtr()))) {
314 registry_key_watchers_.push_back(std::move(reg_key));
315 }
316 }
317
318 // The function to call when the interaction with the system settings is
319 // finished.
320 base::Closure on_finished_callback_;
321
322 // The number of time the registry key watchers must fire.
323 int registry_watcher_count_ = 0;
324
325 // There can be multiple registry key watchers as some settings modify
326 // multiple protocol associations. e.g. Changing the default browser modifies
327 // the http and https associations.
328 std::vector<scoped_ptr<base::win::RegKey>> registry_key_watchers_;
329
330 base::OneShotTimer timer_;
331
332 // Records the time it takes for the final registry watcher to get signaled.
333 base::TimeTicks start_time_;
334
335 // Weak ptrs are used to bind this class to the callbacks of the timer and the
336 // registry watcher. This makes it possible to self-delete after one of the
337 // callbacks is executed to cancel the remaining ones.
338 base::WeakPtrFactory<OpenSystemSettingsHelper> weak_ptr_factory_;
339
340 DISALLOW_COPY_AND_ASSIGN(OpenSystemSettingsHelper);
341};
342
[email protected]3a3e72c2011-11-29 02:59:38343} // namespace
[email protected]12f520c2010-01-06 18:11:15344
pmonette9fa59e882016-02-10 00:12:19345bool SetAsDefaultBrowser() {
[email protected]650b2d52013-02-10 03:41:45346 base::FilePath chrome_exe;
[email protected]d24c4012009-07-28 01:57:31347 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
348 LOG(ERROR) << "Error getting app exe path";
349 return false;
350 }
351
352 // From UI currently we only allow setting default browser for current user.
[email protected]bf6117c7e2010-12-01 06:00:25353 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
grte76ca2852014-12-05 16:42:10354 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe,
355 true /* elevate_if_not_admin */)) {
[email protected]d24c4012009-07-28 01:57:31356 LOG(ERROR) << "Chrome could not be set as default browser.";
357 return false;
358 }
359
[email protected]8e96e502010-10-21 20:57:12360 VLOG(1) << "Chrome registered as default browser.";
[email protected]d24c4012009-07-28 01:57:31361 return true;
362}
363
pmonette9fa59e882016-02-10 00:12:19364bool SetAsDefaultProtocolClient(const std::string& protocol) {
[email protected]4468a5b2011-05-26 07:48:02365 if (protocol.empty())
366 return false;
367
[email protected]650b2d52013-02-10 03:41:45368 base::FilePath chrome_exe;
[email protected]4468a5b2011-05-26 07:48:02369 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
370 LOG(ERROR) << "Error getting app exe path";
371 return false;
372 }
373
[email protected]f911df52013-12-24 23:24:23374 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
[email protected]4468a5b2011-05-26 07:48:02375 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
grte76ca2852014-12-05 16:42:10376 if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe,
377 wprotocol)) {
[email protected]4468a5b2011-05-26 07:48:02378 LOG(ERROR) << "Chrome could not be set as default handler for "
379 << protocol << ".";
380 return false;
381 }
382
383 VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
384 return true;
385}
386
pmonette32a5cfb42016-04-11 22:04:44387DefaultWebClientSetPermission GetDefaultWebClientSetPermission() {
pmonette034a03d92015-10-02 21:04:27388 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
389 if (distribution->GetDefaultBrowserControlPolicy() !=
390 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
391 return SET_DEFAULT_NOT_ALLOWED;
pmonette034a03d92015-10-02 21:04:27392 if (ShellUtil::CanMakeChromeDefaultUnattended())
393 return SET_DEFAULT_UNATTENDED;
pmonette32a5cfb42016-04-11 22:04:44394 // Windows 8 and 10 both introduced a new way to set the default web client
395 // which require user interaction.
pmonettef89ac7c72015-10-06 03:22:01396 return SET_DEFAULT_INTERACTIVE;
pmonette034a03d92015-10-02 21:04:27397}
398
pmonette9fa59e882016-02-10 00:12:19399bool IsElevationNeededForSettingDefaultProtocolClient() {
pmonette868ca642015-09-02 14:34:02400 return base::win::GetVersion() < base::win::VERSION_WIN8;
401}
402
pmonette9fa59e882016-02-10 00:12:19403base::string16 GetApplicationNameForProtocol(const GURL& url) {
[email protected]caa05352014-03-01 00:43:05404 // Windows 8 or above requires a new protocol association query.
405 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
406 return GetAppForProtocolUsingAssocQuery(url);
407 else
408 return GetAppForProtocolUsingRegistry(url);
[email protected]42dc9402013-01-30 07:54:20409}
410
pmonette9fa59e882016-02-10 00:12:19411DefaultWebClientState GetDefaultBrowser() {
pmonette034a03d92015-10-02 21:04:27412 return GetDefaultWebClientStateFromShellUtilDefaultState(
413 ShellUtil::GetChromeDefaultState());
414}
415
[email protected]d24c4012009-07-28 01:57:31416// There is no reliable way to say which browser is default on a machine (each
417// browser can have some of the protocols/shortcuts). So we look for only HTTP
418// protocol handler. Even this handler is located at different places in
419// registry on XP and Vista:
420// - HKCR\http\shell\open\command (XP)
421// - HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\
422// http\UserChoice (Vista)
423// This method checks if Firefox is defualt browser by checking these
424// locations and returns true if Firefox traces are found there. In case of
425// error (or if Firefox is not found)it returns the default value which
426// is false.
pmonette9fa59e882016-02-10 00:12:19427bool IsFirefoxDefaultBrowser() {
[email protected]d24c4012009-07-28 01:57:31428 bool ff_default = false;
[email protected]935aa542010-10-15 01:59:15429 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
[email protected]0085863a2013-12-06 21:19:03430 base::string16 app_cmd;
[email protected]2d6503982010-10-17 04:41:54431 base::win::RegKey key(HKEY_CURRENT_USER,
432 ShellUtil::kRegVistaUrlPrefs, KEY_READ);
[email protected]e06f4d52011-01-19 07:28:46433 if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
[email protected]d24c4012009-07-28 01:57:31434 app_cmd == L"FirefoxURL")
435 ff_default = true;
436 } else {
[email protected]0085863a2013-12-06 21:19:03437 base::string16 key_path(L"http");
[email protected]d24c4012009-07-28 01:57:31438 key_path.append(ShellUtil::kRegShellOpen);
[email protected]2d6503982010-10-17 04:41:54439 base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
[email protected]0085863a2013-12-06 21:19:03440 base::string16 app_cmd;
[email protected]e06f4d52011-01-19 07:28:46441 if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
[email protected]cb1f4ac2014-08-07 16:55:42442 base::string16::npos !=
brettwfce8d192015-08-10 19:07:51443 base::ToLowerASCII(app_cmd).find(L"firefox"))
[email protected]d24c4012009-07-28 01:57:31444 ff_default = true;
445 }
446 return ff_default;
447}
[email protected]12f520c2010-01-06 18:11:15448
pmonette9fa59e882016-02-10 00:12:19449DefaultWebClientState IsDefaultProtocolClient(const std::string& protocol) {
pmonette034a03d92015-10-02 21:04:27450 return GetDefaultWebClientStateFromShellUtilDefaultState(
451 ShellUtil::GetChromeDefaultProtocolClientState(
452 base::UTF8ToUTF16(protocol)));
453}
454
pmonette9fa59e882016-02-10 00:12:19455base::string16 GetAppModelIdForProfile(const base::string16& app_name,
456 const base::FilePath& profile_path) {
[email protected]d2065e062013-12-12 23:49:52457 std::vector<base::string16> components;
[email protected]a0448002012-06-19 04:32:10458 components.push_back(app_name);
[email protected]0085863a2013-12-06 21:19:03459 const base::string16 profile_id(GetProfileIdFromPath(profile_path));
[email protected]a0448002012-06-19 04:32:10460 if (!profile_id.empty())
461 components.push_back(profile_id);
462 return ShellUtil::BuildAppModelId(components);
[email protected]12f520c2010-01-06 18:11:15463}
464
pmonette9fa59e882016-02-10 00:12:19465base::string16 GetChromiumModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45466 const base::FilePath& profile_path) {
[email protected]a0448002012-06-19 04:32:10467 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]650b2d52013-02-10 03:41:45468 base::FilePath chrome_exe;
[email protected]a0448002012-06-19 04:32:10469 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
470 NOTREACHED();
471 return dist->GetBaseAppId();
472 }
473 return GetAppModelIdForProfile(
grte76ca2852014-12-05 16:42:10474 ShellUtil::GetBrowserModelId(dist,
475 InstallUtil::IsPerUserInstall(chrome_exe)),
[email protected]786799692012-09-26 14:16:48476 profile_path);
[email protected]12f520c2010-01-06 18:11:15477}
[email protected]c9bb06f42010-01-13 23:53:48478
pmonette9fa59e882016-02-10 00:12:19479base::string16 GetAppListAppModelIdForProfile(
[email protected]650b2d52013-02-10 03:41:45480 const base::FilePath& profile_path) {
pmonettef89ac7c72015-10-06 03:22:01481 return GetAppModelIdForProfile(GetAppListAppName(), profile_path);
[email protected]99002fd2012-11-06 04:35:52482}
483
pmonette9fa59e882016-02-10 00:12:19484void MigrateTaskbarPins() {
[email protected]935aa542010-10-15 01:59:15485 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]c9bb06f42010-01-13 23:53:48486 return;
487
[email protected]81fcf952012-11-26 22:25:14488 // This needs to happen eventually (e.g. so that the appid is fixed and the
489 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
490 // urgent and shouldn't delay Chrome startup.
gab88257b62016-01-15 03:19:19491 static const int64_t kMigrateTaskbarPinsDelaySeconds = 15;
[email protected]81fcf952012-11-26 22:25:14492 BrowserThread::PostDelayedTask(
[email protected]3a3e72c2011-11-29 02:59:38493 BrowserThread::FILE, FROM_HERE,
gab88257b62016-01-15 03:19:19494 base::Bind(&MigrateTaskbarPinsCallback),
495 base::TimeDelta::FromSeconds(kMigrateTaskbarPinsDelaySeconds));
[email protected]c9bb06f42010-01-13 23:53:48496}
[email protected]43903b82012-06-01 05:26:23497
pmonette9fa59e882016-02-10 00:12:19498int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe,
499 const base::FilePath& path) {
[email protected]8ea8f1ef2013-01-06 18:39:03500 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
501
502 // Enumerate all pinned shortcuts in the given path directly.
[email protected]25a4c1c2013-06-08 04:53:36503 base::FileEnumerator shortcuts_enum(
[email protected]8ea8f1ef2013-01-06 18:39:03504 path, false, // not recursive
[email protected]25a4c1c2013-06-08 04:53:36505 base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
[email protected]8ea8f1ef2013-01-06 18:39:03506
grte76ca2852014-12-05 16:42:10507 bool is_per_user_install = InstallUtil::IsPerUserInstall(chrome_exe);
[email protected]8ea8f1ef2013-01-06 18:39:03508
509 int shortcuts_migrated = 0;
[email protected]650b2d52013-02-10 03:41:45510 base::FilePath target_path;
[email protected]0085863a2013-12-06 21:19:03511 base::string16 arguments;
[email protected]07983302013-01-21 19:41:44512 base::win::ScopedPropVariant propvariant;
[email protected]650b2d52013-02-10 03:41:45513 for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
[email protected]8ea8f1ef2013-01-06 18:39:03514 shortcut = shortcuts_enum.Next()) {
515 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
516 // it is fixed to work with FilePaths with spaces.
517 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
518 chrome_exe != target_path) {
519 continue;
520 }
avi556c05022014-12-22 23:31:43521 base::CommandLine command_line(
522 base::CommandLine::FromString(base::StringPrintf(
523 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
[email protected]8ea8f1ef2013-01-06 18:39:03524
525 // Get the expected AppId for this Chrome shortcut.
[email protected]0085863a2013-12-06 21:19:03526 base::string16 expected_app_id(
[email protected]8ea8f1ef2013-01-06 18:39:03527 GetExpectedAppId(command_line, is_per_user_install));
528 if (expected_app_id.empty())
529 continue;
530
531 // Load the shortcut.
532 base::win::ScopedComPtr<IShellLink> shell_link;
533 base::win::ScopedComPtr<IPersistFile> persist_file;
534 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
535 CLSCTX_INPROC_SERVER)) ||
dcheng81762e02014-11-21 21:22:28536 FAILED(persist_file.QueryFrom(shell_link.get())) ||
[email protected]8ea8f1ef2013-01-06 18:39:03537 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
538 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
539 continue;
540 }
541
542 // Any properties that need to be updated on the shortcut will be stored in
543 // |updated_properties|.
544 base::win::ShortcutProperties updated_properties;
545
546 // Validate the existing app id for the shortcut.
547 base::win::ScopedComPtr<IPropertyStore> property_store;
[email protected]07983302013-01-21 19:41:44548 propvariant.Reset();
dcheng81762e02014-11-21 21:22:28549 if (FAILED(property_store.QueryFrom(shell_link.get())) ||
550 property_store->GetValue(PKEY_AppUserModel_ID, propvariant.Receive()) !=
551 S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03552 // When in doubt, prefer not updating the shortcut.
553 NOTREACHED();
554 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03555 } else {
[email protected]07983302013-01-21 19:41:44556 switch (propvariant.get().vt) {
557 case VT_EMPTY:
558 // If there is no app_id set, set our app_id if one is expected.
559 if (!expected_app_id.empty())
560 updated_properties.set_app_id(expected_app_id);
561 break;
562 case VT_LPWSTR:
[email protected]0085863a2013-12-06 21:19:03563 if (expected_app_id != base::string16(propvariant.get().pwszVal))
[email protected]07983302013-01-21 19:41:44564 updated_properties.set_app_id(expected_app_id);
565 break;
566 default:
567 NOTREACHED();
568 continue;
[email protected]8ea8f1ef2013-01-06 18:39:03569 }
570 }
571
gab88257b62016-01-15 03:19:19572 // Clear dual_mode property from any shortcuts that previously had it (it
573 // was only ever installed on shortcuts with the
574 // |default_chromium_model_id|).
[email protected]b39e32e2013-04-24 08:55:54575 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]0085863a2013-12-06 21:19:03576 base::string16 default_chromium_model_id(
[email protected]b39e32e2013-04-24 08:55:54577 ShellUtil::GetBrowserModelId(dist, is_per_user_install));
gab88257b62016-01-15 03:19:19578 if (expected_app_id == default_chromium_model_id) {
[email protected]07983302013-01-21 19:41:44579 propvariant.Reset();
[email protected]8ea8f1ef2013-01-06 18:39:03580 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
[email protected]07983302013-01-21 19:41:44581 propvariant.Receive()) != S_OK) {
[email protected]8ea8f1ef2013-01-06 18:39:03582 // When in doubt, prefer to not update the shortcut.
583 NOTREACHED();
584 continue;
gab38d242d4d2016-01-14 20:38:12585 }
586 if (propvariant.get().vt == VT_BOOL &&
587 !!propvariant.get().boolVal) {
588 updated_properties.set_dual_mode(false);
[email protected]8ea8f1ef2013-01-06 18:39:03589 }
590 }
591
592 persist_file.Release();
593 shell_link.Release();
594
595 // Update the shortcut if some of its properties need to be updated.
596 if (updated_properties.options &&
597 base::win::CreateOrUpdateShortcutLink(
598 shortcut, updated_properties,
599 base::win::SHORTCUT_UPDATE_EXISTING)) {
600 ++shortcuts_migrated;
601 }
602 }
603 return shortcuts_migrated;
604}
605
pmonette9fa59e882016-02-10 00:12:19606base::FilePath GetStartMenuShortcut(const base::FilePath& chrome_exe) {
[email protected]3f69d6e612012-08-03 18:52:27607 static const int kFolderIds[] = {
608 base::DIR_COMMON_START_MENU,
609 base::DIR_START_MENU,
610 };
611 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
grt2be0cd082015-11-12 15:32:21612 const base::string16 shortcut_name(
613 dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME) +
614 installer::kLnkExt);
615 base::FilePath programs_folder;
[email protected]650b2d52013-02-10 03:41:45616 base::FilePath shortcut;
[email protected]3f69d6e612012-08-03 18:52:27617
618 // Check both the common and the per-user Start Menu folders for system-level
619 // installs.
grte76ca2852014-12-05 16:42:10620 size_t folder = InstallUtil::IsPerUserInstall(chrome_exe) ? 1 : 0;
[email protected]3f69d6e612012-08-03 18:52:27621 for (; folder < arraysize(kFolderIds); ++folder) {
grt2be0cd082015-11-12 15:32:21622 if (!PathService::Get(kFolderIds[folder], &programs_folder)) {
[email protected]3f69d6e612012-08-03 18:52:27623 NOTREACHED();
624 continue;
625 }
626
grt2be0cd082015-11-12 15:32:21627 shortcut = programs_folder.Append(shortcut_name);
628 if (base::PathExists(shortcut))
629 return shortcut;
[email protected]3f69d6e612012-08-03 18:52:27630 }
631
[email protected]650b2d52013-02-10 03:41:45632 return base::FilePath();
[email protected]3f69d6e612012-08-03 18:52:27633}
pmonettef89ac7c72015-10-06 03:22:01634
pmonette32a5cfb42016-04-11 22:04:44635namespace win {
636
637bool SetAsDefaultBrowserUsingIntentPicker() {
638 base::FilePath chrome_exe;
639 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
640 NOTREACHED() << "Error getting app exe path";
641 return false;
642 }
643
644 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
645 if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe)) {
646 LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
647 return false;
648 }
649
650 VLOG(1) << "Set-default-browser Windows UI completed.";
651 return true;
652}
653
654void SetAsDefaultBrowserUsingSystemSettings(
655 const base::Closure& on_finished_callback) {
656 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
657
658 base::FilePath chrome_exe;
659 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
660 NOTREACHED() << "Error getting app exe path";
661 on_finished_callback.Run();
662 return;
663 }
664
665 // The helper manages its own lifetime.
666 static const wchar_t* const kProtocols[] = {L"http", L"https", nullptr};
667 OpenSystemSettingsHelper::Begin(kProtocols, on_finished_callback);
668
669 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
670 ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe);
671}
672
673bool SetAsDefaultProtocolClientUsingIntentPicker(const std::string& protocol) {
674 base::FilePath chrome_exe;
675 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
676 NOTREACHED() << "Error getting app exe path";
677 return false;
678 }
679
680 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
681 base::string16 wprotocol(base::UTF8ToUTF16(protocol));
682 if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(dist, chrome_exe,
683 wprotocol)) {
684 LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
685 return false;
686 }
687
688 VLOG(1) << "Set-default-client Windows UI completed.";
689 return true;
690}
691
692} // namespace win
693
pmonette9fa59e882016-02-10 00:12:19694} // namespace shell_integration