- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / webapp / xhr_proxy.js
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  */
5
6 /**
7  * @fileoverview
8  * The sandbox isn't allowed to make XHRs, so they have to be proxied to the
9  * main process. The XMLHttpRequestProxy class is API-compatible with the
10  * XMLHttpRequest class, but forwards the requests to the main process where
11  * they can be serviced. The forwarding of XHRs and responses is handled by
12  * the WcsSandboxContent class; this class is just a thin wrapper to hook XHR
13  * creations by JS running in the sandbox.
14  *
15  * Because XMLHttpRequest is implemented natively, and because the intent is
16  * to replace its functionality entirely, prototype linking is not a suitable
17  * approach here, so much of the interface definition is duplicated from the
18  * w3c specification: http://www.w3.org/TR/XMLHttpRequest
19  */
20
21 'use strict';
22
23 /** @suppress {duplicate} */
24 var remoting = remoting || {};
25
26 /**
27  * @constructor
28  * @extends {XMLHttpRequest}
29  */
30 remoting.XMLHttpRequestProxy = function() {
31   /**
32    * @type {{headers: Object}}
33    */
34   this.sandbox_ipc = {
35     headers: {}
36   };
37   /**
38    * @type {number}
39    * @private
40    */
41   this.xhr_id_ = -1;
42 };
43
44 remoting.XMLHttpRequestProxy.prototype.open = function(
45     method, url, async, user, password) {
46   if (!async) {
47     console.warn('Synchronous XHRs are not supported.');
48   }
49   this.sandbox_ipc.method = method;
50   this.sandbox_ipc.url = url.toString();
51   this.sandbox_ipc.user = user;
52   this.sandbox_ipc.password = password;
53 };
54
55 remoting.XMLHttpRequestProxy.prototype.send = function(data) {
56   if (remoting.sandboxContent) {
57     this.sandbox_ipc.data = data;
58     this.xhr_id_ = remoting.sandboxContent.sendXhr(this);
59   }
60 };
61
62 remoting.XMLHttpRequestProxy.prototype.setRequestHeader = function(
63     header, value) {
64   this.sandbox_ipc.headers[header] = value;
65 };
66
67 remoting.XMLHttpRequestProxy.prototype.abort = function() {
68   if (this.xhr_id_ != -1) {
69     remoting.sandboxContent.abortXhr(this.xhr_id_);
70   }
71 };
72
73 remoting.XMLHttpRequestProxy.prototype.getResponseHeader = function(header) {
74   console.error('Sandbox: unproxied getResponseHeader(' + header + ') called.');
75 };
76
77 remoting.XMLHttpRequestProxy.prototype.getAllResponseHeaders = function() {
78   console.error('Sandbox: unproxied getAllResponseHeaders called.');
79 };
80
81 remoting.XMLHttpRequestProxy.prototype.overrideMimeType = function() {
82   console.error('Sandbox: unproxied overrideMimeType called.');
83 };
84
85 remoting.XMLHttpRequestProxy.prototype.UNSENT = 0;
86 remoting.XMLHttpRequestProxy.prototype.OPENED = 1;
87 remoting.XMLHttpRequestProxy.prototype.HEADERS_RECEIVED = 2;
88 remoting.XMLHttpRequestProxy.prototype.LOADING = 3;
89 remoting.XMLHttpRequestProxy.prototype.DONE = 4;