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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 // We should not change zoom level when we are responsible for initiating | 134 // We should not change zoom level when we are responsible for initiating |
135 // the zoom. onZoomChange() is called before setZoomComplete() callback | 135 // the zoom. onZoomChange() is called before setZoomComplete() callback |
136 // when we initiate the zoom. | 136 // when we initiate the zoom. |
137 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) | 137 if ((zoomDelta > MIN_ZOOM_DELTA) && !this.setZoomInProgress_) |
138 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); | 138 this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); |
139 }.bind(this)); | 139 }.bind(this)); |
140 } | 140 } |
141 | 141 |
142 // Parse open pdf parameters. | 142 // Parse open pdf parameters. |
143 this.paramsParser_ = new OpenPDFParamsParser(); | 143 this.paramsParser_ = new OpenPDFParamsParser(); |
| 144 this.navigator_ = new Navigator(this.streamDetails_.originalUrl, |
| 145 this.viewport_, this.paramsParser_); |
144 } | 146 } |
145 | 147 |
146 PDFViewer.prototype = { | 148 PDFViewer.prototype = { |
147 /** | 149 /** |
148 * @private | 150 * @private |
149 * Handle key events. These may come from the user directly or via the | 151 * Handle key events. These may come from the user directly or via the |
150 * scripting API. | 152 * scripting API. |
151 * @param {KeyboardEvent} e the event to handle. | 153 * @param {KeyboardEvent} e the event to handle. |
152 */ | 154 */ |
153 handleKeyEvent_: function(e) { | 155 handleKeyEvent_: function(e) { |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 */ | 342 */ |
341 onPasswordSubmitted_: function(event) { | 343 onPasswordSubmitted_: function(event) { |
342 this.plugin_.postMessage({ | 344 this.plugin_.postMessage({ |
343 type: 'getPasswordComplete', | 345 type: 'getPasswordComplete', |
344 password: event.detail.password | 346 password: event.detail.password |
345 }); | 347 }); |
346 }, | 348 }, |
347 | 349 |
348 /** | 350 /** |
349 * @private | 351 * @private |
350 * Helper function to navigate to given URL. This might involve navigating | |
351 * within the PDF page or opening a new url (in the same tab or a new tab). | |
352 * @param {string} url The URL to navigate to. | |
353 * @param {boolean} newTab Whether to perform the navigation in a new tab or | |
354 * in the current tab. | |
355 */ | |
356 navigate_: function(url, newTab) { | |
357 if (url.length == 0) | |
358 return; | |
359 var originalUrl = this.streamDetails_.originalUrl; | |
360 // If |urlFragment| starts with '#', then it's for the same URL with a | |
361 // different URL fragment. | |
362 if (url.charAt(0) == '#') { | |
363 // if '#' is already present in |originalUrl| then remove old fragment | |
364 // and add new url fragment. | |
365 var hashIndex = originalUrl.search('#'); | |
366 if (hashIndex != -1) | |
367 url = originalUrl.substring(0, hashIndex) + url; | |
368 else | |
369 url = originalUrl + url; | |
370 } | |
371 | |
372 // If there's no scheme, add http. | |
373 if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1) | |
374 url = 'http://' + url; | |
375 | |
376 // Make sure url starts with a valid scheme. | |
377 if (url.indexOf('http://') != 0 && | |
378 url.indexOf('https://') != 0 && | |
379 url.indexOf('ftp://') != 0 && | |
380 url.indexOf('file://') != 0 && | |
381 url.indexOf('mailto:') != 0) { | |
382 return; | |
383 } | |
384 // Make sure url is not only a scheme. | |
385 if (url == 'http://' || | |
386 url == 'https://' || | |
387 url == 'ftp://' || | |
388 url == 'file://' || | |
389 url == 'mailto:') { | |
390 return; | |
391 } | |
392 | |
393 if (newTab) { | |
394 // Prefer the tabs API because it guarantees we can just open a new tab. | |
395 // window.open doesn't have this guarantee. | |
396 if (chrome.tabs) | |
397 chrome.tabs.create({ url: url }); | |
398 else | |
399 window.open(url); | |
400 } else { | |
401 var pageNumber = | |
402 this.paramsParser_.getViewportFromUrlParams(url).page; | |
403 if (pageNumber != undefined) | |
404 this.viewport_.goToPage(pageNumber); | |
405 else | |
406 window.location.href = url; | |
407 } | |
408 }, | |
409 | |
410 /** | |
411 * @private | |
412 * An event handler for handling message events received from the plugin. | 352 * An event handler for handling message events received from the plugin. |
413 * @param {MessageObject} message a message event. | 353 * @param {MessageObject} message a message event. |
414 */ | 354 */ |
415 handlePluginMessage_: function(message) { | 355 handlePluginMessage_: function(message) { |
416 switch (message.data.type.toString()) { | 356 switch (message.data.type.toString()) { |
417 case 'documentDimensions': | 357 case 'documentDimensions': |
418 this.documentDimensions_ = message.data; | 358 this.documentDimensions_ = message.data; |
419 this.viewport_.setDocumentDimensions(this.documentDimensions_); | 359 this.viewport_.setDocumentDimensions(this.documentDimensions_); |
420 // If we received the document dimensions, the password was good so we | 360 // If we received the document dimensions, the password was good so we |
421 // can dismiss the password screen. | 361 // can dismiss the password screen. |
(...skipping 27 matching lines...) Expand all Loading... |
449 this.viewport_.goToPage(message.data.page); | 389 this.viewport_.goToPage(message.data.page); |
450 break; | 390 break; |
451 case 'loadProgress': | 391 case 'loadProgress': |
452 this.updateProgress_(message.data.progress); | 392 this.updateProgress_(message.data.progress); |
453 break; | 393 break; |
454 case 'navigate': | 394 case 'navigate': |
455 // If in print preview, always open a new tab. | 395 // If in print preview, always open a new tab. |
456 if (this.isPrintPreview_) | 396 if (this.isPrintPreview_) |
457 this.navigate_(message.data.url, true); | 397 this.navigate_(message.data.url, true); |
458 else | 398 else |
459 this.navigate_(message.data.url, message.data.newTab); | 399 this.navigator_.navigate(message.data.url, message.data.newTab); |
460 break; | 400 break; |
461 case 'setNamedDestinations': | 401 case 'setNamedDestinations': |
462 this.paramsParser_.namedDestinations = message.data.namedDestinations; | 402 this.paramsParser_.namedDestinations = message.data.namedDestinations; |
463 break; | 403 break; |
464 case 'setScrollPosition': | 404 case 'setScrollPosition': |
465 var position = this.viewport_.position; | 405 var position = this.viewport_.position; |
466 if (message.data.x !== undefined) | 406 if (message.data.x !== undefined) |
467 position.x = message.data.x; | 407 position.x = message.data.x; |
468 if (message.data.y !== undefined) | 408 if (message.data.y !== undefined) |
469 position.y = message.data.y; | 409 position.y = message.data.y; |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 this.streamDetails_.tabId != -1); | 624 this.streamDetails_.tabId != -1); |
685 }, | 625 }, |
686 | 626 |
687 /** | 627 /** |
688 * @type {Viewport} the viewport of the PDF viewer. | 628 * @type {Viewport} the viewport of the PDF viewer. |
689 */ | 629 */ |
690 get viewport() { | 630 get viewport() { |
691 return this.viewport_; | 631 return this.viewport_; |
692 } | 632 } |
693 }; | 633 }; |
OLD | NEW |