-
Notifications
You must be signed in to change notification settings - Fork 20
/
content.jsm
153 lines (149 loc) · 4.66 KB
/
content.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var EXPORTED_SYMBOLS = ["PrivateTabContent"];
Components.utils.import("resource://gre/modules/Services.jsm");
__defineGetter__.call(this, "_log", function() {
delete this._log;
Services.scriptloader.loadSubScript("chrome://privatetab/content/log.js");
return _log;
});
function PrivateTabContent(frameGlobal) {
this.fg = frameGlobal;
this.init();
}
PrivateTabContent.prototype = {
instances: { // For global counter
count: 0
},
get privacyContext() {
return this.fg.docShell
.QueryInterface(Components.interfaces.nsILoadContext);
},
get isPrivate() {
return this.privacyContext.usePrivateBrowsing;
},
set isPrivate(isPrivate) {
this.privacyContext.usePrivateBrowsing = isPrivate;
},
init: function() {
++this.instances.count;
this.fg.addEventListener("unload", this, false);
this.fg.addMessageListener("PrivateTab:Action", this);
this.fg.addMessageListener("SessionStore:restoreHistory", this);
},
destroy: function(force) {
_log && _dbgv && _log(
"[frame script] destroy(" + (force || "") + "), instances: " + this.instances.count
+ ", current: " + this.fg.content.location.href.substr(0, 255)
);
this.fg.removeEventListener("unload", this, false);
this.fg.removeMessageListener("PrivateTab:Action", this);
this.fg.removeMessageListener("SessionStore:restoreHistory", this);
this.fg = null;
if(--this.instances.count == 0 && force) {
_log("[frame script] unload content.jsm");
Components.utils.unload("chrome://privatetab/content/content.jsm");
}
},
handleEvent: function(e) {
if(e.type == "unload" && e.target == this.fg)
this.destroy();
},
receiveMessage: function(msg) {
switch(msg.name) {
case "PrivateTab:Action": this.handleActionMessage(msg.data); break;
case "SessionStore:restoreHistory": this.handleSessionRestoring(msg.data);
}
},
handleActionMessage: function(data) {
switch(data.action) {
case "GetState":
this.fg.sendAsyncMessage("PrivateTab:PrivateState", { isPrivate: this.isPrivate });
break;
case "ToggleState":
this.togglePrivate(data.isPrivate, data.silent || false);
break;
case "WaitLoading":
this.waitLoading();
break;
case "GetImageDocumentDataURL":
this.getImageDocumentDataURL();
break;
case "Destroy":
this.destroy(true);
}
},
handleSessionRestoring: function(data) {
var tabData = data.tabData;
var isPrivate = tabData && tabData.attributes && "privateTab-isPrivate" in tabData.attributes || false;
if(isPrivate == this.isPrivate)
_log("[frame script] handleSessionRestoring(): private state is already " + isPrivate);
else {
_log("[frame script] handleSessionRestoring(): private state -> " + isPrivate);
this.isPrivate = isPrivate;
}
},
togglePrivate: function(isPrivate, silent) {
var needChange = true;
if(isPrivate === undefined)
isPrivate = !this.isPrivate;
else if(isPrivate == this.isPrivate) // Nothing to do
needChange = false;
if(needChange)
this.isPrivate = isPrivate;
!silent && this.fg.sendAsyncMessage("PrivateTab:PrivateChanged", {
isPrivate: isPrivate,
reallyChanged: needChange
});
},
waitLoading: function() {
var fg = this.fg;
function feedback() {
fg.sendAsyncMessage("PrivateTab:ContentLoaded", {
principal: fg.content.document.nodePrincipal
});
}
var webProgress = fg.docShell.QueryInterface(Components.interfaces.nsIWebProgress);
if(!webProgress.isLoadingDocument) {
feedback();
return;
}
fg.addEventListener("load", function onLoad(e) {
if(e.target == fg.content.document) {
fg.removeEventListener("load", onLoad, true);
feedback();
}
}, true);
},
getImageDocumentDataURL: function() {
var data = "";
var doc = this.fg.content.document;
var isImageDoc = doc instanceof Components.interfaces.nsIImageDocument;
if(isImageDoc) {
var req = doc.imageRequest;
var image = req && req.image;
try {
var maxSize = Services.prefs.getIntPref("browser.chrome.image_icons.max_size");
}
catch(e) {
Components.utils.reportError(e);
maxSize = 1024;
}
if(image && image.width <= maxSize && image.height <= maxSize) {
var img = doc.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "img")[0];
var canvas = doc.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
data = canvas.toDataURL();
_log("[frame script] getImageDocumentDataURL() => data:");
}
else {
_log("[frame script] getImageDocumentDataURL(): image missing or too large");
}
}
this.fg.sendAsyncMessage("PrivateTab:ImageDocumentDataURL", {
isImageDocument: isImageDoc,
dataURL: data
});
}
};