Bug 1422084 - Intercept loads of about:certerror for special handling r=snorp
authorDylan Roeh <droeh@mozilla.com>
Thu, 01 Mar 2018 19:48:22 -0500
changeset 462420 525be9fb39bd676e406a89116eb55d8638a7b39d
parent 462419 90c70e5122f6da891b59c579a3c7d0c02a6610d4
child 462421 c59f42a79b5a3c74d7d5d2279b5dc52ba3564034
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1422084
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1422084 - Intercept loads of about:certerror for special handling r=snorp
mobile/android/chrome/geckoview/ErrorPageEventHandler.js
mobile/android/chrome/geckoview/GeckoViewNavigationContent.js
mobile/android/chrome/geckoview/jar.mn
new file mode 100644
--- /dev/null
+++ b/mobile/android/chrome/geckoview/ErrorPageEventHandler.js
@@ -0,0 +1,56 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+ChromeUtils.defineModuleGetter(this, "SSLExceptions",
+                               "resource://gre/modules/SSLExceptions.jsm");
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  Services: "resource://gre/modules/Services.jsm",
+});
+
+var EXPORTED_SYMBOLS = ["ErrorPageEventHandler"];
+
+var ErrorPageEventHandler = {
+  handleEvent: function(aEvent) {
+    switch (aEvent.type) {
+      case "click": {
+        // Don't trust synthetic events
+        if (!aEvent.isTrusted)
+          return;
+
+        let target = aEvent.originalTarget;
+        let errorDoc = target.ownerDocument;
+
+        // If the event came from an ssl error page, it is probably either the "Add
+        // Exception…" or "Get me out of here!" button
+        if (errorDoc.documentURI.startsWith("about:certerror?e=nssBadCert")) {
+          let perm = errorDoc.getElementById("permanentExceptionButton");
+          let temp = errorDoc.getElementById("temporaryExceptionButton");
+          if (target == temp || target == perm) {
+            // Handle setting a cert exception and reloading the page
+            try {
+              // Add a new SSL exception for this URL
+              let uri = Services.io.newURI(errorDoc.location.href);
+              let sslExceptions = new SSLExceptions();
+
+              if (target == perm)
+                sslExceptions.addPermanentException(uri, errorDoc.defaultView);
+              else
+                sslExceptions.addTemporaryException(uri, errorDoc.defaultView);
+            } catch (e) {
+              dump("Failed to set cert exception: " + e + "\n");
+            }
+            errorDoc.location.reload();
+          } else if (target == errorDoc.getElementById("getMeOutOfHereButton")) {
+            errorDoc.location = "about:home";
+          }
+        } 
+        break;
+      }
+    }
+  }
+};
--- a/mobile/android/chrome/geckoview/GeckoViewNavigationContent.js
+++ b/mobile/android/chrome/geckoview/GeckoViewNavigationContent.js
@@ -3,16 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 ChromeUtils.import("resource://gre/modules/GeckoViewContentModule.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   Services: "resource://gre/modules/Services.jsm",
+  ErrorPageEventHandler: "chrome://geckoview/content/ErrorPageEventHandler.js"
 });
 
 XPCOMUtils.defineLazyGetter(this, "dump", () =>
     ChromeUtils.import("resource://gre/modules/AndroidLog.jsm",
                        {}).AndroidLog.d.bind(null, "ViewNavigationContent"));
 
 function debug(aMsg) {
   // dump(aMsg);
@@ -30,16 +31,21 @@ class GeckoViewNavigationContent extends
 
     docShell.loadURIDelegate = null;
   }
 
   // nsILoadURIDelegate.
   loadURI(aUri, aWhere, aFlags, aTriggeringPrincipal) {
     debug("loadURI " + (aUri && aUri.spec) + " " + aWhere + " " + aFlags);
 
+    // TODO: Remove this when we have a sensible error API.
+    if (aUri && aUri.displaySpec.startsWith("about:certerror")) {
+      addEventListener("click", ErrorPageEventHandler, true);
+    }
+
     let message = {
       type: "GeckoView:OnLoadUri",
       uri: aUri ? aUri.displaySpec : "",
       where: aWhere,
       flags: aFlags
     };
 
     debug("dispatch " + JSON.stringify(message));
--- a/mobile/android/chrome/geckoview/jar.mn
+++ b/mobile/android/chrome/geckoview/jar.mn
@@ -1,12 +1,13 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 geckoview.jar:
 % content geckoview %content/
 
+  content/ErrorPageEventHandler.js
   content/geckoview.xul
   content/geckoview.js
   content/GeckoViewContent.js
   content/GeckoViewNavigationContent.js
   content/GeckoViewScrollContent.js