Chromium Code Reviews| Index: chrome/browser/resources/pdf/open_pdf_params_parser.js |
| diff --git a/chrome/browser/resources/pdf/open_pdf_params_parser.js b/chrome/browser/resources/pdf/open_pdf_params_parser.js |
| index c0f4b19d1c8889d979221cc19009aa17f7e8a080..29916ba3cc052afc0d931df9fba82100e44729d2 100644 |
| --- a/chrome/browser/resources/pdf/open_pdf_params_parser.js |
| +++ b/chrome/browser/resources/pdf/open_pdf_params_parser.js |
| @@ -7,10 +7,14 @@ |
| /** |
| * Creates a new OpenPDFParamsParser. This parses the open pdf parameters |
| * passed in the url to set initial viewport settings for opening the pdf. |
| + * @param {Object} getNamedDestination to fetch page number for |
| + * namedDestination. |
| */ |
| -function OpenPDFParamsParser() { |
| - // A dictionary of all the named destinations in the PDF. |
| +function OpenPDFParamsParser(getNamedDestination) { |
|
raymes
2015/02/16 22:44:04
nit: getNamedDestinationsFunction
Deepak
2015/02/17 07:12:12
Done.
|
| + // A dictionary of all the named destinations in the PDF for testing. |
| this.namedDestinations = {}; |
| + this.outstandingRequests_ = []; |
| + this.getNamedDestinationFunction_ = getNamedDestination; |
|
raymes
2015/02/16 22:44:04
nit: this.getNamedDestinationsFunction_ = getNamed
Deepak
2015/02/17 07:12:12
Done.
|
| } |
| OpenPDFParamsParser.prototype = { |
| @@ -52,22 +56,34 @@ OpenPDFParamsParser.prototype = { |
| * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/ |
| * pdfs/pdf_open_parameters.pdf for details. |
| * @param {string} url that needs to be parsed. |
| - * @return {Object} A dictionary containing the viewport which should be |
| - * displayed based on the URL. |
| + * @param {Function} callback function to be called with viewport info. |
| */ |
| - getViewportFromUrlParams: function(url) { |
| + getViewportFromUrlParams: function(url, callback) { |
| var viewportPosition = {}; |
| var paramIndex = url.search('#'); |
| - if (paramIndex == -1) |
| - return viewportPosition; |
| + if (paramIndex == -1) { |
| + callback(viewportPosition); |
| + return; |
| + } |
| var paramTokens = url.substring(paramIndex + 1).split('&'); |
| if ((paramTokens.length == 1) && (paramTokens[0].search('=') == -1)) { |
| // Handle the case of http://foo.com/bar#NAMEDDEST. This is not |
| // explicitlymentioned except by example in the Adobe |
| // "PDF Open Parameters" document. |
| + // This code will be used in running testcases only. |
| viewportPosition['page'] = this.namedDestinations[paramTokens[0]]; |
| - return viewportPosition; |
| + if (viewportPosition['page'] != undefined) { |
| + callback(viewportPosition); |
| + return; |
| + } |
| + |
| + this.outstandingRequests_.push({ |
| + callback: callback, |
| + viewportPosition: viewportPosition |
| + }); |
| + this.getNamedDestinationFunction_(paramTokens[0]); |
| + return; |
| } |
| var paramsDictionary = {}; |
| @@ -78,12 +94,6 @@ OpenPDFParamsParser.prototype = { |
| paramsDictionary[keyValueSplit[0]] = keyValueSplit[1]; |
| } |
| - if ('nameddest' in paramsDictionary) { |
| - var page = this.namedDestinations[paramsDictionary['nameddest']]; |
| - if (page != undefined) |
| - viewportPosition['page'] = page; |
| - } |
| - |
| if ('page' in paramsDictionary) { |
| // |pageNumber| is 1-based, but goToPage() take a zero-based page number. |
| var pageNumber = parseInt(paramsDictionary['page']); |
| @@ -94,6 +104,38 @@ OpenPDFParamsParser.prototype = { |
| if ('zoom' in paramsDictionary) |
| this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition); |
| - return viewportPosition; |
| - } |
| + if ('nameddest' in paramsDictionary) { |
| + // This code will be used in running testcases only. |
| + viewportPosition['page'] = |
| + this.namedDestinations[paramsDictionary['nameddest']]; |
| + if (viewportPosition['page'] != undefined) { |
| + callback(viewportPosition); |
| + return; |
| + } |
| + |
| + this.outstandingRequests_.push({ |
| + callback: callback, |
| + viewportPosition: viewportPosition |
| + }); |
| + this.getNamedDestinationFunction_(paramsDictionary['nameddest']); |
| + return; |
| + } |
| + callback(viewportPosition); |
| + return; |
| + }, |
| + |
| + /** |
| + * This is called when a named destination is received and the page number |
| + * corresponding to the request for which a named destination is passed. |
| + * @param {number} pageNumber The page corresponding to the named destination |
| + * requested. |
| + */ |
| + onNamedDestinationReceived: function(pageNumber) { |
| + var outstandingRequest = this.outstandingRequests_.shift(); |
| + if (!outstandingRequest) |
| + return; |
| + if (!outstandingRequest.viewportPosition.page) |
| + outstandingRequest.viewportPosition.page = pageNumber; |
| + outstandingRequest.callback(outstandingRequest.viewportPosition); |
| + }, |
| }; |