Bug 701527 - Add doorhanger prompts for indexedDB permissions [r=mfinkle]
authorMatt Brubeck <mbrubeck@mozilla.com>
Tue, 15 Nov 2011 14:45:10 -0800
changeset 81846 5675736cb251d79bc810bbea6346bffa9226621e
parent 81845 756aaa4cb22dadb765859fd59407c5fb19d3eeee
child 81847 144775136472697f5b2485496211ea575f8f6d6e
push idunknown
push userunknown
push dateunknown
reviewersmfinkle
bugs701527
milestone11.0a1
Bug 701527 - Add doorhanger prompts for indexedDB permissions [r=mfinkle]
mobile/chrome/content/browser.js
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -183,16 +183,17 @@ var BrowserApp = {
 
     // When a restricted key is pressed in DOM full-screen mode, we should display
     // the "Press ESC to exit" warning message.
     window.addEventListener("MozShowFullScreenWarning", showFullScreenWarning, true);
 
     NativeWindow.init();
     Downloads.init();
     OfflineApps.init();
+    IndexedDB.init();
 
     // Init LoginManager
     Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
 
     let uri = "about:support";
     if ("arguments" in window && window.arguments[0])
       uri = window.arguments[0];
 
@@ -242,16 +243,17 @@ var BrowserApp = {
       NativeWindow.doorhanger.show(message, "telemetry-optin", buttons);
     }
 
   },
 
   shutdown: function shutdown() {
     NativeWindow.uninit();
     OfflineApps.uninit();
+    IndexedDB.uninit();
 
     Services.obs.removeObserver(XPInstallObserver, "addon-install-blocked");
     Services.obs.removeObserver(XPInstallObserver, "addon-install-started");
   },
 
   get tabs() {
     return this._tabs;
   },
@@ -2232,8 +2234,89 @@ var OfflineApps = {
     if (!manifest)
       return;
 
     let manifestURI = Services.io.newURI(manifest, aDocument.characterSet, aDocument.documentURIObject);
     let updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"].getService(Ci.nsIOfflineCacheUpdateService);
     updateService.scheduleUpdate(manifestURI, aDocument.documentURIObject, window);
   }
 };
+
+var IndexedDB = {
+  _permissionsPrompt: "indexedDB-permissions-prompt",
+  _permissionsResponse: "indexedDB-permissions-response",
+
+  _quotaPrompt: "indexedDB-quota-prompt",
+  _quotaResponse: "indexedDB-quota-response",
+  _quotaCancel: "indexedDB-quota-cancel",
+
+  init: function IndexedDB_init() {
+    Services.obs.addObserver(this, this._permissionsPrompt, false);
+    Services.obs.addObserver(this, this._quotaPrompt, false);
+    Services.obs.addObserver(this, this._quotaCancel, false);
+  },
+
+  uninit: function IndexedDB_uninit() {
+    Services.obs.removeObserver(this, this._permissionsPrompt, false);
+    Services.obs.removeObserver(this, this._quotaPrompt, false);
+    Services.obs.removeObserver(this, this._quotaCancel, false);
+  },
+
+  observe: function IndexedDB_observe(subject, topic, data) {
+    if (topic != this._permissionsPrompt &&
+        topic != this._quotaPrompt &&
+        topic != this._quotaCancel) {
+      throw new Error("Unexpected topic!");
+    }
+
+    let requestor = subject.QueryInterface(Ci.nsIInterfaceRequestor);
+
+    let contentWindow = requestor.getInterface(Ci.nsIDOMWindow);
+    let contentDocument = contentWindow.document;
+    let browser = BrowserApp.getBrowserForWindow(contentWindow);
+    if (!browser)
+      return;
+
+    let host = contentDocument.documentURIObject.asciiHost;
+
+    let strings = Strings.browser;
+
+    let message, responseTopic;
+    if (topic == this._permissionsPrompt) {
+      message = strings.formatStringFromName("offlineApps.available2", [host], 1);
+      responseTopic = this._permissionsResponse;
+    } else if (topic == this._quotaPrompt) {
+      message = strings.formatStringFromName("indexedDBQuota.wantsTo", [ host, data ], 2);
+      responseTopic = this._quotaResponse;
+    } else if (topic == this._quotaCancel) {
+      responseTopic = this._quotaResponse;
+    }
+
+    let notificationID = responseTopic + host;
+
+    let observer = requestor.getInterface(Ci.nsIObserver);
+    let buttons = [{
+      label: strings.GetStringFromName("offlineApps.allow"),
+      callback: function() {
+        observer.observe(null, responseTopic, Ci.nsIPermissionManager.ALLOW_ACTION);
+      }
+    },
+    {
+      label: strings.GetStringFromName("offlineApps.never"),
+      callback: function() {
+        observer.observe(null, responseTopic, Ci.nsIPermissionManager.DENY_ACTION);
+      }
+    },
+    {
+      label: strings.GetStringFromName("offlineApps.notNow"),
+      callback: function() {
+        observer.observe(null, responseTopic, Ci.nsIPermissionManager.UNKNOWN_ACTION);
+      }
+    }];
+
+    if (topic == this._quotaCancel) {
+      // TODO: Hide the doorhanger
+      return;
+    }
+    let tab = BrowserApp.getTabForBrowser(browser);
+    NativeWindow.doorhanger.show(message, notificationID, buttons, tab.id);
+  }
+};