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

Unified Diff: chrome/browser/resources/pdf/navigator.js

Issue 855323002: Refactoring of code by moving navigate_() to separate file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes as per review comments. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/pdf/navigator.js
diff --git a/chrome/browser/resources/pdf/navigator.js b/chrome/browser/resources/pdf/navigator.js
new file mode 100644
index 0000000000000000000000000000000000000000..f6e16aa3b28867065a87e2a184268ed96745b2e6
--- /dev/null
+++ b/chrome/browser/resources/pdf/navigator.js
@@ -0,0 +1,77 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+'use strict';
+
+/**
+ * Creates a new Navigator for navigating to links inside or outside the PDF.
+ */
+function Navigator() {
+}
+
+Navigator.prototype = {
+ /**
+ * @private
+ * Function to navigate to the given URL. This might involve navigating
+ * within the PDF page or opening a new url (in the same tab or a new tab).
+ * @param {string} url The URL to navigate to.
+ * @param {boolean} newTab Whether to perform the navigation in a new tab or
+ * in the current tab.
raymes 2015/01/21 07:03:44 nit: 4 space indent
Deepak 2015/01/21 08:33:41 Done.
+ * @param {string} originalUrl The original page URL.
+ * @param {Object} viewPort The viewport info of the page.
raymes 2015/01/21 07:03:44 nit: viewPort->viewport (and below)
Deepak 2015/01/21 08:33:41 Done.
+ * @param {Object} paramsParser The parsed object having URL parsed info.
+ */
+ navigate: function(url, newTab, originalUrl, viewPort, paramsParser) {
+ if (url.length == 0)
+ return;
+ // If |urlFragment| starts with '#', then it's for the same URL with a
+ // different URL fragment.
+ if (url.charAt(0) == '#') {
+ // if '#' is already present in |originalUrl| then remove old fragment
+ // and add new url fragment.
+ var hashIndex = originalUrl.search('#');
+ if (hashIndex != -1)
+ url = originalUrl.substring(0, hashIndex) + url;
+ else
+ url = originalUrl + url;
+ }
+
+ // If there's no scheme, add http.
+ if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1)
+ url = 'http://' + url;
+
+ // Make sure inputURL starts with a valid scheme.
+ if (url.indexOf('http://') != 0 &&
+ url.indexOf('https://') != 0 &&
+ url.indexOf('ftp://') != 0 &&
+ url.indexOf('file://') != 0 &&
+ url.indexOf('mailto:') != 0) {
+ return;
+ }
+ // Make sure inputURL is not only a scheme.
+ if (url == 'http://' ||
+ url == 'https://' ||
+ url == 'ftp://' ||
+ url == 'file://' ||
+ url == 'mailto:') {
+ return;
+ }
+
+ if (newTab) {
+ // Prefer the tabs API because it guarantees we can just open a new tab.
+ // window.open doesn't have this guarantee.
+ if (chrome.tabs)
+ chrome.tabs.create({ url: url });
+ else
+ window.open(url);
+ } else {
+ var pageNumber =
+ paramsParser.getViewportFromUrlParams(url).page;
+ if (pageNumber != undefined)
+ viewPort.goToPage(pageNumber);
+ else
+ window.location.href = url;
+ }
+ }
+};

Powered by Google App Engine
This is Rietveld 408576698