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
24
registerWindow(aWindow) {
25
let manifestURI = this._getManifestURI(aWindow);
26
this._docManifestSet.add(manifestURI.spec);
27
}
28
29
handleEvent(event) {
30
if (event.type == "MozApplicationManifest") {
31
this.registerWindow(event.originalTarget.defaultView);
32
}
33
}
34
35
_getManifestURI(aWindow) {
36
if (!aWindow.document.documentElement) {
37
return null;
38
}
39
40
var attr = aWindow.document.documentElement.getAttribute("manifest");
41
if (!attr) {
42
return null;
43
}
44
45
try {
46
return Services.io.newURI(
47
attr,
48
aWindow.document.characterSet,
49
Services.io.newURI(aWindow.location.href)
50
);
51
} catch (e) {
52
return null;
53
}
54
}
55
56
_startFetching(aDocument) {
57
if (!aDocument.documentElement) {
58
return;
59
}
60
61
let manifestURI = this._getManifestURI(aDocument.defaultView);
62
if (!manifestURI) {
63
return;
64
}
65
66
var updateService = Cc[
67
"@mozilla.org/offlinecacheupdate-service;1"
68
].getService(Ci.nsIOfflineCacheUpdateService);
69
updateService.scheduleUpdate(
70
manifestURI,
71
aDocument.documentURIObject,
72
aDocument.nodePrincipal,
73
aDocument.defaultView
74
);
75
}
76
77
receiveMessage(aMessage) {
78
if (aMessage.name == "OfflineApps:StartFetching") {
79
let doc = this._docIdMap.get(aMessage.data.docId);
80
doc = doc && doc.get();
81
if (doc) {
82
this._startFetching(doc);
83
}
84
this._docIdMap.delete(aMessage.data.docId);
85
}
86
}
87
}
88
89
OfflineAppsChild.prototype.QueryInterface = ChromeUtils.generateQI([
90
Ci.nsIObserver,
91
Ci.nsISupportsWeakReference,
92
]);