Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 OpenPDFParamsParser. This parses the open pdf parameters | 8 * Creates a new OpenPDFParamsParser. This parses the open pdf parameters |
| 9 * passed in the url to set initial viewport settings for opening the pdf. | 9 * passed in the url to set initial viewport settings for opening the pdf. |
| 10 * @param {Object} getNamedDestination to fetch page number for | |
| 11 * namedDestination. | |
| 10 */ | 12 */ |
| 11 function OpenPDFParamsParser() { | 13 function OpenPDFParamsParser(getNamedDestination) { |
|
raymes
2015/02/16 22:44:04
nit: getNamedDestinationsFunction
Deepak
2015/02/17 07:12:12
Done.
| |
| 12 // A dictionary of all the named destinations in the PDF. | 14 // A dictionary of all the named destinations in the PDF for testing. |
| 13 this.namedDestinations = {}; | 15 this.namedDestinations = {}; |
| 16 this.outstandingRequests_ = []; | |
| 17 this.getNamedDestinationFunction_ = getNamedDestination; | |
|
raymes
2015/02/16 22:44:04
nit: this.getNamedDestinationsFunction_ = getNamed
Deepak
2015/02/17 07:12:12
Done.
| |
| 14 } | 18 } |
| 15 | 19 |
| 16 OpenPDFParamsParser.prototype = { | 20 OpenPDFParamsParser.prototype = { |
| 17 /** | 21 /** |
| 18 * @private | 22 * @private |
| 19 * Parse zoom parameter of open PDF parameters. If this | 23 * Parse zoom parameter of open PDF parameters. If this |
| 20 * parameter is passed while opening PDF then PDF should be opened | 24 * parameter is passed while opening PDF then PDF should be opened |
| 21 * at the specified zoom level. | 25 * at the specified zoom level. |
| 22 * @param {number} zoom value. | 26 * @param {number} zoom value. |
| 23 * @param {Object} viewportPosition to store zoom and position value. | 27 * @param {Object} viewportPosition to store zoom and position value. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 45 viewportPosition['zoom'] = zoomFactor; | 49 viewportPosition['zoom'] = zoomFactor; |
| 46 }, | 50 }, |
| 47 | 51 |
| 48 /** | 52 /** |
| 49 * @private | 53 * @private |
| 50 * Parse PDF url parameters. These parameters are mentioned in the url | 54 * Parse PDF url parameters. These parameters are mentioned in the url |
| 51 * and specify actions to be performed when opening pdf files. | 55 * and specify actions to be performed when opening pdf files. |
| 52 * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/ | 56 * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/ |
| 53 * pdfs/pdf_open_parameters.pdf for details. | 57 * pdfs/pdf_open_parameters.pdf for details. |
| 54 * @param {string} url that needs to be parsed. | 58 * @param {string} url that needs to be parsed. |
| 55 * @return {Object} A dictionary containing the viewport which should be | 59 * @param {Function} callback function to be called with viewport info. |
| 56 * displayed based on the URL. | |
| 57 */ | 60 */ |
| 58 getViewportFromUrlParams: function(url) { | 61 getViewportFromUrlParams: function(url, callback) { |
| 59 var viewportPosition = {}; | 62 var viewportPosition = {}; |
| 60 var paramIndex = url.search('#'); | 63 var paramIndex = url.search('#'); |
| 61 if (paramIndex == -1) | 64 if (paramIndex == -1) { |
| 62 return viewportPosition; | 65 callback(viewportPosition); |
| 66 return; | |
| 67 } | |
| 63 | 68 |
| 64 var paramTokens = url.substring(paramIndex + 1).split('&'); | 69 var paramTokens = url.substring(paramIndex + 1).split('&'); |
| 65 if ((paramTokens.length == 1) && (paramTokens[0].search('=') == -1)) { | 70 if ((paramTokens.length == 1) && (paramTokens[0].search('=') == -1)) { |
| 66 // Handle the case of http://foo.com/bar#NAMEDDEST. This is not | 71 // Handle the case of http://foo.com/bar#NAMEDDEST. This is not |
| 67 // explicitlymentioned except by example in the Adobe | 72 // explicitlymentioned except by example in the Adobe |
| 68 // "PDF Open Parameters" document. | 73 // "PDF Open Parameters" document. |
| 74 // This code will be used in running testcases only. | |
| 69 viewportPosition['page'] = this.namedDestinations[paramTokens[0]]; | 75 viewportPosition['page'] = this.namedDestinations[paramTokens[0]]; |
| 70 return viewportPosition; | 76 if (viewportPosition['page'] != undefined) { |
| 77 callback(viewportPosition); | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 this.outstandingRequests_.push({ | |
| 82 callback: callback, | |
| 83 viewportPosition: viewportPosition | |
| 84 }); | |
| 85 this.getNamedDestinationFunction_(paramTokens[0]); | |
| 86 return; | |
| 71 } | 87 } |
| 72 | 88 |
| 73 var paramsDictionary = {}; | 89 var paramsDictionary = {}; |
| 74 for (var i = 0; i < paramTokens.length; ++i) { | 90 for (var i = 0; i < paramTokens.length; ++i) { |
| 75 var keyValueSplit = paramTokens[i].split('='); | 91 var keyValueSplit = paramTokens[i].split('='); |
| 76 if (keyValueSplit.length != 2) | 92 if (keyValueSplit.length != 2) |
| 77 continue; | 93 continue; |
| 78 paramsDictionary[keyValueSplit[0]] = keyValueSplit[1]; | 94 paramsDictionary[keyValueSplit[0]] = keyValueSplit[1]; |
| 79 } | 95 } |
| 80 | 96 |
| 81 if ('nameddest' in paramsDictionary) { | |
| 82 var page = this.namedDestinations[paramsDictionary['nameddest']]; | |
| 83 if (page != undefined) | |
| 84 viewportPosition['page'] = page; | |
| 85 } | |
| 86 | |
| 87 if ('page' in paramsDictionary) { | 97 if ('page' in paramsDictionary) { |
| 88 // |pageNumber| is 1-based, but goToPage() take a zero-based page number. | 98 // |pageNumber| is 1-based, but goToPage() take a zero-based page number. |
| 89 var pageNumber = parseInt(paramsDictionary['page']); | 99 var pageNumber = parseInt(paramsDictionary['page']); |
| 90 if (!isNaN(pageNumber) && pageNumber > 0) | 100 if (!isNaN(pageNumber) && pageNumber > 0) |
| 91 viewportPosition['page'] = pageNumber - 1; | 101 viewportPosition['page'] = pageNumber - 1; |
| 92 } | 102 } |
| 93 | 103 |
| 94 if ('zoom' in paramsDictionary) | 104 if ('zoom' in paramsDictionary) |
| 95 this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition); | 105 this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition); |
| 96 | 106 |
| 97 return viewportPosition; | 107 if ('nameddest' in paramsDictionary) { |
| 98 } | 108 // This code will be used in running testcases only. |
| 109 viewportPosition['page'] = | |
| 110 this.namedDestinations[paramsDictionary['nameddest']]; | |
| 111 if (viewportPosition['page'] != undefined) { | |
| 112 callback(viewportPosition); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 this.outstandingRequests_.push({ | |
| 117 callback: callback, | |
| 118 viewportPosition: viewportPosition | |
| 119 }); | |
| 120 this.getNamedDestinationFunction_(paramsDictionary['nameddest']); | |
| 121 return; | |
| 122 } | |
| 123 callback(viewportPosition); | |
| 124 return; | |
| 125 }, | |
| 126 | |
| 127 /** | |
| 128 * This is called when a named destination is received and the page number | |
| 129 * corresponding to the request for which a named destination is passed. | |
| 130 * @param {number} pageNumber The page corresponding to the named destination | |
| 131 * requested. | |
| 132 */ | |
| 133 onNamedDestinationReceived: function(pageNumber) { | |
| 134 var outstandingRequest = this.outstandingRequests_.shift(); | |
| 135 if (!outstandingRequest) | |
| 136 return; | |
| 137 if (!outstandingRequest.viewportPosition.page) | |
| 138 outstandingRequest.viewportPosition.page = pageNumber; | |
| 139 outstandingRequest.callback(outstandingRequest.viewportPosition); | |
| 140 }, | |
| 99 }; | 141 }; |
| OLD | NEW |