|
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 |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /* |
|
6 * Provide supports for Offline Applications |
|
7 */ |
|
8 var OfflineApps = { |
|
9 offlineAppRequested: function(aRequest, aTarget) { |
|
10 if (!Services.prefs.getBoolPref("browser.offline-apps.notify")) |
|
11 return; |
|
12 |
|
13 let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); |
|
14 |
|
15 // don't bother showing UI if the user has already made a decision |
|
16 if (Services.perms.testExactPermission(currentURI, "offline-app") != Ci.nsIPermissionManager.UNKNOWN_ACTION) |
|
17 return; |
|
18 |
|
19 try { |
|
20 if (Services.prefs.getBoolPref("offline-apps.allow_by_default")) { |
|
21 // all pages can use offline capabilities, no need to ask the user |
|
22 return; |
|
23 } |
|
24 } catch(e) { |
|
25 // this pref isn't set by default, ignore failures |
|
26 } |
|
27 |
|
28 let host = currentURI.asciiHost; |
|
29 let notificationID = "offline-app-requested-" + host; |
|
30 let notificationBox = Browser.getNotificationBox(aTarget); |
|
31 |
|
32 let notification = notificationBox.getNotificationWithValue(notificationID); |
|
33 let strings = Strings.browser; |
|
34 if (notification) { |
|
35 notification.documents.push(aRequest); |
|
36 } else { |
|
37 let buttons = [{ |
|
38 label: strings.GetStringFromName("offlineApps.allow"), |
|
39 accessKey: "", |
|
40 callback: function() { |
|
41 for (let i = 0; i < notification.documents.length; i++) |
|
42 OfflineApps.allowSite(notification.documents[i], aTarget); |
|
43 } |
|
44 },{ |
|
45 label: strings.GetStringFromName("contentPermissions.neverForSite"), |
|
46 accessKey: "", |
|
47 callback: function() { |
|
48 for (let i = 0; i < notification.documents.length; i++) |
|
49 OfflineApps.disallowSite(notification.documents[i]); |
|
50 } |
|
51 }]; |
|
52 |
|
53 const priority = notificationBox.PRIORITY_INFO_LOW; |
|
54 let message = strings.formatStringFromName("offlineApps.wantsTo", [host], 1); |
|
55 notification = notificationBox.appendNotification(message, notificationID, "", priority, buttons); |
|
56 notification.documents = [aRequest]; |
|
57 } |
|
58 }, |
|
59 |
|
60 allowSite: function(aRequest, aTarget) { |
|
61 let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); |
|
62 Services.perms.add(currentURI, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION); |
|
63 |
|
64 // When a site is enabled while loading, manifest resources will start |
|
65 // fetching immediately. This one time we need to do it ourselves. |
|
66 // The update must be started on the content process. |
|
67 aTarget.messageManager.sendAsyncMessage("Browser:MozApplicationCache:Fetch", aRequest); |
|
68 }, |
|
69 |
|
70 disallowSite: function(aRequest) { |
|
71 let currentURI = Services.io.newURI(aRequest.location, aRequest.charset, null); |
|
72 Services.perms.add(currentURI, "offline-app", Ci.nsIPermissionManager.DENY_ACTION); |
|
73 }, |
|
74 |
|
75 receiveMessage: function receiveMessage(aMessage) { |
|
76 if (aMessage.name == "Browser:MozApplicationManifest") { |
|
77 this.offlineAppRequested(aMessage.json, aMessage.target); |
|
78 } |
|
79 } |
|
80 }; |
|
81 |