2009-02-18 12:57:20 -08:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-07-13 07:36:09 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2009-05-29 18:28:12 -07:00
|
|
|
const kCountBeforeWeRemember = 5;
|
|
|
|
|
2009-02-18 12:57:20 -08:00
|
|
|
function GeolocationPrompt() {}
|
|
|
|
|
|
|
|
GeolocationPrompt.prototype = {
|
2010-07-21 16:41:25 -07:00
|
|
|
classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
|
2009-02-18 12:57:20 -08:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationPrompt]),
|
2010-07-21 16:41:25 -07:00
|
|
|
|
|
|
|
prompt: function(aRequest) {
|
|
|
|
let pm = Services.perms;
|
|
|
|
let result = pm.testExactPermission(aRequest.requestingURI, "geo");
|
|
|
|
|
2009-05-29 18:28:12 -07:00
|
|
|
if (result == Ci.nsIPermissionManager.ALLOW_ACTION) {
|
2010-07-21 16:41:25 -07:00
|
|
|
aRequest.allow();
|
2009-05-29 18:28:12 -07:00
|
|
|
return;
|
2010-07-21 16:41:25 -07:00
|
|
|
} else if (result == Ci.nsIPermissionManager.DENY_ACTION) {
|
|
|
|
aRequest.cancel();
|
2009-05-29 18:28:12 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-21 16:41:25 -07:00
|
|
|
function setPagePermission(aUri, aAllow) {
|
|
|
|
let contentPrefs = Services.contentPrefs;
|
|
|
|
|
|
|
|
if (!contentPrefs.hasPref(aRequest.requestingURI, "geo.request.remember"))
|
|
|
|
contentPrefs.setPref(aRequest.requestingURI, "geo.request.remember", 0);
|
|
|
|
|
|
|
|
let count = contentPrefs.getPref(aRequest.requestingURI, "geo.request.remember");
|
|
|
|
|
|
|
|
if (aAllow == false)
|
2009-05-29 18:28:12 -07:00
|
|
|
count--;
|
|
|
|
else
|
|
|
|
count++;
|
|
|
|
|
2010-07-21 16:41:25 -07:00
|
|
|
contentPrefs.setPref(aRequest.requestingURI, "geo.request.remember", count);
|
|
|
|
|
2009-05-29 18:28:12 -07:00
|
|
|
if (count == kCountBeforeWeRemember)
|
2010-07-21 16:41:25 -07:00
|
|
|
pm.add(aUri, "geo", Ci.nsIPermissionManager.ALLOW_ACTION);
|
2009-05-29 18:28:12 -07:00
|
|
|
else if (count == -kCountBeforeWeRemember)
|
2010-07-21 16:41:25 -07:00
|
|
|
pm.add(aUri, "geo", Ci.nsIPermissionManager.DENY_ACTION);
|
2009-05-29 18:28:12 -07:00
|
|
|
}
|
2009-02-18 12:57:20 -08:00
|
|
|
|
|
|
|
function getChromeWindow(aWindow) {
|
2010-07-21 16:41:25 -07:00
|
|
|
let chromeWin = aWindow
|
2009-02-18 12:57:20 -08:00
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShellTreeItem)
|
|
|
|
.rootTreeItem
|
|
|
|
.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindow)
|
|
|
|
.QueryInterface(Ci.nsIDOMChromeWindow);
|
|
|
|
return chromeWin;
|
|
|
|
}
|
|
|
|
|
2010-07-21 16:41:25 -07:00
|
|
|
let notificationBox = null;
|
|
|
|
if (aRequest.requestingWindow) {
|
|
|
|
let requestingWindow = aRequest.requestingWindow.top;
|
|
|
|
let chromeWin = getChromeWindow(requestingWindow).wrappedJSObject;
|
|
|
|
notificationBox = chromeWin.getNotificationBox(requestingWindow);
|
|
|
|
} else {
|
|
|
|
let chromeWin = aRequest.requestingElement.ownerDocument.defaultView;
|
|
|
|
notificationBox = chromeWin.Browser.getNotificationBox();
|
|
|
|
}
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-07-21 16:41:25 -07:00
|
|
|
let notification = notificationBox.getNotificationWithValue("geolocation");
|
2009-02-18 12:57:20 -08:00
|
|
|
if (!notification) {
|
2010-07-21 16:41:25 -07:00
|
|
|
let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
|
2009-02-18 12:57:20 -08:00
|
|
|
|
2010-07-21 16:41:25 -07:00
|
|
|
let buttons = [{
|
2009-05-29 18:28:12 -07:00
|
|
|
label: browserBundle.GetStringFromName("geolocation.share"),
|
2009-10-14 13:36:47 -07:00
|
|
|
accessKey: null,
|
2009-05-29 18:28:12 -07:00
|
|
|
callback: function(notification) {
|
2010-07-21 16:41:25 -07:00
|
|
|
setPagePermission(aRequest.requestingURI, true);
|
|
|
|
aRequest.allow();
|
2009-02-18 12:57:20 -08:00
|
|
|
},
|
2009-05-29 18:28:12 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: browserBundle.GetStringFromName("geolocation.dontShare"),
|
2009-10-14 13:36:47 -07:00
|
|
|
accessKey: null,
|
2009-05-29 18:28:12 -07:00
|
|
|
callback: function(notification) {
|
2010-07-21 16:41:25 -07:00
|
|
|
setPagePermission(aRequest.requestingURI, false);
|
|
|
|
aRequest.cancel();
|
2009-05-29 18:28:12 -07:00
|
|
|
},
|
|
|
|
}];
|
2010-07-21 16:41:25 -07:00
|
|
|
|
|
|
|
let message = browserBundle.formatStringFromName("geolocation.siteWantsToKnow",
|
|
|
|
[aRequest.requestingURI.host], 1);
|
|
|
|
|
|
|
|
let newBar = notificationBox.appendNotification(message,
|
2009-05-29 18:28:12 -07:00
|
|
|
"geolocation",
|
|
|
|
"chrome://browser/skin/images/geo-16.png",
|
|
|
|
notificationBox.PRIORITY_WARNING_MEDIUM,
|
|
|
|
buttons);
|
|
|
|
// Make this a geolocation notification.
|
2010-07-21 16:41:25 -07:00
|
|
|
newBar.setAttribute("type", "geo");
|
2009-02-18 12:57:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//module initialization
|
2010-06-29 14:02:25 -07:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([GeolocationPrompt]);
|