Bug 1420485: Follow-up: Remove unnecessary single-use stringToCryptoHash function. r=me
authorKris Maglione <maglione.k@gmail.com>
Sun, 11 Mar 2018 22:25:56 -0700
changeset 462587 d5c3153701c6c4245499dbcfc833a1ed6b45ca3c
parent 462586 b28213a70982f163998f473f51827d9be943c44a
child 462588 7582d9f9c6e3f62bcbc7111e70fa152004386711
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)
reviewersme
bugs1420485
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 1420485: Follow-up: Remove unnecessary single-use stringToCryptoHash function. r=me MozReview-Commit-ID: H1tPnsN0v2J
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/ExtensionUtils.jsm
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -29,27 +29,28 @@ const DocumentEncoder = Components.Const
   "nsIDocumentEncoder", "init");
 
 const Timer = Components.Constructor("@mozilla.org/timer;1", "nsITimer", "initWithCallback");
 
 ChromeUtils.import("resource://gre/modules/ExtensionChild.jsm");
 ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
 ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
 
+Cu.importGlobalProperties(["crypto", "TextDecoder", "TextEncoder"]);
+
 const {
   DefaultMap,
   DefaultWeakMap,
   defineLazyGetter,
   getInnerWindowID,
   getWinUtils,
   promiseDocumentIdle,
   promiseDocumentLoaded,
   promiseDocumentReady,
   runSafeSyncWithoutClone,
-  stringToCryptoHash,
 } = ExtensionUtils;
 
 const {
   BaseContext,
   CanOfAPIs,
   SchemaAPIManager,
 } = ExtensionCommon;
 
@@ -306,17 +307,18 @@ class Script {
   }
 
   async addCSSCode(cssCode) {
     if (!cssCode) {
       return;
     }
 
     // Store the hash of the cssCode.
-    this.cssCodeHash = await stringToCryptoHash(cssCode);
+    const buffer = await crypto.subtle.digest("SHA-1", new TextEncoder().encode(cssCode));
+    this.cssCodeHash = new TextDecoder().decode(buffer);
 
     // Cache and preload the cssCode stylesheet.
     this.cssCodeCache.addCSSCode(this.cssCodeHash, cssCode);
   }
 
   compileScripts() {
     return this.js.map(url => this.scriptCache.get(url));
   }
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -8,34 +8,25 @@
 var EXPORTED_SYMBOLS = ["ExtensionUtils"];
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "ConsoleAPI",
                                "resource://gre/modules/Console.jsm");
 
-Cu.importGlobalProperties(["crypto", "TextDecoder", "TextEncoder"]);
-
 function getConsole() {
   return new ConsoleAPI({
     maxLogLevelPref: "extensions.webextensions.log.level",
     prefix: "WebExtensions",
   });
 }
 
 XPCOMUtils.defineLazyGetter(this, "console", getConsole);
 
-XPCOMUtils.defineLazyGetter(this, "utf8Encoder", () => {
-  return new TextEncoder("utf-8");
-});
-XPCOMUtils.defineLazyGetter(this, "utf8Decoder", () => {
-  return new TextDecoder("utf-8");
-});
-
 // xpcshell doesn't handle idle callbacks well.
 XPCOMUtils.defineLazyGetter(this, "idleTimeout",
                             () => Services.appinfo.name === "XPCShell" ? 500 : undefined);
 
 // It would be nicer to go through `Services.appinfo`, but some tests need to be
 // able to replace that field with a custom implementation before it is first
 // called.
 // eslint-disable-next-line mozilla/use-services
@@ -656,31 +647,16 @@ function checkLoadURL(url, principal, op
                                   Services.io.newURI(url),
                                   flags);
   } catch (e) {
     return false;
   }
   return true;
 }
 
-/**
- * Return the cryptographic hash given a string of text (using MD5 by default).
- *
- * @param {string} text
- *   The string of text to hash.
- * @param {string} [algo]
- *   An optional algorithm to be re-used to generate the hash ("SHA-1" by default).
- * @returns {string} text
- *   The hashed string.
- */
-async function stringToCryptoHash(text, algo = "SHA-1") {
-  const buffer = await crypto.subtle.digest(algo, utf8Encoder.encode(text));
-  return utf8Decoder.decode(buffer);
-}
-
 var ExtensionUtils = {
   checkLoadURL,
   defineLazyGetter,
   flushJarCache,
   getConsole,
   getInnerWindowID,
   getMessageManager,
   getUniqueId,
@@ -689,17 +665,16 @@ var ExtensionUtils = {
   instanceOf,
   normalizeTime,
   promiseDocumentIdle,
   promiseDocumentLoaded,
   promiseDocumentReady,
   promiseEvent,
   promiseObserved,
   runSafeSyncWithoutClone,
-  stringToCryptoHash,
   withHandlingUserInput,
   DefaultMap,
   DefaultWeakMap,
   EventEmitter,
   ExtensionError,
   LimitedSet,
   MessageManagerProxy,
 };