michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Provide supports for Offline Applications michael@0: */ michael@0: var OfflineApps = { michael@0: offlineAppRequested: function(aRequest, aTarget) { michael@0: if (!Services.prefs.getBoolPref("browser.offline-apps.notify")) michael@0: return; michael@0: michael@0: let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); michael@0: michael@0: // don't bother showing UI if the user has already made a decision michael@0: if (Services.perms.testExactPermission(currentURI, "offline-app") != Ci.nsIPermissionManager.UNKNOWN_ACTION) michael@0: return; michael@0: michael@0: try { michael@0: if (Services.prefs.getBoolPref("offline-apps.allow_by_default")) { michael@0: // all pages can use offline capabilities, no need to ask the user michael@0: return; michael@0: } michael@0: } catch(e) { michael@0: // this pref isn't set by default, ignore failures michael@0: } michael@0: michael@0: let host = currentURI.asciiHost; michael@0: let notificationID = "offline-app-requested-" + host; michael@0: let notificationBox = Browser.getNotificationBox(aTarget); michael@0: michael@0: let notification = notificationBox.getNotificationWithValue(notificationID); michael@0: let strings = Strings.browser; michael@0: if (notification) { michael@0: notification.documents.push(aRequest); michael@0: } else { michael@0: let buttons = [{ michael@0: label: strings.GetStringFromName("offlineApps.allow"), michael@0: accessKey: "", michael@0: callback: function() { michael@0: for (let i = 0; i < notification.documents.length; i++) michael@0: OfflineApps.allowSite(notification.documents[i], aTarget); michael@0: } michael@0: },{ michael@0: label: strings.GetStringFromName("contentPermissions.neverForSite"), michael@0: accessKey: "", michael@0: callback: function() { michael@0: for (let i = 0; i < notification.documents.length; i++) michael@0: OfflineApps.disallowSite(notification.documents[i]); michael@0: } michael@0: }]; michael@0: michael@0: const priority = notificationBox.PRIORITY_INFO_LOW; michael@0: let message = strings.formatStringFromName("offlineApps.wantsTo", [host], 1); michael@0: notification = notificationBox.appendNotification(message, notificationID, "", priority, buttons); michael@0: notification.documents = [aRequest]; michael@0: } michael@0: }, michael@0: michael@0: allowSite: function(aRequest, aTarget) { michael@0: let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); michael@0: Services.perms.add(currentURI, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION); michael@0: michael@0: // When a site is enabled while loading, manifest resources will start michael@0: // fetching immediately. This one time we need to do it ourselves. michael@0: // The update must be started on the content process. michael@0: aTarget.messageManager.sendAsyncMessage("Browser:MozApplicationCache:Fetch", aRequest); michael@0: }, michael@0: michael@0: disallowSite: function(aRequest) { michael@0: let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); michael@0: Services.perms.add(currentURI, "offline-app", Ci.nsIPermissionManager.DENY_ACTION); michael@0: }, michael@0: michael@0: receiveMessage: function receiveMessage(aMessage) { michael@0: if (aMessage.name == "Browser:MozApplicationManifest") { michael@0: this.offlineAppRequested(aMessage.json, aMessage.target); michael@0: } michael@0: } michael@0: }; michael@0: