Changed json to human readable text.

As part of improving the interface on chrome://browser-switch/internals,
the json that the url checker was displaying has been translated
into a human readable form with information about which browser it
will open in and the reason for it.


Bug: 1258133
Change-Id: Ifa467bcab42fe368c00207c434ea042fb6635e70
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3224299
Commit-Queue: Sneha Nagpaul <snehanagpaul@google.com>
Reviewed-by: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/main@{#932241}
diff --git a/chrome/browser/resources/browser_switch/internals/browser_switch_internals.html b/chrome/browser/resources/browser_switch/internals/browser_switch_internals.html
index f68be0d4..62f7abc 100644
--- a/chrome/browser/resources/browser_switch/internals/browser_switch_internals.html
+++ b/chrome/browser/resources/browser_switch/internals/browser_switch_internals.html
@@ -7,6 +7,12 @@
   <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
 
   <style>
+    ul {
+      list-style-type: none;
+      padding: 0;
+      margin: 0;
+    }
+
     table {
       border-collapse: collapse;
       font-family: monospace;
@@ -59,7 +65,10 @@
         placeholder="http://example.com/">
   </label>
 
-  <pre id=output></pre>
+  <ul>
+    <li id="output"></li>
+    <li id="reason"></li>
+  </ul>
 
   <h2>Sitelist</h2>
   <table id="sitelist"></table>
diff --git a/chrome/browser/resources/browser_switch/internals/browser_switch_internals.ts b/chrome/browser/resources/browser_switch/internals/browser_switch_internals.ts
index 2d59a32..8d6ed74 100644
--- a/chrome/browser/resources/browser_switch/internals/browser_switch_internals.ts
+++ b/chrome/browser/resources/browser_switch/internals/browser_switch_internals.ts
@@ -1,10 +1,18 @@
 // Copyright 2019 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
+import '../strings.m.js';
 
+import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
 import {addWebUIListener, sendWithPromise} from 'chrome://resources/js/cr.m.js';
 import {$} from 'chrome://resources/js/util.m.js';
 
+type Decision = {
+  action: 'stay' | 'go';
+  matching_rule?: string;
+  reason: 'globally_disabled' | 'protocol' | 'sitelist' | 'greylist' | 'default';
+};
+
 type RuleSet = {
   sitelist: Array<string>;
   greylist: Array<string>;
@@ -102,6 +110,62 @@
   }
 }
 
+/**
+ * Gets the English name of the alternate browser.
+ */
+function getAltBrowserName(): string {
+  return loadTimeData.getString('browserName');
+}
+
+/**
+ * Takes the json from the url checker and makes it readable.
+ */
+function urlOutputText(decision: Decision): Array<string> {
+  let opensIn = '';
+  const altBrowserName = getAltBrowserName();
+
+  switch (decision.action) {
+    case 'stay':
+      // TODO(crbug.com/1258133): Make it show the name of the browser.
+        opensIn = 'Opens in: This browser\n';
+        break;
+    case 'go':
+        opensIn = `Opens in: ${altBrowserName}\n`;
+        break;
+  }
+
+  let reason = '';
+  if (decision.matching_rule) {
+    if (decision.matching_rule.startsWith('!')) {
+      reason += `Reason: The inverted rule ${JSON.stringify(decision.matching_rule)} was found in `;
+    } else {
+      reason += `Reason: ${JSON.stringify(decision.matching_rule)} was found in `;
+    }
+  }
+  // if undefined - add nothing to the output
+
+  switch (decision.reason) {
+    case 'globally_disabled':
+        reason += 'Reason: The BrowserSwitcherEnabled policy is false.\n';
+        break;
+    case 'protocol':
+        reason += 'Reason: LBS only supports http://, https://, and file:// URLs.\n';
+        break;
+    case 'sitelist':
+        reason += 'the sitelist.\n';
+        break;
+    case 'greylist':
+        reason += 'the greylist.\n';
+        break;
+    case 'default' :
+      // TODO(crbug.com/1258133): Make it show the name of the browser.
+        reason += 'Reason: LBS stays in Google Chrome by default.\n';
+        break;
+  }
+
+  return [opensIn, reason];
+}
+
 function checkUrl() {
   let url = ($('url-checker-input') as HTMLInputElement).value;
   if (!url) {
@@ -112,12 +176,15 @@
     url = 'http://' + url;
   }
   sendWithPromise('getDecision', url)
-      .then(decision => {
+      .then((decision) => {
         // URL is valid.
-        $('output').innerText = JSON.stringify(decision, null, 2);
+        const [output, reason] = urlOutputText(decision);
+        $('output').innerText = output;
+        $('reason').innerText = reason;
       })
-      .catch(() => {
+      .catch((errorMessage) => {
         // URL is invalid.
+        console.warn(errorMessage);
         $('output').innerText =
             'Invalid URL. Make sure it is formatted properly.';
       });