| 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"> | 7 <include src="../../../../ui/webui/resources/js/util.js"> |
| 8 <include src="open_pdf_params_parser.js"> | 8 <include src="open_pdf_params_parser.js"> |
| 9 <include src="pdf_scripting_api.js"> | 9 <include src="pdf_scripting_api.js"> |
| 10 <include src="viewport.js"> | 10 <include src="viewport.js"> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 /** | 28 /** |
| 29 * The minimum number of pixels to offset the toolbar by from the bottom and | 29 * The minimum number of pixels to offset the toolbar by from the bottom and |
| 30 * right side of the screen. | 30 * right side of the screen. |
| 31 */ | 31 */ |
| 32 PDFViewer.MIN_TOOLBAR_OFFSET = 15; | 32 PDFViewer.MIN_TOOLBAR_OFFSET = 15; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Creates a new PDFViewer. There should only be one of these objects per | 35 * Creates a new PDFViewer. There should only be one of these objects per |
| 36 * document. | 36 * document. |
| 37 */ | 37 */ |
| 38 function PDFViewer() { | 38 function PDFViewer(streamDetails) { |
| 39 this.streamDetails = streamDetails; |
| 39 this.loaded = false; | 40 this.loaded = false; |
| 40 | 41 |
| 41 // The sizer element is placed behind the plugin element to cause scrollbars | 42 // 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 | 43 // to be displayed in the window. It is sized according to the document size |
| 43 // of the pdf and zoom level. | 44 // of the pdf and zoom level. |
| 44 this.sizer_ = $('sizer'); | 45 this.sizer_ = $('sizer'); |
| 45 this.toolbar_ = $('toolbar'); | 46 this.toolbar_ = $('toolbar'); |
| 46 this.pageIndicator_ = $('page-indicator'); | 47 this.pageIndicator_ = $('page-indicator'); |
| 47 this.progressBar_ = $('progress-bar'); | 48 this.progressBar_ = $('progress-bar'); |
| 48 this.passwordScreen_ = $('password-screen'); | 49 this.passwordScreen_ = $('password-screen'); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 70 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), | 71 this.plugin_.addEventListener('message', this.handlePluginMessage_.bind(this), |
| 71 false); | 72 false); |
| 72 | 73 |
| 73 // Handle scripting messages from outside the extension that wish to interact | 74 // 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 | 75 // with it. We also send a message indicating that extension has loaded and |
| 75 // is ready to receive messages. | 76 // is ready to receive messages. |
| 76 window.addEventListener('message', this.handleScriptingMessage_.bind(this), | 77 window.addEventListener('message', this.handleScriptingMessage_.bind(this), |
| 77 false); | 78 false); |
| 78 this.sendScriptingMessage_({type: 'readyToReceive'}); | 79 this.sendScriptingMessage_({type: 'readyToReceive'}); |
| 79 | 80 |
| 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); | 81 this.plugin_.setAttribute('src', this.streamDetails.originalUrl); |
| 103 this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl); | 82 this.plugin_.setAttribute('stream-url', this.streamDetails.streamUrl); |
| 104 var headers = ''; | 83 var headers = ''; |
| 105 for (var header in this.streamDetails.responseHeaders) { | 84 for (var header in this.streamDetails.responseHeaders) { |
| 106 headers += header + ': ' + | 85 headers += header + ': ' + |
| 107 this.streamDetails.responseHeaders[header] + '\n'; | 86 this.streamDetails.responseHeaders[header] + '\n'; |
| 108 } | 87 } |
| 109 this.plugin_.setAttribute('headers', headers); | 88 this.plugin_.setAttribute('headers', headers); |
| 110 | 89 |
| 111 if (window.top == window) | 90 if (window.top == window) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 123 | 102 |
| 124 // Setup the button event listeners. | 103 // Setup the button event listeners. |
| 125 $('fit-to-width-button').addEventListener('click', | 104 $('fit-to-width-button').addEventListener('click', |
| 126 this.viewport_.fitToWidth.bind(this.viewport_)); | 105 this.viewport_.fitToWidth.bind(this.viewport_)); |
| 127 $('fit-to-page-button').addEventListener('click', | 106 $('fit-to-page-button').addEventListener('click', |
| 128 this.viewport_.fitToPage.bind(this.viewport_)); | 107 this.viewport_.fitToPage.bind(this.viewport_)); |
| 129 $('zoom-in-button').addEventListener('click', | 108 $('zoom-in-button').addEventListener('click', |
| 130 this.viewport_.zoomIn.bind(this.viewport_)); | 109 this.viewport_.zoomIn.bind(this.viewport_)); |
| 131 $('zoom-out-button').addEventListener('click', | 110 $('zoom-out-button').addEventListener('click', |
| 132 this.viewport_.zoomOut.bind(this.viewport_)); | 111 this.viewport_.zoomOut.bind(this.viewport_)); |
| 133 $('save-button-link').href = this.streamDetails.originalUrl; | 112 $('save-button-link').addEventListener('click', this.save_.bind(this)); |
| 134 $('print-button').addEventListener('click', this.print_.bind(this)); | 113 $('print-button').addEventListener('click', this.print_.bind(this)); |
| 135 | 114 |
| 136 // Setup the keyboard event listener. | 115 // Setup the keyboard event listener. |
| 137 document.onkeydown = this.handleKeyEvent_.bind(this); | 116 document.onkeydown = this.handleKeyEvent_.bind(this); |
| 138 | 117 |
| 139 // Set up the zoom API. | 118 // Set up the zoom API. |
| 140 if (chrome.tabs) { | 119 if (chrome.tabs) { |
| 141 chrome.tabs.setZoomSettings({mode: 'manual', scope: 'per-tab'}, | 120 chrome.tabs.setZoomSettings({mode: 'manual', scope: 'per-tab'}, |
| 142 this.afterZoom_.bind(this)); | 121 this.afterZoom_.bind(this)); |
| 143 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { | 122 chrome.tabs.onZoomChange.addListener(function(zoomChangeInfo) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 173 var fromScriptingAPI = e.type == 'scriptingKeypress'; | 152 var fromScriptingAPI = e.type == 'scriptingKeypress'; |
| 174 | 153 |
| 175 var pageUpHandler = function() { | 154 var pageUpHandler = function() { |
| 176 // Go to the previous page if we are fit-to-page. | 155 // Go to the previous page if we are fit-to-page. |
| 177 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 156 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 178 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 157 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
| 179 // Since we do the movement of the page. | 158 // Since we do the movement of the page. |
| 180 e.preventDefault(); | 159 e.preventDefault(); |
| 181 } else if (fromScriptingAPI) { | 160 } else if (fromScriptingAPI) { |
| 182 position.y -= this.viewport.size.height; | 161 position.y -= this.viewport.size.height; |
| 183 this.viewport.position = position; | 162 this.viewport_.position = position; |
| 184 } | 163 } |
| 185 }; | 164 }.bind(this); |
| 186 var pageDownHandler = function() { | 165 var pageDownHandler = function() { |
| 187 // Go to the next page if we are fit-to-page. | 166 // Go to the next page if we are fit-to-page. |
| 188 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 167 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 189 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 168 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
| 190 // Since we do the movement of the page. | 169 // Since we do the movement of the page. |
| 191 e.preventDefault(); | 170 e.preventDefault(); |
| 192 } else if (fromScriptingAPI) { | 171 } else if (fromScriptingAPI) { |
| 193 position.y += this.viewport.size.height; | 172 position.y += this.viewport.size.height; |
| 194 this.viewport.position = position; | 173 this.viewport_.position = position; |
| 195 } | 174 } |
| 196 }; | 175 }.bind(this); |
| 197 | 176 |
| 198 switch (e.keyCode) { | 177 switch (e.keyCode) { |
| 199 case 32: // Space key. | 178 case 32: // Space key. |
| 200 if (e.shiftKey) | 179 if (e.shiftKey) |
| 201 pageUpHandler(); | 180 pageUpHandler(); |
| 202 else | 181 else |
| 203 pageDownHandler(); | 182 pageDownHandler(); |
| 204 return; | 183 return; |
| 205 case 33: // Page up key. | 184 case 33: // Page up key. |
| 206 pageUpHandler(); | 185 pageUpHandler(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 235 position.x += Viewport.SCROLL_INCREMENT; | 214 position.x += Viewport.SCROLL_INCREMENT; |
| 236 this.viewport.position = position; | 215 this.viewport.position = position; |
| 237 } | 216 } |
| 238 return; | 217 return; |
| 239 case 40: // Down arrow key. | 218 case 40: // Down arrow key. |
| 240 if (fromScriptingAPI) { | 219 if (fromScriptingAPI) { |
| 241 position.y += Viewport.SCROLL_INCREMENT; | 220 position.y += Viewport.SCROLL_INCREMENT; |
| 242 this.viewport.position = position; | 221 this.viewport.position = position; |
| 243 } | 222 } |
| 244 return; | 223 return; |
| 245 case 83: // s key. | |
| 246 if (e.ctrlKey || e.metaKey) { | |
| 247 // Simulate a click on the button so that the <a download ...> | |
| 248 // attribute is used. | |
| 249 $('save-button-link').click(); | |
| 250 // Since we do the saving of the page. | |
| 251 e.preventDefault(); | |
| 252 } | |
| 253 return; | |
| 254 case 80: // p key. | 224 case 80: // p key. |
| 255 if (e.ctrlKey || e.metaKey) { | 225 if (e.ctrlKey || e.metaKey) { |
| 256 this.print_(); | 226 this.print_(); |
| 257 // Since we do the printing of the page. | 227 // Since we do the printing of the page. |
| 258 e.preventDefault(); | 228 e.preventDefault(); |
| 259 } | 229 } |
| 260 return; | 230 return; |
| 261 } | 231 } |
| 262 }, | 232 }, |
| 263 | 233 |
| 264 /** | 234 /** |
| 265 * @private | 235 * @private |
| 266 * Notify the plugin to print. | 236 * Notify the plugin to print. |
| 267 */ | 237 */ |
| 268 print_: function() { | 238 print_: function() { |
| 269 this.plugin_.postMessage({ | 239 this.plugin_.postMessage({ |
| 270 type: 'print', | 240 type: 'print', |
| 271 }); | 241 }); |
| 272 }, | 242 }, |
| 273 | 243 |
| 274 /** | 244 /** |
| 275 * @private | 245 * @private |
| 246 * Notify the plugin to save. |
| 247 */ |
| 248 save_: function() { |
| 249 this.plugin_.postMessage({ |
| 250 type: 'save' |
| 251 }); |
| 252 }, |
| 253 |
| 254 |
| 255 /** |
| 256 * @private |
| 276 * Handle open pdf parameters. This function updates the viewport as per | 257 * Handle open pdf parameters. This function updates the viewport as per |
| 277 * the parameters mentioned in the url while opening pdf. The order is | 258 * the parameters mentioned in the url while opening pdf. The order is |
| 278 * important as later actions can override the effects of previous actions. | 259 * important as later actions can override the effects of previous actions. |
| 279 */ | 260 */ |
| 280 handleURLParams_: function() { | 261 handleURLParams_: function() { |
| 281 if (this.urlParams_.page) | 262 if (this.urlParams_.page) |
| 282 this.viewport_.goToPage(this.urlParams_.page); | 263 this.viewport_.goToPage(this.urlParams_.page); |
| 283 if (this.urlParams_.position) { | 264 if (this.urlParams_.position) { |
| 284 // Make sure we don't cancel effect of page parameter. | 265 // Make sure we don't cancel effect of page parameter. |
| 285 this.viewport_.position = { | 266 this.viewport_.position = { |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 }, | 548 }, |
| 568 | 549 |
| 569 /** | 550 /** |
| 570 * @type {Viewport} the viewport of the PDF viewer. | 551 * @type {Viewport} the viewport of the PDF viewer. |
| 571 */ | 552 */ |
| 572 get viewport() { | 553 get viewport() { |
| 573 return this.viewport_; | 554 return this.viewport_; |
| 574 } | 555 } |
| 575 }; | 556 }; |
| 576 | 557 |
| 577 var viewer = new PDFViewer(); | 558 var viewer; |
| 559 function initPlugin(streamDetails) { |
| 560 viewer = new PDFViewer(streamDetails); |
| 561 } |
| 562 |
| 563 function initUrl() { |
| 564 // If the viewer is started from the browser plugin, the plugin ID will be |
| 565 // passed in. |
| 566 var params = window.location.search.substring(1).split('='); |
| 567 var viewId = ''; |
| 568 if (params.length == 2 && params[0] == 'id') |
| 569 viewId = params[1]; |
| 570 |
| 571 if (viewId) { |
| 572 chrome.runtime.sendMessage('mhjfbmdgcfjbbpaeojofohoefgiehjai', |
| 573 {viewId: viewId}, |
| 574 initPlugin); |
| 575 return; |
| 576 } |
| 577 |
| 578 // The viewer may be started via a background page from the extension. |
| 579 var streamDetails; |
| 580 if (chrome.extension && |
| 581 chrome.extension.getBackgroundPage && |
| 582 chrome.extension.getBackgroundPage()) { |
| 583 streamDetails = |
| 584 chrome.extension.getBackgroundPage().popStreamDetails(); |
| 585 if (streamDetails) { |
| 586 initPlugin(streamDetails); |
| 587 return; |
| 588 } |
| 589 } |
| 590 |
| 591 // The viewer may be started directly by passing in the URL of the PDF to load |
| 592 // as the query string. This is used for print preview in particular. The URL |
| 593 // of this page will be of the form |
| 594 // "chrome-extension://<extension id>?<pdf url>". We pull out the <pdf url> |
| 595 // part here. |
| 596 var url = window.location.search.substring(1); |
| 597 streamDetails = { |
| 598 streamUrl: url, |
| 599 originalUrl: url, |
| 600 responseHeaders: '' |
| 601 }; |
| 602 initPlugin(streamDetails); |
| 603 } |
| 604 |
| 605 initUrl(); |
| OLD | NEW |