Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / wcs_sandbox_content.js
blobdd85d1d859af19fefc8d4d6b0359aad475b76984
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.
4 */
6 /**
7 * @fileoverview
8 * The sandbox side of the application/sandbox WCS interface, used by the
9 * sandbox to exchange messages with the application.
12 'use strict';
14 /** @suppress {duplicate} */
15 var remoting = remoting || {};
17 /** @constructor */
18 remoting.WcsSandboxContent = function() {
19 /** @private {Window} */
20 this.parentWindow_ = null;
21 /** @private {number} */
22 this.nextXhrId_ = 0;
23 /** @private {Object<number, XMLHttpRequest>} */
24 this.pendingXhrs_ = {};
26 window.addEventListener('message', this.onMessage_.bind(this), false);
29 /**
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']) {
39 case 'proxyXhrs':
40 // Since the WCS driver code constructs XHRs directly, the only
41 // mechanism for proxying them is to replace the XMLHttpRequest
42 // constructor.
43 XMLHttpRequest = remoting.XMLHttpRequestProxy;
44 break;
46 case 'sendIq':
47 /** @type {string} */
48 var stanza = event.data['stanza'];
49 if (stanza === undefined) {
50 console.error('sendIq: missing IQ stanza.');
51 break;
53 if (remoting.wcs) {
54 remoting.wcs.sendIq(stanza);
55 } else {
56 console.error('Dropping IQ stanza:', stanza);
58 break;
60 case 'setAccessToken':
61 /** @type {string} */
62 var token = event.data['token'];
63 if (token === undefined) {
64 console.error('setAccessToken: missing access token.');
65 break;
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).
71 if (remoting.wcs) {
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));
79 break;
81 case 'xhrStateChange':
82 /** @type {number} */
83 var id = event.data['id'];
84 if (id === undefined) {
85 console.error('xhrStateChange: missing id.');
86 break;
88 var pendingXhr = this.pendingXhrs_[id];
89 if (!pendingXhr) {
90 console.error('xhrStateChange: unrecognized id:', id);
91 break;
93 /** @type {XMLHttpRequest} */
94 var xhr = event.data['xhr'];
95 if (xhr === undefined) {
96 console.error('xhrStateChange: missing xhr');
97 break;
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();
108 break;
110 default:
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.
120 * @private
122 remoting.WcsSandboxContent.prototype.onLocalJid_ = function(localJid) {
123 remoting.wcs.setOnIq(this.onIq_.bind(this));
124 var message = {
125 'command': 'onLocalJid',
126 'localJid': localJid
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.
135 * @private
137 remoting.WcsSandboxContent.prototype.onError_ = function(error) {
138 var message = {
139 'command': 'onError',
140 'error': error
142 this.parentWindow_.postMessage(message, '*');
146 * Forward an XHR to the container process to send. This is analogous to XHR's
147 * send method.
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;
155 var message = {
156 'command': 'sendXhr',
157 'id': id,
158 'parameters': xhr.sandboxIpc
160 this.parentWindow_.postMessage(message, '*');
161 delete xhr.sandboxIpc;
162 return id;
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
174 // in this case.
175 return;
177 var message = {
178 'command': 'abortXhr',
179 'id': id
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
189 * @private
191 remoting.WcsSandboxContent.prototype.onIq_ = function(stanza) {
192 remoting.wcs.setOnIq(this.onIq_.bind(this));
193 var message = {
194 'command': 'onIq',
195 'stanza': stanza
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') {
209 return;
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;