Source code

Revision control

Other Tools

1
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
"use strict";
6
7
var EXPORTED_SYMBOLS = ["AboutReaderChild"];
8
9
const { ActorChild } = ChromeUtils.import(
11
);
12
13
ChromeUtils.defineModuleGetter(
14
this,
15
"AboutReader",
17
);
18
ChromeUtils.defineModuleGetter(
19
this,
20
"ReaderMode",
22
);
23
ChromeUtils.defineModuleGetter(
24
this,
25
"Readerable",
27
);
28
29
class AboutReaderChild extends ActorChild {
30
constructor(dispatcher) {
31
super(dispatcher);
32
33
this._articlePromise = null;
34
this._isLeavingReaderableReaderMode = false;
35
}
36
37
receiveMessage(message) {
38
switch (message.name) {
39
case "Reader:ToggleReaderMode":
40
if (!this.isAboutReader) {
41
this._articlePromise = ReaderMode.parseDocument(
42
this.content.document
43
).catch(Cu.reportError);
44
ReaderMode.enterReaderMode(this.mm.docShell, this.content);
45
} else {
46
this._isLeavingReaderableReaderMode = this.isReaderableAboutReader;
47
ReaderMode.leaveReaderMode(this.mm.docShell, this.content);
48
}
49
break;
50
51
case "Reader:PushState":
52
this.updateReaderButton(!!(message.data && message.data.isArticle));
53
break;
54
}
55
}
56
57
get isAboutReader() {
58
if (!this.content) {
59
return false;
60
}
61
return this.content.document.documentURI.startsWith("about:reader");
62
}
63
64
get isReaderableAboutReader() {
65
return (
66
this.isAboutReader &&
67
!this.content.document.documentElement.dataset.isError
68
);
69
}
70
71
handleEvent(aEvent) {
72
if (aEvent.originalTarget.defaultView != this.content) {
73
return;
74
}
75
76
switch (aEvent.type) {
77
case "AboutReaderContentLoaded":
78
if (!this.isAboutReader) {
79
return;
80
}
81
82
if (this.content.document.body) {
83
// Update the toolbar icon to show the "reader active" icon.
84
this.mm.sendAsyncMessage("Reader:UpdateReaderButton");
85
new AboutReader(this.mm, this.content, this._articlePromise);
86
this._articlePromise = null;
87
}
88
break;
89
90
case "pagehide":
91
this.cancelPotentialPendingReadabilityCheck();
92
// this._isLeavingReaderableReaderMode is used here to keep the Reader Mode icon
93
// visible in the location bar when transitioning from reader-mode page
94
// back to the readable source page.
95
this.mm.sendAsyncMessage("Reader:UpdateReaderButton", {
96
isArticle: this._isLeavingReaderableReaderMode,
97
});
98
if (this._isLeavingReaderableReaderMode) {
99
this._isLeavingReaderableReaderMode = false;
100
}
101
break;
102
103
case "pageshow":
104
// If a page is loaded from the bfcache, we won't get a "DOMContentLoaded"
105
// event, so we need to rely on "pageshow" in this case.
106
if (aEvent.persisted) {
107
this.updateReaderButton();
108
}
109
break;
110
case "DOMContentLoaded":
111
this.updateReaderButton();
112
break;
113
}
114
}
115
116
/**
117
* NB: this function will update the state of the reader button asynchronously
118
* after the next mozAfterPaint call (assuming reader mode is enabled and
119
* this is a suitable document). Calling it on things which won't be
120
* painted is not going to work.
121
*/
122
updateReaderButton(forceNonArticle) {
123
if (
124
!Readerable.isEnabledForParseOnLoad ||
125
this.isAboutReader ||
126
!this.content ||
127
!(this.content.document instanceof this.content.HTMLDocument) ||
128
this.content.document.mozSyntheticDocument
129
) {
130
return;
131
}
132
133
this.scheduleReadabilityCheckPostPaint(forceNonArticle);
134
}
135
136
cancelPotentialPendingReadabilityCheck() {
137
if (this._pendingReadabilityCheck) {
138
this.mm.removeEventListener(
139
"MozAfterPaint",
140
this._pendingReadabilityCheck
141
);
142
delete this._pendingReadabilityCheck;
143
}
144
}
145
146
scheduleReadabilityCheckPostPaint(forceNonArticle) {
147
if (this._pendingReadabilityCheck) {
148
// We need to stop this check before we re-add one because we don't know
149
// if forceNonArticle was true or false last time.
150
this.cancelPotentialPendingReadabilityCheck();
151
}
152
this._pendingReadabilityCheck = this.onPaintWhenWaitedFor.bind(
153
this,
154
forceNonArticle
155
);
156
this.mm.addEventListener("MozAfterPaint", this._pendingReadabilityCheck);
157
}
158
159
onPaintWhenWaitedFor(forceNonArticle, event) {
160
// In non-e10s, we'll get called for paints other than ours, and so it's
161
// possible that this page hasn't been laid out yet, in which case we
162
// should wait until we get an event that does relate to our layout. We
163
// determine whether any of our this.content got painted by checking if there
164
// are any painted rects.
165
if (!event.clientRects.length) {
166
return;
167
}
168
169
this.cancelPotentialPendingReadabilityCheck();
170
// Only send updates when there are articles; there's no point updating with
171
// |false| all the time.
172
if (Readerable.isProbablyReaderable(this.content.document)) {
173
this.mm.sendAsyncMessage("Reader:UpdateReaderButton", {
174
isArticle: true,
175
});
176
} else if (forceNonArticle) {
177
this.mm.sendAsyncMessage("Reader:UpdateReaderButton", {
178
isArticle: false,
179
});
180
}
181
}
182
}