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

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

Issue 823813002: Modify pdf_scripting_api.js to support postMessage using BrowserPlugin (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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 * Create a new PDFScriptingAPI. This provides a scripting interface to 6 * Create a new PDFScriptingAPI. This provides a scripting interface to
7 * the PDF viewer so that it can be customized by things like print preview. 7 * the PDF viewer so that it can be customized by things like print preview.
8 * @param {Window} window the window of the page containing the pdf viewer. 8 * @param {Window} window the window of the page containing the pdf viewer.
9 * @param {string} extensionUrl the url of the PDF extension. 9 * @param {string} plugin the plugin element containing the pdf viewer.
Sam McNally 2015/01/06 05:31:04 Not a string anymore.
raymes 2015/01/07 03:17:58 Done.
10 */ 10 */
11 function PDFScriptingAPI(window, extensionUrl) { 11 function PDFScriptingAPI(window, plugin) {
12 this.extensionUrl_ = extensionUrl; 12 this.setPlugin(plugin);
13 this.messageQueue_ = []; 13 this.messageQueue_ = [];
14
14 window.addEventListener('message', function(event) { 15 window.addEventListener('message', function(event) {
15 if (event.origin != this.extensionUrl_) { 16 if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai') {
16 console.error('Received message that was not from the extension: ' + 17 console.error('Received message that was not from the extension: ' +
17 event); 18 event);
18 return; 19 return;
19 } 20 }
20 switch (event.data.type) { 21 switch (event.data.type) {
21 case 'readyToReceive':
22 this.setDestinationWindow(event.source);
23 break;
24 case 'viewport': 22 case 'viewport':
25 if (this.viewportChangedCallback_) 23 if (this.viewportChangedCallback_)
26 this.viewportChangedCallback_(event.data.pageX, 24 this.viewportChangedCallback_(event.data.pageX,
27 event.data.pageY, 25 event.data.pageY,
28 event.data.pageWidth, 26 event.data.pageWidth,
29 event.data.viewportWidth, 27 event.data.viewportWidth,
30 event.data.viewportHeight); 28 event.data.viewportHeight);
31 break; 29 break;
32 case 'documentLoaded': 30 case 'documentLoaded':
33 if (this.loadCallback_) 31 if (this.loadCallback_)
34 this.loadCallback_(); 32 this.loadCallback_();
35 break; 33 break;
36 case 'getAccessibilityJSONReply': 34 case 'getAccessibilityJSONReply':
37 if (this.accessibilityCallback_) { 35 if (this.accessibilityCallback_) {
38 this.accessibilityCallback_(event.data.json); 36 this.accessibilityCallback_(event.data.json);
39 this.accessibilityCallback_ = null; 37 this.accessibilityCallback_ = null;
40 } 38 }
41 break; 39 break;
40 case 'readyToReceive':
41 // Safely flush any pending messages.
42 while (this.messageQueue_.length > 0)
43 this.sendMessage_(this.messageQueue_.shift());
44 break;
42 } 45 }
43 }.bind(this), false); 46 }.bind(this), false);
44 } 47 }
45 48
46 PDFScriptingAPI.prototype = { 49 PDFScriptingAPI.prototype = {
47 /** 50 /**
48 * @private 51 * @private
49 * Send a message to the extension. If we try to send messages prior to the 52 * Send a message to the extension. If we try to send messages prior to the
50 * extension being ready to receive messages (i.e. before it has finished 53 * extension being ready to receive messages (i.e. before it has finished
51 * loading) we queue up the messages and flush them later. 54 * loading) we queue up the messages and flush them later.
52 * @param {Object} message The message to send. 55 * @param {Object} message The message to send.
53 */ 56 */
54 sendMessage_: function(message) { 57 sendMessage_: function(message) {
55 if (!this.pdfWindow_) { 58 if (this.plugin_)
59 this.plugin_.postMessage(message, '*');
60 else
56 this.messageQueue_.push(message); 61 this.messageQueue_.push(message);
57 return;
58 }
59
60 this.pdfWindow_.postMessage(message, this.extensionUrl_);
61 }, 62 },
62 63
63 /** 64 /**
64 * Sets the destination window containing the PDF viewer. This will be called 65 * Sets the plugin element containing the PDF viewer. The element will usually
65 * when a 'readyToReceive' message is received from the PDF viewer or it can 66 * be passed into the PDFScriptingAPI constructor but may also be set later.
66 * be called during tests. It then flushes any pending messages to the window. 67 * @param {Object} plugin the plugin element containing the PDF viewer.
67 * @param {Window} pdfWindow the window containing the PDF viewer. 68 */
68 */ 69 setPlugin: function(plugin) {
69 setDestinationWindow: function(pdfWindow) { 70 this.plugin_ = plugin;
70 this.pdfWindow_ = pdfWindow; 71
71 while (this.messageQueue_.length != 0) { 72 // Send an initialization message to the plugin which allows it to respond
72 this.pdfWindow_.postMessage(this.messageQueue_.shift(), 73 // to the page containing it.
73 this.extensionUrl_); 74 if (this.plugin_) {
75 this.sendMessage_({
76 type: 'setParentWindow',
77 });
74 } 78 }
75 }, 79 },
76 80
77 /** 81 /**
78 * Sets the callback which will be run when the PDF viewport changes. 82 * Sets the callback which will be run when the PDF viewport changes.
79 * @param {Function} callback the callback to be called. 83 * @param {Function} callback the callback to be called.
80 */ 84 */
81 setViewportChangedCallback: function(callback) { 85 setViewportChangedCallback: function(callback) {
82 this.viewportChangedCallback_ = callback; 86 this.viewportChangedCallback_ = callback;
83 }, 87 },
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 162
159 /** 163 /**
160 * Creates a PDF viewer with a scripting interface. This is basically 1) an 164 * Creates a PDF viewer with a scripting interface. This is basically 1) an
161 * iframe which is navigated to the PDF viewer extension and 2) a scripting 165 * iframe which is navigated to the PDF viewer extension and 2) a scripting
162 * interface which provides access to various features of the viewer for use 166 * interface which provides access to various features of the viewer for use
163 * by print preview and accessibility. 167 * by print preview and accessibility.
164 * @param {string} src the source URL of the PDF to load initially. 168 * @param {string} src the source URL of the PDF to load initially.
165 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer. 169 * @return {HTMLIFrameElement} the iframe element containing the PDF viewer.
166 */ 170 */
167 function PDFCreateOutOfProcessPlugin(src) { 171 function PDFCreateOutOfProcessPlugin(src) {
168 var EXTENSION_URL = 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai';
169 var iframe = window.document.createElement('iframe'); 172 var iframe = window.document.createElement('iframe');
170 iframe.setAttribute('src', EXTENSION_URL + '/index.html?' + src); 173 iframe.setAttribute('src',
171 var client = new PDFScriptingAPI(window, EXTENSION_URL); 174 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?' + src);
Sam McNally 2015/01/06 05:31:04 Either line this up with the ( above or break the
raymes 2015/01/07 03:17:58 Done.
172 175 var client = new PDFScriptingAPI(window);
176 iframe.onload = function() {
177 client.setPlugin(iframe.contentWindow);
178 };
173 // Add the functions to the iframe so that they can be called directly. 179 // Add the functions to the iframe so that they can be called directly.
174 iframe.setViewportChangedCallback = 180 iframe.setViewportChangedCallback =
175 client.setViewportChangedCallback.bind(client); 181 client.setViewportChangedCallback.bind(client);
176 iframe.setLoadCallback = client.setLoadCallback.bind(client); 182 iframe.setLoadCallback = client.setLoadCallback.bind(client);
177 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); 183 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client);
178 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); 184 iframe.loadPreviewPage = client.loadPreviewPage.bind(client);
179 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); 185 iframe.sendKeyEvent = client.sendKeyEvent.bind(client);
180 return iframe; 186 return iframe;
181 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698