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