| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.isPrintPreview_ = | 51 this.isPrintPreview_ = |
| 52 this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; | 52 this.streamDetails_.originalUrl.indexOf('chrome://print') == 0; |
| 53 this.isMaterial_ = location.pathname.substring(1) == 'index-material.html'; |
| 53 | 54 |
| 54 // The sizer element is placed behind the plugin element to cause scrollbars | 55 // The sizer element is placed behind the plugin element to cause scrollbars |
| 55 // to be displayed in the window. It is sized according to the document size | 56 // to be displayed in the window. It is sized according to the document size |
| 56 // of the pdf and zoom level. | 57 // of the pdf and zoom level. |
| 57 this.sizer_ = $('sizer'); | 58 this.sizer_ = $('sizer'); |
| 58 this.toolbar_ = $('toolbar'); | 59 this.toolbar_ = $('toolbar'); |
| 59 this.pageIndicator_ = $('page-indicator'); | 60 this.pageIndicator_ = $('page-indicator'); |
| 60 this.progressBar_ = $('progress-bar'); | 61 this.progressBar_ = $('progress-bar'); |
| 61 this.passwordScreen_ = $('password-screen'); | 62 this.passwordScreen_ = $('password-screen'); |
| 62 this.passwordScreen_.addEventListener('password-submitted', | 63 this.passwordScreen_.addEventListener('password-submitted', |
| 63 this.onPasswordSubmitted_.bind(this)); | 64 this.onPasswordSubmitted_.bind(this)); |
| 64 this.errorScreen_ = $('error-screen'); | 65 this.errorScreen_ = $('error-screen'); |
| 66 this.toolbarHeight_ = this.isMaterial_ ? $('pdf-toolbar').clientHeight : 0; |
| 65 | 67 |
| 66 // Create the viewport. | 68 // Create the viewport. |
| 67 this.viewport_ = new Viewport(window, | 69 this.viewport_ = new Viewport(window, |
| 68 this.sizer_, | 70 this.sizer_, |
| 69 this.viewportChanged_.bind(this), | 71 this.viewportChanged_.bind(this), |
| 70 this.beforeZoom_.bind(this), | 72 this.beforeZoom_.bind(this), |
| 71 this.afterZoom_.bind(this), | 73 this.afterZoom_.bind(this), |
| 72 getScrollbarWidth()); | 74 getScrollbarWidth(), |
| 75 this.toolbarHeight_); |
| 73 // Create the plugin object dynamically so we can set its src. The plugin | 76 // Create the plugin object dynamically so we can set its src. The plugin |
| 74 // element is sized to fill the entire window and is set to be fixed | 77 // element is sized to fill the entire window and is set to be fixed |
| 75 // positioning, acting as a viewport. The plugin renders into this viewport | 78 // positioning, acting as a viewport. The plugin renders into this viewport |
| 76 // according to the scroll position of the window. | 79 // according to the scroll position of the window. |
| 77 this.plugin_ = document.createElement('embed'); | 80 this.plugin_ = document.createElement('embed'); |
| 78 // NOTE: The plugin's 'id' field must be set to 'plugin' since | 81 // NOTE: The plugin's 'id' field must be set to 'plugin' since |
| 79 // chrome/renderer/printing/print_web_view_helper.cc actually references it. | 82 // chrome/renderer/printing/print_web_view_helper.cc actually references it. |
| 80 this.plugin_.id = 'plugin'; | 83 this.plugin_.id = 'plugin'; |
| 81 this.plugin_.type = 'application/x-google-chrome-pdf'; | 84 this.plugin_.type = 'application/x-google-chrome-pdf'; |
| 82 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), | 85 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), |
| 83 false); | 86 false); |
| 87 if (this.isMaterial_) { |
| 88 this.plugin_.style.height = |
| 89 (window.innerHeight - this.toolbarHeight_) + 'px'; |
| 90 this.plugin_.style.width = window.innerWidth + 'px'; |
| 91 } |
| 84 | 92 |
| 85 // Handle scripting messages from outside the extension that wish to interact | 93 // Handle scripting messages from outside the extension that wish to interact |
| 86 // with it. We also send a message indicating that extension has loaded and | 94 // with it. We also send a message indicating that extension has loaded and |
| 87 // is ready to receive messages. | 95 // is ready to receive messages. |
| 88 window.addEventListener('message', this.handleScriptingMessage.bind(this), | 96 window.addEventListener('message', this.handleScriptingMessage.bind(this), |
| 89 false); | 97 false); |
| 90 | 98 |
| 91 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); | 99 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); |
| 100 if (this.isMaterial_) |
| 101 $('title').textContent = document.title; |
| 92 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); | 102 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); |
| 93 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); | 103 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); |
| 94 var headers = ''; | 104 var headers = ''; |
| 95 for (var header in this.streamDetails_.responseHeaders) { | 105 for (var header in this.streamDetails_.responseHeaders) { |
| 96 headers += header + ': ' + | 106 headers += header + ': ' + |
| 97 this.streamDetails_.responseHeaders[header] + '\n'; | 107 this.streamDetails_.responseHeaders[header] + '\n'; |
| 98 } | 108 } |
| 99 this.plugin_.setAttribute('headers', headers); | 109 this.plugin_.setAttribute('headers', headers); |
| 100 | 110 |
| 101 if (!this.streamDetails_.embedded) | 111 if (!this.streamDetails_.embedded) |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 this.viewport_.setZoom(urlParams.zoom); | 313 this.viewport_.setZoom(urlParams.zoom); |
| 304 }, | 314 }, |
| 305 | 315 |
| 306 /** | 316 /** |
| 307 * @private | 317 * @private |
| 308 * Update the loading progress of the document in response to a progress | 318 * Update the loading progress of the document in response to a progress |
| 309 * message being received from the plugin. | 319 * message being received from the plugin. |
| 310 * @param {number} progress the progress as a percentage. | 320 * @param {number} progress the progress as a percentage. |
| 311 */ | 321 */ |
| 312 updateProgress_: function(progress) { | 322 updateProgress_: function(progress) { |
| 313 this.progressBar_.progress = progress; | 323 if (this.isMaterial_) |
| 324 this.progressBar_.value = progress; |
| 325 else |
| 326 this.progressBar_.progress = progress; |
| 327 |
| 314 if (progress == -1) { | 328 if (progress == -1) { |
| 315 // Document load failed. | 329 // Document load failed. |
| 316 this.errorScreen_.style.visibility = 'visible'; | 330 this.errorScreen_.style.visibility = 'visible'; |
| 317 this.sizer_.style.display = 'none'; | 331 this.sizer_.style.display = 'none'; |
| 318 this.toolbar_.style.visibility = 'hidden'; | 332 this.toolbar_.style.visibility = 'hidden'; |
| 319 if (this.passwordScreen_.active) { | 333 if (this.passwordScreen_.active) { |
| 320 this.passwordScreen_.deny(); | 334 this.passwordScreen_.deny(); |
| 321 this.passwordScreen_.active = false; | 335 this.passwordScreen_.active = false; |
| 322 } | 336 } |
| 323 } else if (progress == 100) { | 337 } else if (progress == 100) { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 }, | 550 }, |
| 537 | 551 |
| 538 /** | 552 /** |
| 539 * @private | 553 * @private |
| 540 * A callback that's called after the viewport changes. | 554 * A callback that's called after the viewport changes. |
| 541 */ | 555 */ |
| 542 viewportChanged_: function() { | 556 viewportChanged_: function() { |
| 543 if (!this.documentDimensions_) | 557 if (!this.documentDimensions_) |
| 544 return; | 558 return; |
| 545 | 559 |
| 560 if (this.isMaterial_) { |
| 561 this.plugin_.style.height = |
| 562 (window.innerHeight - this.toolbarHeight_) + 'px'; |
| 563 this.plugin_.style.width = window.innerWidth + 'px'; |
| 564 } |
| 565 |
| 546 // Update the buttons selected. | 566 // Update the buttons selected. |
| 547 $('fit-to-page-button').classList.remove('polymer-selected'); | 567 $('fit-to-page-button').classList.remove('polymer-selected'); |
| 548 $('fit-to-width-button').classList.remove('polymer-selected'); | 568 $('fit-to-width-button').classList.remove('polymer-selected'); |
| 549 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 569 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 550 $('fit-to-page-button').classList.add('polymer-selected'); | 570 $('fit-to-page-button').classList.add('polymer-selected'); |
| 551 } else if (this.viewport_.fittingType == | 571 } else if (this.viewport_.fittingType == |
| 552 Viewport.FittingType.FIT_TO_WIDTH) { | 572 Viewport.FittingType.FIT_TO_WIDTH) { |
| 553 $('fit-to-width-button').classList.add('polymer-selected'); | 573 $('fit-to-width-button').classList.add('polymer-selected'); |
| 554 } | 574 } |
| 555 | 575 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 this.streamDetails_.tabId != -1); | 704 this.streamDetails_.tabId != -1); |
| 685 }, | 705 }, |
| 686 | 706 |
| 687 /** | 707 /** |
| 688 * @type {Viewport} the viewport of the PDF viewer. | 708 * @type {Viewport} the viewport of the PDF viewer. |
| 689 */ | 709 */ |
| 690 get viewport() { | 710 get viewport() { |
| 691 return this.viewport_; | 711 return this.viewport_; |
| 692 } | 712 } |
| 693 }; | 713 }; |
| OLD | NEW |