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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs",
PdfJsTelemetry: "resource://pdf.js/PdfJsTelemetry.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
SetClipboardSearchString: "resource://gre/modules/Finder.sys.mjs",
});
var Svc = {};
XPCOMUtils.defineLazyServiceGetter(
Svc,
"mime",
"@mozilla.org/mime;1",
"nsIMIMEService"
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"matchesCountLimit",
"accessibility.typeaheadfind.matchesCountLimit"
);
let gFindTypes = [
"find",
"findagain",
"findhighlightallchange",
"findcasesensitivitychange",
"findbarclose",
"finddiacriticmatchingchange",
];
export class PdfjsParent extends JSWindowActorParent {
constructor() {
super();
this._boundToFindbar = null;
this._findFailedString = null;
}
didDestroy() {
this._removeEventListener();
}
receiveMessage(aMsg) {
switch (aMsg.name) {
case "PDFJS:Parent:updateControlState":
return this._updateControlState(aMsg);
case "PDFJS:Parent:updateMatchesCount":
return this._updateMatchesCount(aMsg);
case "PDFJS:Parent:addEventListener":
return this._addEventListener();
case "PDFJS:Parent:saveURL":
return this._saveURL(aMsg);
case "PDFJS:Parent:recordExposure":
return this._recordExposure();
case "PDFJS:Parent:reportTelemetry":
return this._reportTelemetry(aMsg);
}
return undefined;
}
/*
* Internal
*/
get browser() {
return this.browsingContext.top.embedderElement;
}
_recordExposure() {
lazy.NimbusFeatures.pdfjs.recordExposureEvent({ once: true });
}
_reportTelemetry(aMsg) {
lazy.PdfJsTelemetry.report(aMsg.data);
}
_saveURL(aMsg) {
const data = aMsg.data;
this.browser.ownerGlobal.saveURL(
data.blobUrl /* aURL */,
data.originalUrl /* aOriginalURL */,
data.filename /* aFileName */,
null /* aFilePickerTitleKey */,
true /* aShouldBypassCache */,
false /* aSkipPrompt */,
null /* aReferrerInfo */,
null /* aCookieJarSettings*/,
null /* aSourceDocument */,
lazy.PrivateBrowsingUtils.isBrowserPrivate(
this.browser
) /* aIsContentWindowPrivate */,
Services.scriptSecurityManager.getSystemPrincipal() /* aPrincipal */
);
}
_updateControlState(aMsg) {
let data = aMsg.data;
let browser = this.browser;
let tabbrowser = browser.getTabBrowser();
let tab = tabbrowser.getTabForBrowser(browser);
tabbrowser.getFindBar(tab).then(fb => {
if (!fb) {
// The tab or window closed.
return;
}
fb.updateControlState(data.result, data.findPrevious);
if (
data.result === Ci.nsITypeAheadFind.FIND_FOUND ||
data.result === Ci.nsITypeAheadFind.FIND_WRAPPED ||
(data.result === Ci.nsITypeAheadFind.FIND_PENDING &&
!this._findFailedString)
) {
this._findFailedString = null;
lazy.SetClipboardSearchString(data.rawQuery);
} else if (!this._findFailedString) {
this._findFailedString = data.rawQuery;
lazy.SetClipboardSearchString(data.rawQuery);
}
const matchesCount = this._requestMatchesCount(data.matchesCount);
fb.onMatchesCountResult(matchesCount);
});
}
_updateMatchesCount(aMsg) {
let data = aMsg.data;
let browser = this.browser;
let tabbrowser = browser.getTabBrowser();
let tab = tabbrowser.getTabForBrowser(browser);
tabbrowser.getFindBar(tab).then(fb => {
if (!fb) {
// The tab or window closed.
return;
}
const matchesCount = this._requestMatchesCount(data);
fb.onMatchesCountResult(matchesCount);
});
}
_requestMatchesCount(data) {
if (!data) {
return { current: 0, total: 0 };
}
let result = {
current: data.current,
total: data.total,
limit:
typeof lazy.matchesCountLimit === "number" ? lazy.matchesCountLimit : 0,
};
if (result.total > result.limit) {
result.total = -1;
}
return result;
}
handleEvent(aEvent) {
const type = aEvent.type;
// Handle the tab find initialized event specially:
if (type == "TabFindInitialized") {
let browser = aEvent.target.linkedBrowser;
this._hookupEventListeners(browser);
aEvent.target.removeEventListener(type, this);
return;
}
if (type == "SwapDocShells") {
this._removeEventListener();
let newBrowser = aEvent.detail;
newBrowser.addEventListener(
"EndSwapDocShells",
evt => {
this._hookupEventListeners(newBrowser);
},
{ once: true }
);
return;
}
// Ignore events findbar events which arrive while the Pdfjs document is in
// the BFCache.
if (this.windowContext.isInBFCache) {
return;
}
// To avoid forwarding the message as a CPOW, create a structured cloneable
// version of the event for both performance, and ease of usage, reasons.
let detail = null;
if (type !== "findbarclose") {
detail = {
query: aEvent.detail.query,
caseSensitive: aEvent.detail.caseSensitive,
entireWord: aEvent.detail.entireWord,
highlightAll: aEvent.detail.highlightAll,
findPrevious: aEvent.detail.findPrevious,
matchDiacritics: aEvent.detail.matchDiacritics,
};
}
let browser = aEvent.currentTarget.browser;
if (!this._boundToFindbar) {
throw new Error(
"FindEventManager was not bound for the current browser."
);
}
browser.sendMessageToActor(
"PDFJS:Child:handleEvent",
{ type, detail },
"Pdfjs"
);
aEvent.preventDefault();
}
_addEventListener() {
let browser = this.browser;
if (this._boundToFindbar) {
throw new Error(
"FindEventManager was bound 2nd time without unbinding it first."
);
}
this._hookupEventListeners(browser);
}
/**
* Either hook up all the find event listeners if a findbar exists,
* or listen for a find bar being created and hook up event listeners
* when it does get created.
*/
_hookupEventListeners(aBrowser) {
let tabbrowser = aBrowser.getTabBrowser();
let tab = tabbrowser.getTabForBrowser(aBrowser);
let findbar = tabbrowser.getCachedFindBar(tab);
if (findbar) {
// And we need to start listening to find events.
for (var i = 0; i < gFindTypes.length; i++) {
var type = gFindTypes[i];
findbar.addEventListener(type, this, true);
}
this._boundToFindbar = findbar;
} else {
tab.addEventListener("TabFindInitialized", this);
}
aBrowser.addEventListener("SwapDocShells", this);
return !!findbar;
}
_removeEventListener() {
let browser = this.browser;
// make sure the listener has been removed.
let findbar = this._boundToFindbar;
if (findbar) {
// No reason to listen to find events any longer.
for (var i = 0; i < gFindTypes.length; i++) {
var type = gFindTypes[i];
findbar.removeEventListener(type, this, true);
}
} else if (browser) {
// If we registered a `TabFindInitialized` listener which never fired,
// make sure we remove it.
let tabbrowser = browser.getTabBrowser();
let tab = tabbrowser.getTabForBrowser(browser);
tab?.removeEventListener("TabFindInitialized", this);
}
this._boundToFindbar = null;
// Clean up any SwapDocShells event listeners.
browser?.removeEventListener("SwapDocShells", this);
}
}
|