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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from 'chrome://resources/js/assert.m.js';
import {NativeEventTarget as EventTarget} from 'chrome://resources/js/cr/event_target.m.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js';
import {SaveRequestType} from './constants.js';
import {PartialPoint, Point, Viewport} from './viewport.js';
/** @typedef {{ type: string }} */
export let MessageData;
/**
* @typedef {{
* dataToSave: Array,
* token: string,
* fileName: string
* }}
*/
let SaveDataMessageData;
/**
* @typedef {{
* type: string,
* to: string,
* cc: string,
* bcc: string,
* subject: string,
* body: string,
* }}
*/
let EmailMessageData;
/**
* @typedef {{
* type: string,
* url: string,
* grayscale: boolean,
* modifiable: boolean,
* pageNumbers: !Array<number>
* }}
*/
export let PrintPreviewParams;
/**
* Creates a cryptographically secure pseudorandom 128-bit token.
* @return {string} The generated token as a hex string.
*/
function createToken() {
const randomBytes = new Uint8Array(16);
return window.crypto.getRandomValues(randomBytes)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
/** @abstract */
export class ContentController {
constructor() {}
beforeZoom() {}
afterZoom() {}
viewportChanged() {}
/** @abstract */
rotateClockwise() {}
/** @abstract */
rotateCounterclockwise() {}
/** @abstract */
setTwoUpView(enableTwoUpView) {}
/** Triggers printing of the current document. */
print() {}
/** Undo an edit action. */
undo() {}
/** Redo an edit action. */
redo() {}
/**
* Requests that the current document be saved.
* @param {!SaveRequestType} requestType The type of save request. If
* ANNOTATION, a response is required, otherwise the controller may save
* the document to disk internally.
* @return {Promise<{fileName: string, dataToSave: ArrayBuffer}>}
* @abstract
*/
save(requestType) {}
/**
* Loads PDF document from `data` activates UI.
* @param {string} fileName
* @param {!ArrayBuffer} data
* @return {Promise<void>}
* @abstract
*/
load(fileName, data) {}
/**
* Unloads the current document and removes the UI.
* @abstract
*/
unload() {}
}
// PDF plugin controller, responsible for communicating with the embedded plugin
// element. Dispatches a 'plugin-message' event containing the message from the
// plugin, if a message type not handled by this controller is received.
export class PluginController extends ContentController {
/**
* @param {!HTMLEmbedElement} plugin
* @param {!Viewport} viewport
* @param {function():boolean} getIsUserInitiatedCallback
* @param {function():?Promise} getLoadedCallback
*/
constructor(plugin, viewport, getIsUserInitiatedCallback, getLoadedCallback) {
super();
/** @private {!HTMLEmbedElement} */
this.plugin_ = plugin;
/** @private {!Viewport} */
this.viewport_ = viewport;
/** @private {!function():boolean} */
this.getIsUserInitiatedCallback_ = getIsUserInitiatedCallback;
/** @private {!function():?Promise} */
this.getLoadedCallback_ = getLoadedCallback;
/** @private {!Map<string, PromiseResolver>} */
this.pendingTokens_ = new Map();
this.plugin_.addEventListener(
'message', e => this.handlePluginMessage_(e), false);
/** @private {!EventTarget} */
this.eventTarget_ = new EventTarget();
}
/** @return {!EventTarget} */
getEventTarget() {
return this.eventTarget_;
}
/**
* Notify the plugin to stop reacting to scroll events while zoom is taking
* place to avoid flickering.
* @override
*/
beforeZoom() {
this.postMessage_({type: 'stopScrolling'});
if (this.viewport_.pinchPhase === Viewport.PinchPhase.PINCH_START) {
const position = this.viewport_.position;
const zoom = this.viewport_.getZoom();
const pinchPhase = this.viewport_.pinchPhase;
const layoutOptions = this.viewport_.getLayoutOptions();
this.postMessage_({
type: 'viewport',
userInitiated: true,
zoom: zoom,
layoutOptions: layoutOptions,
xOffset: position.x,
yOffset: position.y,
pinchPhase: pinchPhase
});
}
}
/**
* Notify the plugin of the zoom change and to continue reacting to scroll
* events.
* @override
*/
afterZoom() {
const position = this.viewport_.position;
const zoom = this.viewport_.getZoom();
const layoutOptions = this.viewport_.getLayoutOptions();
const pinchVector = this.viewport_.pinchPanVector || {x: 0, y: 0};
const pinchCenter = this.viewport_.pinchCenter || {x: 0, y: 0};
const pinchPhase = this.viewport_.pinchPhase;
this.postMessage_({
type: 'viewport',
userInitiated: this.getIsUserInitiatedCallback_(),
zoom: zoom,
layoutOptions: layoutOptions,
xOffset: position.x,
yOffset: position.y,
pinchPhase: pinchPhase,
pinchX: pinchCenter.x,
pinchY: pinchCenter.y,
pinchVectorX: pinchVector.x,
pinchVectorY: pinchVector.y
});
}
/**
* Post a message to the PPAPI plugin. Some messages will cause an async reply
* to be received through handlePluginMessage_().
* @param {!MessageData} message Message to post.
* @private
*/
postMessage_(message) {
this.plugin_.postMessage(message);
}
/** @override */
rotateClockwise() {
this.postMessage_({type: 'rotateClockwise'});
}
/** @override */
rotateCounterclockwise() {
this.postMessage_({type: 'rotateCounterclockwise'});
}
/** @override */
setTwoUpView(enableTwoUpView) {
this.postMessage_({
type: 'setTwoUpView',
enableTwoUpView: enableTwoUpView,
});
}
/** @override */
print() {
this.postMessage_({type: 'print'});
}
selectAll() {
this.postMessage_({type: 'selectAll'});
}
getSelectedText() {
this.postMessage_({type: 'getSelectedText'});
}
/** @param {!PrintPreviewParams} printPreviewParams */
resetPrintPreviewMode(printPreviewParams) {
this.postMessage_({
type: 'resetPrintPreviewMode',
url: printPreviewParams.url,
grayscale: printPreviewParams.grayscale,
// If the PDF isn't modifiable we send 0 as the page count so that no
// blank placeholder pages get appended to the PDF.
pageCount:
(printPreviewParams.modifiable ?
printPreviewParams.pageNumbers.length :
0)
});
}
/** @param {string} newColor New color, in hex, for the PDF plugin. */
backgroundColorChanged(newColor) {
this.postMessage_({
type: 'backgroundColorChanged',
backgroundColor: newColor,
});
}
/**
* @param {string} url
* @param {number} index
*/
loadPreviewPage(url, index) {
this.postMessage_({type: 'loadPreviewPage', url: url, index: index});
}
/** @param {string} password */
getPasswordComplete(password) {
this.postMessage_({type: 'getPasswordComplete', password: password});
}
/** @param {string} destination */
getNamedDestination(destination) {
this.postMessage_({
type: 'getNamedDestination',
namedDestination: destination,
});
}
/** @override */
save(requestType) {
const resolver = new PromiseResolver();
const newToken = createToken();
this.pendingTokens_.set(newToken, resolver);
this.postMessage_({
type: 'save',
token: newToken,
saveRequestType: requestType,
});
return resolver.promise;
}
/** @override */
async load(fileName, data) {
const url = URL.createObjectURL(new Blob([data]));
this.plugin_.removeAttribute('headers');
this.plugin_.setAttribute('stream-url', url);
this.plugin_.setAttribute('has-edits', '');
this.plugin_.style.display = 'block';
try {
await this.getLoadedCallback_();
} finally {
URL.revokeObjectURL(url);
}
}
/** @override */
unload() {
this.plugin_.style.display = 'none';
}
/**
* An event handler for handling message events received from the plugin.
* @param {!Event} messageEvent a message event.
* @private
*/
handlePluginMessage_(messageEvent) {
const messageData = /** @type {!MessageData} */ (messageEvent.data);
switch (messageData.type) {
case 'email':
const emailData = /** @type {!EmailMessageData} */ (messageData);
const href = 'mailto:' + emailData.to + '?cc=' + emailData.cc +
'&bcc=' + emailData.bcc + '&subject=' + emailData.subject +
'&body=' + emailData.body;
window.location.href = href;
break;
case 'goToPage':
this.viewport_.goToPage(
/** @type {{type: string, page: number}} */ (messageData).page);
break;
case 'setScrollPosition':
this.viewport_.scrollTo(/** @type {!PartialPoint} */ (messageData));
break;
case 'scrollBy':
this.viewport_.scrollBy(/** @type {!Point} */ (messageData));
break;
case 'saveData':
this.saveData_(/** @type {!SaveDataMessageData} */ (messageData));
break;
case 'consumeSaveToken':
const saveTokenData =
/** @type {{ type: string, token: string }} */ (messageData);
const resolver = this.pendingTokens_.get(saveTokenData.token);
assert(this.pendingTokens_.delete(saveTokenData.token));
resolver.resolve(null);
break;
default:
this.eventTarget_.dispatchEvent(
new CustomEvent('plugin-message', {detail: messageData}));
}
}
/**
* Handles the pdf file buffer received from the plugin.
* @param {!SaveDataMessageData} messageData data of the message event.
* @private
*/
saveData_(messageData) {
assert(
loadTimeData.getBoolean('pdfFormSaveEnabled') ||
loadTimeData.getBoolean('pdfAnnotationsEnabled'));
// Verify a token that was created by this instance is included to avoid
// being spammed.
const resolver = this.pendingTokens_.get(messageData.token);
assert(this.pendingTokens_.delete(messageData.token));
if (!messageData.dataToSave) {
resolver.reject();
return;
}
// Verify the file size and the first bytes to make sure it's a PDF. Cap at
// 100 MB. This cap should be kept in sync with and is also enforced in
// pdf/out_of_process_instance.cc.
const MIN_FILE_SIZE = '%PDF1.0'.length;
const MAX_FILE_SIZE = 100 * 1000 * 1000;
const buffer = messageData.dataToSave;
const bufView = new Uint8Array(buffer);
assert(
bufView.length <= MAX_FILE_SIZE,
`File too large to be saved: ${bufView.length} bytes.`);
assert(bufView.length >= MIN_FILE_SIZE);
assert(
String.fromCharCode(bufView[0], bufView[1], bufView[2], bufView[3]) ===
'%PDF');
resolver.resolve(messageData);
}
}
|