| 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 <include src="../../../../ui/webui/resources/js/util.js"> | |
| 8 <include src="open_pdf_params_parser.js"> | |
| 9 <include src="pdf_scripting_api.js"> | |
| 10 <include src="viewport.js"> | |
| 11 | |
| 12 /** | 7 /** |
| 13 * @return {number} Width of a scrollbar in pixels | 8 * @return {number} Width of a scrollbar in pixels |
| 14 */ | 9 */ |
| 15 function getScrollbarWidth() { | 10 function getScrollbarWidth() { |
| 16 var div = document.createElement('div'); | 11 var div = document.createElement('div'); |
| 17 div.style.visibility = 'hidden'; | 12 div.style.visibility = 'hidden'; |
| 18 div.style.overflow = 'scroll'; | 13 div.style.overflow = 'scroll'; |
| 19 div.style.width = '50px'; | 14 div.style.width = '50px'; |
| 20 div.style.height = '50px'; | 15 div.style.height = '50px'; |
| 21 div.style.position = 'absolute'; | 16 div.style.position = 'absolute'; |
| 22 document.body.appendChild(div); | 17 document.body.appendChild(div); |
| 23 var result = div.offsetWidth - div.clientWidth; | 18 var result = div.offsetWidth - div.clientWidth; |
| 24 div.parentNode.removeChild(div); | 19 div.parentNode.removeChild(div); |
| 25 return result; | 20 return result; |
| 26 } | 21 } |
| 27 | 22 |
| 28 /** | 23 /** |
| 29 * The minimum number of pixels to offset the toolbar by from the bottom and | 24 * The minimum number of pixels to offset the toolbar by from the bottom and |
| 30 * right side of the screen. | 25 * right side of the screen. |
| 31 */ | 26 */ |
| 32 PDFViewer.MIN_TOOLBAR_OFFSET = 15; | 27 PDFViewer.MIN_TOOLBAR_OFFSET = 15; |
| 33 | 28 |
| 34 /** | 29 /** |
| 35 * Creates a new PDFViewer. There should only be one of these objects per | 30 * Creates a new PDFViewer. There should only be one of these objects per |
| 36 * document. | 31 * document. |
| 32 * @param {Object} streamDetails The stream object which points to the data |
| 33 * contained in the PDF. |
| 37 */ | 34 */ |
| 38 function PDFViewer() { | 35 function PDFViewer(streamDetails) { |
| 36 this.streamDetails = streamDetails; |
| 39 this.loaded = false; | 37 this.loaded = false; |
| 40 | 38 |
| 41 // The sizer element is placed behind the plugin element to cause scrollbars | 39 // The sizer element is placed behind the plugin element to cause scrollbars |
| 42 // to be displayed in the window. It is sized according to the document size | 40 // to be displayed in the window. It is sized according to the document size |
| 43 // of the pdf and zoom level. | 41 // of the pdf and zoom level. |
| 44 this.sizer_ = $('sizer'); | 42 this.sizer_ = $('sizer'); |
| 45 this.toolbar_ = $('toolbar'); | 43 this.toolbar_ = $('toolbar'); |
| 46 this.pageIndicator_ = $('page-indicator'); | 44 this.pageIndicator_ = $('page-indicator'); |
| 47 this.progressBar_ = $('progress-bar'); | 45 this.progressBar_ = $('progress-bar'); |
| 48 this.passwordScreen_ = $('password-screen'); | 46 this.passwordScreen_ = $('password-screen'); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 70 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), | 68 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), |
| 71 false); | 69 false); |
| 72 | 70 |
| 73 // Handle scripting messages from outside the extension that wish to interact | 71 // Handle scripting messages from outside the extension that wish to interact |
| 74 // with it. We also send a message indicating that extension has loaded and | 72 // with it. We also send a message indicating that extension has loaded and |
| 75 // is ready to receive messages. | 73 // is ready to receive messages. |
| 76 window.addEventListener('message', this.handleScriptingMessage_.bind(this), | 74 window.addEventListener('message', this.handleScriptingMessage_.bind(this), |
| 77 false); | 75 false); |
| 78 this.sendScriptingMessage_({type: 'readyToReceive'}); | 76 this.sendScriptingMessage_({type: 'readyToReceive'}); |
| 79 | 77 |
| 80 // If the viewer is started from a MIME type request, there will be a | |
| 81 // background page and stream details object with the details of the request. | |
| 82 // Otherwise, we take the query string of the URL to indicate the URL of the | |
| 83 // PDF to load. This is used for print preview in particular. | |
| 84 if (chrome.extension.getBackgroundPage && | |
| 85 chrome.extension.getBackgroundPage()) { | |
| 86 this.streamDetails = | |
| 87 chrome.extension.getBackgroundPage().popStreamDetails(); | |
| 88 } | |
| 89 | |
| 90 if (!this.streamDetails) { | |
| 91 // The URL of this page will be of the form | |
| 92 // "chrome-extension://<extension id>?<pdf url>". We pull out the <pdf url> | |
| 93 // part here. | |
| 94 var url = window.location.search.substring(1); | |
| 95 this.streamDetails = { | |
| 96 streamUrl: url, | |
| 97 originalUrl: url, | |
| 98 responseHeaders: '' | |
| 99 }; | |
| 100 } | |
| 101 | |
| 102 this.plugin_.setAttribute('src', this.streamDetails.originalUrl); | 78 this.plugin_.setAttribute('src', this.streamDetails.originalUrl); |
| 103 this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl); | 79 this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl); |
| 104 var headers = ''; | 80 var headers = ''; |
| 105 for (var header in this.streamDetails.responseHeaders) { | 81 for (var header in this.streamDetails.responseHeaders) { |
| 106 headers += header + ': ' + | 82 headers += header + ': ' + |
| 107 this.streamDetails.responseHeaders[header] + '\n'; | 83 this.streamDetails.responseHeaders[header] + '\n'; |
| 108 } | 84 } |
| 109 this.plugin_.setAttribute('headers', headers); | 85 this.plugin_.setAttribute('headers', headers); |
| 110 | 86 |
| 111 if (window.top == window) | 87 if (window.top == window) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 var pageUpHandler = function() { | 151 var pageUpHandler = function() { |
| 176 // Go to the previous page if we are fit-to-page. | 152 // Go to the previous page if we are fit-to-page. |
| 177 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 153 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 178 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 154 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
| 179 // Since we do the movement of the page. | 155 // Since we do the movement of the page. |
| 180 e.preventDefault(); | 156 e.preventDefault(); |
| 181 } else if (fromScriptingAPI) { | 157 } else if (fromScriptingAPI) { |
| 182 position.y -= this.viewport.size.height; | 158 position.y -= this.viewport.size.height; |
| 183 this.viewport.position = position; | 159 this.viewport.position = position; |
| 184 } | 160 } |
| 185 }; | 161 }.bind(this); |
| 186 var pageDownHandler = function() { | 162 var pageDownHandler = function() { |
| 187 // Go to the next page if we are fit-to-page. | 163 // Go to the next page if we are fit-to-page. |
| 188 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 164 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 189 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 165 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
| 190 // Since we do the movement of the page. | 166 // Since we do the movement of the page. |
| 191 e.preventDefault(); | 167 e.preventDefault(); |
| 192 } else if (fromScriptingAPI) { | 168 } else if (fromScriptingAPI) { |
| 193 position.y += this.viewport.size.height; | 169 position.y += this.viewport.size.height; |
| 194 this.viewport.position = position; | 170 this.viewport.position = position; |
| 195 } | 171 } |
| 196 }; | 172 }.bind(this); |
| 197 | 173 |
| 198 switch (e.keyCode) { | 174 switch (e.keyCode) { |
| 199 case 32: // Space key. | 175 case 32: // Space key. |
| 200 if (e.shiftKey) | 176 if (e.shiftKey) |
| 201 pageUpHandler(); | 177 pageUpHandler(); |
| 202 else | 178 else |
| 203 pageDownHandler(); | 179 pageDownHandler(); |
| 204 return; | 180 return; |
| 205 case 33: // Page up key. | 181 case 33: // Page up key. |
| 206 pageUpHandler(); | 182 pageUpHandler(); |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 window.parent.postMessage(message, '*'); | 542 window.parent.postMessage(message, '*'); |
| 567 }, | 543 }, |
| 568 | 544 |
| 569 /** | 545 /** |
| 570 * @type {Viewport} the viewport of the PDF viewer. | 546 * @type {Viewport} the viewport of the PDF viewer. |
| 571 */ | 547 */ |
| 572 get viewport() { | 548 get viewport() { |
| 573 return this.viewport_; | 549 return this.viewport_; |
| 574 } | 550 } |
| 575 }; | 551 }; |
| 576 | |
| 577 var viewer = new PDFViewer(); | |
| OLD | NEW |