| 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. |
| 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 |
| 245 bool ShellIntegration::SetAsDefaultBrowser() { | 296 bool ShellIntegration::SetAsDefaultBrowser() { |
| 246 base::FilePath chrome_exe; | 297 base::FilePath chrome_exe; |
| 247 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 298 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 248 LOG(ERROR) << "Error getting app exe path"; | 299 LOG(ERROR) << "Error getting app exe path"; |
| 249 return false; | 300 return false; |
| 250 } | 301 } |
| 251 | 302 |
| 252 // From UI currently we only allow setting default browser for current user. | 303 // From UI currently we only allow setting default browser for current user. |
| 253 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 304 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 254 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, | 305 if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER, chrome_exe, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 VLOG(1) << "Set-default-client Windows UI completed."; | 371 VLOG(1) << "Set-default-client Windows UI completed."; |
| 321 return true; | 372 return true; |
| 322 } | 373 } |
| 323 | 374 |
| 324 ShellIntegration::DefaultWebClientSetPermission | 375 ShellIntegration::DefaultWebClientSetPermission |
| 325 ShellIntegration::CanSetAsDefaultBrowser() { | 376 ShellIntegration::CanSetAsDefaultBrowser() { |
| 326 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 377 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 327 if (distribution->GetDefaultBrowserControlPolicy() != | 378 if (distribution->GetDefaultBrowserControlPolicy() != |
| 328 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) | 379 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) |
| 329 return SET_DEFAULT_NOT_ALLOWED; | 380 return SET_DEFAULT_NOT_ALLOWED; |
| 330 | |
| 331 if (ShellUtil::CanMakeChromeDefaultUnattended()) | 381 if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| 332 return SET_DEFAULT_UNATTENDED; | 382 return SET_DEFAULT_UNATTENDED; |
| 333 else | 383 if (IsSetAsDefaultAsynchronous()) |
| 334 return SET_DEFAULT_INTERACTIVE; | 384 return SET_DEFAULT_ASYNCHRONOUS; |
| 385 return SET_DEFAULT_INTERACTIVE; |
| 335 } | 386 } |
| 336 | 387 |
| 337 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() { | 388 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() { |
| 338 return base::win::GetVersion() < base::win::VERSION_WIN8; | 389 return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 339 } | 390 } |
| 340 | 391 |
| 341 base::string16 ShellIntegration::GetApplicationNameForProtocol( | 392 base::string16 ShellIntegration::GetApplicationNameForProtocol( |
| 342 const GURL& url) { | 393 const GURL& url) { |
| 343 // Windows 8 or above requires a new protocol association query. | 394 // Windows 8 or above requires a new protocol association query. |
| 344 if (base::win::GetVersion() >= base::win::VERSION_WIN8) | 395 if (base::win::GetVersion() >= base::win::VERSION_WIN8) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 return dist->GetBaseAppId(); | 463 return dist->GetBaseAppId(); |
| 413 } | 464 } |
| 414 return GetAppModelIdForProfile( | 465 return GetAppModelIdForProfile( |
| 415 ShellUtil::GetBrowserModelId(dist, | 466 ShellUtil::GetBrowserModelId(dist, |
| 416 InstallUtil::IsPerUserInstall(chrome_exe)), | 467 InstallUtil::IsPerUserInstall(chrome_exe)), |
| 417 profile_path); | 468 profile_path); |
| 418 } | 469 } |
| 419 | 470 |
| 420 base::string16 ShellIntegration::GetAppListAppModelIdForProfile( | 471 base::string16 ShellIntegration::GetAppListAppModelIdForProfile( |
| 421 const base::FilePath& profile_path) { | 472 const base::FilePath& profile_path) { |
| 422 return ShellIntegration::GetAppModelIdForProfile( | 473 return GetAppModelIdForProfile(GetAppListAppName(), profile_path); |
| 423 GetAppListAppName(), profile_path); | |
| 424 } | 474 } |
| 425 | 475 |
| 426 void ShellIntegration::MigrateChromiumShortcuts() { | 476 void ShellIntegration::MigrateChromiumShortcuts() { |
| 427 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 477 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 428 return; | 478 return; |
| 429 | 479 |
| 430 // This needs to happen eventually (e.g. so that the appid is fixed and the | 480 // This needs to happen eventually (e.g. so that the appid is fixed and the |
| 431 // run-time Chrome icon is merged with the taskbar shortcut), but this is not | 481 // run-time Chrome icon is merged with the taskbar shortcut), but this is not |
| 432 // urgent and shouldn't delay Chrome startup. | 482 // urgent and shouldn't delay Chrome startup. |
| 433 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15; | 483 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 } | 631 } |
| 582 | 632 |
| 583 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + | 633 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + |
| 584 installer::kLnkExt); | 634 installer::kLnkExt); |
| 585 if (base::PathExists(shortcut)) | 635 if (base::PathExists(shortcut)) |
| 586 return shortcut; | 636 return shortcut; |
| 587 } | 637 } |
| 588 | 638 |
| 589 return base::FilePath(); | 639 return base::FilePath(); |
| 590 } | 640 } |
| 641 |
| 642 // static |
| 643 bool ShellIntegration::DefaultWebClientWorker::ShouldReportAttemptResults() { |
| 644 return base::win::GetVersion() >= base::win::VERSION_WIN10 && |
| 645 IsSetAsDefaultAsynchronous(); |
| 646 } |
| 647 |
| 648 bool ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() { |
| 649 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 650 |
| 651 if (!IsSetAsDefaultAsynchronous()) |
| 652 return false; |
| 653 |
| 654 // On Windows 10+, there is no official way to prompt the user to set a |
| 655 // default browser. This is the workaround: |
| 656 // 1. Unregister the default browser. |
| 657 // 2. Open "How to make Chrome my default browser" link with openwith.exe. |
| 658 // 3. Windows will prompt the user with "How would you link to open this?". |
| 659 // 4. If Chrome is selected, we intercept the attempt to open the URL and |
| 660 // instead call OnSetAsDefaultAttemptComplete(), passing true to indicate |
| 661 // success. |
| 662 // 5. If Chrome is not selected, the url is opened in the selected browser. |
| 663 // After a certain amount of time, we notify the observer that the |
| 664 // process failed. |
| 665 |
| 666 if (!StartupBrowserCreator::SetDefaultBrowserCallback( |
| 667 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, |
| 668 AttemptResult::SUCCESS))) { |
| 669 // Another worker is currently processing. Note that this will still cause |
| 670 // SetAsDefaultBrowserAsynchronous() to be invoked in SetAsDefault() but |
| 671 // the other worker will happily intercept the attempt. |
| 672 BrowserThread::PostTask( |
| 673 BrowserThread::UI, FROM_HERE, |
| 674 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, |
| 675 AttemptResult::OTHER_WORKER)); |
| 676 return false; |
| 677 } |
| 678 |
| 679 // Start the timer. |
| 680 if (!async_timer_) |
| 681 async_timer_.reset(new base::OneShotTimer()); |
| 682 std::string value = variations::GetVariationParamValue( |
| 683 kAsyncSetAsDefaultExperimentName, "TimerDuration"); |
| 684 int seconds = 0; |
| 685 if (!value.empty()) |
| 686 base::StringToInt(value, &seconds); |
| 687 if (!seconds) |
| 688 seconds = 120; // Default value of 2 minutes. |
| 689 async_timer_->Start( |
| 690 FROM_HERE, base::TimeDelta::FromSeconds(seconds), |
| 691 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, |
| 692 AttemptResult::FAILURE)); |
| 693 return true; |
| 694 } |
| 695 |
| 696 void ShellIntegration::DefaultBrowserWorker::FinalizeSetAsDefault() { |
| 697 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 698 DCHECK(set_as_default_initialized()); |
| 699 |
| 700 async_timer_.reset(); |
| 701 StartupBrowserCreator::ClearDefaultBrowserCallback(); |
| 702 } |
| 703 |
| 704 // static |
| 705 bool ShellIntegration::DefaultBrowserWorker::SetAsDefaultBrowserAsynchronous() { |
| 706 DCHECK(IsSetAsDefaultAsynchronous()); |
| 707 |
| 708 ResetDefaultBrowser(); |
| 709 |
| 710 base::CommandLine cmdline(base::FilePath(L"openwith.exe")); |
| 711 cmdline.AppendArgNative(StartupBrowserCreator::GetDefaultBrowserUrl()); |
| 712 return base::LaunchProcess(cmdline, base::LaunchOptions()).IsValid(); |
| 713 } |
| OLD | NEW |