OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @return {number} Width of a scrollbar in pixels | 8 * @return {number} Width of a scrollbar in pixels |
9 */ | 9 */ |
10 function getScrollbarWidth() { | 10 function getScrollbarWidth() { |
(...skipping 21 matching lines...) Expand all Loading... | |
32 | 32 |
33 /** | 33 /** |
34 * The minimum number of pixels to offset the toolbar by from the bottom and | 34 * The minimum number of pixels to offset the toolbar by from the bottom and |
35 * right side of the screen. | 35 * right side of the screen. |
36 */ | 36 */ |
37 PDFViewer.MIN_TOOLBAR_OFFSET = 15; | 37 PDFViewer.MIN_TOOLBAR_OFFSET = 15; |
38 | 38 |
39 /** | 39 /** |
40 * Creates a new PDFViewer. There should only be one of these objects per | 40 * Creates a new PDFViewer. There should only be one of these objects per |
41 * document. | 41 * document. |
42 * @param {Object} streamDetails The stream object which points to the data | |
43 * contained in the PDF. | |
44 */ | 42 */ |
45 function PDFViewer(streamDetails) { | 43 function PDFViewer() { |
46 this.streamDetails = streamDetails; | 44 this.pluginLoaded_ = false; |
47 this.loaded = false; | 45 this.documentLoaded_ = false; |
Sam McNally
2015/01/06 05:31:03
The public "loaded" property is still used by the
raymes
2015/01/07 03:17:58
I removed the use of it.
| |
46 | |
48 | 47 |
49 // The sizer element is placed behind the plugin element to cause scrollbars | 48 // The sizer element is placed behind the plugin element to cause scrollbars |
50 // to be displayed in the window. It is sized according to the document size | 49 // to be displayed in the window. It is sized according to the document size |
51 // of the pdf and zoom level. | 50 // of the pdf and zoom level. |
52 this.sizer_ = $('sizer'); | 51 this.sizer_ = $('sizer'); |
53 this.toolbar_ = $('toolbar'); | 52 this.toolbar_ = $('toolbar'); |
54 this.pageIndicator_ = $('page-indicator'); | 53 this.pageIndicator_ = $('page-indicator'); |
55 this.progressBar_ = $('progress-bar'); | 54 this.progressBar_ = $('progress-bar'); |
56 this.passwordScreen_ = $('password-screen'); | 55 this.passwordScreen_ = $('password-screen'); |
57 this.passwordScreen_.addEventListener('password-submitted', | |
58 this.onPasswordSubmitted_.bind(this)); | |
59 this.errorScreen_ = $('error-screen'); | 56 this.errorScreen_ = $('error-screen'); |
60 | 57 |
61 // Create the viewport. | |
62 this.viewport_ = new Viewport(window, | |
63 this.sizer_, | |
64 this.viewportChanged_.bind(this), | |
65 this.beforeZoom_.bind(this), | |
66 this.afterZoom_.bind(this), | |
67 getScrollbarWidth()); | |
68 var isPrintPreview = | |
69 this.streamDetails.originalUrl.indexOf('chrome://print') == 0; | |
70 // Create the plugin object dynamically so we can set its src. The plugin | |
71 // element is sized to fill the entire window and is set to be fixed | |
72 // positioning, acting as a viewport. The plugin renders into this viewport | |
73 // according to the scroll position of the window. | |
74 // | |
75 // TODO(sammc): Remove special casing for print preview. This is currently | |
76 // necessary because setting the src for an embed element triggers origin | |
77 // checking and the PDF extension is not allowed to embed URLs with a scheme | |
78 // of "chrome", which is used by print preview. | |
79 this.plugin_ = document.createElement(isPrintPreview ? 'object' : 'embed'); | |
80 // NOTE: The plugin's 'id' field must be set to 'plugin' since | |
81 // chrome/renderer/printing/print_web_view_helper.cc actually references it. | |
82 this.plugin_.id = 'plugin'; | |
83 this.plugin_.type = 'application/x-google-chrome-pdf'; | |
84 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), | |
85 false); | |
86 | |
87 // Handle scripting messages from outside the extension that wish to interact | 58 // Handle scripting messages from outside the extension that wish to interact |
88 // with it. We also send a message indicating that extension has loaded and | 59 // with it. |
89 // is ready to receive messages. | |
90 window.addEventListener('message', this.handleScriptingMessage_.bind(this), | 60 window.addEventListener('message', this.handleScriptingMessage_.bind(this), |
91 false); | 61 false); |
92 this.sendScriptingMessage_({type: 'readyToReceive'}); | |
93 | |
94 document.title = getFilenameFromURL(this.streamDetails.originalUrl); | |
95 this.plugin_.setAttribute('src', this.streamDetails.originalUrl); | |
96 this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl); | |
97 var headers = ''; | |
98 for (var header in this.streamDetails.responseHeaders) { | |
99 headers += header + ': ' + | |
100 this.streamDetails.responseHeaders[header] + '\n'; | |
101 } | |
102 this.plugin_.setAttribute('headers', headers); | |
103 | |
104 if (!this.streamDetails.embedded) | |
105 this.plugin_.setAttribute('full-frame', ''); | |
106 document.body.appendChild(this.plugin_); | |
107 | |
108 // TODO(raymes): Remove this spurious message once crbug.com/388606 is fixed. | |
109 // This is a hack to initialize pepper sync scripting and avoid re-entrancy. | |
110 this.plugin_.postMessage({ | |
111 type: 'viewport', | |
112 zoom: 1, | |
113 xOffset: 0, | |
114 yOffset: 0 | |
115 }); | |
116 | |
117 // Setup the button event listeners. | |
118 $('fit-to-width-button').addEventListener('click', | |
119 this.viewport_.fitToWidth.bind(this.viewport_)); | |
120 $('fit-to-page-button').addEventListener('click', | |
121 this.viewport_.fitToPage.bind(this.viewport_)); | |
122 $('zoom-in-button').addEventListener('click', | |
123 this.viewport_.zoomIn.bind(this.viewport_)); | |
124 $('zoom-out-button').addEventListener('click', | |
125 this.viewport_.zoomOut.bind(this.viewport_)); | |
126 $('save-button').addEventListener('click', this.save_.bind(this)); | |
127 $('print-button').addEventListener('click', this.print_.bind(this)); | |
128 | |
129 // Setup the keyboard event listener. | |
130 document.onkeydown = this.handleKeyEvent_.bind(this); | |
131 | |
132 // Set up the zoom API. | |
133 if (this.shouldManageZoom_()) { | |
134 chrome.tabs.setZoomSettings(this.streamDetails.tabId, | |
135 {mode: 'manual', scope: 'per-tab'}, | |
136 this.afterZoom_.bind(this)); | |
137 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { | |
138 if (zoomChangeInfo.tabId != this.streamDetails.tabId) | |
139 return; | |
140 // If the zoom level is close enough to the current zoom level, don't | |
141 // change it. This avoids us getting into an infinite loop of zoom changes | |
142 // due to floating point error. | |
143 var MIN_ZOOM_DELTA = 0.01; | |
144 var zoomDelta = Math.abs(this.viewport_.zoom - | |
145 zoomChangeInfo.newZoomFactor); | |
146 // We should not change zoom level when we are responsible for initiating | |
147 // the zoom. onZoomChange() is called before setZoomComplete() callback | |
148 // when we initiate the zoom. | |
149 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) | |
150 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); | |
151 }.bind(this)); | |
152 } | |
153 | |
154 // Parse open pdf parameters. | |
155 var paramsParser = new OpenPDFParamsParser(this.streamDetails.originalUrl); | |
156 this.urlParams_ = paramsParser.urlParams; | |
157 } | 62 } |
158 | 63 |
159 PDFViewer.prototype = { | 64 PDFViewer.prototype = { |
160 /** | 65 /** |
66 * Initialize the PDF viewer with the details of the stream object pointing to | |
67 * the PDF data. | |
68 * @param {Object} streamDetails The stream object which points to the data | |
69 * contained in the PDF. | |
70 */ | |
71 init: function(streamDetails) { | |
Sam McNally
2015/01/06 05:31:04
How about setStreamDetails?
raymes
2015/01/07 03:17:57
I ended up not needing the init function so I've g
| |
72 this.streamDetails_ = streamDetails; | |
73 | |
74 // Create the viewport. | |
75 this.viewport_ = new Viewport(window, | |
76 this.sizer_, | |
77 this.viewportChanged_.bind(this), | |
78 this.beforeZoom_.bind(this), | |
79 this.afterZoom_.bind(this), | |
80 getScrollbarWidth()); | |
81 | |
82 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); | |
83 | |
84 var isPrintPreview = | |
85 this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; | |
86 // Create the plugin object dynamically so we can set its src. The plugin | |
87 // element is sized to fill the entire window and is set to be fixed | |
88 // positioning, acting as a viewport. The plugin renders into this viewport | |
89 // according to the scroll position of the window. | |
90 // | |
91 // TODO(sammc): Remove special casing for print preview. This is currently | |
92 // necessary because setting the src for an embed element triggers origin | |
93 // checking and the PDF extension is not allowed to embed URLs with a scheme | |
94 // of "chrome", which is used by print preview. | |
95 this.plugin_ = document.createElement(isPrintPreview ? 'object' : 'embed'); | |
96 // NOTE: The plugin's 'id' field must be set to 'plugin' since | |
97 // chrome/renderer/printing/print_web_view_helper.cc actually references it. | |
98 this.plugin_.id = 'plugin'; | |
99 this.plugin_.type = 'application/x-google-chrome-pdf'; | |
100 this.plugin_.addEventListener('message', | |
101 this.handlePluginMessage_.bind(this), false); | |
102 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); | |
103 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); | |
104 var headers = ''; | |
105 for (var header in this.streamDetails_.responseHeaders) { | |
106 headers += header + ': ' + | |
107 this.streamDetails_.responseHeaders[header] + '\n'; | |
108 } | |
109 this.plugin_.setAttribute('headers', headers); | |
110 | |
111 if (!this.streamDetails_.embedded) | |
112 this.plugin_.setAttribute('full-frame', ''); | |
113 document.body.appendChild(this.plugin_); | |
114 | |
115 // Add the password event listener. | |
116 this.passwordScreen_.addEventListener('password-submitted', | |
117 this.onPasswordSubmitted_.bind(this)); | |
118 | |
119 // Setup the button event listeners. | |
120 $('fit-to-width-button').addEventListener('click', | |
121 this.viewport_.fitToWidth.bind(this.viewport_)); | |
122 $('fit-to-page-button').addEventListener('click', | |
123 this.viewport_.fitToPage.bind(this.viewport_)); | |
124 $('zoom-in-button').addEventListener('click', | |
125 this.viewport_.zoomIn.bind(this.viewport_)); | |
126 $('zoom-out-button').addEventListener('click', | |
127 this.viewport_.zoomOut.bind(this.viewport_)); | |
128 $('save-button').addEventListener('click', this.save_.bind(this)); | |
129 $('print-button').addEventListener('click', this.print_.bind(this)); | |
130 | |
131 // Setup the keyboard event listener. | |
132 document.onkeydown = this.handleKeyEvent_.bind(this); | |
133 | |
134 // Set up the zoom API. | |
135 if (this.shouldManageZoom_()) { | |
136 chrome.tabs.setZoomSettings(this.streamDetails_.tabId, | |
137 {mode: 'manual', scope: 'per-tab'}, | |
138 this.afterZoom_.bind(this)); | |
139 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { | |
140 if (zoomChangeInfo.tabId != this.streamDetails_.tabId) | |
141 return; | |
142 // If the zoom level is close enough to the current zoom level, don't | |
143 // change it. This avoids us getting into an infinite loop of zoom | |
144 // changes due to floating point error. | |
145 var MIN_ZOOM_DELTA = 0.01; | |
146 var zoomDelta = Math.abs(this.viewport_.zoom - | |
147 zoomChangeInfo.newZoomFactor); | |
148 // We should not change zoom level when we are responsible for | |
149 // initiating the zoom. onZoomChange() is called before | |
150 // setZoomComplete() callback when we initiate the zoom. | |
151 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) | |
152 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); | |
153 }.bind(this)); | |
154 } | |
155 | |
156 // Parse open pdf parameters. | |
157 var paramsParser = new OpenPDFParamsParser(this.streamDetails_.originalUrl); | |
158 this.urlParams_ = paramsParser.urlParams; | |
159 | |
160 this.pluginLoaded_ = true; | |
161 this.sendScriptingMessage_({ | |
162 type: 'readyToReceive' | |
163 }); | |
164 }, | |
165 | |
166 /** | |
161 * @private | 167 * @private |
162 * Handle key events. These may come from the user directly or via the | 168 * Handle key events. These may come from the user directly or via the |
163 * scripting API. | 169 * scripting API. |
164 * @param {KeyboardEvent} e the event to handle. | 170 * @param {KeyboardEvent} e the event to handle. |
165 */ | 171 */ |
166 handleKeyEvent_: function(e) { | 172 handleKeyEvent_: function(e) { |
167 var position = this.viewport_.position; | 173 var position = this.viewport_.position; |
168 // Certain scroll events may be sent from outside of the extension. | 174 // Certain scroll events may be sent from outside of the extension. |
169 var fromScriptingAPI = e.type == 'scriptingKeypress'; | 175 var fromScriptingAPI = e.type == 'scriptingKeypress'; |
170 | 176 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 this.toolbar_.style.visibility = 'hidden'; | 330 this.toolbar_.style.visibility = 'hidden'; |
325 if (this.passwordScreen_.active) { | 331 if (this.passwordScreen_.active) { |
326 this.passwordScreen_.deny(); | 332 this.passwordScreen_.deny(); |
327 this.passwordScreen_.active = false; | 333 this.passwordScreen_.active = false; |
328 } | 334 } |
329 } else if (progress == 100) { | 335 } else if (progress == 100) { |
330 // Document load complete. | 336 // Document load complete. |
331 if (this.lastViewportPosition_) | 337 if (this.lastViewportPosition_) |
332 this.viewport_.position = this.lastViewportPosition_; | 338 this.viewport_.position = this.lastViewportPosition_; |
333 this.handleURLParams_(); | 339 this.handleURLParams_(); |
334 this.loaded = true; | 340 this.documentLoaded_ = true; |
335 var loadEvent = new Event('pdfload'); | |
336 window.dispatchEvent(loadEvent); | |
337 this.sendScriptingMessage_({ | 341 this.sendScriptingMessage_({ |
338 type: 'documentLoaded' | 342 type: 'documentLoaded' |
339 }); | 343 }); |
340 } | 344 } |
341 }, | 345 }, |
342 | 346 |
343 /** | 347 /** |
344 * @private | 348 * @private |
345 * An event handler for handling password-submitted events. These are fired | 349 * An event handler for handling password-submitted events. These are fired |
346 * when an event is entered into the password screen. | 350 * when an event is entered into the password screen. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 position.y = message.data.y; | 414 position.y = message.data.y; |
411 this.viewport_.position = position; | 415 this.viewport_.position = position; |
412 break; | 416 break; |
413 case 'setTranslatedStrings': | 417 case 'setTranslatedStrings': |
414 this.passwordScreen_.text = message.data.getPasswordString; | 418 this.passwordScreen_.text = message.data.getPasswordString; |
415 this.progressBar_.text = message.data.loadingString; | 419 this.progressBar_.text = message.data.loadingString; |
416 this.progressBar_.style.visibility = 'visible'; | 420 this.progressBar_.style.visibility = 'visible'; |
417 this.errorScreen_.text = message.data.loadFailedString; | 421 this.errorScreen_.text = message.data.loadFailedString; |
418 break; | 422 break; |
419 case 'cancelStreamUrl': | 423 case 'cancelStreamUrl': |
420 chrome.streamsPrivate.abort(this.streamDetails.streamUrl); | 424 chrome.streamsPrivate.abort(this.streamDetails_.streamUrl); |
421 break; | 425 break; |
422 } | 426 } |
423 }, | 427 }, |
424 | 428 |
425 /** | 429 /** |
426 * @private | 430 * @private |
427 * A callback that's called before the zoom changes. Notify the plugin to stop | 431 * A callback that's called before the zoom changes. Notify the plugin to stop |
428 * reacting to scroll events while zoom is taking place to avoid flickering. | 432 * reacting to scroll events while zoom is taking place to avoid flickering. |
429 */ | 433 */ |
430 beforeZoom_: function() { | 434 beforeZoom_: function() { |
431 this.plugin_.postMessage({ | 435 this.plugin_.postMessage({ |
432 type: 'stopScrolling' | 436 type: 'stopScrolling' |
433 }); | 437 }); |
434 }, | 438 }, |
435 | 439 |
436 /** | 440 /** |
437 * @private | 441 * @private |
438 * A callback that's called after the zoom changes. Notify the plugin of the | 442 * A callback that's called after the zoom changes. Notify the plugin of the |
439 * zoom change and to continue reacting to scroll events. | 443 * zoom change and to continue reacting to scroll events. |
440 */ | 444 */ |
441 afterZoom_: function() { | 445 afterZoom_: function() { |
442 var position = this.viewport_.position; | 446 var position = this.viewport_.position; |
443 var zoom = this.viewport_.zoom; | 447 var zoom = this.viewport_.zoom; |
444 if (this.shouldManageZoom_() && !this.setZoomInProgress_) { | 448 if (this.shouldManageZoom_() && !this.setZoomInProgress_) { |
445 this.setZoomInProgress_ = true; | 449 this.setZoomInProgress_ = true; |
446 chrome.tabs.setZoom(this.streamDetails.tabId, zoom, | 450 chrome.tabs.setZoom(this.streamDetails_.tabId, zoom, |
447 this.setZoomComplete_.bind(this, zoom)); | 451 this.setZoomComplete_.bind(this, zoom)); |
448 } | 452 } |
449 this.plugin_.postMessage({ | 453 this.plugin_.postMessage({ |
450 type: 'viewport', | 454 type: 'viewport', |
451 zoom: zoom, | 455 zoom: zoom, |
452 xOffset: position.x, | 456 xOffset: position.x, |
453 yOffset: position.y | 457 yOffset: position.y |
454 }); | 458 }); |
455 }, | 459 }, |
456 | 460 |
457 /** | 461 /** |
458 * @private | 462 * @private |
459 * A callback that's called after chrome.tabs.setZoom is complete. This will | 463 * A callback that's called after chrome.tabs.setZoom is complete. This will |
460 * call chrome.tabs.setZoom again if the zoom level has changed since it was | 464 * call chrome.tabs.setZoom again if the zoom level has changed since it was |
461 * last called. | 465 * last called. |
462 * @param {number} lastZoom the zoom level that chrome.tabs.setZoom was called | 466 * @param {number} lastZoom the zoom level that chrome.tabs.setZoom was called |
463 * with. | 467 * with. |
464 */ | 468 */ |
465 setZoomComplete_: function(lastZoom) { | 469 setZoomComplete_: function(lastZoom) { |
466 var zoom = this.viewport_.zoom; | 470 var zoom = this.viewport_.zoom; |
467 if (zoom != lastZoom) { | 471 if (zoom != lastZoom) { |
468 chrome.tabs.setZoom(this.streamDetails.tabId, zoom, | 472 chrome.tabs.setZoom(this.streamDetails_.tabId, zoom, |
469 this.setZoomComplete_.bind(this, zoom)); | 473 this.setZoomComplete_.bind(this, zoom)); |
470 } else { | 474 } else { |
471 this.setZoomInProgress_ = false; | 475 this.setZoomInProgress_ = false; |
472 } | 476 } |
473 }, | 477 }, |
474 | 478 |
475 /** | 479 /** |
476 * @private | 480 * @private |
477 * A callback that's called after the viewport changes. | 481 * A callback that's called after the viewport changes. |
478 */ | 482 */ |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
571 pageCount: (message.data.modifiable ? | 575 pageCount: (message.data.modifiable ? |
572 message.data.pageNumbers.length : 0) | 576 message.data.pageNumbers.length : 0) |
573 }); | 577 }); |
574 break; | 578 break; |
575 case 'sendKeyEvent': | 579 case 'sendKeyEvent': |
576 var e = document.createEvent('Event'); | 580 var e = document.createEvent('Event'); |
577 e.initEvent('scriptingKeypress'); | 581 e.initEvent('scriptingKeypress'); |
578 e.keyCode = message.data.keyCode; | 582 e.keyCode = message.data.keyCode; |
579 this.handleKeyEvent_(e); | 583 this.handleKeyEvent_(e); |
580 break; | 584 break; |
585 case 'setParentWindow': | |
586 // Set the parent window to which messages will be sent. | |
587 this.parentWindow_ = message.source; | |
588 if (this.pluginLoaded_) { | |
589 this.sendScriptingMessage_({ | |
590 type: 'readyToReceive' | |
591 }); | |
592 } | |
593 break; | |
581 } | 594 } |
582 | 595 |
583 }, | 596 }, |
584 | 597 |
585 /** | 598 /** |
586 * @private | 599 * @private |
587 * Send a scripting message outside the extension (typically to | 600 * Send a scripting message outside the extension (typically to |
588 * PDFScriptingAPI in a page containing the extension). | 601 * PDFScriptingAPI in a page containing the extension). |
589 * @param {Object} message the message to send. | 602 * @param {Object} message the message to send. |
590 */ | 603 */ |
591 sendScriptingMessage_: function(message) { | 604 sendScriptingMessage_: function(message) { |
592 window.parent.postMessage(message, '*'); | 605 if (this.parentWindow_) |
Sam McNally
2015/01/06 05:31:03
Won't this lose messages?
raymes
2015/01/07 03:17:58
Yes, but that's ok. I've made it always send a doc
| |
606 this.parentWindow_.postMessage(message, '*'); | |
593 }, | 607 }, |
594 | 608 |
595 /** | 609 /** |
596 * @private | 610 * @private |
597 * Return whether this PDFViewer should manage zoom for its containing page. | 611 * Return whether this PDFViewer should manage zoom for its containing page. |
598 * @return {boolean} Whether this PDFViewer should manage zoom for its | 612 * @return {boolean} Whether this PDFViewer should manage zoom for its |
599 * containing page. | 613 * containing page. |
600 */ | 614 */ |
601 shouldManageZoom_: function() { | 615 shouldManageZoom_: function() { |
602 return !!(chrome.tabs && !this.streamDetails.embedded && | 616 return !!(chrome.tabs && !this.streamDetails_.embedded && |
603 this.streamDetails.tabId != -1); | 617 this.streamDetails_.tabId != -1); |
604 }, | 618 }, |
605 | 619 |
606 /** | 620 /** |
607 * @type {Viewport} the viewport of the PDF viewer. | 621 * @type {Viewport} the viewport of the PDF viewer. |
608 */ | 622 */ |
609 get viewport() { | 623 get viewport() { |
610 return this.viewport_; | 624 return this.viewport_; |
611 } | 625 } |
612 }; | 626 }; |
OLD | NEW |