Commit 53b6e7a7 authored by n.bansal's avatar n.bansal Committed by Commit bot

OOP PDF - Add OpenPDFParamsParser class

This patch introduces OpenPDFParamsParser class which is responsible
to parse open pdf parameters and set them in a dictionary. This
dictionary will be used to set initial viewport settings when opening
pdf document.

BUG=303491

Review URL: https://codereview.chromium.org/477933003

Cr-Commit-Position: refs/heads/master@{#292360}
parent 0b0bc195
// 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 OpenPDFParamsParser. This parses the open pdf parameters
* passed in the url to set initial viewport settings for opening the pdf.
* @param {string} url to be parsed.
*/
function OpenPDFParamsParser(url) {
this.url_ = url;
this.urlParams = {};
this.parseOpenPDFParams_();
}
OpenPDFParamsParser.prototype = {
/**
* @private
* Parse open PDF parameters. These parameters are mentioned in the url
* and specify actions to be performed when opening pdf files.
* See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/
* pdfs/pdf_open_parameters.pdf for details.
*/
parseOpenPDFParams_: function() {
var originalUrl = this.url_;
var paramIndex = originalUrl.search('#');
if (paramIndex == -1)
return;
var paramTokens = originalUrl.substring(paramIndex + 1).split('&');
var paramsDictionary = {};
for (var i = 0; i < paramTokens.length; ++i) {
var keyValueSplit = paramTokens[i].split('=');
if (keyValueSplit.length != 2)
continue;
paramsDictionary[keyValueSplit[0]] = keyValueSplit[1];
}
if ('page' in paramsDictionary) {
// |pageNumber| is 1-based, but goToPage() take a zero-based page number.
var pageNumber = parseInt(paramsDictionary['page']);
if (!isNaN(pageNumber))
this.urlParams['page'] = pageNumber - 1;
}
}
};
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
'use strict'; 'use strict';
<include src="../../../../ui/webui/resources/js/util.js"> <include src="../../../../ui/webui/resources/js/util.js">
<include src="open_pdf_params_parser.js">
<include src="pdf_scripting_api.js"> <include src="pdf_scripting_api.js">
<include src="viewport.js"> <include src="viewport.js">
...@@ -150,6 +151,10 @@ function PDFViewer() { ...@@ -150,6 +151,10 @@ function PDFViewer() {
this.viewport_.setZoom(zoomChangeInfo.newZoomFactor); this.viewport_.setZoom(zoomChangeInfo.newZoomFactor);
}.bind(this)); }.bind(this));
} }
// Parse open pdf parameters.
var paramsParser = new OpenPDFParamsParser(this.streamDetails.originalUrl);
this.urlParams_ = paramsParser.urlParams;
} }
PDFViewer.prototype = { PDFViewer.prototype = {
...@@ -263,37 +268,6 @@ PDFViewer.prototype = { ...@@ -263,37 +268,6 @@ PDFViewer.prototype = {
}); });
}, },
/**
* @private
* Handle open PDF parameters. These parameters are mentioned in the URL
* and specify actions to be performed when opening PDF files.
* See http://crbug.com/64309 for details.
*/
handleOpenPDFParams_: function() {
var originalUrl = this.streamDetails.originalUrl;
var paramIndex = originalUrl.search('#');
if (paramIndex == -1)
return;
var paramTokens = originalUrl.substring(paramIndex + 1).split('&');
var paramsDictionary = {};
for (var i = 0; i < paramTokens.length; ++i) {
var keyValueSplit = paramTokens[i].split('=');
if (keyValueSplit.length != 2)
continue;
paramsDictionary[keyValueSplit[0]] = keyValueSplit[1];
}
// Order is important as later actions can override the effects
// of previous actions.
if ('page' in paramsDictionary) {
// |pageNumber| is 1-based, but goToPage() take a zero-based page number.
var pageNumber = parseInt(paramsDictionary['page']);
if (!isNaN(pageNumber))
this.viewport_.goToPage(pageNumber - 1);
}
},
/** /**
* @private * @private
* Update the loading progress of the document in response to a progress * Update the loading progress of the document in response to a progress
...@@ -313,7 +287,6 @@ PDFViewer.prototype = { ...@@ -313,7 +287,6 @@ PDFViewer.prototype = {
} }
} else if (progress == 100) { } else if (progress == 100) {
// Document load complete. // Document load complete.
this.handleOpenPDFParams_();
this.loaded = true; this.loaded = true;
var loadEvent = new Event('pdfload'); var loadEvent = new Event('pdfload');
window.dispatchEvent(loadEvent); window.dispatchEvent(loadEvent);
...@@ -322,6 +295,11 @@ PDFViewer.prototype = { ...@@ -322,6 +295,11 @@ PDFViewer.prototype = {
}); });
if (this.lastViewportPosition_) if (this.lastViewportPosition_)
this.viewport_.position = this.lastViewportPosition_; this.viewport_.position = this.lastViewportPosition_;
// Handle open pdf params. Order is important as later actions can
// override the effects of previous actions.
if (this.urlParams_.page)
this.viewport_.goToPage(this.urlParams_.page);
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment