Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/browser/resources/pdf/pdf.js

Issue 870453002: Make the scripting OOP PDF API easier to use (take 2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf_scripting_api.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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':
627 case 'print': 639 case 'print':
628 case 'selectAll': 640 case 'selectAll':
629 this.plugin_.postMessage(message.data); 641 this.plugin_.postMessage(message.data);
630 break; 642 break;
643 case 'isDocumentLoaded':
644 // Since this is only hit after the document has been loaded, we can
645 // send a reply immediately.
646 this.sendScriptingMessage_({
647 type: 'documentLoaded'
648 });
649 break;
650 }
651 },
652
653 /**
654 * @private
655 * Handle scripting messages specific to print preview.
656 * @param {MessageObject} message the message to handle.
657 * @return {boolean} true if the message was handled, false otherwise.
658 */
659 handlePrintPreviewScriptingMessage_: function(message) {
660 if (!this.isPrintPreview_)
661 return false;
662
663 switch (message.data.type.toString()) {
664 case 'loadPreviewPage':
665 this.plugin_.postMessage(message.data);
666 return true;
631 case 'resetPrintPreviewMode': 667 case 'resetPrintPreviewMode':
632 if (!this.isPrintPreview_)
633 break;
634
635 if (!this.inPrintPreviewMode_) { 668 if (!this.inPrintPreviewMode_) {
636 this.inPrintPreviewMode_ = true; 669 this.inPrintPreviewMode_ = true;
637 this.viewport_.fitToPage(); 670 this.viewport_.fitToPage();
638 } 671 }
639 672
640 // Stash the scroll location so that it can be restored when the new 673 // Stash the scroll location so that it can be restored when the new
641 // document is loaded. 674 // document is loaded.
642 this.lastViewportPosition_ = this.viewport_.position; 675 this.lastViewportPosition_ = this.viewport_.position;
643 676
644 // TODO(raymes): Disable these properly in the plugin. 677 // TODO(raymes): Disable these properly in the plugin.
645 var printButton = $('print-button'); 678 var printButton = $('print-button');
646 if (printButton) 679 if (printButton)
647 printButton.parentNode.removeChild(printButton); 680 printButton.parentNode.removeChild(printButton);
648 var saveButton = $('save-button'); 681 var saveButton = $('save-button');
649 if (saveButton) 682 if (saveButton)
650 saveButton.parentNode.removeChild(saveButton); 683 saveButton.parentNode.removeChild(saveButton);
651 684
652 this.pageIndicator_.pageLabels = message.data.pageNumbers; 685 this.pageIndicator_.pageLabels = message.data.pageNumbers;
653 686
654 this.plugin_.postMessage({ 687 this.plugin_.postMessage({
655 type: 'resetPrintPreviewMode', 688 type: 'resetPrintPreviewMode',
656 url: message.data.url, 689 url: message.data.url,
657 grayscale: message.data.grayscale, 690 grayscale: message.data.grayscale,
658 // If the PDF isn't modifiable we send 0 as the page count so that no 691 // 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. 692 // blank placeholder pages get appended to the PDF.
660 pageCount: (message.data.modifiable ? 693 pageCount: (message.data.modifiable ?
661 message.data.pageNumbers.length : 0) 694 message.data.pageNumbers.length : 0)
662 }); 695 });
663 break; 696 return true;
664 case 'sendKeyEvent': 697 case 'sendKeyEvent':
665 var e = document.createEvent('Event'); 698 var e = document.createEvent('Event');
666 e.initEvent('scriptingKeypress'); 699 e.initEvent('scriptingKeypress');
667 e.keyCode = message.data.keyCode; 700 e.keyCode = message.data.keyCode;
668 this.handleKeyEvent_(e); 701 this.handleKeyEvent_(e);
669 break; 702 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 } 703 }
704
705 return false;
683 }, 706 },
684 707
685 /** 708 /**
686 * @private 709 * @private
687 * Send a scripting message outside the extension (typically to 710 * Send a scripting message outside the extension (typically to
688 * PDFScriptingAPI in a page containing the extension). 711 * PDFScriptingAPI in a page containing the extension).
689 * @param {Object} message the message to send. 712 * @param {Object} message the message to send.
690 */ 713 */
691 sendScriptingMessage_: function(message) { 714 sendScriptingMessage_: function(message) {
692 if (this.parentWindow_) 715 if (this.parentWindow_)
(...skipping 11 matching lines...) Expand all
704 this.streamDetails_.tabId != -1); 727 this.streamDetails_.tabId != -1);
705 }, 728 },
706 729
707 /** 730 /**
708 * @type {Viewport} the viewport of the PDF viewer. 731 * @type {Viewport} the viewport of the PDF viewer.
709 */ 732 */
710 get viewport() { 733 get viewport() {
711 return this.viewport_; 734 return this.viewport_;
712 } 735 }
713 }; 736 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf_scripting_api.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698