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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var OfflineApps = { michael@0: offlineAppRequested: function(aContentWindow) { michael@0: if (!Services.prefs.getBoolPref("browser.offline-apps.notify")) michael@0: return; michael@0: michael@0: let tab = BrowserApp.getTabForWindow(aContentWindow); michael@0: let currentURI = aContentWindow.document.documentURIObject; 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") != Services.perms.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: michael@0: let strings = Strings.browser; michael@0: let buttons = [{ michael@0: label: strings.GetStringFromName("offlineApps.allow"), michael@0: callback: function() { michael@0: OfflineApps.allowSite(aContentWindow.document); michael@0: } michael@0: }, michael@0: { michael@0: label: strings.GetStringFromName("offlineApps.dontAllow2"), michael@0: callback: function(aChecked) { michael@0: if (aChecked) michael@0: OfflineApps.disallowSite(aContentWindow.document); michael@0: } michael@0: }]; michael@0: michael@0: let requestor = BrowserApp.manifest ? "'" + BrowserApp.manifest.name + "'" : host; michael@0: let message = strings.formatStringFromName("offlineApps.ask", [requestor], 1); michael@0: let options = { checkbox: Strings.browser.GetStringFromName("offlineApps.dontAskAgain") }; michael@0: NativeWindow.doorhanger.show(message, notificationID, buttons, tab.id, options); michael@0: }, michael@0: michael@0: allowSite: function(aDocument) { michael@0: Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.ALLOW_ACTION); michael@0: michael@0: // When a site is enabled while loading, manifest resources will michael@0: // start fetching immediately. This one time we need to do it michael@0: // ourselves. michael@0: this._startFetching(aDocument); michael@0: }, michael@0: michael@0: disallowSite: function(aDocument) { michael@0: Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.DENY_ACTION); michael@0: }, michael@0: michael@0: _startFetching: function(aDocument) { michael@0: if (!aDocument.documentElement) michael@0: return; michael@0: michael@0: let manifest = aDocument.documentElement.getAttribute("manifest"); michael@0: if (!manifest) michael@0: return; michael@0: michael@0: let manifestURI = Services.io.newURI(manifest, aDocument.characterSet, aDocument.documentURIObject); michael@0: let updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"].getService(Ci.nsIOfflineCacheUpdateService); michael@0: updateService.scheduleUpdate(manifestURI, aDocument.documentURIObject, window); michael@0: } michael@0: };