Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| 25 #include "base/timer/timer.h" | |
| 22 #include "base/win/registry.h" | 26 #include "base/win/registry.h" |
| 23 #include "base/win/scoped_comptr.h" | 27 #include "base/win/scoped_comptr.h" |
| 24 #include "base/win/scoped_propvariant.h" | 28 #include "base/win/scoped_propvariant.h" |
| 25 #include "base/win/shortcut.h" | 29 #include "base/win/shortcut.h" |
| 26 #include "base/win/windows_version.h" | 30 #include "base/win/windows_version.h" |
| 27 #include "chrome/browser/policy/policy_path_parser.h" | 31 #include "chrome/browser/policy/policy_path_parser.h" |
| 32 #include "chrome/browser/ui/startup/startup_browser_creator.h" | |
| 28 #include "chrome/browser/web_applications/web_app.h" | 33 #include "chrome/browser/web_applications/web_app.h" |
| 29 #include "chrome/common/chrome_constants.h" | 34 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_paths_internal.h" | 35 #include "chrome/common/chrome_paths_internal.h" |
| 31 #include "chrome/common/chrome_switches.h" | 36 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/installer/setup/setup_util.h" | 37 #include "chrome/installer/setup/setup_util.h" |
| 33 #include "chrome/installer/util/browser_distribution.h" | 38 #include "chrome/installer/util/browser_distribution.h" |
| 34 #include "chrome/installer/util/create_reg_key_work_item.h" | 39 #include "chrome/installer/util/create_reg_key_work_item.h" |
| 35 #include "chrome/installer/util/install_util.h" | 40 #include "chrome/installer/util/install_util.h" |
| 36 #include "chrome/installer/util/set_reg_value_work_item.h" | 41 #include "chrome/installer/util/set_reg_value_work_item.h" |
| 37 #include "chrome/installer/util/shell_util.h" | 42 #include "chrome/installer/util/shell_util.h" |
| 38 #include "chrome/installer/util/util_constants.h" | 43 #include "chrome/installer/util/util_constants.h" |
| 39 #include "chrome/installer/util/work_item.h" | 44 #include "chrome/installer/util/work_item.h" |
| 40 #include "chrome/installer/util/work_item_list.h" | 45 #include "chrome/installer/util/work_item_list.h" |
| 46 #include "components/variations/variations_associated_data.h" | |
| 41 #include "content/public/browser/browser_thread.h" | 47 #include "content/public/browser/browser_thread.h" |
| 42 | 48 |
| 43 using content::BrowserThread; | 49 using content::BrowserThread; |
| 44 | 50 |
| 45 namespace { | 51 namespace { |
| 46 | 52 |
| 47 const wchar_t kAppListAppNameSuffix[] = L"AppList"; | 53 const wchar_t kAppListAppNameSuffix[] = L"AppList"; |
| 48 | 54 |
| 55 const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault"; | |
| 56 const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default"; | |
| 57 const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default"; | |
| 58 | |
| 49 // Helper function for ShellIntegration::GetAppId to generates profile id | 59 // Helper function for ShellIntegration::GetAppId to generates profile id |
| 50 // from profile path. "profile_id" is composed of sanitized basenames of | 60 // from profile path. "profile_id" is composed of sanitized basenames of |
| 51 // user data dir and profile dir joined by a ".". | 61 // user data dir and profile dir joined by a ".". |
| 52 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { | 62 base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) { |
| 53 // Return empty string if profile_path is empty | 63 // Return empty string if profile_path is empty |
| 54 if (profile_path.empty()) | 64 if (profile_path.empty()) |
| 55 return base::string16(); | 65 return base::string16(); |
| 56 | 66 |
| 57 base::FilePath default_user_data_dir; | 67 base::FilePath default_user_data_dir; |
| 58 // Return empty string if profile_path is in default user data | 68 // Return empty string if profile_path is in default user data |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 case ShellUtil::NOT_DEFAULT: | 243 case ShellUtil::NOT_DEFAULT: |
| 234 return ShellIntegration::NOT_DEFAULT; | 244 return ShellIntegration::NOT_DEFAULT; |
| 235 case ShellUtil::IS_DEFAULT: | 245 case ShellUtil::IS_DEFAULT: |
| 236 return ShellIntegration::IS_DEFAULT; | 246 return ShellIntegration::IS_DEFAULT; |
| 237 default: | 247 default: |
| 238 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); | 248 DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state); |
| 239 return ShellIntegration::UNKNOWN_DEFAULT; | 249 return ShellIntegration::UNKNOWN_DEFAULT; |
| 240 } | 250 } |
| 241 } | 251 } |
| 242 | 252 |
| 253 // Resets the default browser choice for the current user. | |
| 254 void ResetDefaultBrowser() { | |
| 255 static const wchar_t* const kUrlAssociationKeyFormats[] = { | |
| 256 L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" | |
| 257 L"%ls\\UserChoice", | |
| 258 L"SOFTWARE\\Microsoft\\Windows\\Roaming\\OpenWith\\UrlAssociations\\" | |
| 259 L"%ls\\UserChoice"}; | |
| 260 static const wchar_t* const kProtocols[] = {L"http", L"https"}; | |
| 261 | |
| 262 for (const wchar_t* format : kUrlAssociationKeyFormats) { | |
| 263 for (const wchar_t* protocol : kProtocols) { | |
| 264 base::win::RegKey registry_key( | |
| 265 HKEY_CURRENT_USER, base::StringPrintf(format, protocol).c_str(), | |
| 266 KEY_SET_VALUE); | |
| 267 registry_key.DeleteValue(L"Hash"); | |
| 268 } | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 // Returns true if the AsyncSetAsDefault field trial is activated. | |
| 273 bool IsAsyncSetAsDefaultEnabled() { | |
| 274 using base::CommandLine; | |
| 275 | |
| 276 // Note: It's important to query the field trial state first, to ensure that | |
| 277 // UMA reports the correct group. | |
|
Peter Kasting
2015/09/28 23:57:49
Is this what we actually want? If we're letting t
rkaplow
2015/09/29 14:20:53
This is still correct although it's a bit subtle.
| |
| 278 const std::string group_name = | |
| 279 base::FieldTrialList::FindFullName("AsyncSetAsDefault"); | |
| 280 if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableAsyncSetAsDefault)) | |
| 281 return false; | |
| 282 if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableAsyncSetAsDefault)) | |
| 283 return true; | |
| 284 | |
| 285 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); | |
| 286 } | |
| 287 | |
| 243 } // namespace | 288 } // namespace |
| 244 | 289 |
| 290 // static | |
| 291 bool ShellIntegration::IsSetAsDefaultAsynchronous() { | |
| 292 return base::win::GetVersion() >= base::win::VERSION_WIN10 && | |
| 293 IsAsyncSetAsDefaultEnabled(); | |
| 294 } | |
| 295 | |
| 296 void ShellIntegration::SetAsDefaultBrowserAsynchronous() { | |
| 297 DCHECK(ShellIntegration::IsSetAsDefaultAsynchronous()); | |
| 298 | |
| 299 ResetDefaultBrowser(); | |
| 300 | |
| 301 base::CommandLine cmdline(base::FilePath(L"openwith.exe")); | |
| 302 cmdline.AppendArgNative(StartupBrowserCreator::GetDefaultBrowserUrl()); | |
| 303 base::Process process(base::LaunchProcess(cmdline, base::LaunchOptions())); | |
| 304 UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.AsyncSetAsDefault.Launched", | |
| 305 process.IsValid()); | |
| 306 } | |
| 307 | |
| 245 ShellIntegration::DefaultWebClientSetPermission | 308 ShellIntegration::DefaultWebClientSetPermission |
| 246 ShellIntegration::CanSetAsDefaultBrowser() { | 309 ShellIntegration::CanSetAsDefaultBrowser() { |
| 247 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 310 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 248 if (distribution->GetDefaultBrowserControlPolicy() != | 311 if (distribution->GetDefaultBrowserControlPolicy() != |
| 249 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 312 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
| 250 return SET_DEFAULT_NOT_ALLOWED; | 313 return SET_DEFAULT_NOT_ALLOWED; |
| 251 | |
| 252 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 314 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 253 return SET_DEFAULT_UNATTENDED; | 315 return SET_DEFAULT_UNATTENDED; |
| 254 else | 316 if (IsSetAsDefaultAsynchronous()) |
| 255 return SET_DEFAULT_INTERACTIVE; | 317 return SET_DEFAULT_ASYNCHRONOUS; |
| 318 return SET_DEFAULT_INTERACTIVE; | |
| 256 } | 319 } |
| 257 | 320 |
| 258 bool ShellIntegration::SetAsDefaultBrowser() { | 321 bool ShellIntegration::SetAsDefaultBrowser() { |
| 259 base::FilePath chrome_exe; | 322 base::FilePath chrome_exe; |
| 260 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 323 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 261 LOG(ERROR) << "Error getting app exe path"; | 324 LOG(ERROR) << "Error getting app exe path"; |
| 262 return false; | 325 return false; |
| 263 } | 326 } |
| 264 | 327 |
| 265 // From UI currently we only allow setting default browser for current user. | 328 // From UI currently we only allow setting default browser for current user. |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 } | 644 } |
| 582 | 645 |
| 583 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + | 646 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + |
| 584 installer::kLnkExt); | 647 installer::kLnkExt); |
| 585 if (base::PathExists(shortcut)) | 648 if (base::PathExists(shortcut)) |
| 586 return shortcut; | 649 return shortcut; |
| 587 } | 650 } |
| 588 | 651 |
| 589 return base::FilePath(); | 652 return base::FilePath(); |
| 590 } | 653 } |
| 654 | |
| 655 void ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() { | |
| 656 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 657 | |
| 658 if (!IsSetAsDefaultAsynchronous()) | |
| 659 return; | |
| 660 | |
| 661 // On Windows 10+, there is no official way to prompt the user to set a | |
| 662 // default browser. This is the workaround: | |
| 663 // 1. Unregister the default browser. | |
| 664 // 2. Open "How to make Chrome my default browser" link with openwith.exe. | |
| 665 // 3. Windows will prompt the user with "How would you link to open this?". | |
| 666 // 4. If Chrome is selected, we intercept the attempt to open the URL and | |
| 667 // instead call OnSetAsDefaultAttemptComplete(), passing true to indicate | |
| 668 // success. | |
| 669 // 5. If Chrome is not selected, the url is opened in the selected browser. | |
| 670 // After a certain amount of time, we notify the observer that the | |
| 671 // process failed. | |
| 672 | |
| 673 // Remember the start time. | |
| 674 start_time_ = base::TimeTicks::Now(); | |
| 675 | |
| 676 if (!StartupBrowserCreator::SetDefaultBrowserCallback(base::Bind( | |
| 677 &DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, true))) { | |
| 678 // Another worker is currently processing. | |
| 679 OnSetAsDefaultAttemptComplete(false); | |
| 680 return; | |
| 681 } | |
| 682 | |
| 683 // Start the timer. | |
| 684 if (!async_timer_) | |
| 685 async_timer_.reset(new base::OneShotTimer()); | |
| 686 std::string value = variations::GetVariationParamValue( | |
| 687 kAsyncSetAsDefaultExperimentName, "TimerDuration"); | |
| 688 int seconds = 0; | |
| 689 if (!value.empty()) | |
| 690 base::StringToInt(value, &seconds); | |
| 691 if (!seconds) | |
| 692 seconds = 120; // Default value of 2 minutes. | |
| 693 async_timer_->Start( | |
| 694 FROM_HERE, base::TimeDelta::FromSeconds(seconds), | |
| 695 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, | |
| 696 false)); | |
| 697 } | |
| 698 | |
| 699 void ShellIntegration::DefaultBrowserWorker::FinalizeSetAsDefault( | |
| 700 bool succeeded) { | |
| 701 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 702 | |
| 703 if (async_timer_) { | |
| 704 async_timer_.reset(); | |
| 705 | |
| 706 StartupBrowserCreator::ClearDefaultBrowserCallback(); | |
| 707 UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.AsyncSetAsDefault.Succeeded", | |
| 708 succeeded); | |
| 709 UMA_HISTOGRAM_TIMES("DefaultBrowser.AsyncSetAsDefault.Duration", | |
| 710 base::TimeTicks::Now() - start_time_); | |
| 711 } | |
| 712 } | |
| OLD | NEW |