aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/xmlhttprequest.rs
blob: 76099a4d0d0631d7a4e5997918ed3fa75484cf41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::BindingDeclarations::XMLHttpRequestBinding;
use dom::bindings::str::ByteString;
use self::XMLHttpRequestBinding::XMLHttpRequestResponseType;
use self::XMLHttpRequestBinding::XMLHttpRequestResponseTypeValues::_empty;
use dom::bindings::codegen::InheritTypes::XMLHttpRequestDerived;
use dom::document::Document;
use dom::eventtarget::{EventTarget, XMLHttpRequestTargetTypeId};
use dom::bindings::error::Fallible;
use dom::bindings::js::JS;
use js::jsapi::JSContext;
use js::jsval::{JSVal, NullValue};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::window::Window;
use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTarget;
use dom::xmlhttprequestupload::XMLHttpRequestUpload;
use servo_util::str::DOMString;

#[deriving(Eq,Encodable)]
pub enum XMLHttpRequestId {
    XMLHttpRequestTypeId,
    XMLHttpRequestUploadTypeId
}

#[deriving(Encodable)]
pub struct XMLHttpRequest {
    eventtarget: XMLHttpRequestEventTarget,
    ready_state: u16,
    timeout: u32,
    with_credentials: bool,
    upload: JS<XMLHttpRequestUpload>,
    response_url: DOMString,
    status: u16,
    status_text: ByteString,
    response_type: XMLHttpRequestResponseType,
    response_text: DOMString,
    response_xml: Option<JS<Document>>
}

impl XMLHttpRequest {
    pub fn new_inherited(owner: &JS<Window>) -> XMLHttpRequest {
        XMLHttpRequest {
            eventtarget: XMLHttpRequestEventTarget::new_inherited(XMLHttpRequestTypeId),
            ready_state: 0,
            timeout: 0u32,
            with_credentials: false,
            upload: XMLHttpRequestUpload::new(owner),
            response_url: ~"",
            status: 0,
            status_text: ByteString::new(vec!()),
            response_type: _empty,
            response_text: ~"",
            response_xml: None
        }
    }
    pub fn new(window: &JS<Window>) -> JS<XMLHttpRequest> {
        reflect_dom_object(~XMLHttpRequest::new_inherited(window),
                           window,
                           XMLHttpRequestBinding::Wrap)
    }
    pub fn Constructor(owner: &JS<Window>) -> Fallible<JS<XMLHttpRequest>> {
        Ok(XMLHttpRequest::new(owner))
    }
    pub fn ReadyState(&self) -> u16 {
        self.ready_state
    }
    pub fn Open(&self, _method: ByteString, _url: DOMString) {

    }
    pub fn Open_(&self, _method: ByteString, _url: DOMString, _async: bool,
                 _username: Option<DOMString>, _password: Option<DOMString>) {

    }
    pub fn SetRequestHeader(&self, _name: ByteString, _value: ByteString) {

    }
    pub fn Timeout(&self) -> u32 {
        self.timeout
    }
    pub fn SetTimeout(&mut self, timeout: u32) {
        self.timeout = timeout
    }
    pub fn WithCredentials(&self) -> bool {
        self.with_credentials
    }
    pub fn SetWithCredentials(&mut self, with_credentials: bool) {
        self.with_credentials = with_credentials
    }
    pub fn Upload(&self) -> JS<XMLHttpRequestUpload> {
        self.upload.clone()
    }
    pub fn Send(&self, _data: Option<DOMString>) {

    }
    pub fn Abort(&self) {

    }
    pub fn ResponseURL(&self) -> DOMString {
        self.response_url.clone()
    }
    pub fn Status(&self) -> u16 {
        self.status
    }
    pub fn StatusText(&self) -> ByteString {
        self.status_text.clone()
    }
    pub fn GetResponseHeader(&self, _name: ByteString) -> Option<ByteString> {
        None
    }
    pub fn GetAllResponseHeaders(&self) -> ByteString {
        ByteString::new(vec!())
    }
    pub fn OverrideMimeType(&self, _mime: DOMString) {

    }
    pub fn ResponseType(&self) -> XMLHttpRequestResponseType {
        self.response_type
    }
    pub fn SetResponseType(&mut self, response_type: XMLHttpRequestResponseType) {
        self.response_type = response_type
    }
    pub fn Response(&self, _cx: *JSContext) -> JSVal {
        NullValue()
    }
    pub fn ResponseText(&self) -> DOMString {
        self.response_text.clone()
    }
    pub fn GetResponseXML(&self) -> Option<JS<Document>> {
        self.response_xml.clone()
    }
}

impl Reflectable for XMLHttpRequest {
    fn reflector<'a>(&'a self) -> &'a Reflector {
        self.eventtarget.reflector()
    }

    fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
        self.eventtarget.mut_reflector()
    }
}

impl XMLHttpRequestDerived for EventTarget {
    fn is_xmlhttprequest(&self) -> bool {
        match self.type_id {
            XMLHttpRequestTargetTypeId(XMLHttpRequestTypeId) => true,
            _ => false
        }
    }
}