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 fectch page number for | |
11 * namedDestination. | |
10 */ | 12 */ |
11 function OpenPDFParamsParser() { | 13 function OpenPDFParamsParser(getNamedDestination) { |
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.getNamedDestination_ = getNamedDestination; | |
raymes
2015/02/16 02:02:30
nit: getNamedDestinationFunction_
Deepak
2015/02/16 06:45:38
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 {Object} callback function to be called with viewport info. |
raymes
2015/02/16 02:02:30
nit: {Function}
Deepak
2015/02/16 06:45:38
Done.
| |
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. |
69 viewportPosition['page'] = this.namedDestinations[paramTokens[0]]; | 74 viewportPosition['page'] = this.namedDestinations[paramTokens[0]]; |
raymes
2015/02/16 02:02:30
Let's not bother caching the namedDestinations her
Deepak
2015/02/16 06:45:38
This is done for testcases. As we don't want to lo
raymes
2015/02/16 22:44:03
I still don't think I agree - I think we should ch
| |
70 return viewportPosition; | 75 if (viewportPosition['page'] != undefined) { |
76 callback(viewportPosition); | |
77 return; | |
78 } | |
79 | |
80 this.outstandingRequests_.push({ | |
81 callback: callback, | |
82 viewportPosition: viewportPosition | |
83 }); | |
84 this.getNamedDestination_(paramTokens[0]); | |
85 return; | |
71 } | 86 } |
72 | 87 |
73 var paramsDictionary = {}; | 88 var paramsDictionary = {}; |
74 for (var i = 0; i < paramTokens.length; ++i) { | 89 for (var i = 0; i < paramTokens.length; ++i) { |
75 var keyValueSplit = paramTokens[i].split('='); | 90 var keyValueSplit = paramTokens[i].split('='); |
76 if (keyValueSplit.length != 2) | 91 if (keyValueSplit.length != 2) |
77 continue; | 92 continue; |
78 paramsDictionary[keyValueSplit[0]] = keyValueSplit[1]; | 93 paramsDictionary[keyValueSplit[0]] = keyValueSplit[1]; |
79 } | 94 } |
80 | 95 |
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) { | 96 if ('page' in paramsDictionary) { |
88 // |pageNumber| is 1-based, but goToPage() take a zero-based page number. | 97 // |pageNumber| is 1-based, but goToPage() take a zero-based page number. |
89 var pageNumber = parseInt(paramsDictionary['page']); | 98 var pageNumber = parseInt(paramsDictionary['page']); |
90 if (!isNaN(pageNumber) && pageNumber > 0) | 99 if (!isNaN(pageNumber) && pageNumber > 0) |
91 viewportPosition['page'] = pageNumber - 1; | 100 viewportPosition['page'] = pageNumber - 1; |
92 } | 101 } |
93 | 102 |
94 if ('zoom' in paramsDictionary) | 103 if ('zoom' in paramsDictionary) |
95 this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition); | 104 this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition); |
96 | 105 |
97 return viewportPosition; | 106 if ('nameddest' in paramsDictionary) { |
98 } | 107 viewportPosition['page'] = |
108 this.namedDestinations[paramsDictionary['nameddest']]; | |
109 if (viewportPosition['page'] != undefined) { | |
110 callback(viewportPosition); | |
111 return; | |
112 } | |
raymes
2015/02/16 02:02:30
Same here - let's remove the above few lines and r
Deepak
2015/02/16 06:45:38
Same reason as above.
| |
113 this.outstandingRequests_.push({ | |
114 callback: callback, | |
115 viewportPosition: viewportPosition | |
116 }); | |
117 this.getNamedDestination_(paramsDictionary['nameddest']); | |
118 return; | |
119 } | |
120 callback(viewportPosition); | |
121 return; | |
122 }, | |
123 | |
124 /** | |
125 * It fetch outstanding request if any then update page number for | |
126 * namedDestination and call callback with viewport info. | |
raymes
2015/02/16 02:02:31
This is called when a named destination is receive
Deepak
2015/02/16 06:45:38
Done.
| |
127 * @param {number} pageNumber for viewport. | |
raymes
2015/02/16 02:02:31
nit: pageNumber The page corresponding to the name
Deepak
2015/02/16 06:45:38
Done.
| |
128 */ | |
129 onNamedDestinationReceived: function(pageNumber) { | |
130 var outstandingRequest = this.outstandingRequests_.shift(); | |
131 if (!outstandingRequest) | |
132 return; | |
133 if (!outstandingRequest.viewportPosition.page) | |
134 outstandingRequest.viewportPosition.page = pageNumber; | |
135 outstandingRequest.callback(outstandingRequest.viewportPosition); | |
136 }, | |
99 }; | 137 }; |
OLD | NEW |