Chromium Code Reviews| Index: chrome/browser/shell_integration_win.cc |
| diff --git a/chrome/browser/shell_integration_win.cc b/chrome/browser/shell_integration_win.cc |
| index 07f7b4b9f8e465faaad7240654e82ebedbb91644..2f8dd75b6e5193c80768f5bf42095e3ae6fb2c40 100644 |
| --- a/chrome/browser/shell_integration_win.cc |
| +++ b/chrome/browser/shell_integration_win.cc |
| @@ -5,6 +5,7 @@ |
| #include "chrome/browser/shell_integration.h" |
| #include <windows.h> |
| +#include <shellapi.h> |
| #include <shlwapi.h> |
| #include <shobjidl.h> |
| #include <propkey.h> // Needs to come after shobjidl.h. |
| @@ -14,6 +15,8 @@ |
| #include "base/files/file_enumerator.h" |
| #include "base/files/file_util.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/metrics/field_trial.h" |
| +#include "base/metrics/histogram_macros.h" |
| #include "base/path_service.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| @@ -25,6 +28,7 @@ |
| #include "base/win/shortcut.h" |
| #include "base/win/windows_version.h" |
| #include "chrome/browser/policy/policy_path_parser.h" |
| +#include "chrome/browser/ui/startup/startup_browser_creator.h" |
| #include "chrome/browser/web_applications/web_app.h" |
| #include "chrome/common/chrome_constants.h" |
| #include "chrome/common/chrome_paths_internal.h" |
| @@ -38,6 +42,7 @@ |
| #include "chrome/installer/util/util_constants.h" |
| #include "chrome/installer/util/work_item.h" |
| #include "chrome/installer/util/work_item_list.h" |
| +#include "components/variations/variations_associated_data.h" |
| #include "content/public/browser/browser_thread.h" |
| using content::BrowserThread; |
| @@ -45,6 +50,12 @@ using content::BrowserThread; |
| namespace { |
| const wchar_t kAppListAppNameSuffix[] = L"AppList"; |
| +const wchar_t kSetDefaultBrowserHelpUrl[] = |
| + L"https://support.google.com/chrome?p=default_browser"; |
| + |
| +const char kAsyncSetAsDefaultExperimentName[] = "AsyncSetAsDefault"; |
| +const char kEnableAsyncSetAsDefault[] = "enable-async-set-as-default"; |
| +const char kDisableAsyncSetAsDefault[] = "disable-async-set-as-default"; |
| // Helper function for ShellIntegration::GetAppId to generates profile id |
| // from profile path. "profile_id" is composed of sanitized basenames of |
| @@ -240,8 +251,54 @@ ShellIntegration::DefaultWebClientState |
| } |
| } |
| +// Resets the default browser choice for the current user. |
| +void ResetDefaultBrowser() { |
| + static const wchar_t* kUrlAssociationKeyFormats[] = { |
|
grt (UTC plus 2)
2015/09/22 18:21:47
static const wchar_t* const
Patrick Monette
2015/09/23 22:40:44
Done.
|
| + L"SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\" |
| + L"%ls\\UserChoice", |
| + L"SOFTWARE\\Microsoft\\Windows\\Roaming\\OpenWith\\UrlAssociations\\" |
| + L"%ls\\UserChoice"}; |
| + static const wchar_t* kProtocols[] = {L"http", L"https"}; |
|
grt (UTC plus 2)
2015/09/22 18:21:47
static const wchar_t* const
Patrick Monette
2015/09/23 22:40:44
Done.
|
| + |
| + for (const wchar_t* format : kUrlAssociationKeyFormats) { |
| + for (const wchar_t* protocol : kProtocols) { |
| + base::win::RegKey registry_key( |
| + HKEY_CURRENT_USER, base::StringPrintf(format, protocol).c_str(), |
| + KEY_WRITE); |
|
grt (UTC plus 2)
2015/09/22 18:21:47
KEY_WRITE -> KEY_SET_VALUE
Patrick Monette
2015/09/23 22:40:44
Done.
|
| + registry_key.DeleteValue(L"Hash"); |
| + } |
| + } |
| +} |
| + |
| +// Returns true if the AsyncSetAsDefault field trial is activated. |
| +bool IsAsyncSetAsDefaultEnabled() { |
| + using base::CommandLine; |
| + |
| + const std::string group_name = |
| + base::FieldTrialList::FindFullName("AsyncSetAsDefault"); |
| + if (CommandLine::ForCurrentProcess()->HasSwitch(kDisableAsyncSetAsDefault)) |
| + return false; |
| + if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableAsyncSetAsDefault)) |
| + return true; |
| + |
| + return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE); |
| +} |
| + |
| } // namespace |
| +// static |
| +bool ShellIntegration::IsSetAsDefaultAsynchronous() { |
| + return base::win::GetVersion() >= base::win::VERSION_WIN10 && |
| + IsAsyncSetAsDefaultEnabled(); |
| +} |
| + |
| +void ShellIntegration::SetAsDefaultBrowserAsynchronous() { |
| + DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN10); |
|
grt (UTC plus 2)
2015/09/22 18:21:47
DCHECK_GE
Patrick Monette
2015/09/23 22:40:44
Done.
|
| + |
| + ResetDefaultBrowser(); |
| + ShellExecute(0, 0, kSetDefaultBrowserHelpUrl, 0, 0, SW_SHOW); |
|
grt (UTC plus 2)
2015/09/22 18:21:47
does something like this work?
CommandLine cmd(L"o
Patrick Monette
2015/09/23 22:40:44
Done.
|
| +} |
| + |
| ShellIntegration::DefaultWebClientSetPermission |
| ShellIntegration::CanSetAsDefaultBrowser() { |
| BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| @@ -251,6 +308,8 @@ ShellIntegration::DefaultWebClientSetPermission |
| if (ShellUtil::CanMakeChromeDefaultUnattended()) |
| return SET_DEFAULT_UNATTENDED; |
| + else if (IsSetAsDefaultAsynchronous()) |
| + return SET_DEFAULT_ASYNCHRONOUS; |
| else |
| return SET_DEFAULT_INTERACTIVE; |
| } |
| @@ -588,3 +647,62 @@ base::FilePath ShellIntegration::GetStartMenuShortcut( |
| return base::FilePath(); |
| } |
| + |
| +void ShellIntegration::DefaultBrowserWorker::InitializeSetAsDefault() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + if (IsSetAsDefaultAsynchronous()) { |
| + StartupBrowserCreator::AddUrlFilter( |
| + GURL(kSetDefaultBrowserHelpUrl), |
| + base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, true)); |
| + |
| + // Remember the start time. |
| + start_time_ = base::TimeTicks::Now(); |
| + |
| + // Start the timer. |
| + if (!async_timer_) { |
| + async_timer_.reset(new base::OneShotTimer<DefaultWebClientWorker>()); |
| + } |
| + |
| + base::TimeDelta timer_duration = base::TimeDelta::FromMinutes(2); |
| + std::string value = variations::GetVariationParamValue( |
| + kAsyncSetAsDefaultExperimentName, "TimerDuration"); |
| + if (!value.empty()) { |
| + timer_duration = base::TimeDelta::FromSeconds(std::atoi(value.c_str())); |
| + } |
| + |
| + async_timer_->Start( |
| + FROM_HERE, timer_duration, |
| + base::Bind(&DefaultBrowserWorker::CompleteSetAsDefault, this, false)); |
| + } |
| +} |
| + |
| +bool ShellIntegration::DefaultBrowserWorker::UninitializeSetAsDefault( |
| + bool succeeded) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + if (IsSetAsDefaultAsynchronous()) { |
| + GURL default_browser_help_url(kSetDefaultBrowserHelpUrl); |
| + if (!async_timer_) { |
| + // If the timer isnt set, that means it has already fired and |
| + // CompleteSetAsDefault should not be called again. |
| + DCHECK(!StartupBrowserCreator::UrlFilterExists(default_browser_help_url)); |
| + return false; |
| + } |
| + |
| + // Record the duration from starting to set as default browser to now. |
| + UMA_HISTOGRAM_TIMES("AsyncSetAsDefault.Duration", |
| + base::TimeTicks::Now() - start_time_); |
| + if (succeeded) { |
|
Alexei Svitkine (slow)
2015/09/22 15:20:43
Nit: No {}'s for 1-line bodies. Same for code in t
Patrick Monette
2015/09/22 17:11:09
Done.
|
| + UMA_HISTOGRAM_COUNTS("AsyncSetAsDefault.Success", 1); |
|
Alexei Svitkine (slow)
2015/09/22 15:20:43
For, for a boolean histogram, you should use UMA_H
Patrick Monette
2015/09/22 17:11:09
Done.
The reasoning was that we already have the t
|
| + } |
| + |
| + // Destroy timer. |
| + async_timer_.reset(); |
| + |
| + // Remove filter if it exists. |
| + if (StartupBrowserCreator::UrlFilterExists(default_browser_help_url)) |
| + StartupBrowserCreator::RemoveUrlFilter(default_browser_help_url); |
| + } |
| + return true; |
| +} |