|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 var OfflineApps = { |
|
7 offlineAppRequested: function(aContentWindow) { |
|
8 if (!Services.prefs.getBoolPref("browser.offline-apps.notify")) |
|
9 return; |
|
10 |
|
11 let tab = BrowserApp.getTabForWindow(aContentWindow); |
|
12 let currentURI = aContentWindow.document.documentURIObject; |
|
13 |
|
14 // Don't bother showing UI if the user has already made a decision |
|
15 if (Services.perms.testExactPermission(currentURI, "offline-app") != Services.perms.UNKNOWN_ACTION) |
|
16 return; |
|
17 |
|
18 try { |
|
19 if (Services.prefs.getBoolPref("offline-apps.allow_by_default")) { |
|
20 // All pages can use offline capabilities, no need to ask the user |
|
21 return; |
|
22 } |
|
23 } catch(e) { |
|
24 // This pref isn't set by default, ignore failures |
|
25 } |
|
26 |
|
27 let host = currentURI.asciiHost; |
|
28 let notificationID = "offline-app-requested-" + host; |
|
29 |
|
30 let strings = Strings.browser; |
|
31 let buttons = [{ |
|
32 label: strings.GetStringFromName("offlineApps.allow"), |
|
33 callback: function() { |
|
34 OfflineApps.allowSite(aContentWindow.document); |
|
35 } |
|
36 }, |
|
37 { |
|
38 label: strings.GetStringFromName("offlineApps.dontAllow2"), |
|
39 callback: function(aChecked) { |
|
40 if (aChecked) |
|
41 OfflineApps.disallowSite(aContentWindow.document); |
|
42 } |
|
43 }]; |
|
44 |
|
45 let requestor = BrowserApp.manifest ? "'" + BrowserApp.manifest.name + "'" : host; |
|
46 let message = strings.formatStringFromName("offlineApps.ask", [requestor], 1); |
|
47 let options = { checkbox: Strings.browser.GetStringFromName("offlineApps.dontAskAgain") }; |
|
48 NativeWindow.doorhanger.show(message, notificationID, buttons, tab.id, options); |
|
49 }, |
|
50 |
|
51 allowSite: function(aDocument) { |
|
52 Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.ALLOW_ACTION); |
|
53 |
|
54 // When a site is enabled while loading, manifest resources will |
|
55 // start fetching immediately. This one time we need to do it |
|
56 // ourselves. |
|
57 this._startFetching(aDocument); |
|
58 }, |
|
59 |
|
60 disallowSite: function(aDocument) { |
|
61 Services.perms.add(aDocument.documentURIObject, "offline-app", Services.perms.DENY_ACTION); |
|
62 }, |
|
63 |
|
64 _startFetching: function(aDocument) { |
|
65 if (!aDocument.documentElement) |
|
66 return; |
|
67 |
|
68 let manifest = aDocument.documentElement.getAttribute("manifest"); |
|
69 if (!manifest) |
|
70 return; |
|
71 |
|
72 let manifestURI = Services.io.newURI(manifest, aDocument.characterSet, aDocument.documentURIObject); |
|
73 let updateService = Cc["@mozilla.org/offlinecacheupdate-service;1"].getService(Ci.nsIOfflineCacheUpdateService); |
|
74 updateService.scheduleUpdate(manifestURI, aDocument.documentURIObject, window); |
|
75 } |
|
76 }; |