| 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // We should not change zoom level when we are responsible for initiating | 146 // We should not change zoom level when we are responsible for initiating |
| 147 // the zoom. onZoomChange() is called before setZoomComplete() callback | 147 // the zoom. onZoomChange() is called before setZoomComplete() callback |
| 148 // when we initiate the zoom. | 148 // when we initiate the zoom. |
| 149 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) | 149 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) |
| 150 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); | 150 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); |
| 151 }.bind(this)); | 151 }.bind(this)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Parse open pdf parameters. | 154 // Parse open pdf parameters. |
| 155 this.paramsParser_ = new OpenPDFParamsParser(); | 155 this.paramsParser_ = new OpenPDFParamsParser(); |
| 156 this.navigator_ = new Navigator(this.streamDetails_.originalUrl, |
| 157 this.viewport_, this.paramsParser_); |
| 156 } | 158 } |
| 157 | 159 |
| 158 PDFViewer.prototype = { | 160 PDFViewer.prototype = { |
| 159 /** | 161 /** |
| 160 * @private | 162 * @private |
| 161 * Handle key events. These may come from the user directly or via the | 163 * Handle key events. These may come from the user directly or via the |
| 162 * scripting API. | 164 * scripting API. |
| 163 * @param {KeyboardEvent} e the event to handle. | 165 * @param {KeyboardEvent} e the event to handle. |
| 164 */ | 166 */ |
| 165 handleKeyEvent_: function(e) { | 167 handleKeyEvent_: function(e) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 */ | 357 */ |
| 356 onPasswordSubmitted_: function(event) { | 358 onPasswordSubmitted_: function(event) { |
| 357 this.plugin_.postMessage({ | 359 this.plugin_.postMessage({ |
| 358 type: 'getPasswordComplete', | 360 type: 'getPasswordComplete', |
| 359 password: event.detail.password | 361 password: event.detail.password |
| 360 }); | 362 }); |
| 361 }, | 363 }, |
| 362 | 364 |
| 363 /** | 365 /** |
| 364 * @private | 366 * @private |
| 365 * Helper function to navigate to given URL. This might involve navigating | |
| 366 * within the PDF page or opening a new url (in the same tab or a new tab). | |
| 367 * @param {string} url The URL to navigate to. | |
| 368 * @param {boolean} newTab Whether to perform the navigation in a new tab or | |
| 369 * in the current tab. | |
| 370 */ | |
| 371 navigate_: function(url, newTab) { | |
| 372 if (url.length == 0) | |
| 373 return; | |
| 374 var originalUrl = this.streamDetails_.originalUrl; | |
| 375 // If |urlFragment| starts with '#', then it's for the same URL with a | |
| 376 // different URL fragment. | |
| 377 if (url.charAt(0) == '#') { | |
| 378 // if '#' is already present in |originalUrl| then remove old fragment | |
| 379 // and add new url fragment. | |
| 380 var hashIndex = originalUrl.search('#'); | |
| 381 if (hashIndex != -1) | |
| 382 url = originalUrl.substring(0, hashIndex) + url; | |
| 383 else | |
| 384 url = originalUrl + url; | |
| 385 } | |
| 386 | |
| 387 // If there's no scheme, add http. | |
| 388 if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1) | |
| 389 url = 'http://' + url; | |
| 390 | |
| 391 // Make sure url starts with a valid scheme. | |
| 392 if (url.indexOf('http://') != 0 && | |
| 393 url.indexOf('https://') != 0 && | |
| 394 url.indexOf('ftp://') != 0 && | |
| 395 url.indexOf('file://') != 0 && | |
| 396 url.indexOf('mailto:') != 0) { | |
| 397 return; | |
| 398 } | |
| 399 // Make sure url is not only a scheme. | |
| 400 if (url == 'http://' || | |
| 401 url == 'https://' || | |
| 402 url == 'ftp://' || | |
| 403 url == 'file://' || | |
| 404 url == 'mailto:') { | |
| 405 return; | |
| 406 } | |
| 407 | |
| 408 if (newTab) { | |
| 409 // Prefer the tabs API because it guarantees we can just open a new tab. | |
| 410 // window.open doesn't have this guarantee. | |
| 411 if (chrome.tabs) | |
| 412 chrome.tabs.create({ url: url }); | |
| 413 else | |
| 414 window.open(url); | |
| 415 } else { | |
| 416 var pageNumber = | |
| 417 this.paramsParser_.getViewportFromUrlParams(url).page; | |
| 418 if (pageNumber != undefined) | |
| 419 this.viewport_.goToPage(pageNumber); | |
| 420 else | |
| 421 window.location.href = url; | |
| 422 } | |
| 423 }, | |
| 424 | |
| 425 /** | |
| 426 * @private | |
| 427 * An event handler for handling message events received from the plugin. | 367 * An event handler for handling message events received from the plugin. |
| 428 * @param {MessageObject} message a message event. | 368 * @param {MessageObject} message a message event. |
| 429 */ | 369 */ |
| 430 handlePluginMessage_: function(message) { | 370 handlePluginMessage_: function(message) { |
| 431 switch (message.data.type.toString()) { | 371 switch (message.data.type.toString()) { |
| 432 case 'documentDimensions': | 372 case 'documentDimensions': |
| 433 this.documentDimensions_ = message.data; | 373 this.documentDimensions_ = message.data; |
| 434 this.viewport_.setDocumentDimensions(this.documentDimensions_); | 374 this.viewport_.setDocumentDimensions(this.documentDimensions_); |
| 435 // If we received the document dimensions, the password was good so we | 375 // If we received the document dimensions, the password was good so we |
| 436 // can dismiss the password screen. | 376 // can dismiss the password screen. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 464 this.viewport_.goToPage(message.data.page); | 404 this.viewport_.goToPage(message.data.page); |
| 465 break; | 405 break; |
| 466 case 'loadProgress': | 406 case 'loadProgress': |
| 467 this.updateProgress_(message.data.progress); | 407 this.updateProgress_(message.data.progress); |
| 468 break; | 408 break; |
| 469 case 'navigate': | 409 case 'navigate': |
| 470 // If in print preview, always open a new tab. | 410 // If in print preview, always open a new tab. |
| 471 if (this.isPrintPreview_) | 411 if (this.isPrintPreview_) |
| 472 this.navigate_(message.data.url, true); | 412 this.navigate_(message.data.url, true); |
| 473 else | 413 else |
| 474 this.navigate_(message.data.url, message.data.newTab); | 414 this.navigator_.navigate(message.data.url, message.data.newTab); |
| 475 break; | 415 break; |
| 476 case 'setNamedDestinations': | 416 case 'setNamedDestinations': |
| 477 this.paramsParser_.namedDestinations = message.data.namedDestinations; | 417 this.paramsParser_.namedDestinations = message.data.namedDestinations; |
| 478 break; | 418 break; |
| 479 case 'setScrollPosition': | 419 case 'setScrollPosition': |
| 480 var position = this.viewport_.position; | 420 var position = this.viewport_.position; |
| 481 if (message.data.x !== undefined) | 421 if (message.data.x !== undefined) |
| 482 position.x = message.data.x; | 422 position.x = message.data.x; |
| 483 if (message.data.y !== undefined) | 423 if (message.data.y !== undefined) |
| 484 position.y = message.data.y; | 424 position.y = message.data.y; |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 this.streamDetails_.tabId != -1); | 667 this.streamDetails_.tabId != -1); |
| 728 }, | 668 }, |
| 729 | 669 |
| 730 /** | 670 /** |
| 731 * @type {Viewport} the viewport of the PDF viewer. | 671 * @type {Viewport} the viewport of the PDF viewer. |
| 732 */ | 672 */ |
| 733 get viewport() { | 673 get viewport() { |
| 734 return this.viewport_; | 674 return this.viewport_; |
| 735 } | 675 } |
| 736 }; | 676 }; |
| OLD | NEW |