Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/browser/shell_integration_win.cc

Issue 1349163008: Setting chrome as the default browser is now fixed on Windows 10 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test for default browser callback + comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/shell_integration.h" 5 #include "chrome/browser/shell_integration.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <shlwapi.h> 8 #include <shlwapi.h>
9 #include <shobjidl.h> 9 #include <shobjidl.h>
10 #include <propkey.h> // Needs to come after shobjidl.h. 10 #include <propkey.h> // Needs to come after shobjidl.h.
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/files/file_enumerator.h" 14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/metrics/histogram_macros.h"
17 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/process/launch.h"
18 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
22 #include "base/win/registry.h" 25 #include "base/win/registry.h"
23 #include "base/win/scoped_comptr.h" 26 #include "base/win/scoped_comptr.h"
24 #include "base/win/scoped_propvariant.h" 27 #include "base/win/scoped_propvariant.h"
25 #include "base/win/shortcut.h" 28 #include "base/win/shortcut.h"
26 #include "base/win/windows_version.h" 29 #include "base/win/windows_version.h"
27 #include "chrome/browser/policy/policy_path_parser.h" 30 #include "chrome/browser/policy/policy_path_parser.h"
31 #include "chrome/browser/ui/startup/startup_browser_creator.h"
28 #include "chrome/browser/web_applications/web_app.h" 32 #include "chrome/browser/web_applications/web_app.h"
29 #include "chrome/common/chrome_constants.h" 33 #include "chrome/common/chrome_constants.h"
30 #include "chrome/common/chrome_paths_internal.h" 34 #include "chrome/common/chrome_paths_internal.h"
31 #include "chrome/common/chrome_switches.h" 35 #include "chrome/common/chrome_switches.h"
32 #include "chrome/installer/setup/setup_util.h" 36 #include "chrome/installer/setup/setup_util.h"
33 #include "chrome/installer/util/browser_distribution.h" 37 #include "chrome/installer/util/browser_distribution.h"
34 #include "chrome/installer/util/create_reg_key_work_item.h" 38 #include "chrome/installer/util/create_reg_key_work_item.h"
35 #include "chrome/installer/util/install_util.h" 39 #include "chrome/installer/util/install_util.h"
36 #include "chrome/installer/util/set_reg_value_work_item.h" 40 #include "chrome/installer/util/set_reg_value_work_item.h"
37 #include "chrome/installer/util/shell_util.h" 41 #include "chrome/installer/util/shell_util.h"
38 #include "chrome/installer/util/util_constants.h" 42 #include "chrome/installer/util/util_constants.h"
39 #include "chrome/installer/util/work_item.h" 43 #include "chrome/installer/util/work_item.h"
40 #include "chrome/installer/util/work_item_list.h" 44 #include "chrome/installer/util/work_item_list.h"
45 #include "components/variations/variations_associated_data.h"
41 #include "content/public/browser/browser_thread.h" 46 #include "content/public/browser/browser_thread.h"
42 47
43 using content::BrowserThread; 48 using content::BrowserThread;
44 49
45 namespace { 50 namespace {
46 51
47 const wchar_t kAppListAppNameSuffix[] = L"AppList"; 52 const wchar_t kAppListAppNameSuffix[] = L"AppList";
48 53
54 const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault";
55 const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default";
56 const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default";
57
49 // Helper function for ShellIntegration::GetAppId to generates profile id 58 // Helper function for ShellIntegration::GetAppId to generates profile id
50 // from profile path. "profile_id" is composed of sanitized basenames of 59 // from profile path. "profile_id" is composed of sanitized basenames of
51 // user data dir and profile dir joined by a ".". 60 // user data dir and profile dir joined by a ".".
52 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { 61 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
53 // Return empty string if profile_path is empty 62 // Return empty string if profile_path is empty
54 if (profile_path.empty()) 63 if (profile_path.empty())
55 return base::string16(); 64 return base::string16();
56 65
57 base::FilePath default_user_data_dir; 66 base::FilePath default_user_data_dir;
58 // Return empty string if profile_path is in default user data 67 // Return empty string if profile_path is in default user data
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 case ShellUtil::NOT_DEFAULT: 242 case ShellUtil::NOT_DEFAULT:
234 return ShellIntegration::NOT_DEFAULT; 243 return ShellIntegration::NOT_DEFAULT;
235 case ShellUtil::IS_DEFAULT: 244 case ShellUtil::IS_DEFAULT:
236 return ShellIntegration::IS_DEFAULT; 245 return ShellIntegration::IS_DEFAULT;
237 default: 246 default:
238 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); 247 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
239 return ShellIntegration::UNKNOWN_DEFAULT; 248 return ShellIntegration::UNKNOWN_DEFAULT;
240 } 249 }
241 } 250 }
242 251
252 // Resets the default browser choice for the current user.
253 void ResetDefaultBrowser() {
254 static const wchar_t* const kUrlAssociationKeyFormats[] = {
255 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\"
256 L"%ls\\UserChoice",
Peter Kasting 2015/09/25 20:52:27 Nit: Can we replace the "ls" params in these with
Patrick Monette 2015/09/28 23:46:37 No, the types must match in StringPrintf
Peter Kasting 2015/09/28 23:57:49 I'm confused. I am suggesting that the types matc
Patrick Monette 2015/09/29 14:44:18 Yes. Apparently it is because the %s specifier is
257 L"SOFTWARE\\Microsoft\\Windows\\Roaming\\OpenWith\\UrlAssociations\\"
258 L"%ls\\UserChoice"};
259 static const wchar_t* const kProtocols[] = {L"http", L"https"};
260
261 for (const wchar_t* format : kUrlAssociationKeyFormats) {
262 for (const wchar_t* protocol : kProtocols) {
263 base::win::RegKey registry_key(
264 HKEY_CURRENT_USER, base::StringPrintf(format, protocol).c_str(),
265 KEY_SET_VALUE);
266 registry_key.DeleteValue(L"Hash");
267 }
268 }
269 }
270
271 // Returns true if the AsyncSetAsDefault field trial is activated.
272 bool IsAsyncSetAsDefaultEnabled() {
273 using base::CommandLine;
274
275 const std::string group_name =
Peter Kasting 2015/09/25 20:52:27 Nit: Init this just above its first use. At that
Patrick Monette 2015/09/28 23:46:38 There is a comment about that in the finch101 guid
276 base::FieldTrialList::FindFullName("AsyncSetAsDefault");
277 if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableAsyncSetAsDefault))
278 return false;
279 if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableAsyncSetAsDefault))
280 return true;
281
282 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE);
283 }
284
243 } // namespace 285 } // namespace
244 286
287 // static
288 bool ShellIntegration::IsSetAsDefaultAsynchronous() {
289 return base::win::GetVersion() >= base::win::VERSION_WIN10 &&
290 IsAsyncSetAsDefaultEnabled();
291 }
292
293 void ShellIntegration::SetAsDefaultBrowserAsynchronous() {
294 DCHECK(ShellIntegration::IsSetAsDefaultAsynchronous());
295
296 ResetDefaultBrowser();
297
298 base::CommandLine cmdline(base::FilePath(L"openwith.exe"));
299 cmdline.AppendArgNative(StartupBrowserCreator::GetDefaultBrowserUrl());
300 base::Process process(base::LaunchProcess(cmdline, base::LaunchOptions()));
301 UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.AsyncSetAsDefault.Launched",
302 process.IsValid());
303 }
304
245 ShellIntegration::DefaultWebClientSetPermission 305 ShellIntegration::DefaultWebClientSetPermission
246 ShellIntegration::CanSetAsDefaultBrowser() { 306 ShellIntegration::CanSetAsDefaultBrowser() {
247 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 307 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
248 if (distribution->GetDefaultBrowserControlPolicy() != 308 if (distribution->GetDefaultBrowserControlPolicy() !=
249 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) 309 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
250 return SET_DEFAULT_NOT_ALLOWED; 310 return SET_DEFAULT_NOT_ALLOWED;
251 311
252 if (ShellUtil::CanMakeChromeDefaultUnattended()) 312 if (ShellUtil::CanMakeChromeDefaultUnattended())
253 return SET_DEFAULT_UNATTENDED; 313 return SET_DEFAULT_UNATTENDED;
254 else 314 if (IsSetAsDefaultAsynchronous())
Peter Kasting 2015/09/25 20:52:27 Nit: Blank line above this, or else remove all the
Patrick Monette 2015/09/28 23:46:37 Done.
255 return SET_DEFAULT_INTERACTIVE; 315 return SET_DEFAULT_ASYNCHRONOUS;
316
317 return SET_DEFAULT_INTERACTIVE;
256 } 318 }
257 319
258 bool ShellIntegration::SetAsDefaultBrowser() { 320 bool ShellIntegration::SetAsDefaultBrowser() {
259 base::FilePath chrome_exe; 321 base::FilePath chrome_exe;
260 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 322 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
261 LOG(ERROR) << "Error getting app exe path"; 323 LOG(ERROR) << "Error getting app exe path";
262 return false; 324 return false;
263 } 325 }
264 326
265 // From UI currently we only allow setting default browser for current user. 327 // From UI currently we only allow setting default browser for current user.
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 643 }
582 644
583 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + 645 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
584 installer::kLnkExt); 646 installer::kLnkExt);
585 if (base::PathExists(shortcut)) 647 if (base::PathExists(shortcut))
586 return shortcut; 648 return shortcut;
587 } 649 }
588 650
589 return base::FilePath(); 651 return base::FilePath();
590 } 652 }
653
654 void ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() {
655 DCHECK_CURRENTLY_ON(BrowserThread::UI);
656
657 if (IsSetAsDefaultAsynchronous()) {
Peter Kasting 2015/09/25 20:52:27 Nit: Reverse this + early return to avoid indentin
Patrick Monette 2015/09/28 23:46:37 Done.
658 // On windows 10+, there is no official way to prompt the user to set a
Peter Kasting 2015/09/25 20:52:27 Nit: Capitalize Windows
Patrick Monette 2015/09/28 23:46:37 Done.
659 // default browser. This is the workaround:
660 // 1. Unregister the default browser.
661 // 2. Open "How to make Chrome my default browser" link with openwith.exe.
662 // 3. Windows prompt the user with "How would you link to open this?".
Peter Kasting 2015/09/25 20:52:27 Nit: prompt -> will prompt
Patrick Monette 2015/09/28 23:46:38 Done.
663 // 4. If Chrome is selected, the http url is intercepted and
664 // CompleteSetAsDefault is called with succeeded equals to true.
Peter Kasting 2015/09/25 20:52:27 Nit: Avoid passive voice, plus grammar issues. Ho
Patrick Monette 2015/09/28 23:46:38 Done.
665 // 5. If Chrome is not selected, the url is opened in the selected browser.
666 // After a certain amount of time, we notify the observer that the
667 // process failed.
668
669 // Remember the start time.
670 start_time_ = base::TimeTicks::Now();
671
672 if (!StartupBrowserCreator::SetDefaultBrowserCallback(base::Bind(
673 &DefaultBrowserWorker::CompleteSetAsDefault, this, true))) {
674 // Another worker is currently processing. Complete here and leave the
675 // other worker alone.
676 CompleteSetAsDefault(false);
grt (UTC plus 2) 2015/09/28 14:31:04 i don't think this is right. when InitializeSetAsD
Patrick Monette 2015/09/28 23:46:38 I could set add if (set_as_default_in_progress_) b
677 return;
678 }
679
680 // Start the timer.
681 if (!async_timer_)
682 async_timer_.reset(new base::OneShotTimer());
Peter Kasting 2015/09/25 20:52:27 Nit: Because this doesn't actually start the timer
Patrick Monette 2015/09/28 23:46:38 Done.
683
684 base::TimeDelta timer_duration = base::TimeDelta::FromMinutes(2);
Peter Kasting 2015/09/25 20:52:27 Nit: If you move this down lower you can init as:
Patrick Monette 2015/09/28 23:46:37 Done.
685 std::string value = variations::GetVariationParamValue(
686 kAsyncSetAsDefaultExperimentName, "TimerDuration");
687 int seconds = 0;
688 if (!value.empty())
689 base::StringToInt(value, &seconds);
690 if (seconds)
691 timer_duration = base::TimeDelta::FromSeconds(seconds);
692
693 async_timer_->Start(
694 FROM_HERE, timer_duration,
695 base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, false));
696 }
697 }
698
699 void ShellIntegration::DefaultBrowserWorker::FinalizeSetAsDefault(
700 bool succeeded) {
701 DCHECK_CURRENTLY_ON(BrowserThread::UI);
702
703 if (async_timer_) {
704 // Destroy timer.
Peter Kasting 2015/09/25 20:52:27 Nit: This comment and the next one both simply res
Patrick Monette 2015/09/28 23:46:38 Done.
705 async_timer_.reset();
706
707 StartupBrowserCreator::ClearDefaultBrowserCallback();
708
709 // Record the duration from starting to set as default browser to now.
710 UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.AsyncSetAsDefault.Succeeded",
711 succeeded);
712 UMA_HISTOGRAM_TIMES("DefaultBrowser.AsyncSetAsDefault.Duration",
713 base::TimeTicks::Now() - start_time_);
714 }
715 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698