Chromium Code Reviews| 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 30 matching lines...) Expand all Loading... | |
| 41 * document. | 41 * document. |
| 42 * @constructor | 42 * @constructor |
| 43 * @param {Object} streamDetails The stream object which points to the data | 43 * @param {Object} streamDetails The stream object which points to the data |
| 44 * contained in the PDF. | 44 * contained in the PDF. |
| 45 */ | 45 */ |
| 46 function PDFViewer(streamDetails) { | 46 function PDFViewer(streamDetails) { |
| 47 this.streamDetails_ = streamDetails; | 47 this.streamDetails_ = streamDetails; |
| 48 this.loaded_ = false; | 48 this.loaded_ = false; |
| 49 this.parentWindow_ = null; | 49 this.parentWindow_ = null; |
| 50 | 50 |
| 51 this.delayedScriptingMessages_ = []; | |
| 52 | |
| 51 this.isPrintPreview_ = | 53 this.isPrintPreview_ = |
| 52 this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; | 54 this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; |
| 53 this.isMaterial_ = location.pathname.substring(1) == 'index-material.html'; | 55 this.isMaterial_ = location.pathname.substring(1) == 'index-material.html'; |
| 54 | 56 |
| 55 // The sizer element is placed behind the plugin element to cause scrollbars | 57 // The sizer element is placed behind the plugin element to cause scrollbars |
| 56 // to be displayed in the window. It is sized according to the document size | 58 // to be displayed in the window. It is sized according to the document size |
| 57 // of the pdf and zoom level. | 59 // of the pdf and zoom level. |
| 58 this.sizer_ = $('sizer'); | 60 this.sizer_ = $('sizer'); |
| 59 this.toolbar_ = $('toolbar'); | 61 this.toolbar_ = $('toolbar'); |
| 60 this.pageIndicator_ = $('page-indicator'); | 62 this.pageIndicator_ = $('page-indicator'); |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 if (this.passwordScreen_.active) { | 335 if (this.passwordScreen_.active) { |
| 334 this.passwordScreen_.deny(); | 336 this.passwordScreen_.deny(); |
| 335 this.passwordScreen_.active = false; | 337 this.passwordScreen_.active = false; |
| 336 } | 338 } |
| 337 } else if (progress == 100) { | 339 } else if (progress == 100) { |
| 338 // Document load complete. | 340 // Document load complete. |
| 339 if (this.lastViewportPosition_) | 341 if (this.lastViewportPosition_) |
| 340 this.viewport_.position = this.lastViewportPosition_; | 342 this.viewport_.position = this.lastViewportPosition_; |
| 341 this.handleURLParams_(); | 343 this.handleURLParams_(); |
| 342 this.loaded_ = true; | 344 this.loaded_ = true; |
| 343 this.sendScriptingMessage_({ | 345 while (this.delayedScriptingMessages_.length > 0) |
| 344 type: 'documentLoaded' | 346 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); |
| 345 }); | |
| 346 } | 347 } |
| 347 }, | 348 }, |
| 348 | 349 |
| 349 /** | 350 /** |
| 350 * @private | 351 * @private |
| 351 * An event handler for handling password-submitted events. These are fired | 352 * An event handler for handling password-submitted events. These are fired |
| 352 * when an event is entered into the password screen. | 353 * when an event is entered into the password screen. |
| 353 * @param {Object} event a password-submitted event. | 354 * @param {Object} event a password-submitted event. |
| 354 */ | 355 */ |
| 355 onPasswordSubmitted_: function(event) { | 356 onPasswordSubmitted_: function(event) { |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 type: 'viewport', | 607 type: 'viewport', |
| 607 pageX: visiblePageDimensions.x, | 608 pageX: visiblePageDimensions.x, |
| 608 pageY: visiblePageDimensions.y, | 609 pageY: visiblePageDimensions.y, |
| 609 pageWidth: visiblePageDimensions.width, | 610 pageWidth: visiblePageDimensions.width, |
| 610 viewportWidth: size.width, | 611 viewportWidth: size.width, |
| 611 viewportHeight: size.height | 612 viewportHeight: size.height |
| 612 }); | 613 }); |
| 613 }, | 614 }, |
| 614 | 615 |
| 615 /** | 616 /** |
| 616 * @private | |
| 617 * Handle a scripting message from outside the extension (typically sent by | 617 * Handle a scripting message from outside the extension (typically sent by |
| 618 * PDFScriptingAPI in a page containing the extension) to interact with the | 618 * PDFScriptingAPI in a page containing the extension) to interact with the |
| 619 * plugin. | 619 * plugin. |
| 620 * @param {MessageObject} message the message to handle. | 620 * @param {MessageObject} message the message to handle. |
| 621 */ | 621 */ |
| 622 handleScriptingMessage: function(message) { | 622 handleScriptingMessage: function(message) { |
| 623 if (this.parentWindow_ != message.source) | |
| 624 this.parentWindow_ = message.source; | |
| 625 | |
| 626 if (this.handlePrintPreviewScriptingMessage_(message)) | |
| 627 return; | |
| 628 | |
| 629 // Delay scripting messages from users of the scripting API until the | |
| 630 // document is loaded. This simplifies use of the APIs. | |
| 631 if (!this.loaded_) { | |
| 632 this.delayedScriptingMessages_.push(message); | |
| 633 return; | |
| 634 } | |
| 635 | |
| 623 switch (message.data.type.toString()) { | 636 switch (message.data.type.toString()) { |
| 624 case 'getAccessibilityJSON': | 637 case 'getAccessibilityJSON': |
| 625 case 'getSelectedText': | 638 case 'getSelectedText': |
| 626 case 'loadPreviewPage': | 639 case 'loadPreviewPage': |
|
Sam McNally
2015/01/22 06:12:12
Isn't this used for print preview?
| |
| 627 case 'print': | 640 case 'print': |
| 628 case 'selectAll': | 641 case 'selectAll': |
| 629 this.plugin_.postMessage(message.data); | 642 this.plugin_.postMessage(message.data); |
| 630 break; | 643 break; |
| 644 case 'isDocumentLoaded': | |
| 645 // Since this is only hit after the document has been loaded, we can | |
| 646 // send a reply immediately. | |
| 647 this.sendScriptingMessage_({ | |
| 648 type: 'documentLoaded' | |
| 649 }); | |
| 650 break; | |
| 651 } | |
| 652 }, | |
| 653 | |
| 654 /** | |
| 655 * @private | |
| 656 * Handle scripting messages specific to print preview. | |
| 657 * @param {MessageObject} message the message to handle. | |
| 658 * @return {boolean} true if the message was handled, false otherwise. | |
| 659 */ | |
| 660 handlePrintPreviewScriptingMessage_: function(message) { | |
| 661 if (!this.isPrintPreview_) | |
| 662 return false; | |
| 663 | |
| 664 switch (message.data.type.toString()) { | |
| 631 case 'resetPrintPreviewMode': | 665 case 'resetPrintPreviewMode': |
| 632 if (!this.isPrintPreview_) | |
| 633 break; | |
| 634 | |
| 635 if (!this.inPrintPreviewMode_) { | 666 if (!this.inPrintPreviewMode_) { |
| 636 this.inPrintPreviewMode_ = true; | 667 this.inPrintPreviewMode_ = true; |
| 637 this.viewport_.fitToPage(); | 668 this.viewport_.fitToPage(); |
| 638 } | 669 } |
| 639 | 670 |
| 640 // Stash the scroll location so that it can be restored when the new | 671 // Stash the scroll location so that it can be restored when the new |
| 641 // document is loaded. | 672 // document is loaded. |
| 642 this.lastViewportPosition_ = this.viewport_.position; | 673 this.lastViewportPosition_ = this.viewport_.position; |
| 643 | 674 |
| 644 // TODO(raymes): Disable these properly in the plugin. | 675 // TODO(raymes): Disable these properly in the plugin. |
| 645 var printButton = $('print-button'); | 676 var printButton = $('print-button'); |
| 646 if (printButton) | 677 if (printButton) |
| 647 printButton.parentNode.removeChild(printButton); | 678 printButton.parentNode.removeChild(printButton); |
| 648 var saveButton = $('save-button'); | 679 var saveButton = $('save-button'); |
| 649 if (saveButton) | 680 if (saveButton) |
| 650 saveButton.parentNode.removeChild(saveButton); | 681 saveButton.parentNode.removeChild(saveButton); |
| 651 | 682 |
| 652 this.pageIndicator_.pageLabels = message.data.pageNumbers; | 683 this.pageIndicator_.pageLabels = message.data.pageNumbers; |
| 653 | 684 |
| 654 this.plugin_.postMessage({ | 685 this.plugin_.postMessage({ |
| 655 type: 'resetPrintPreviewMode', | 686 type: 'resetPrintPreviewMode', |
| 656 url: message.data.url, | 687 url: message.data.url, |
| 657 grayscale: message.data.grayscale, | 688 grayscale: message.data.grayscale, |
| 658 // If the PDF isn't modifiable we send 0 as the page count so that no | 689 // If the PDF isn't modifiable we send 0 as the page count so that no |
| 659 // blank placeholder pages get appended to the PDF. | 690 // blank placeholder pages get appended to the PDF. |
| 660 pageCount: (message.data.modifiable ? | 691 pageCount: (message.data.modifiable ? |
| 661 message.data.pageNumbers.length : 0) | 692 message.data.pageNumbers.length : 0) |
| 662 }); | 693 }); |
| 663 break; | 694 return true; |
| 664 case 'sendKeyEvent': | 695 case 'sendKeyEvent': |
| 665 var e = document.createEvent('Event'); | 696 var e = document.createEvent('Event'); |
| 666 e.initEvent('scriptingKeypress'); | 697 e.initEvent('scriptingKeypress'); |
| 667 e.keyCode = message.data.keyCode; | 698 e.keyCode = message.data.keyCode; |
| 668 this.handleKeyEvent_(e); | 699 this.handleKeyEvent_(e); |
| 669 break; | 700 return true; |
| 670 case 'setParentWindow': | |
| 671 if (this.parentWindow_ != message.source) { | |
| 672 this.parentWindow_ = message.source; | |
| 673 // If the document has already loaded, we always send a message that | |
| 674 // indicates that so that the embedder is aware. | |
| 675 if (this.loaded_) { | |
| 676 this.sendScriptingMessage_({ | |
| 677 type: 'documentLoaded' | |
| 678 }); | |
| 679 } | |
| 680 } | |
| 681 break; | |
| 682 } | 701 } |
| 702 | |
| 703 return false; | |
| 683 }, | 704 }, |
| 684 | 705 |
| 685 /** | 706 /** |
| 686 * @private | 707 * @private |
| 687 * Send a scripting message outside the extension (typically to | 708 * Send a scripting message outside the extension (typically to |
| 688 * PDFScriptingAPI in a page containing the extension). | 709 * PDFScriptingAPI in a page containing the extension). |
| 689 * @param {Object} message the message to send. | 710 * @param {Object} message the message to send. |
| 690 */ | 711 */ |
| 691 sendScriptingMessage_: function(message) { | 712 sendScriptingMessage_: function(message) { |
| 692 if (this.parentWindow_) | 713 if (this.parentWindow_) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 704 this.streamDetails_.tabId != -1); | 725 this.streamDetails_.tabId != -1); |
| 705 }, | 726 }, |
| 706 | 727 |
| 707 /** | 728 /** |
| 708 * @type {Viewport} the viewport of the PDF viewer. | 729 * @type {Viewport} the viewport of the PDF viewer. |
| 709 */ | 730 */ |
| 710 get viewport() { | 731 get viewport() { |
| 711 return this.viewport_; | 732 return this.viewport_; |
| 712 } | 733 } |
| 713 }; | 734 }; |
| OLD | NEW |