Source code

Revision control

Other Tools

1
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
2
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
var EXPORTED_SYMBOLS = ["OfflineAppsChild"];
8
9
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
10
const { ActorChild } = ChromeUtils.import(
12
);
13
14
class OfflineAppsChild extends ActorChild {
15
constructor(dispatcher) {
16
super(dispatcher);
17
18
this._docId = 0;
19
this._docIdMap = new Map();
20
21
this._docManifestSet = new Set();
22
23
this._observerAdded = false;
24
}
25
26
registerWindow(aWindow) {
27
if (!this._observerAdded) {
28
this._observerAdded = true;
29
Services.obs.addObserver(this, "offline-cache-update-completed", true);
30
}
31
let manifestURI = this._getManifestURI(aWindow);
32
this._docManifestSet.add(manifestURI.spec);
33
}
34
35
handleEvent(event) {
36
if (event.type == "MozApplicationManifest") {
37
this.offlineAppRequested(event.originalTarget.defaultView);
38
}
39
}
40
41
_getManifestURI(aWindow) {
42
if (!aWindow.document.documentElement) {
43
return null;
44
}
45
46
var attr = aWindow.document.documentElement.getAttribute("manifest");
47
if (!attr) {
48
return null;
49
}
50
51
try {
52
return Services.io.newURI(
53
attr,
54
aWindow.document.characterSet,
55
Services.io.newURI(aWindow.location.href)
56
);
57
} catch (e) {
58
return null;
59
}
60
}
61
62
offlineAppRequested(aContentWindow) {
63
this.registerWindow(aContentWindow);
64
if (!Services.prefs.getBoolPref("browser.offline-apps.notify")) {
65
return;
66
}
67
68
let currentURI = aContentWindow.document.documentURIObject;
69
// don't bother showing UI if the user has already made a decision
70
if (
71
Services.perms.testExactPermission(currentURI, "offline-app") !=
72
Services.perms.UNKNOWN_ACTION
73
) {
74
return;
75
}
76
77
try {
78
if (Services.prefs.getBoolPref("offline-apps.allow_by_default")) {
79
// all pages can use offline capabilities, no need to ask the user
80
return;
81
}
82
} catch (e) {
83
// this pref isn't set by default, ignore failures
84
}
85
let docId = ++this._docId;
86
this._docIdMap.set(docId, Cu.getWeakReference(aContentWindow.document));
87
this.mm.sendAsyncMessage("OfflineApps:RequestPermission", {
88
uri: currentURI.spec,
89
docId,
90
});
91
}
92
93
_startFetching(aDocument) {
94
if (!aDocument.documentElement) {
95
return;
96
}
97
98
let manifestURI = this._getManifestURI(aDocument.defaultView);
99
if (!manifestURI) {
100
return;
101
}
102
103
var updateService = Cc[
104
"@mozilla.org/offlinecacheupdate-service;1"
105
].getService(Ci.nsIOfflineCacheUpdateService);
106
updateService.scheduleUpdate(
107
manifestURI,
108
aDocument.documentURIObject,
109
aDocument.nodePrincipal,
110
aDocument.defaultView
111
);
112
}
113
114
receiveMessage(aMessage) {
115
if (aMessage.name == "OfflineApps:StartFetching") {
116
let doc = this._docIdMap.get(aMessage.data.docId);
117
doc = doc && doc.get();
118
if (doc) {
119
this._startFetching(doc);
120
}
121
this._docIdMap.delete(aMessage.data.docId);
122
}
123
}
124
125
observe(aSubject, aTopic, aState) {
126
if (aTopic == "offline-cache-update-completed") {
127
let cacheUpdate = aSubject.QueryInterface(Ci.nsIOfflineCacheUpdate);
128
let uri = cacheUpdate.manifestURI;
129
if (uri && this._docManifestSet.has(uri.spec)) {
130
this.mm.sendAsyncMessage("OfflineApps:CheckUsage", { uri: uri.spec });
131
}
132
}
133
}
134
}
135
136
OfflineAppsChild.prototype.QueryInterface = ChromeUtils.generateQI([
137
Ci.nsIObserver,
138
Ci.nsISupportsWeakReference,
139
]);