Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: chrome/browser/resources/pdf/open_pdf_params_parser.js

Issue 918953002: Fix for PDFs with lots of named destinations take a long time to load. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes as per review comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/resources/pdf/navigator.js ('k') | chrome/browser/resources/pdf/pdf.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 this.outstandingRequests_ = [];
13 this.namedDestinations = {}; 15 this.getNamedDestinationsFunction_ = getNamedDestinationsFunction;
14 } 16 }
15 17
16 OpenPDFParamsParser.prototype = { 18 OpenPDFParamsParser.prototype = {
17 /** 19 /**
18 * @private 20 * @private
19 * Parse zoom parameter of open PDF parameters. If this 21 * Parse zoom parameter of open PDF parameters. If this
20 * parameter is passed while opening PDF then PDF should be opened 22 * parameter is passed while opening PDF then PDF should be opened
21 * at the specified zoom level. 23 * at the specified zoom level.
22 * @param {number} zoom value. 24 * @param {number} zoom value.
23 * @param {Object} viewportPosition to store zoom and position value. 25 * @param {Object} viewportPosition to store zoom and position value.
(...skipping 21 matching lines...) Expand all
45 viewportPosition['zoom'] = zoomFactor; 47 viewportPosition['zoom'] = zoomFactor;
46 }, 48 },
47 49
48 /** 50 /**
49 * @private 51 * @private
50 * Parse PDF url parameters. These parameters are mentioned in the url 52 * Parse PDF url parameters. These parameters are mentioned in the url
51 * and specify actions to be performed when opening pdf files. 53 * and specify actions to be performed when opening pdf files.
52 * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/ 54 * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/
53 * pdfs/pdf_open_parameters.pdf for details. 55 * pdfs/pdf_open_parameters.pdf for details.
54 * @param {string} url that needs to be parsed. 56 * @param {string} url that needs to be parsed.
55 * @return {Object} A dictionary containing the viewport which should be 57 * @param {Function} callback function to be called with viewport info.
56 * displayed based on the URL.
57 */ 58 */
58 getViewportFromUrlParams: function(url) { 59 getViewportFromUrlParams: function(url, callback) {
59 var viewportPosition = {}; 60 var viewportPosition = {};
61 viewportPosition['url'] = url;
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 };
OLDNEW
« no previous file with comments | « chrome/browser/resources/pdf/navigator.js ('k') | chrome/browser/resources/pdf/pdf.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698