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