1 /* Copyright 2013 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
8 * The sandbox side of the application/sandbox WCS interface, used by the
9 * sandbox to exchange messages with the application.
14 /** @suppress {duplicate} */
15 var remoting = remoting || {};
18 remoting.WcsSandboxContent = function() {
19 /** @private {Window} */
20 this.parentWindow_ = null;
21 /** @private {number} */
23 /** @private {Object<number, XMLHttpRequest>} */
24 this.pendingXhrs_ = {};
26 window.addEventListener('message', this.onMessage_.bind(this), false);
30 * Event handler to process messages from the application.
32 * @param {Event} event
34 remoting.WcsSandboxContent.prototype.onMessage_ = function(event) {
35 this.parentWindow_ = event.source;
37 switch (event.data['command']) {
40 // Since the WCS driver code constructs XHRs directly, the only
41 // mechanism for proxying them is to replace the XMLHttpRequest
43 XMLHttpRequest = remoting.XMLHttpRequestProxy;
48 var stanza = event.data['stanza'];
49 if (stanza === undefined) {
50 console.error('sendIq: missing IQ stanza.');
54 remoting.wcs.sendIq(stanza);
56 console.error('Dropping IQ stanza:', stanza);
60 case 'setAccessToken':
62 var token = event.data['token'];
63 if (token === undefined) {
64 console.error('setAccessToken: missing access token.');
67 // The WCS driver JS requires that remoting.wcsLoader be a global
68 // variable, so it can't be a member of this class.
69 // TODO(jamiewalch): remoting.wcs doesn't need to be global and should
70 // be made a member (http://crbug.com/172348).
72 remoting.wcs.updateAccessToken(token);
73 } else if (!remoting.wcsLoader) {
74 remoting.wcsLoader = new remoting.WcsLoader();
75 remoting.wcsLoader.start(token,
76 this.onLocalJid_.bind(this),
77 this.onError_.bind(this));
81 case 'xhrStateChange':
83 var id = event.data['id'];
84 if (id === undefined) {
85 console.error('xhrStateChange: missing id.');
88 var pendingXhr = this.pendingXhrs_[id];
90 console.error('xhrStateChange: unrecognized id:', id);
93 /** @type {XMLHttpRequest} */
94 var xhr = event.data['xhr'];
95 if (xhr === undefined) {
96 console.error('xhrStateChange: missing xhr');
99 for (var member in xhr) {
100 pendingXhr[member] = xhr[member];
102 if (xhr.readyState == 4) {
103 delete this.pendingXhrs_[id];
105 if (pendingXhr.onreadystatechange) {
106 pendingXhr.onreadystatechange();
111 console.error('Unexpected message:', event.data['command'], event.data);
116 * Callback method to indicate that the WCS driver has loaded and provide the
117 * full JID of the client.
119 * @param {string} localJid The full JID of the WCS client.
122 remoting.WcsSandboxContent.prototype.onLocalJid_ = function(localJid) {
123 remoting.wcs.setOnIq(this.onIq_.bind(this));
125 'source': 'wcs-sandbox',
126 'command': 'onLocalJid',
129 this.parentWindow_.postMessage(message, '*');
133 * Callback method to indicate that something went wrong loading the WCS driver.
135 * @param {!remoting.Error} error Details of the error.
138 remoting.WcsSandboxContent.prototype.onError_ = function(error) {
140 'source': 'wcs-sandbox',
141 'command': 'onError',
144 this.parentWindow_.postMessage(message, '*');
148 * Forward an XHR to the container process to send. This is analogous to XHR's
151 * @param {remoting.XMLHttpRequestProxy} xhr The XHR to send.
152 * @return {number} The unique ID allocated to the XHR. Used to abort it.
154 remoting.WcsSandboxContent.prototype.sendXhr = function(xhr) {
155 var id = this.nextXhrId_++;
156 this.pendingXhrs_[id] = xhr;
158 'source': 'wcs-sandbox',
159 'command': 'sendXhr',
161 'parameters': xhr.sandboxIpc
163 this.parentWindow_.postMessage(message, '*');
164 delete xhr.sandboxIpc;
169 * Abort a forwarded XHR. This is analogous to XHR's abort method.
171 * @param {number} id The unique ID of the XHR to abort, as returned by sendXhr.
173 remoting.WcsSandboxContent.prototype.abortXhr = function(id) {
174 if (!this.pendingXhrs_[id]) {
175 // The XHR is removed when it reaches the "ready" state. Calling abort
176 // subsequently is unusual, but legal, so just silently ignore the request
181 'source': 'wcs-sandbox',
182 'command': 'abortXhr',
185 this.parentWindow_.postMessage(message, '*');
189 * Callback to indicate than an IQ stanza has been received from the WCS
190 * driver, and should be forwarded to the main process.
192 * @param {string} stanza
195 remoting.WcsSandboxContent.prototype.onIq_ = function(stanza) {
196 remoting.wcs.setOnIq(this.onIq_.bind(this));
198 'source': 'wcs-sandbox',
202 this.parentWindow_.postMessage(message, '*');
206 * Entry point for the WCS sandbox process.
208 function onSandboxInit() {
209 // The WCS code registers for a couple of events that aren't supported in
210 // Apps V2, so ignore those for now.
211 var oldAEL = window.addEventListener;
212 window.addEventListener = function(type, listener, useCapture) {
213 if (type == 'beforeunload' || type == 'unload') {
216 oldAEL(type, listener, useCapture);
219 remoting.settings = new remoting.Settings();
220 remoting.sandboxContent = new remoting.WcsSandboxContent();
223 window.addEventListener('load', onSandboxInit, false);
225 /** @type {remoting.WcsSandboxContent} */
226 remoting.sandboxContent = null;