Bug 748214 - Cannot use the Geolocation API within the desktop web runtime [r=myk]
authorEdward Lee <edilee@mozilla.com>
Thu, 31 May 2012 10:04:52 -0700
changeset 99483 9265be2869cabd5b4a9ac94ae53be8e1778e424d
parent 99482 4b6e98c0317bd2ed4e1594dc7ed24413810df3d7
child 99484 0a4417ac4889c92ca712d02f4e9f02f77df140fb
push idunknown
push userunknown
push dateunknown
reviewersmyk
bugs748214
milestone15.0a1
Bug 748214 - Cannot use the Geolocation API within the desktop web runtime [r=myk] Show a prompt asking the user to share or not geolocation info with the option to remember.
browser/installer/package-manifest.in
browser/locales/en-US/webapprt/webapp.properties
webapprt/ContentPermission.js
webapprt/Makefile.in
webapprt/components.manifest
--- a/browser/installer/package-manifest.in
+++ b/browser/installer/package-manifest.in
@@ -642,14 +642,15 @@ bin/components/@DLL_PREFIX@nkgnomevfs@DL
 @BINPATH@/webapp-uninstaller@BIN_SUFFIX@
 #endif
 @BINPATH@/webapprt-stub@BIN_SUFFIX@
 @BINPATH@/webapprt/webapprt.ini
 @BINPATH@/webapprt/chrome.manifest
 @BINPATH@/webapprt/chrome/webapprt@JAREXT@
 @BINPATH@/webapprt/chrome/webapprt.manifest
 @BINPATH@/webapprt/components/CommandLineHandler.js
+@BINPATH@/webapprt/components/ContentPermission.js
 @BINPATH@/webapprt/components/ContentPolicy.js
 @BINPATH@/webapprt/components/DirectoryProvider.js
 @BINPATH@/webapprt/components/components.manifest
 @BINPATH@/webapprt/defaults/preferences/prefs.js
 @BINPATH@/webapprt/modules/WebappRT.jsm
 #endif
--- a/browser/locales/en-US/webapprt/webapp.properties
+++ b/browser/locales/en-US/webapprt/webapp.properties
@@ -10,8 +10,16 @@
 
 # LOCALIZATION NOTE (quitApplicationCmdMac.label): %S will be replaced with
 # the name of the webapp.
 quitApplicationCmdMac.label=Quit %S
 
 # LOCALIZATION NOTE (hideApplicationCmdMac.label): %S will be replaced with
 # the name of the webapp.
 hideApplicationCmdMac.label=Hide %S
+
+# LOCALIZATION NOTE (geolocation.title): %S will be replaced with the name of
+# the webapp.
+geolocation.title=%S - Share Location
+geolocation.description=Do you want to share your location?
+geolocation.sharelocation=Share Location
+geolocation.dontshare=Don't Share
+geolocation.remember=Remember my choice
new file mode 100644
--- /dev/null
+++ b/webapprt/ContentPermission.js
@@ -0,0 +1,89 @@
+/* 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/. */
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://webapprt/modules/WebappRT.jsm");
+
+function ContentPermission() {}
+
+ContentPermission.prototype = {
+  classID: Components.ID("{07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPermissionPrompt]),
+
+  prompt: function(request) {
+    // Only handle geolocation requests for now
+    if (request.type != "geolocation") {
+      return;
+    }
+
+    // Reuse any remembered permission preferences
+    let result = Services.perms.testExactPermission(request.uri, "geo");
+    if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
+      request.allow();
+      return;
+    }
+    else if (result == Ci.nsIPermissionManager.DENY_ACTION) {
+      request.cancel();
+      return;
+    }
+
+    function getChromeWindow(aWindow) {
+      var chromeWin = aWindow
+        .QueryInterface(Ci.nsIInterfaceRequestor)
+        .getInterface(Ci.nsIWebNavigation)
+        .QueryInterface(Ci.nsIDocShellTreeItem)
+        .rootTreeItem
+        .QueryInterface(Ci.nsIInterfaceRequestor)
+        .getInterface(Ci.nsIDOMWindow)
+        .QueryInterface(Ci.nsIDOMChromeWindow);
+      return chromeWin;
+    }
+
+    // Display a prompt at the top level
+    let {name} = WebappRT.config.app.manifest;
+    let requestingWindow = request.window.top;
+    let chromeWin = getChromeWindow(requestingWindow);
+    let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
+
+    // Construct a prompt with share/don't and remember checkbox
+    let remember = {value: false};
+    let choice = Services.prompt.confirmEx(
+      chromeWin,
+      bundle.formatStringFromName("geolocation.title", [name], 1),
+      bundle.GetStringFromName("geolocation.description"),
+      // Set both buttons to strings with the cancel button being default
+      Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
+        Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
+        Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
+      bundle.GetStringFromName("geolocation.sharelocation"),
+      bundle.GetStringFromName("geolocation.dontshare"),
+      null,
+      bundle.GetStringFromName("geolocation.remember"),
+      remember);
+
+    // Persist the choice if the user wants to remember
+    if (remember.value) {
+      let action = Ci.nsIPermissionManager.ALLOW_ACTION;
+      if (choice != 0) {
+        action = Ci.nsIPermissionManager.DENY_ACTION;
+      }
+      Services.perms.add(request.uri, "geo", action);
+    }
+
+    // Trigger the selected choice
+    if (choice == 0) {
+      request.allow();
+    }
+    else {
+      request.cancel();
+    }
+  }
+};
+
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentPermission]);
--- a/webapprt/Makefile.in
+++ b/webapprt/Makefile.in
@@ -24,16 +24,17 @@ else
 ifeq ($(OS_ARCH),Darwin)
 DIRS += mac
 endif # mac
 endif # windows
 
 EXTRA_PP_COMPONENTS = \
   components.manifest \
   CommandLineHandler.js \
+  ContentPermission.js \
   ContentPolicy.js \
   DirectoryProvider.js \
   $(NULL)
 
 EXTRA_JS_MODULES = \
   WebappRT.jsm \
   $(NULL)
 
--- a/webapprt/components.manifest
+++ b/webapprt/components.manifest
@@ -1,13 +1,17 @@
 # CommandLineHandler.js
 component {6d69c782-40a3-469b-8bfd-3ee366105a4a} CommandLineHandler.js application=webapprt@mozilla.org
 contract @mozilla.org/webapprt/clh;1 {6d69c782-40a3-469b-8bfd-3ee366105a4a} application=webapprt@mozilla.org
 category command-line-handler x-default @mozilla.org/webapprt/clh;1 application=webapprt@mozilla.org
 
+# ContentPermission.js
+component {07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4} ContentPermission.js
+contract @mozilla.org/content-permission/prompt;1 {07ef5b2e-88fb-47bd-8cec-d3b0bef11ac4}
+
 # ContentPolicy.js
 component {75acd178-3d5a-48a7-bd92-fba383520ae6} ContentPolicy.js application=webapprt@mozilla.org
 contract @mozilla.org/webapprt/content-policy;1 {75acd178-3d5a-48a7-bd92-fba383520ae6} application=webapprt@mozilla.org
 category content-policy webapprt-content-policy @mozilla.org/webapprt/content-policy;1 application=webapprt@mozilla.org
 
 # DirectoryProvider.js
 component {e1799fda-4b2f-4457-b671-e0641d95698d} DirectoryProvider.js application=webapprt@mozilla.org
 contract @mozilla.org/webapprt/directory-provider;1 {e1799fda-4b2f-4457-b671-e0641d95698d} application=webapprt@mozilla.org