OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 * Creates a new Navigator for navigating to links inside or outside the PDF. | 8 * Creates a new Navigator for navigating to links inside or outside the PDF. |
9 * @param {string} originalUrl The original page URL. | 9 * @param {string} originalUrl The original page URL. |
10 * @param {Object} viewport The viewport info of the page. | 10 * @param {Object} viewport The viewport info of the page. |
11 * @param {Object} paramsParser The object for URL parsing. | 11 * @param {Object} paramsParser The object for URL parsing. |
12 * @param {Function} navigateInCurrentTabCallback The Callback function that | 12 * @param {Object} navigatorDelegate The object with callback functions that |
13 * gets called when navigation happens in the current tab. | 13 * get called when navigation happens in the current tab, a new tab, |
14 * @param {Function} navigateInNewTabCallback The Callback function | 14 * and a new window. |
15 * that gets called when navigation happens in the new tab. | |
16 */ | 15 */ |
17 function Navigator(originalUrl, | 16 function Navigator(originalUrl, viewport, paramsParser, navigatorDelegate) { |
18 viewport, | |
19 paramsParser, | |
20 navigateInCurrentTabCallback, | |
21 navigateInNewTabCallback) { | |
22 this.originalUrl_ = originalUrl; | 17 this.originalUrl_ = originalUrl; |
23 this.viewport_ = viewport; | 18 this.viewport_ = viewport; |
24 this.paramsParser_ = paramsParser; | 19 this.paramsParser_ = paramsParser; |
25 this.navigateInCurrentTabCallback_ = navigateInCurrentTabCallback; | 20 this.navigatorDelegate_ = navigatorDelegate; |
26 this.navigateInNewTabCallback_ = navigateInNewTabCallback; | |
27 } | 21 } |
28 | 22 |
29 /** | 23 /** |
24 * Creates a new NavigatorDelegate for calling callback funcions when | |
raymes
2016/09/07 06:41:19
nit: Creates a new NavigatorDelegate for calling b
jaepark
2016/09/07 17:49:23
Done.
| |
25 * navigation happens. | |
26 * @param {boolean} isInTab Indicates if the PDF viewer is displayed in a tab. | |
27 * @param {boolean} isSourceFileUrl Indicates if the navigation source is a | |
28 * file:// URL. | |
29 */ | |
30 function NavigatorDelegate(isInTab, isSourceFileUrl) { | |
raymes
2016/09/07 06:41:19
nit: perhaps move this class above "function Navig
jaepark
2016/09/07 17:49:23
Done.
| |
31 this.isInTab_ = isInTab; | |
32 this.isSourceFileUrl_ = isSourceFileUrl; | |
33 } | |
34 | |
35 NavigatorDelegate.prototype = { | |
36 /** | |
37 * @public | |
38 * Called when navigation happens in the current tab. | |
raymes
2016/09/07 06:41:19
nit: Called when navigation should happen in the c
jaepark
2016/09/07 17:49:23
Done.
| |
39 * @param {string} url The url to be opened in the current tab. | |
40 */ | |
41 navigateInCurrentTab: function(url) { | |
42 // When the PDFviewer is inside a browser tab, prefer the tabs API because | |
43 // it can navigate from one file:// URL to another. | |
44 if (chrome.tabs && this.isInTab_ && this.isSourceFileUrl_) | |
45 chrome.tabs.update({url: url}); | |
46 else | |
47 window.location.href = url; | |
48 }, | |
49 | |
50 /** | |
51 * @public | |
52 * Called when navigation happens in the new tab. | |
raymes
2016/09/07 06:41:19
nit: Called when navigation should happen in a new
jaepark
2016/09/07 17:49:23
Done.
| |
53 * @param {string} url The url to be opened in the new tab. | |
54 * @param {boolean} active Indicates if the new tab should be the active tab. | |
55 */ | |
56 navigateInNewTab: function(url, active) { | |
57 // Prefer the tabs API because it guarantees we can just open a new tab. | |
58 // window.open doesn't have this guarantee. | |
59 if (chrome.tabs) | |
60 chrome.tabs.create({url: url, active: active}); | |
61 else | |
62 window.open(url); | |
63 }, | |
64 | |
65 /** | |
66 * @public | |
67 * Called when navigation happens in the new window. | |
raymes
2016/09/07 06:41:19
nit: Called when navigation should happen in a new
jaepark
2016/09/07 17:49:23
Done.
| |
68 * @param {string} url The url to be opened in the new window. | |
69 */ | |
70 navigateInNewWindow: function(url) { | |
71 // Prefer the windows API because it guarantees we can just open a new | |
72 // window. window.open with '_blank' argument doesn't have this guarantee. | |
73 if (chrome.windows) | |
74 chrome.windows.create({url: url}); | |
75 else | |
76 window.open(url, '_blank'); | |
77 } | |
78 }; | |
79 | |
80 /** | |
30 * Represents options when navigating to a new url. C++ counterpart of | 81 * Represents options when navigating to a new url. C++ counterpart of |
31 * the enum is in ui/base/window_open_disposition.h. This enum represents | 82 * the enum is in ui/base/window_open_disposition.h. This enum represents |
32 * the only values that are passed from Plugin. | 83 * the only values that are passed from Plugin. |
33 * @enum {number} | 84 * @enum {number} |
34 */ | 85 */ |
35 Navigator.WindowOpenDisposition = { | 86 Navigator.WindowOpenDisposition = { |
36 CURRENT_TAB: 1, | 87 CURRENT_TAB: 1, |
37 NEW_FOREGROUND_TAB: 3, | 88 NEW_FOREGROUND_TAB: 3, |
38 NEW_BACKGROUND_TAB: 4, | 89 NEW_BACKGROUND_TAB: 4, |
39 NEW_WINDOW: 6, | 90 NEW_WINDOW: 6, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 | 122 |
72 if (!this.isValidUrl_(url)) | 123 if (!this.isValidUrl_(url)) |
73 return; | 124 return; |
74 | 125 |
75 switch (disposition) { | 126 switch (disposition) { |
76 case Navigator.WindowOpenDisposition.CURRENT_TAB: | 127 case Navigator.WindowOpenDisposition.CURRENT_TAB: |
77 this.paramsParser_.getViewportFromUrlParams( | 128 this.paramsParser_.getViewportFromUrlParams( |
78 url, this.onViewportReceived_.bind(this)); | 129 url, this.onViewportReceived_.bind(this)); |
79 break; | 130 break; |
80 case Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB: | 131 case Navigator.WindowOpenDisposition.NEW_BACKGROUND_TAB: |
81 this.navigateInNewTabCallback_(url, false); | 132 this.navigatorDelegate_.navigateInNewTab(url, false); |
82 break; | 133 break; |
83 case Navigator.WindowOpenDisposition.NEW_FOREGROUND_TAB: | 134 case Navigator.WindowOpenDisposition.NEW_FOREGROUND_TAB: |
84 this.navigateInNewTabCallback_(url, true); | 135 this.navigatorDelegate_.navigateInNewTab(url, true); |
85 break; | 136 break; |
86 case Navigator.WindowOpenDisposition.NEW_WINDOW: | 137 case Navigator.WindowOpenDisposition.NEW_WINDOW: |
87 // TODO(jaepark): Shift + left clicking a link in PDF should open the | 138 this.navigatorDelegate_.navigateInNewWindow(url); |
88 // link in a new window. See http://crbug.com/628057. | |
89 this.paramsParser_.getViewportFromUrlParams( | |
90 url, this.onViewportReceived_.bind(this)); | |
91 break; | 139 break; |
92 case Navigator.WindowOpenDisposition.SAVE_TO_DISK: | 140 case Navigator.WindowOpenDisposition.SAVE_TO_DISK: |
93 // TODO(jaepark): Alt + left clicking a link in PDF should | 141 // TODO(jaepark): Alt + left clicking a link in PDF should |
94 // download the link. | 142 // download the link. |
95 this.paramsParser_.getViewportFromUrlParams( | 143 this.paramsParser_.getViewportFromUrlParams( |
96 url, this.onViewportReceived_.bind(this)); | 144 url, this.onViewportReceived_.bind(this)); |
97 break; | 145 break; |
98 default: | 146 default: |
99 break; | 147 break; |
100 } | 148 } |
(...skipping 13 matching lines...) Expand all Loading... | |
114 | 162 |
115 var newUrl = viewportPosition.url; | 163 var newUrl = viewportPosition.url; |
116 hashIndex = newUrl.search('#'); | 164 hashIndex = newUrl.search('#'); |
117 if (hashIndex != -1) | 165 if (hashIndex != -1) |
118 newUrl = newUrl.substring(0, hashIndex); | 166 newUrl = newUrl.substring(0, hashIndex); |
119 | 167 |
120 var pageNumber = viewportPosition.page; | 168 var pageNumber = viewportPosition.page; |
121 if (pageNumber != undefined && originalUrl == newUrl) | 169 if (pageNumber != undefined && originalUrl == newUrl) |
122 this.viewport_.goToPage(pageNumber); | 170 this.viewport_.goToPage(pageNumber); |
123 else | 171 else |
124 this.navigateInCurrentTabCallback_(viewportPosition.url); | 172 this.navigatorDelegate_.navigateInCurrentTab(viewportPosition.url); |
125 }, | 173 }, |
126 | 174 |
127 /** | 175 /** |
128 * @private | 176 * @private |
129 * Checks if the URL starts with a scheme and s not just a scheme. | 177 * Checks if the URL starts with a scheme and s not just a scheme. |
130 * @param {string} The input URL | 178 * @param {string} The input URL |
131 * @return {boolean} Whether the url is valid. | 179 * @return {boolean} Whether the url is valid. |
132 */ | 180 */ |
133 isValidUrl_: function(url) { | 181 isValidUrl_: function(url) { |
134 // Make sure |url| starts with a valid scheme. | 182 // Make sure |url| starts with a valid scheme. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 if (isRelative) { | 250 if (isRelative) { |
203 var slashIndex = this.originalUrl_.lastIndexOf('/'); | 251 var slashIndex = this.originalUrl_.lastIndexOf('/'); |
204 var path = slashIndex != -1 ? | 252 var path = slashIndex != -1 ? |
205 this.originalUrl_.substr(0, slashIndex) : this.originalUrl_; | 253 this.originalUrl_.substr(0, slashIndex) : this.originalUrl_; |
206 return path + '/' + url; | 254 return path + '/' + url; |
207 } | 255 } |
208 | 256 |
209 return 'http://' + url; | 257 return 'http://' + url; |
210 } | 258 } |
211 }; | 259 }; |
OLD | NEW |