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

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

Issue 265283005: Add accessibility function to PDFScriptingAPI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/pdf/pdf.js ('k') | chrome/test/data/pdf/basic_plugin_test.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 /** 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} extensionUrl the url of the PDF extension.
10 */ 10 */
11 function PDFScriptingAPI(window, extensionUrl) { 11 function PDFScriptingAPI(window, extensionUrl) {
12 this.extensionUrl_ = extensionUrl; 12 this.extensionUrl_ = extensionUrl;
13 this.readyToReceive = false;
14 this.messageQueue_ = []; 13 this.messageQueue_ = [];
15 window.addEventListener('message', function(event) { 14 window.addEventListener('message', function(event) {
16 if (event.origin != this.extensionUrl_) 15 if (event.origin != this.extensionUrl_) {
16 console.error('Received message that was not from the extension: ' +
17 event);
17 return; 18 return;
19 }
18 switch (event.data.type) { 20 switch (event.data.type) {
19 case 'readyToReceive': 21 case 'readyToReceive':
20 this.pdfWindow_ = event.source; 22 this.setDestinationWindow(event.source);
21 this.flushPendingMessages_();
22 break; 23 break;
23 case 'viewport': 24 case 'viewport':
24 if (this.viewportChangedCallback_) 25 if (this.viewportChangedCallback_)
25 this.viewportChangedCallback_(event.data.pageX, 26 this.viewportChangedCallback_(event.data.pageX,
26 event.data.pageY, 27 event.data.pageY,
27 event.data.pageWidth, 28 event.data.pageWidth,
28 event.data.viewportWidth, 29 event.data.viewportWidth,
29 event.data.viewportHeight); 30 event.data.viewportHeight);
30 break; 31 break;
31 case 'documentLoaded': 32 case 'documentLoaded':
32 if (this.loadCallback_) 33 if (this.loadCallback_)
33 this.loadCallback_(); 34 this.loadCallback_();
34 break; 35 break;
36 case 'getAccessibilityJSONReply':
37 if (this.accessibilityCallback_) {
38 this.accessibilityCallback_(event.data.json);
39 this.accessibilityCallback_ = null;
40 }
41 break;
35 } 42 }
36 }.bind(this), false); 43 }.bind(this), false);
37 } 44 }
38 45
39 PDFScriptingAPI.prototype = { 46 PDFScriptingAPI.prototype = {
40 /** 47 /**
41 * @private 48 * @private
42 * Send a message to the extension. If we try to send messages prior to the 49 * Send a message to the extension. If we try to send messages prior to the
43 * extension being ready to receive messages (i.e. before it has finished 50 * extension being ready to receive messages (i.e. before it has finished
44 * loading) we queue up the messages and flush them later. 51 * loading) we queue up the messages and flush them later.
45 * @param {MessageObject} the message to send. 52 * @param {Object} the message to send.
46 */ 53 */
47 sendMessage_: function(message) { 54 sendMessage_: function(message) {
48 if (!this.readyToReceive) { 55 if (!this.pdfWindow_) {
49 this.messageQueue_.push(message); 56 this.messageQueue_.push(message);
50 return; 57 return;
51 } 58 }
52 59
53 this.pdfWindow_.postMessage(message, this.extensionUrl_); 60 this.pdfWindow_.postMessage(message, this.extensionUrl_);
54 }, 61 },
55 62
56 /** 63 /**
57 * @private 64 * Sets the destination window containing the PDF viewer. This will be called
58 * Flushes all pending messages to the extension. 65 * when a 'readyToReceive' message is received from the PDF viewer or it can
66 * be called during tests. It then flushes any pending messages to the window.
67 * @param {Window} pdfWindow the window containing the PDF viewer.
59 */ 68 */
60 flushPendingMessages_: function() { 69 setDestinationWindow: function(pdfWindow) {
61 this.readyToReceive = true; 70 this.pdfWindow_ = pdfWindow;
62 while (this.messageQueue_.length != 0) { 71 while (this.messageQueue_.length != 0) {
63 this.pdfWindow_.postMessage(this.messageQueue_.shift(), 72 this.pdfWindow_.postMessage(this.messageQueue_.shift(),
64 this.extensionUrl_); 73 this.extensionUrl_);
65 } 74 }
66 }, 75 },
67 76
68 /** 77 /**
69 * Sets the callback which will be run when the PDF viewport changes. 78 * Sets the callback which will be run when the PDF viewport changes.
70 * @param {Function} callback the callback to be called. 79 * @param {Function} callback the callback to be called.
71 */ 80 */
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 */ 115 */
107 loadPreviewPage: function(url, index) { 116 loadPreviewPage: function(url, index) {
108 this.sendMessage_({ 117 this.sendMessage_({
109 type: 'loadPreviewPage', 118 type: 'loadPreviewPage',
110 url: url, 119 url: url,
111 index: index 120 index: index
112 }); 121 });
113 }, 122 },
114 123
115 /** 124 /**
125 * Get accessibility JSON for the document.
126 * @param {Function} callback a callback to be called with the accessibility
127 * json that has been retrieved.
128 * @param {number} [page] the 0-indexed page number to get accessibility data
129 * for. If this is not provided, data about the entire document is
130 * returned.
131 * @return {boolean} true if the function is successful, false if there is an
132 * outstanding request for accessibility data that has not been answered.
133 */
134 getAccessibilityJSON: function(callback, page) {
135 if (this.accessibilityCallback_)
136 return false;
137 this.accessibilityCallback_ = callback;
138 var message = {
139 type: 'getAccessibilityJSON',
140 };
141 if (page || page == 0)
142 message.page = page;
143 this.sendMessage_(message);
144 return true;
145 },
146
147 /**
116 * Send a key event to the extension. 148 * Send a key event to the extension.
117 * @param {number} keyCode the key code to send to the extension. 149 * @param {number} keyCode the key code to send to the extension.
118 */ 150 */
119 sendKeyEvent: function(keyCode) { 151 sendKeyEvent: function(keyCode) {
120 // TODO(raymes): Figure out a way to do this. It's only used to do scrolling 152 // TODO(raymes): Figure out a way to do this. It's only used to do scrolling
121 // the viewport, so probably just expose viewport controls instead. 153 // the viewport, so probably just expose viewport controls instead.
122 }, 154 },
123 }; 155 };
124 156
125 /** 157 /**
(...skipping 12 matching lines...) Expand all
138 170
139 // Add the functions to the iframe so that they can be called directly. 171 // Add the functions to the iframe so that they can be called directly.
140 iframe.setViewportChangedCallback = 172 iframe.setViewportChangedCallback =
141 client.setViewportChangedCallback.bind(client); 173 client.setViewportChangedCallback.bind(client);
142 iframe.setLoadCallback = client.setLoadCallback.bind(client); 174 iframe.setLoadCallback = client.setLoadCallback.bind(client);
143 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client); 175 iframe.resetPrintPreviewMode = client.resetPrintPreviewMode.bind(client);
144 iframe.loadPreviewPage = client.loadPreviewPage.bind(client); 176 iframe.loadPreviewPage = client.loadPreviewPage.bind(client);
145 iframe.sendKeyEvent = client.sendKeyEvent.bind(client); 177 iframe.sendKeyEvent = client.sendKeyEvent.bind(client);
146 return iframe; 178 return iframe;
147 } 179 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/pdf/pdf.js ('k') | chrome/test/data/pdf/basic_plugin_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698