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

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: grt comments #1 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",
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 =
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_GE(base::win::GetVersion(), base::win::VERSION_WIN10);
295
296 ResetDefaultBrowser();
297
298 base::CommandLine cmdline(base::FilePath(L"openwith.exe"));
299 cmdline.AppendArgNative(StartupBrowserCreator::GetAsyncSetAsDefaultUrl());
300 base::LaunchProcess(cmdline, base::LaunchOptions());
301 }
grt (UTC plus 2) 2015/09/24 18:24:12 suggestion: it would be good to know if this fails
Patrick Monette 2015/09/25 20:06:25 Done.
302
245 ShellIntegration::DefaultWebClientSetPermission 303 ShellIntegration::DefaultWebClientSetPermission
246 ShellIntegration::CanSetAsDefaultBrowser() { 304 ShellIntegration::CanSetAsDefaultBrowser() {
247 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 305 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
248 if (distribution->GetDefaultBrowserControlPolicy() != 306 if (distribution->GetDefaultBrowserControlPolicy() !=
249 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) 307 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
250 return SET_DEFAULT_NOT_ALLOWED; 308 return SET_DEFAULT_NOT_ALLOWED;
251 309
252 if (ShellUtil::CanMakeChromeDefaultUnattended()) 310 if (ShellUtil::CanMakeChromeDefaultUnattended())
253 return SET_DEFAULT_UNATTENDED; 311 return SET_DEFAULT_UNATTENDED;
312 else if (IsSetAsDefaultAsynchronous())
grt (UTC plus 2) 2015/09/24 18:24:11 nit: remove these "else"es: if (foo) return
Patrick Monette 2015/09/25 20:06:25 Done.
313 return SET_DEFAULT_ASYNCHRONOUS;
254 else 314 else
255 return SET_DEFAULT_INTERACTIVE; 315 return SET_DEFAULT_INTERACTIVE;
256 } 316 }
257 317
258 bool ShellIntegration::SetAsDefaultBrowser() { 318 bool ShellIntegration::SetAsDefaultBrowser() {
259 base::FilePath chrome_exe; 319 base::FilePath chrome_exe;
260 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 320 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
261 LOG(ERROR) << "Error getting app exe path"; 321 LOG(ERROR) << "Error getting app exe path";
262 return false; 322 return false;
263 } 323 }
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 641 }
582 642
583 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + 643 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
584 installer::kLnkExt); 644 installer::kLnkExt);
585 if (base::PathExists(shortcut)) 645 if (base::PathExists(shortcut))
586 return shortcut; 646 return shortcut;
587 } 647 }
588 648
589 return base::FilePath(); 649 return base::FilePath();
590 } 650 }
651
652 void ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() {
653 DCHECK_CURRENTLY_ON(BrowserThread::UI);
654
655 if (IsSetAsDefaultAsynchronous()) {
656 // On windows 10+, there is no official way to prompt the user to set a
657 // default browser. This is the workaround:
658 // 1. Unregister the default browser.
659 // 2. Open "How to make Chrome my default browser" link with openwith.exe.
660 // 3. Windows prompt the user with "How would you link to open this?".
661 // 4. If Chrome is selected, the http url is intercepted and
662 // CompleteSetAsDefault is called with succeeded equals to true.
663 // 5. If Chrome is not selected, the url is opened in the selected browser.
664 // After a certain amount of time, we notify the observer that the
665 // process failed.
666 StartupBrowserCreator::SetAsyncSetAsDefaultFilter(
667 base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, true));
668
669 // Remember the start time.
670 start_time_ = base::TimeTicks::Now();
671
672 // Start the timer.
673 if (!async_timer_)
674 async_timer_.reset(new base::OneShotTimer<DefaultBrowserWorker>());
675
676 base::TimeDelta timer_duration = base::TimeDelta::FromMinutes(2);
677 std::string value = variations::GetVariationParamValue(
678 kAsyncSetAsDefaultExperimentName, "TimerDuration");
679 int seconds = 0;
680 if (!value.empty())
681 base::StringToInt(value, &seconds);
682 if (seconds)
683 timer_duration = base::TimeDelta::FromSeconds(seconds);
684
685 async_timer_->Start(
686 FROM_HERE, timer_duration,
687 base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, false));
688 }
689 }
690
691 void ShellIntegration::DefaultBrowserWorker::FinalizeSetAsDefault(
692 bool succeeded) {
693 DCHECK_CURRENTLY_ON(BrowserThread::UI);
694
695 if (IsSetAsDefaultAsynchronous()) {
grt (UTC plus 2) 2015/09/24 18:24:12 how about: if (async_timer_) { so that there a
Patrick Monette 2015/09/25 20:06:24 Done.
696 // Destroy timer.
697 async_timer_.reset();
698
699 StartupBrowserCreator::ClearAsyncSetAsDefaultFilter();
700
701 // Record the duration from starting to set as default browser to now.
702 UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.AsyncSetAsDefault.Succeeded",
703 succeeded);
704 UMA_HISTOGRAM_TIMES("DefaultBrowser.AsyncSetAsDefault.Duration",
705 base::TimeTicks::Now() - start_time_);
706 }
707 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698