mobile/android/chrome/content/OfflineApps.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/OfflineApps.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var OfflineApps = {
    1.10 +  offlineAppRequested: function(aContentWindow) {
    1.11 +    if (!Services.prefs.getBoolPref("browser.offline-apps.notify"))
    1.12 +      return;
    1.13 +
    1.14 +    let tab = BrowserApp.getTabForWindow(aContentWindow);
    1.15 +    let currentURI = aContentWindow.document.documentURIObject;
    1.16 +
    1.17 +    // Don't bother showing UI if the user has already made a decision
    1.18 +    if (Services.perms.testExactPermission(currentURI, "offline-app") != Services.perms.UNKNOWN_ACTION)
    1.19 +      return;
    1.20 +
    1.21 +    try {
    1.22 +      if (Services.prefs.getBoolPref("offline-apps.allow_by_default")) {
    1.23 +        // All pages can use offline capabilities, no need to ask the user
    1.24 +        return;
    1.25 +      }
    1.26 +    } catch(e) {
    1.27 +      // This pref isn't set by default, ignore failures
    1.28 +    }
    1.29 +
    1.30 +    let host = currentURI.asciiHost;
    1.31 +    let notificationID = "offline-app-requested-" + host;
    1.32 +
    1.33 +    let strings = Strings.browser;
    1.34 +    let buttons = [{
    1.35 +      label: strings.GetStringFromName("offlineApps.allow"),
    1.36 +      callback: function() {
    1.37 +        OfflineApps.allowSite(aContentWindow.document);
    1.38 +      }
    1.39 +    },
    1.40 +    {
    1.41 +      label: strings.GetStringFromName("offlineApps.dontAllow2"),
    1.42 +      callback: function(aChecked) {
    1.43 +        if (aChecked)
    1.44 +          OfflineApps.disallowSite(aContentWindow.document);
    1.45 +      }
    1.46 +    }];
    1.47 +
    1.48 +    let requestor = BrowserApp.manifest ? "'" + BrowserApp.manifest.name + "'" : host;
    1.49 +    let message = strings.formatStringFromName("offlineApps.ask", [requestor], 1);
    1.50 +    let options = { checkbox: Strings.browser.GetStringFromName("offlineApps.dontAskAgain") };
    1.51 +    NativeWindow.doorhanger.show(message, notificationID, buttons, tab.id, options);
    1.52 +  },
    1.53 +
    1.54 +  allowSite: function(aDocument) {
    1.55 +    Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.ALLOW_ACTION);
    1.56 +
    1.57 +    // When a site is enabled while loading, manifest resources will
    1.58 +    // start fetching immediately.  This one time we need to do it
    1.59 +    // ourselves.
    1.60 +    this._startFetching(aDocument);
    1.61 +  },
    1.62 +
    1.63 +  disallowSite: function(aDocument) {
    1.64 +    Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.DENY_ACTION);
    1.65 +  },
    1.66 +
    1.67 +  _startFetching: function(aDocument) {
    1.68 +    if (!aDocument.documentElement)
    1.69 +      return;
    1.70 +
    1.71 +    let manifest = aDocument.documentElement.getAttribute("manifest");
    1.72 +    if (!manifest)
    1.73 +      return;
    1.74 +
    1.75 +    let manifestURI = Services.io.newURI(manifest, aDocument.characterSet, aDocument.documentURIObject);
    1.76 +    let updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"].getService(Ci.nsIOfflineCacheUpdateService);
    1.77 +    updateService.scheduleUpdate(manifestURI, aDocument.documentURIObject, window);
    1.78 +  }
    1.79 +};

mercurial