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 'command': 'onLocalJid',
128 this.parentWindow_
.postMessage(message
, '*');
132 * Callback method to indicate that something went wrong loading the WCS driver.
134 * @param {!remoting.Error} error Details of the error.
137 remoting
.WcsSandboxContent
.prototype.onError_ = function(error
) {
139 'command': 'onError',
142 this.parentWindow_
.postMessage(message
, '*');
146 * Forward an XHR to the container process to send. This is analogous to XHR's
149 * @param {remoting.XMLHttpRequestProxy} xhr The XHR to send.
150 * @return {number} The unique ID allocated to the XHR. Used to abort it.
152 remoting
.WcsSandboxContent
.prototype.sendXhr = function(xhr
) {
153 var id
= this.nextXhrId_
++;
154 this.pendingXhrs_
[id
] = xhr
;
156 'command': 'sendXhr',
158 'parameters': xhr
.sandboxIpc
160 this.parentWindow_
.postMessage(message
, '*');
161 delete xhr
.sandboxIpc
;
166 * Abort a forwarded XHR. This is analogous to XHR's abort method.
168 * @param {number} id The unique ID of the XHR to abort, as returned by sendXhr.
170 remoting
.WcsSandboxContent
.prototype.abortXhr = function(id
) {
171 if (!this.pendingXhrs_
[id
]) {
172 // The XHR is removed when it reaches the "ready" state. Calling abort
173 // subsequently is unusual, but legal, so just silently ignore the request
178 'command': 'abortXhr',
181 this.parentWindow_
.postMessage(message
, '*');
185 * Callback to indicate than an IQ stanza has been received from the WCS
186 * driver, and should be forwarded to the main process.
188 * @param {string} stanza
191 remoting
.WcsSandboxContent
.prototype.onIq_ = function(stanza
) {
192 remoting
.wcs
.setOnIq(this.onIq_
.bind(this));
197 this.parentWindow_
.postMessage(message
, '*');
201 * Entry point for the WCS sandbox process.
203 function onSandboxInit() {
204 // The WCS code registers for a couple of events that aren't supported in
205 // Apps V2, so ignore those for now.
206 var oldAEL
= window
.addEventListener
;
207 window
.addEventListener = function(type
, listener
, useCapture
) {
208 if (type
== 'beforeunload' || type
== 'unload') {
211 oldAEL(type
, listener
, useCapture
);
214 remoting
.settings
= new remoting
.Settings();
215 remoting
.sandboxContent
= new remoting
.WcsSandboxContent();
218 window
.addEventListener('load', onSandboxInit
, false);
220 /** @type {remoting.WcsSandboxContent} */
221 remoting
.sandboxContent
= null;