Bug 1381590 - Make Storage Inspector work with file:// URLs r?pbro draft
authorMike Ratcliffe <mratcliffe@mozilla.com>
Mon, 12 Mar 2018 17:37:49 +0000
changeset 767878 72dcd3837e51864f66556572f62f4d3e1866fe25
parent 767726 0d81c80876dd09536f78d1158e1e6ff78f9ad226
push id102725
push userbmo:mratcliffe@mozilla.com
push dateThu, 15 Mar 2018 08:19:16 +0000
reviewerspbro
bugs1381590
milestone61.0a1
Bug 1381590 - Make Storage Inspector work with file:// URLs r?pbro MozReview-Commit-ID: I8QPDaovalG
devtools/client/storage/test/browser.ini
devtools/client/storage/test/browser_storage_file_url.js
devtools/client/storage/test/storage-file-url.html
devtools/server/actors/storage.js
--- a/devtools/client/storage/test/browser.ini
+++ b/devtools/client/storage/test/browser.ini
@@ -3,16 +3,17 @@ tags = devtools
 subsuite = devtools
 support-files =
   storage-blank.html
   storage-cache-error.html
   storage-complex-values.html
   storage-cookies.html
   storage-cookies-samesite.html
   storage-empty-objectstores.html
+  storage-file-url.html
   storage-idb-delete-blocked.html
   storage-indexeddb-duplicate-names.html
   storage-listings.html
   storage-listings-usercontextid.html
   storage-listings-with-fragment.html
   storage-localstorage.html
   storage-overflow.html
   storage-search.html
@@ -45,16 +46,17 @@ tags = usercontextid
 [browser_storage_delete_tree.js]
 [browser_storage_delete_usercontextid.js]
 tags = usercontextid
 [browser_storage_dom_cache_disabled.js]
 [browser_storage_dynamic_updates_cookies.js]
 [browser_storage_dynamic_updates_localStorage.js]
 [browser_storage_dynamic_updates_sessionStorage.js]
 [browser_storage_empty_objectstores.js]
+[browser_storage_file_url.js]
 [browser_storage_indexeddb_delete.js]
 [browser_storage_indexeddb_delete_blocked.js]
 [browser_storage_indexeddb_duplicate_names.js]
 [browser_storage_localstorage_add.js]
 [browser_storage_localstorage_edit.js]
 [browser_storage_localstorage_error.js]
 [browser_storage_localstorage_rapid_add_remove.js]
 [browser_storage_overflow.js]
new file mode 100644
--- /dev/null
+++ b/devtools/client/storage/test/browser_storage_file_url.js
@@ -0,0 +1,64 @@
+/* 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/. */
+
+/* import-globals-from head.js */
+
+// Test to verify that various storage types work when using file:// URLs.
+
+"use strict";
+
+add_task(function* () {
+  const TESTPAGE = "storage-file-url.html";
+
+  // We need to load TESTPAGE using a file:// path so we need to get that from
+  // the current test path.
+  const testPath = getResolvedURI(gTestPath);
+  let dir = getChromeDir(testPath);
+
+  // Then append TESTPAGE to the test path.
+  dir.append(TESTPAGE);
+
+  // Then generate a FileURI to ensure the path is valid.
+  const uriString = Services.io.newFileURI(dir).spec;
+
+  // Now we have a valid file:// URL pointing to TESTPAGE.
+  yield openTabAndSetupStorage(uriString);
+
+  // uriString points to the test inside objdir e.g.
+  // `/path/to/fx/objDir/_tests/testing/mochitest/browser/devtools/client/
+  // storage/test/storage-file-url.html`.
+  //
+  // When opened in the browser this may resolve to a different path e.g.
+  // `path/to/fx/repo/devtools/client/storage/test/storage-file-url.html`.
+  //
+  // The easiest way to get the actual path is to request it from the content
+  // process.
+  let browser = gBrowser.selectedBrowser;
+  let actualPath = yield ContentTask.spawn(browser, null, () => {
+    return content.document.location.href;
+  });
+
+  const cookiePath = actualPath.substr(0, actualPath.lastIndexOf("/") + 1)
+                               .replace(/file:\/\//g, "");
+  yield checkState([
+    [
+      ["cookies", actualPath],
+      [
+        getCookieId("test1", "", cookiePath),
+        getCookieId("test2", "", cookiePath)
+      ]
+    ], [
+      ["indexedDB", actualPath, "MyDatabase (default)", "MyObjectStore"],
+      [12345, 54321, 67890, 98765]
+    ], [
+      ["localStorage", actualPath],
+      ["test3", "test4"]
+    ], [
+      ["sessionStorage", actualPath],
+      ["test5", "test6"]
+    ]
+  ]);
+
+  yield finishTests();
+});
new file mode 100644
--- /dev/null
+++ b/devtools/client/storage/test/storage-file-url.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8" />
+  <title>Storage Test</title>
+  <script>
+    function init() {
+      createIndexedDB();
+      createCookies();
+      createLocalStorage();
+      createSessionStorage();
+    }
+
+    function createIndexedDB() {
+      var open = indexedDB.open("MyDatabase", 1);
+
+      open.onupgradeneeded = function () {
+        var db = open.result;
+        var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
+        var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
+      };
+
+      open.onsuccess = function () {
+        var db = open.result;
+        var tx = db.transaction("MyObjectStore", "readwrite");
+        var store = tx.objectStore("MyObjectStore");
+        var index = store.index("NameIndex");
+
+        store.put({id: 12345, name: {first: "John", last: "Doe"}, age: 42});
+        store.put({id: 54321, name: {first: "Ralph", last: "Wood"}, age: 38});
+        store.put({id: 67890, name: {first: "Bob", last: "Smith"}, age: 35});
+        store.put({id: 98765, name: {first: "Freddie", last: "Krueger"}, age: 40});
+
+        tx.oncomplete = function () {
+          db.close();
+        };
+      };
+    }
+
+    function createCookies() {
+      document.cookie = "test1=Jean Dupond";
+      document.cookie = "test2=dnopuD naeJ";
+    }
+
+    function createLocalStorage() {
+      localStorage.setItem("test3", "John Doe");
+      localStorage.setItem("test4", "eoD nhoJ");
+    }
+
+    function createSessionStorage() {
+      sessionStorage.setItem("test5", "John Smith");
+      sessionStorage.setItem("test6", "htimS nhoJ");
+    }
+  </script>
+</head>
+<body onload="init();">
+  <h1>IndexedDB Test</h1>
+</body>
+</html>
--- a/devtools/server/actors/storage.js
+++ b/devtools/server/actors/storage.js
@@ -162,21 +162,21 @@ StorageActors.defaults = function(typeNa
         return null;
       }
 
       switch (location.protocol) {
         case "data:":
           // data: URLs do not support storage of any type.
           return null;
         case "about:":
-          // Fallthrough.
+          return location.protocol + location.pathname;
         case "chrome:":
-          // Fallthrough.
+          return location.protocol + location.pathname;
         case "file:":
-          return location.protocol + location.pathname;
+          return `${location.protocol}//${location.pathname}`;
         case "resource:":
           return location.origin + location.pathname;
         case "moz-extension:":
           return location.origin;
         case "javascript:":
           return location.href;
         default:
           // http: or unknown protocol.