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

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

Issue 2939273002: DO NOT SUBMIT: what chrome/browser/resources/ could eventually look like with clang-format (Closed)
Patch Set: Created 3 years, 6 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
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 /** 5 /**
6 * Turn a dictionary received from postMessage into a key event. 6 * Turn a dictionary received from postMessage into a key event.
7 * @param {Object} dict A dictionary representing the key event. 7 * @param {Object} dict A dictionary representing the key event.
8 * @return {Event} A key event. 8 * @return {Event} A key event.
9 */ 9 */
10 function DeserializeKeyEvent(dict) { 10 function DeserializeKeyEvent(dict) {
(...skipping 21 matching lines...) Expand all
32 altKey: event.altKey, 32 altKey: event.altKey,
33 metaKey: event.metaKey 33 metaKey: event.metaKey
34 }; 34 };
35 } 35 }
36 36
37 /** 37 /**
38 * An enum containing a value specifying whether the PDF is currently loading, 38 * An enum containing a value specifying whether the PDF is currently loading,
39 * has finished loading or failed to load. 39 * has finished loading or failed to load.
40 * @enum {string} 40 * @enum {string}
41 */ 41 */
42 var LoadState = { 42 var LoadState = {LOADING: 'loading', SUCCESS: 'success', FAILED: 'failed'};
43 LOADING: 'loading',
44 SUCCESS: 'success',
45 FAILED: 'failed'
46 };
47 43
48 /** 44 /**
49 * Create a new PDFScriptingAPI. This provides a scripting interface to 45 * Create a new PDFScriptingAPI. This provides a scripting interface to
50 * the PDF viewer so that it can be customized by things like print preview. 46 * the PDF viewer so that it can be customized by things like print preview.
51 * @param {Window} window the window of the page containing the pdf viewer. 47 * @param {Window} window the window of the page containing the pdf viewer.
52 * @param {Object} plugin the plugin element containing the pdf viewer. 48 * @param {Object} plugin the plugin element containing the pdf viewer.
53 * @constructor 49 * @constructor
54 */ 50 */
55 function PDFScriptingAPI(window, plugin) { 51 function PDFScriptingAPI(window, plugin) {
56 this.loadState_ = LoadState.LOADING; 52 this.loadState_ = LoadState.LOADING;
57 this.pendingScriptingMessages_ = []; 53 this.pendingScriptingMessages_ = [];
58 this.setPlugin(plugin); 54 this.setPlugin(plugin);
59 55
60 window.addEventListener('message', function(event) { 56 window.addEventListener('message', function(event) {
61 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && 57 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' &&
62 event.origin != 'chrome://print') { 58 event.origin != 'chrome://print') {
63 console.error('Received message that was not from the extension: ' + 59 console.error(
64 event); 60 'Received message that was not from the extension: ' + event);
65 return; 61 return;
66 } 62 }
67 switch (event.data.type) { 63 switch (event.data.type) {
68 case 'viewport': 64 case 'viewport':
69 /** 65 /**
70 * @type {{ 66 * @type {{
71 * pageX: number, 67 * pageX: number,
72 * pageY: number, 68 * pageY: number,
73 * pageWidth: number, 69 * pageWidth: number,
74 * viewportWidth: number, 70 * viewportWidth: number,
75 * viewportHeight: number 71 * viewportHeight: number
76 * }} 72 * }}
77 */ 73 */
78 var viewportData = event.data; 74 var viewportData = event.data;
79 if (this.viewportChangedCallback_) 75 if (this.viewportChangedCallback_)
80 this.viewportChangedCallback_(viewportData.pageX, 76 this.viewportChangedCallback_(
81 viewportData.pageY, 77 viewportData.pageX, viewportData.pageY, viewportData.pageWidth,
82 viewportData.pageWidth, 78 viewportData.viewportWidth, viewportData.viewportHeight);
83 viewportData.viewportWidth,
84 viewportData.viewportHeight);
85 break; 79 break;
86 case 'documentLoaded': 80 case 'documentLoaded':
87 var data = /** @type {{load_state: LoadState}} */ (event.data); 81 var data = /** @type {{load_state: LoadState}} */ (event.data);
88 this.loadState_ = data.load_state; 82 this.loadState_ = data.load_state;
89 if (this.loadCallback_) 83 if (this.loadCallback_)
90 this.loadCallback_(this.loadState_ == LoadState.SUCCESS); 84 this.loadCallback_(this.loadState_ == LoadState.SUCCESS);
91 break; 85 break;
92 case 'getSelectedTextReply': 86 case 'getSelectedTextReply':
93 var data = /** @type {{selectedText: string}} */ (event.data); 87 var data = /** @type {{selectedText: string}} */ (event.data);
94 if (this.selectedTextCallback_) { 88 if (this.selectedTextCallback_) {
(...skipping 17 matching lines...) Expand all
112 * can happen in print preview). 106 * can happen in print preview).
113 * @param {Object} message The message to send. 107 * @param {Object} message The message to send.
114 */ 108 */
115 sendMessage_: function(message) { 109 sendMessage_: function(message) {
116 if (this.plugin_) 110 if (this.plugin_)
117 this.plugin_.postMessage(message, '*'); 111 this.plugin_.postMessage(message, '*');
118 else 112 else
119 this.pendingScriptingMessages_.push(message); 113 this.pendingScriptingMessages_.push(message);
120 }, 114 },
121 115
122 /** 116 /**
123 * Sets the plugin element containing the PDF viewer. The element will usually 117 * Sets the plugin element containing the PDF viewer. The element will usually
124 * be passed into the PDFScriptingAPI constructor but may also be set later. 118 * be passed into the PDFScriptingAPI constructor but may also be set later.
125 * @param {Object} plugin the plugin element containing the PDF viewer. 119 * @param {Object} plugin the plugin element containing the PDF viewer.
126 */ 120 */
127 setPlugin: function(plugin) { 121 setPlugin: function(plugin) {
128 this.plugin_ = plugin; 122 this.plugin_ = plugin;
129 123
130 if (this.plugin_) { 124 if (this.plugin_) {
131 // Send a message to ensure the postMessage channel is initialized which 125 // Send a message to ensure the postMessage channel is initialized which
132 // allows us to receive messages. 126 // allows us to receive messages.
133 this.sendMessage_({ 127 this.sendMessage_({type: 'initialize'});
134 type: 'initialize'
135 });
136 // Flush pending messages. 128 // Flush pending messages.
137 while (this.pendingScriptingMessages_.length > 0) 129 while (this.pendingScriptingMessages_.length > 0)
138 this.sendMessage_(this.pendingScriptingMessages_.shift()); 130 this.sendMessage_(this.pendingScriptingMessages_.shift());
139 } 131 }
140 }, 132 },
141 133
142 /** 134 /**
143 * Sets the callback which will be run when the PDF viewport changes. 135 * Sets the callback which will be run when the PDF viewport changes.
144 * @param {Function} callback the callback to be called. 136 * @param {Function} callback the callback to be called.
145 */ 137 */
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 modifiable: modifiable 175 modifiable: modifiable
184 }); 176 });
185 }, 177 },
186 178
187 /** 179 /**
188 * Load a page into the document while in print preview mode. 180 * Load a page into the document while in print preview mode.
189 * @param {string} url the url of the pdf page to load. 181 * @param {string} url the url of the pdf page to load.
190 * @param {number} index the index of the page to load. 182 * @param {number} index the index of the page to load.
191 */ 183 */
192 loadPreviewPage: function(url, index) { 184 loadPreviewPage: function(url, index) {
193 this.sendMessage_({ 185 this.sendMessage_({type: 'loadPreviewPage', url: url, index: index});
194 type: 'loadPreviewPage',
195 url: url,
196 index: index
197 });
198 }, 186 },
199 187
200 /** 188 /**
201 * Select all the text in the document. May only be called after document 189 * Select all the text in the document. May only be called after document
202 * load. 190 * load.
203 */ 191 */
204 selectAll: function() { 192 selectAll: function() {
205 this.sendMessage_({ 193 this.sendMessage_({type: 'selectAll'});
206 type: 'selectAll'
207 });
208 }, 194 },
209 195
210 /** 196 /**
211 * Get the selected text in the document. The callback will be called with the 197 * Get the selected text in the document. The callback will be called with the
212 * text that is selected. May only be called after document load. 198 * text that is selected. May only be called after document load.
213 * @param {Function} callback a callback to be called with the selected text. 199 * @param {Function} callback a callback to be called with the selected text.
214 * @return {boolean} true if the function is successful, false if there is an 200 * @return {boolean} true if the function is successful, false if there is an
215 * outstanding request for selected text that has not been answered. 201 * outstanding request for selected text that has not been answered.
216 */ 202 */
217 getSelectedText: function(callback) { 203 getSelectedText: function(callback) {
218 if (this.selectedTextCallback_) 204 if (this.selectedTextCallback_)
219 return false; 205 return false;
220 this.selectedTextCallback_ = callback; 206 this.selectedTextCallback_ = callback;
221 this.sendMessage_({ 207 this.sendMessage_({type: 'getSelectedText'});
222 type: 'getSelectedText'
223 });
224 return true; 208 return true;
225 }, 209 },
226 210
227 /** 211 /**
228 * Print the document. May only be called after document load. 212 * Print the document. May only be called after document load.
229 */ 213 */
230 print: function() { 214 print: function() {
231 this.sendMessage_({ 215 this.sendMessage_({type: 'print'});
232 type: 'print'
233 });
234 }, 216 },
235 217
236 /** 218 /**
237 * Send a key event to the extension. 219 * Send a key event to the extension.
238 * @param {Event} keyEvent the key event to send to the extension. 220 * @param {Event} keyEvent the key event to send to the extension.
239 */ 221 */
240 sendKeyEvent: function(keyEvent) { 222 sendKeyEvent: function(keyEvent) {
241 this.sendMessage_({ 223 this.sendMessage_(
242 type: 'sendKeyEvent', 224 {type: 'sendKeyEvent', keyEvent: SerializeKeyEvent(keyEvent)});
243 keyEvent: SerializeKeyEvent(keyEvent)
244 });
245 }, 225 },
246 }; 226 };
247 227
248 /** 228 /**
249 * Creates a PDF viewer with a scripting interface. This is basically 1) an 229 * Creates a PDF viewer with a scripting interface. This is basically 1) an
250 * iframe which is navigated to the PDF viewer extension and 2) a scripting 230 * iframe which is navigated to the PDF viewer extension and 2) a scripting
251 * interface which provides access to various features of the viewer for use 231 * interface which provides access to various features of the viewer for use
252 * by print preview and accessibility. 232 * by print preview and accessibility.
253 * @param {string} src the source URL of the PDF to load initially. 233 * @param {string} src the source URL of the PDF to load initially.
254 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. 234 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer.
255 */ 235 */
256 function PDFCreateOutOfProcessPlugin(src) { 236 function PDFCreateOutOfProcessPlugin(src) {
257 var client = new PDFScriptingAPI(window, null); 237 var client = new PDFScriptingAPI(window, null);
258 var iframe = assertInstanceof(window.document.createElement('iframe'), 238 var iframe = assertInstanceof(
259 HTMLIFrameElement); 239 window.document.createElement('iframe'), HTMLIFrameElement);
260 iframe.setAttribute('src', 'pdf_preview.html?' + src); 240 iframe.setAttribute('src', 'pdf_preview.html?' + src);
261 // Prevent the frame from being tab-focusable. 241 // Prevent the frame from being tab-focusable.
262 iframe.setAttribute('tabindex', '-1'); 242 iframe.setAttribute('tabindex', '-1');
263 243
264 iframe.onload = function() { 244 iframe.onload = function() {
265 client.setPlugin(iframe.contentWindow); 245 client.setPlugin(iframe.contentWindow);
266 }; 246 };
267 247
268 // Add the functions to the iframe so that they can be called directly. 248 // Add the functions to the iframe so that they can be called directly.
269 iframe.setViewportChangedCallback = 249 iframe.setViewportChangedCallback =
270 client.setViewportChangedCallback.bind(client); 250 client.setViewportChangedCallback.bind(client);
271 iframe.setLoadCallback = client.setLoadCallback.bind(client); 251 iframe.setLoadCallback = client.setLoadCallback.bind(client);
272 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client); 252 iframe.setKeyEventCallback = client.setKeyEventCallback.bind(client);
273 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); 253 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client);
274 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); 254 iframe.loadPreviewPage = client.loadPreviewPage.bind(client);
275 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); 255 iframe.sendKeyEvent = client.sendKeyEvent.bind(client);
276 return iframe; 256 return iframe;
277 } 257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698