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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { classes: Cc, interfaces: Ci, manager: Cm, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Timer.jsm");
////////////////////////////////////////////////////////////////////////////////
//// Constants
//// SVG placeholder image for blocked image content
const PLACEHOLDER_IMG = "chrome://browser/skin/images/placeholder_image.svg";
//// Telemetry
const TELEMETRY_TAP_TO_LOAD_ENABLED = "TAP_TO_LOAD_ENABLED";
const TELEMETRY_SHOW_IMAGE_SIZE = "TAP_TO_LOAD_IMAGE_SIZE";
const TOPIC_GATHER_TELEMETRY = "gather-telemetry";
//// Gecko preference
const PREF_IMAGEBLOCKING = "browser.image_blocking";
//// Enabled options
const OPTION_NEVER = 0;
const OPTION_ALWAYS = 1;
const OPTION_WIFI_ONLY = 2;
/**
* Content policy for blocking images
*/
function ImageBlockingPolicy() {
Services.obs.addObserver(this, TOPIC_GATHER_TELEMETRY, false);
}
ImageBlockingPolicy.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver]),
classDescription: "Click-To-Play Image",
classID: Components.ID("{f55f77f9-d33d-4759-82fc-60db3ee0bb91}"),
contractID: "@mozilla.org/browser/blockimages-policy;1",
xpcom_categories: [{category: "content-policy", service: true}],
// nsIContentPolicy interface implementation
shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) {
// When enabled or when on cellular, and option for cellular-only is selected
if (this._enabled() == OPTION_NEVER || (this._enabled() == OPTION_WIFI_ONLY && this._usingCellular())) {
if (contentType === Ci.nsIContentPolicy.TYPE_IMAGE || contentType === Ci.nsIContentPolicy.TYPE_IMAGESET) {
// Accept any non-http(s) image URLs
if (!contentLocation.schemeIs("http") && !contentLocation.schemeIs("https")) {
return Ci.nsIContentPolicy.ACCEPT;
}
if (node instanceof Ci.nsIDOMHTMLImageElement) {
// Accept if the user has asked to view the image
if (node.getAttribute("data-ctv-show") == "true") {
sendImageSizeTelemetry(node.getAttribute("data-ctv-src"));
return Ci.nsIContentPolicy.ACCEPT;
}
setTimeout(() => {
// Cache the original image URL and swap in our placeholder
node.setAttribute("data-ctv-src", contentLocation.spec);
node.setAttribute("src", PLACEHOLDER_IMG);
// For imageset (img + srcset) the "srcset" is used even after we reset the "src" causing a loop.
// We are given the final image URL anyway, so it's OK to just remove the "srcset" value.
node.removeAttribute("srcset");
}, 0);
}
// Reject any image that is not associated with a DOM element
return Ci.nsIContentPolicy.REJECT;
}
}
// Accept all other content types
return Ci.nsIContentPolicy.ACCEPT;
},
shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra) {
return Ci.nsIContentPolicy.ACCEPT;
},
_usingCellular: function() {
let network = Cc["@mozilla.org/network/network-link-service;1"].getService(Ci.nsINetworkLinkService);
return !(network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_UNKNOWN ||
network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_ETHERNET ||
network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_USB ||
network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_WIFI);
},
_enabled: function() {
return Services.prefs.getIntPref(PREF_IMAGEBLOCKING);
},
observe : function (subject, topic, data) {
if (topic == TOPIC_GATHER_TELEMETRY) {
Services.telemetry.getHistogramById(TELEMETRY_TAP_TO_LOAD_ENABLED).add(this._enabled());
}
},
};
function sendImageSizeTelemetry(imageURL) {
let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
xhr.open("HEAD", imageURL, true);
xhr.onreadystatechange = function (e) {
if (xhr.readyState != 4) {
return;
}
if (xhr.status != 200) {
return;
}
let contentLength = xhr.getResponseHeader("Content-Length");
if (!contentLength) {
return;
}
let imageSize = contentLength / 1024;
Services.telemetry.getHistogramById(TELEMETRY_SHOW_IMAGE_SIZE).add(imageSize);
};
xhr.send(null);
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ImageBlockingPolicy]);
|