1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * Creates a new Navigator for navigating to links inside or outside the PDF.
9 * @param {string} originalUrl The original page URL.
10 * @param {Object} viewport The viewport info of the page.
11 * @param {Object} paramsParser The object for URL parsing.
12 * @param {Function} navigateInCurrentTabCallback The Callback function that
13 * gets called when navigation happens in the current tab.
14 * @param {Function} navigateInNewTabCallback The Callback function that gets
15 * called when navigation happens in the new tab.
17 function Navigator(originalUrl,
20 navigateInCurrentTabCallback,
21 navigateInNewTabCallback) {
22 this.originalUrl_ = originalUrl;
23 this.viewport_ = viewport;
24 this.paramsParser_ = paramsParser;
25 this.navigateInCurrentTabCallback_ = navigateInCurrentTabCallback;
26 this.navigateInNewTabCallback_ = navigateInNewTabCallback;
29 Navigator.prototype = {
32 * Function to navigate to the given URL. This might involve navigating
33 * within the PDF page or opening a new url (in the same tab or a new tab).
34 * @param {string} url The URL to navigate to.
35 * @param {boolean} newTab Whether to perform the navigation in a new tab or
38 navigate: function(url, newTab) {
41 // If |urlFragment| starts with '#', then it's for the same URL with a
42 // different URL fragment.
43 if (url.charAt(0) == '#') {
44 // if '#' is already present in |originalUrl| then remove old fragment
45 // and add new url fragment.
46 var hashIndex = this.originalUrl_.search('#');
48 url = this.originalUrl_.substring(0, hashIndex) + url;
50 url = this.originalUrl_ + url;
53 // If there's no scheme, add http.
54 if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1)
55 url = 'http://' + url;
57 // Make sure inputURL starts with a valid scheme.
58 if (url.indexOf('http://') != 0 &&
59 url.indexOf('https://') != 0 &&
60 url.indexOf('ftp://') != 0 &&
61 url.indexOf('file://') != 0 &&
62 url.indexOf('mailto:') != 0) {
65 // Make sure inputURL is not only a scheme.
66 if (url == 'http://' ||
75 this.navigateInNewTabCallback_(url);
77 this.paramsParser_.getViewportFromUrlParams(
78 url, this.onViewportReceived_.bind(this));
84 * Called when the viewport position is received.
85 * @param {Object} viewportPosition Dictionary containing the viewport
88 onViewportReceived_: function(viewportPosition) {
89 var pageNumber = viewportPosition.page;
90 if (pageNumber != undefined)
91 this.viewport_.goToPage(pageNumber);
93 this.navigateInCurrentTabCallback_(viewportPosition['url']);