| 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 NavigatorDelegate for calling browser-specific functions to | 8 * Creates a new NavigatorDelegate for calling browser-specific functions to |
| 9 * do the actual navigating. | 9 * do the actual navigating. |
| 10 * @param {number} tabId The tab ID of the PDF viewer or -1 if the viewer is | 10 * @param {number} tabId The tab ID of the PDF viewer or -1 if the viewer is |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 }, | 171 }, |
| 172 | 172 |
| 173 /** | 173 /** |
| 174 * @private | 174 * @private |
| 175 * Checks if the URL starts with a scheme and is not just a scheme. | 175 * Checks if the URL starts with a scheme and is not just a scheme. |
| 176 * @param {string} The input URL | 176 * @param {string} The input URL |
| 177 * @return {boolean} Whether the url is valid. | 177 * @return {boolean} Whether the url is valid. |
| 178 */ | 178 */ |
| 179 isValidUrl_: function(url) { | 179 isValidUrl_: function(url) { |
| 180 // Make sure |url| starts with a valid scheme. | 180 // Make sure |url| starts with a valid scheme. |
| 181 if (!url.startsWith('http://') && | 181 if (!url.startsWith('http://') && !url.startsWith('https://') && |
| 182 !url.startsWith('https://') && | 182 !url.startsWith('ftp://') && !url.startsWith('file://') && |
| 183 !url.startsWith('ftp://') && | |
| 184 !url.startsWith('file://') && | |
| 185 !url.startsWith('mailto:')) { | 183 !url.startsWith('mailto:')) { |
| 186 return false; | 184 return false; |
| 187 } | 185 } |
| 188 | 186 |
| 189 // Navigations to file:-URLs are only allowed from file:-URLs. | 187 // Navigations to file:-URLs are only allowed from file:-URLs. |
| 190 if (url.startsWith('file:') && !this.originalUrl_.startsWith('file:')) | 188 if (url.startsWith('file:') && !this.originalUrl_.startsWith('file:')) |
| 191 return false; | 189 return false; |
| 192 | 190 |
| 193 | 191 |
| 194 // Make sure |url| is not only a scheme. | 192 // Make sure |url| is not only a scheme. |
| 195 if (url == 'http://' || | 193 if (url == 'http://' || url == 'https://' || url == 'ftp://' || |
| 196 url == 'https://' || | 194 url == 'file://' || url == 'mailto:') { |
| 197 url == 'ftp://' || | |
| 198 url == 'file://' || | |
| 199 url == 'mailto:') { | |
| 200 return false; | 195 return false; |
| 201 } | 196 } |
| 202 | 197 |
| 203 return true; | 198 return true; |
| 204 }, | 199 }, |
| 205 | 200 |
| 206 /** | 201 /** |
| 207 * @private | 202 * @private |
| 208 * Attempt to figure out what a URL is when there is no scheme. | 203 * Attempt to figure out what a URL is when there is no scheme. |
| 209 * @param {string} The input URL | 204 * @param {string} The input URL |
| 210 * @return {string} The URL with a scheme or the original URL if it is not | 205 * @return {string} The URL with a scheme or the original URL if it is not |
| 211 * possible to determine the scheme. | 206 * possible to determine the scheme. |
| 212 */ | 207 */ |
| 213 guessUrlWithoutScheme_: function(url) { | 208 guessUrlWithoutScheme_: function(url) { |
| 214 // If the original URL is mailto:, that does not make sense to start with, | 209 // If the original URL is mailto:, that does not make sense to start with, |
| 215 // and neither does adding |url| to it. | 210 // and neither does adding |url| to it. |
| 216 // If the original URL is not a valid URL, this cannot make a valid URL. | 211 // If the original URL is not a valid URL, this cannot make a valid URL. |
| 217 // In both cases, just bail out. | 212 // In both cases, just bail out. |
| 218 if (this.originalUrl_.startsWith('mailto:') || | 213 if (this.originalUrl_.startsWith('mailto:') || |
| 219 !this.isValidUrl_(this.originalUrl_)) { | 214 !this.isValidUrl_(this.originalUrl_)) { |
| 220 return url; | 215 return url; |
| 221 } | 216 } |
| 222 | 217 |
| 223 // Check for absolute paths. | 218 // Check for absolute paths. |
| 224 if (url.startsWith('/')) { | 219 if (url.startsWith('/')) { |
| 225 var schemeEndIndex = this.originalUrl_.indexOf('://'); | 220 var schemeEndIndex = this.originalUrl_.indexOf('://'); |
| 226 var firstSlash = this.originalUrl_.indexOf('/', schemeEndIndex + 3); | 221 var firstSlash = this.originalUrl_.indexOf('/', schemeEndIndex + 3); |
| 227 // e.g. http://www.foo.com/bar -> http://www.foo.com | 222 // e.g. http://www.foo.com/bar -> http://www.foo.com |
| 228 var domain = firstSlash != -1 ? | 223 var domain = firstSlash != -1 ? this.originalUrl_.substr(0, firstSlash) : |
| 229 this.originalUrl_.substr(0, firstSlash) : this.originalUrl_; | 224 this.originalUrl_; |
| 230 return domain + url; | 225 return domain + url; |
| 231 } | 226 } |
| 232 | 227 |
| 233 // Check for obvious relative paths. | 228 // Check for obvious relative paths. |
| 234 var isRelative = false; | 229 var isRelative = false; |
| 235 if (url.startsWith('.') || url.startsWith('\\')) | 230 if (url.startsWith('.') || url.startsWith('\\')) |
| 236 isRelative = true; | 231 isRelative = true; |
| 237 | 232 |
| 238 // In Adobe Acrobat Reader XI, it looks as though links with less than | 233 // In Adobe Acrobat Reader XI, it looks as though links with less than |
| 239 // 2 dot separators in the domain are considered relative links, and | 234 // 2 dot separators in the domain are considered relative links, and |
| 240 // those with 2 of more are considered http URLs. e.g. | 235 // those with 2 of more are considered http URLs. e.g. |
| 241 // | 236 // |
| 242 // www.foo.com/bar -> http | 237 // www.foo.com/bar -> http |
| 243 // foo.com/bar -> relative link | 238 // foo.com/bar -> relative link |
| 244 if (!isRelative) { | 239 if (!isRelative) { |
| 245 var domainSeparatorIndex = url.indexOf('/'); | 240 var domainSeparatorIndex = url.indexOf('/'); |
| 246 var domainName = domainSeparatorIndex == -1 ? | 241 var domainName = domainSeparatorIndex == -1 ? |
| 247 url : url.substr(0, domainSeparatorIndex); | 242 url : |
| 243 url.substr(0, domainSeparatorIndex); |
| 248 var domainDotCount = (domainName.match(/\./g) || []).length; | 244 var domainDotCount = (domainName.match(/\./g) || []).length; |
| 249 if (domainDotCount < 2) | 245 if (domainDotCount < 2) |
| 250 isRelative = true; | 246 isRelative = true; |
| 251 } | 247 } |
| 252 | 248 |
| 253 if (isRelative) { | 249 if (isRelative) { |
| 254 var slashIndex = this.originalUrl_.lastIndexOf('/'); | 250 var slashIndex = this.originalUrl_.lastIndexOf('/'); |
| 255 var path = slashIndex != -1 ? | 251 var path = slashIndex != -1 ? this.originalUrl_.substr(0, slashIndex) : |
| 256 this.originalUrl_.substr(0, slashIndex) : this.originalUrl_; | 252 this.originalUrl_; |
| 257 return path + '/' + url; | 253 return path + '/' + url; |
| 258 } | 254 } |
| 259 | 255 |
| 260 return 'http://' + url; | 256 return 'http://' + url; |
| 261 } | 257 } |
| 262 }; | 258 }; |
| OLD | NEW |