Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/resources/pdf/navigator.js

Issue 2402873002: Add check for file:-navigations from PDFs (Closed)
Patch Set: Put logic at single location, add test Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {boolean} isInTab Indicates if the PDF viewer is displayed in a tab. 10 * @param {boolean} isInTab Indicates if the PDF viewer is displayed in a tab.
11 * @param {boolean} isSourceFileUrl Indicates if the navigation source is a
12 * file:// URL.
13 */ 11 */
14 function NavigatorDelegate(isInTab, isSourceFileUrl) { 12 function NavigatorDelegate(isInTab) {
15 this.isInTab_ = isInTab; 13 this.isInTab_ = isInTab;
16 this.isSourceFileUrl_ = isSourceFileUrl;
17 } 14 }
18 15
19 /** 16 /**
20 * Creates a new Navigator for navigating to links inside or outside the PDF. 17 * Creates a new Navigator for navigating to links inside or outside the PDF.
21 * @param {string} originalUrl The original page URL. 18 * @param {string} originalUrl The original page URL.
22 * @param {Object} viewport The viewport info of the page. 19 * @param {Object} viewport The viewport info of the page.
23 * @param {Object} paramsParser The object for URL parsing. 20 * @param {Object} paramsParser The object for URL parsing.
24 * @param {Object} navigatorDelegate The object with callback functions that 21 * @param {Object} navigatorDelegate The object with callback functions that
25 * get called when navigation happens in the current tab, a new tab, 22 * get called when navigation happens in the current tab, a new tab,
26 * and a new window. 23 * and a new window.
27 */ 24 */
28 function Navigator(originalUrl, viewport, paramsParser, navigatorDelegate) { 25 function Navigator(originalUrl, viewport, paramsParser, navigatorDelegate) {
29 this.originalUrl_ = originalUrl; 26 this.originalUrl_ = originalUrl;
30 this.viewport_ = viewport; 27 this.viewport_ = viewport;
31 this.paramsParser_ = paramsParser; 28 this.paramsParser_ = paramsParser;
32 this.navigatorDelegate_ = navigatorDelegate; 29 this.navigatorDelegate_ = navigatorDelegate;
33 } 30 }
34 31
35 NavigatorDelegate.prototype = { 32 NavigatorDelegate.prototype = {
36 /** 33 /**
37 * @public 34 * @public
38 * Called when navigation should happen in the current tab. 35 * Called when navigation should happen in the current tab.
39 * @param {string} url The url to be opened in the current tab. 36 * @param {string} url The url to be opened in the current tab.
40 */ 37 */
41 navigateInCurrentTab: function(url) { 38 navigateInCurrentTab: function(url) {
42 // When the PDFviewer is inside a browser tab, prefer the tabs API because 39 // When the PDFviewer is inside a browser tab, prefer the tabs API because
43 // it can navigate from one file:// URL to another. 40 // it can navigate from one file:// URL to another.
44 if (chrome.tabs && this.isInTab_ && this.isSourceFileUrl_) 41 if (chrome.tabs && this.isInTab_)
45 chrome.tabs.update({url: url}); 42 chrome.tabs.update({url: url});
46 else 43 else
47 window.location.href = url; 44 window.location.href = url;
48 }, 45 },
49 46
50 /** 47 /**
51 * @public 48 * @public
52 * Called when navigation should happen in the new tab. 49 * Called when navigation should happen in the new tab.
53 * @param {string} url The url to be opened in the new tab. 50 * @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. 51 * @param {boolean} active Indicates if the new tab should be the active tab.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 164
168 var pageNumber = viewportPosition.page; 165 var pageNumber = viewportPosition.page;
169 if (pageNumber != undefined && originalUrl == newUrl) 166 if (pageNumber != undefined && originalUrl == newUrl)
170 this.viewport_.goToPage(pageNumber); 167 this.viewport_.goToPage(pageNumber);
171 else 168 else
172 this.navigatorDelegate_.navigateInCurrentTab(viewportPosition.url); 169 this.navigatorDelegate_.navigateInCurrentTab(viewportPosition.url);
173 }, 170 },
174 171
175 /** 172 /**
176 * @private 173 * @private
177 * Checks if the URL starts with a scheme and s not just a scheme. 174 * Checks if the URL starts with a scheme and is not just a scheme.
178 * @param {string} The input URL 175 * @param {string} The input URL
179 * @return {boolean} Whether the url is valid. 176 * @return {boolean} Whether the url is valid.
180 */ 177 */
181 isValidUrl_: function(url) { 178 isValidUrl_: function(url) {
182 // Make sure |url| starts with a valid scheme. 179 // Make sure |url| starts with a valid scheme.
183 if (!url.startsWith('http://') && 180 if (!url.startsWith('http://') &&
184 !url.startsWith('https://') && 181 !url.startsWith('https://') &&
185 !url.startsWith('ftp://') && 182 !url.startsWith('ftp://') &&
186 !url.startsWith('file://') && 183 !url.startsWith('file://') &&
187 !url.startsWith('mailto:')) { 184 !url.startsWith('mailto:')) {
188 return false; 185 return false;
189 } 186 }
190 187
188 // Navigations to file:-URLs are only allowed from file:-URLs.
189 if (url.startsWith('file:') && !this.originalUrl_.startsWith('file:'))
190 return false;
191
192
191 // Make sure |url| is not only a scheme. 193 // Make sure |url| is not only a scheme.
192 if (url == 'http://' || 194 if (url == 'http://' ||
193 url == 'https://' || 195 url == 'https://' ||
194 url == 'ftp://' || 196 url == 'ftp://' ||
195 url == 'file://' || 197 url == 'file://' ||
196 url == 'mailto:') { 198 url == 'mailto:') {
197 return false; 199 return false;
198 } 200 }
199 201
200 return true; 202 return true;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 if (isRelative) { 252 if (isRelative) {
251 var slashIndex = this.originalUrl_.lastIndexOf('/'); 253 var slashIndex = this.originalUrl_.lastIndexOf('/');
252 var path = slashIndex != -1 ? 254 var path = slashIndex != -1 ?
253 this.originalUrl_.substr(0, slashIndex) : this.originalUrl_; 255 this.originalUrl_.substr(0, slashIndex) : this.originalUrl_;
254 return path + '/' + url; 256 return path + '/' + url;
255 } 257 }
256 258
257 return 'http://' + url; 259 return 'http://' + url;
258 } 260 }
259 }; 261 };
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | chrome/test/data/pdf/navigator_test.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698