Adding an experiment to automatically open the default browser chooser

TBR=pmonette@chromium.org

Bug: 751299
Change-Id: I3d2026ad1b48652ba38cfcb73c073c58878bf956
Reviewed-on: https://chromium-review.googlesource.com/949974
Reviewed-by: Patrick Monette <pmonette@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#663}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/chrome/browser/resources/welcome/welcome_win10.html b/chrome/browser/resources/welcome/welcome_win10.html
index a700387..84b8c28 100644
--- a/chrome/browser/resources/welcome/welcome_win10.html
+++ b/chrome/browser/resources/welcome/welcome_win10.html
@@ -8,9 +8,12 @@
 
   <link rel="import" href="chrome://resources/cr_elements/icons.html">
   <link rel="import" href="chrome://resources/html/cr.html">
+  <link rel="import" href="chrome://resources/html/load_time_data.html">
+  <link rel="import" href="chrome://resources/html/util.html">
+
   <link rel="import" href="chrome://resources/html/action_link.html">
   <link rel="import" href="chrome://resources/html/action_link_css.html">
-  <link rel="import" href="chrome://resources/html/util.html">
+
   <link rel="import" href="chrome://resources/polymer/v1_0/iron-icon/iron-icon.html">
   <link rel="import" href="chrome://resources/polymer/v1_0/paper-button/paper-button.html">
   <link rel="import" href="chrome://resources/polymer/v1_0/paper-styles/color.html">
@@ -18,6 +21,8 @@
   <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
   <link rel="stylesheet" href="/welcome_win10.css">
 
+  <script src="strings.js"></script>
+
   <dom-module id="welcome-win10">
     <template>
       <style include="action-link">
@@ -31,6 +36,10 @@
           transition: opacity 300ms;
         }
 
+        :host(.accelerated) {
+          --expandable-section-height: 26.375em;
+        }
+
         a {
           color: var(--google-blue-500);
           text-decoration: none;
@@ -274,7 +283,7 @@
         }
       </style>
       <div class="header-logo" role="presentation"></div>
-      <div class="heading">$i18n{headerText}</div>
+      <div class="heading" role="heading" aria-level="1">$i18n{headerText}</div>
       <div class="sections">
         <div class$="[[computeClasses(isCombined)]]">
           <template is="dom-if" if="[[isCombined]]">
diff --git a/chrome/browser/resources/welcome/welcome_win10.js b/chrome/browser/resources/welcome/welcome_win10.js
index a961634..91a5bac 100644
--- a/chrome/browser/resources/welcome/welcome_win10.js
+++ b/chrome/browser/resources/welcome/welcome_win10.js
@@ -31,18 +31,15 @@
 
   ready: function() {
     this.isCombined = false;
-    this.isAccelerated = false;
+    this.isAccelerated = loadTimeData.getBoolean('accelerated_flow_enabled');
 
+    // The accelerated flow can be overridden with a query parameter.
     const FLOWTYPE_KEY = 'flowtype';
     const FLOW_TYPE_MAP = {'regular': false, 'accelerated': true};
     var params = new URLSearchParams(location.search);
     if (params.has(FLOWTYPE_KEY)) {
       if (params.get(FLOWTYPE_KEY) in FLOW_TYPE_MAP) {
         this.isAccelerated = FLOW_TYPE_MAP[params.get(FLOWTYPE_KEY)];
-
-        // Adjust the height since the accelerated flow contains fewer steps.
-        this.customStyle['--expandable-section-height'] = '26.375em';
-        this.updateStyles();
       } else {
         console.log(
             'Found invalid value for the \'flowtype\' parameter: %s',
@@ -50,6 +47,10 @@
       }
     }
 
+    // The accelerated flow contains fewer steps. To account for this, toggle
+    // the 'accelerated' class which reduces the height of the steps section.
+    this.classList.toggle('accelerated', this.isAccelerated);
+
     // Asynchronously check if Chrome is pinned to the taskbar.
     cr.sendWithPromise('getPinnedToTaskbarState')
         .then(this.receivePinnedState_.bind(this));
diff --git a/chrome/browser/ui/webui/welcome_win10_ui.cc b/chrome/browser/ui/webui/welcome_win10_ui.cc
index c1a27b1..ff736d6 100644
--- a/chrome/browser/ui/webui/welcome_win10_ui.cc
+++ b/chrome/browser/ui/webui/welcome_win10_ui.cc
@@ -11,6 +11,7 @@
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/ui/webui/welcome_win10_handler.h"
+#include "chrome/common/chrome_features.h"
 #include "chrome/common/pref_names.h"
 #include "chrome/common/url_constants.h"
 #include "chrome/grit/browser_resources.h"
@@ -87,8 +88,15 @@
   content::WebUIDataSource* html_source =
       content::WebUIDataSource::Create(url.host());
 
+  html_source->SetJsonPath("strings.js");
+
   AddLocalizedStrings(html_source, is_first_run);
 
+  // Controls the accelerated default browser flow experiment.
+  html_source->AddBoolean("accelerated_flow_enabled",
+                          base::FeatureList::IsEnabled(
+                              features::kWin10AcceleratedDefaultBrowserFlow));
+
   html_source->AddResourcePath("welcome_win10.css", IDR_WELCOME_WIN10_CSS);
   html_source->AddResourcePath("welcome_win10.js", IDR_WELCOME_WIN10_JS);
   html_source->AddResourcePath("default.webp", IDR_WELCOME_WIN10_DEFAULT_WEBP);
diff --git a/chrome/browser/win/settings_app_monitor.cc b/chrome/browser/win/settings_app_monitor.cc
index dbe3677..68183217 100644
--- a/chrome/browser/win/settings_app_monitor.cc
+++ b/chrome/browser/win/settings_app_monitor.cc
@@ -20,8 +20,10 @@
 
 #include "base/bind.h"
 #include "base/callback.h"
+#include "base/feature_list.h"
 #include "base/location.h"
 #include "base/memory/ref_counted.h"
+#include "base/metrics/histogram_macros.h"
 #include "base/sequenced_task_runner.h"
 #include "base/single_thread_task_runner.h"
 #include "base/strings/pattern.h"
@@ -30,6 +32,7 @@
 #include "base/strings/string_util.h"
 #include "base/threading/sequenced_task_runner_handle.h"
 #include "base/win/scoped_variant.h"
+#include "chrome/common/chrome_features.h"
 #include "ui/base/win/atl_module.h"
 
 namespace shell_integration {
@@ -110,6 +113,10 @@
   // Installs an event handler to observe events of interest.
   HRESULT InstallObservers();
 
+  // Invokes the |browser_button| if the Win10AcceleratedDefaultBrowserFlow
+  // feature is enabled.
+  void MaybeInvokeChooser(IUIAutomationElement* browser_button);
+
   // The task runner for the automation thread.
   base::SingleThreadTaskRunner* task_runner_ = nullptr;
 
@@ -128,6 +135,9 @@
   // State to suppress duplicate "focus changed" events.
   ElementType last_focused_element_ = ElementType::UNKNOWN;
 
+  // The browser chooser must only be invoked once.
+  bool browser_chooser_invoked_ = false;
+
   // Weak pointers to the context are given to event handlers.
   base::WeakPtrFactory<Context> weak_ptr_factory_;
 
@@ -204,6 +214,7 @@
   cache_request->AddProperty(UIA_AutomationIdPropertyId);
   cache_request->AddProperty(UIA_NamePropertyId);
   cache_request->AddProperty(UIA_ClassNamePropertyId);
+  cache_request->AddPattern(UIA_InvokePatternId);
 #if DCHECK_IS_ON()
   cache_request->AddProperty(UIA_ControlTypePropertyId);
   cache_request->AddProperty(UIA_IsPeripheralPropertyId);
@@ -687,6 +698,7 @@
   last_focused_element_ = element_type;
 
   if (element_type == ElementType::DEFAULT_BROWSER) {
+    MaybeInvokeChooser(sender.Get());
     monitor_runner_->PostTask(
         FROM_HERE, base::Bind(&SettingsAppMonitor::OnAppFocused, monitor_));
   } else if (element_type == ElementType::CHECK_IT_OUT) {
@@ -778,6 +790,26 @@
   return result;
 }
 
+void SettingsAppMonitor::Context::MaybeInvokeChooser(
+    IUIAutomationElement* browser_button) const {
+  if (browser_chooser_invoked_ ||
+      !base::FeatureList::IsEnabled(
+          features::kWin10AcceleratedDefaultBrowserFlow)) {
+    return;
+  }
+
+  // Invoke the dialog and record whether it was successful.
+  Microsoft::WRL::ComPtr<IUIAutomationInvokePattern> invoke_pattern;
+  bool succeeded = SUCCEEDED(browser_button->GetCachedPatternAs(
+                       UIA_InvokePatternId, IID_PPV_ARGS(&invoke_pattern))) &&
+                   invoke_pattern && SUCCEEDED(invoke_pattern->Invoke());
+
+  if (succeeded)
+    browser_chooser_invoked_ = true;
+
+  UMA_HISTOGRAM_BOOLEAN("DefaultBrowser.Win10ChooserInvoked", succeeded);
+}
+
 
 // SettingsAppMonitor ----------------------------------------------------------
 
diff --git a/chrome/common/chrome_features.cc b/chrome/common/chrome_features.cc
index a05a43a..6a64586 100644
--- a/chrome/common/chrome_features.cc
+++ b/chrome/common/chrome_features.cc
@@ -610,6 +610,12 @@
 const base::Feature kTPMFirmwareUpdate{"TPMFirmwareUpdate",
                                        base::FEATURE_DISABLED_BY_DEFAULT};
 
+#if defined(OS_WIN)
+// Enables the accelerated default browser flow for Windows 10.
+const base::Feature kWin10AcceleratedDefaultBrowserFlow{
+    "Win10AcceleratedDefaultBrowserFlow", base::FEATURE_DISABLED_BY_DEFAULT};
+#endif  // defined(OS_WIN)
+
 #endif  // defined(OS_CHROMEOS)
 
 }  // namespace features
diff --git a/chrome/common/chrome_features.h b/chrome/common/chrome_features.h
index 647b333..49b9993 100644
--- a/chrome/common/chrome_features.h
+++ b/chrome/common/chrome_features.h
@@ -326,6 +326,10 @@
 extern const base::Feature kTPMFirmwareUpdate;
 #endif  // defined(OS_CHROMEOS)
 
+#if defined(OS_WIN)
+extern const base::Feature kWin10AcceleratedDefaultBrowserFlow;
+#endif  // defined(OS_WIN)
+
 bool PrefServiceEnabled();
 
 // DON'T ADD RANDOM STUFF HERE. Put it in the main section above in
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index 09b6cc5..b4ec730 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -14414,6 +14414,16 @@
   <summary>Whether Chrome was the default browser when it started up.</summary>
 </histogram>
 
+<histogram name="DefaultBrowser.Win10ChooserInvoked" enum="BooleanSuccess">
+  <owner>pmonette@chromium.org</owner>
+  <summary>
+    When changing the default browser on Windows 10, records whether the browser
+    chooser is successfully invoked when opening the settings page. This
+    histogram is only recorded when the Win10AcceleratedDefaultBrowserFlow
+    experiment is enabled.
+  </summary>
+</histogram>
+
 <histogram name="DefaultBrowserWarning.DontSetAsDefault" enum="BooleanHit">
   <obsolete>
     Deprecated 2015/11. The same information is available as the value Failure