Webidl and basic implementation of XHR object

This commit is contained in:
Manish Goregaokar 2014-05-02 11:04:21 +05:30
parent 803c922248
commit 619da07a4f
10 changed files with 364 additions and 17 deletions

View file

@ -125,6 +125,9 @@ DOMInterfaces = {
'window',
],
},
'XMLHttpRequest': {},
'XMLHttpRequestEventTarget': {},
'XMLHttpRequestUpload': {},
'TestBinding': {},

View file

@ -193,22 +193,11 @@ class CGMethodCall(CGThing):
# easy case!
signature = possibleSignatures[0]
# (possibly) important optimization: if signature[1] has >
# argCount arguments and signature[1][argCount] is optional and
# there is only one signature for argCount+1, then the
# signature for argCount+1 is just ourselves and we can fall
# through.
if (len(signature[1]) > argCount and
signature[1][argCount].optional and
(argCount+1) in allowedArgCounts and
len(method.signaturesForArgCount(argCount+1)) == 1):
argCountCases.append(
CGCase(str(argCount), None, True))
else:
sigIndex = signatures.index(signature)
argCountCases.append(
CGCase(str(argCount), getPerSignatureCall(signature,
signatureIndex=sigIndex)))
sigIndex = signatures.index(signature)
argCountCases.append(
CGCase(str(argCount), getPerSignatureCall(signature,
signatureIndex=sigIndex)))
continue
distinguishingIndex = method.distinguishingIndexForArgCount(argCount)
@ -2677,6 +2666,7 @@ use js::jsapi::JSContext;
use js::jsval::JSVal;
#[repr(uint)]
#[deriving(Encodable)]
pub enum valuelist {
%s
}

View file

@ -10,6 +10,7 @@ use self::EventListenerBinding::EventListener;
use dom::event::Event;
use dom::eventdispatcher::dispatch_event;
use dom::node::NodeTypeId;
use dom::xmlhttprequest::XMLHttpRequestId;
use dom::virtualmethods::VirtualMethods;
use servo_util::str::DOMString;
@ -23,8 +24,9 @@ pub enum ListenerPhase {
#[deriving(Eq,Encodable)]
pub enum EventTargetTypeId {
NodeTargetTypeId(NodeTypeId),
WindowTypeId,
NodeTargetTypeId(NodeTypeId)
XMLHttpRequestTargetTypeId(XMLHttpRequestId)
}
#[deriving(Eq,Encodable)]

View file

@ -0,0 +1,63 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* http://xhr.spec.whatwg.org/#interface-xmlhttprequest
*
* To the extent possible under law, the editor has waived all copyright
* and related or neighboring rights to this work. In addition, as of 1 May 2014,
* the editor has made this specification available under the Open Web Foundation
* Agreement Version 1.0, which is available at
* http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
*/
enum XMLHttpRequestResponseType {
"",
"arraybuffer",
"blob",
"document",
"json",
"text"
};
[Constructor,
Exposed=Window,Worker]
interface XMLHttpRequest : XMLHttpRequestEventTarget {
// event handler
// attribute EventHandler onreadystatechange;
// states
const unsigned short UNSENT = 0;
const unsigned short OPENED = 1;
const unsigned short HEADERS_RECEIVED = 2;
const unsigned short LOADING = 3;
const unsigned short DONE = 4;
readonly attribute unsigned short readyState;
// request
// void open(/* ByteString */ DOMString method, /* [EnsureUTF16] */ DOMString url);
// void open(/* ByteString */ DOMString method, /* [EnsureUTF16] */ DOMString url, boolean async, optional /* [EnsureUTF16] */ DOMString? username = null, optional /* [EnsureUTF16] */ DOMString? password = null);
// void setRequestHeader(/* ByteString */ DOMString name, /* ByteString */ DOMString value);
attribute unsigned long timeout;
attribute boolean withCredentials;
readonly attribute XMLHttpRequestUpload upload;
// void send(optional /*(ArrayBufferView or Blob or Document or [EnsureUTF16] */ DOMString/* or FormData or URLSearchParams)*/? data = null);
// void abort();
// response
readonly attribute DOMString responseURL;
readonly attribute unsigned short status;
readonly attribute /* ByteString*/ DOMString statusText;
// DOMString? /*ByteString?*/ getResponseHeader(/*ByteString*/ DOMString name);
// DOMString /*ByteString*/ getAllResponseHeaders();
// void overrideMimeType(DOMString mime);
attribute XMLHttpRequestResponseType responseType;
// readonly attribute any response;
readonly attribute DOMString responseText;
[Exposed=Window] readonly attribute Document? responseXML;
};

View file

@ -0,0 +1,28 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* http://xhr.spec.whatwg.org/#interface-xmlhttprequest
*
* To the extent possible under law, the editor has waived all copyright
* and related or neighboring rights to this work. In addition, as of 1 May 2014,
* the editor has made this specification available under the Open Web Foundation
* Agreement Version 1.0, which is available at
* http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
*/
/* https://github.com/mozilla/servo/issues/1223: [NoInterfaceObject] */
interface XMLHttpRequestEventTarget : EventTarget {
// event handlers
/* Needs EventHandler: https://github.com/mozilla/servo/issues/1238
attribute EventHandler onloadstart;
attribute EventHandler onprogress;
attribute EventHandler onabort;
attribute EventHandler onerror;
attribute EventHandler onload;
attribute EventHandler ontimeout;
attribute EventHandler onloadend;
*/
};

View file

@ -0,0 +1,18 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* http://xhr.spec.whatwg.org/#interface-xmlhttprequest
*
* To the extent possible under law, the editor has waived all copyright
* and related or neighboring rights to this work. In addition, as of 1 May 2014,
* the editor has made this specification available under the Open Web Foundation
* Agreement Version 1.0, which is available at
* http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
*/
[Exposed=Window,Worker]
interface XMLHttpRequestUpload : XMLHttpRequestEventTarget {
};

View file

@ -0,0 +1,152 @@
/* 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 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: DOMString,
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: ~"",
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: DOMString, _url: DOMString) {
}
pub fn Open_(&self, _method: DOMString, _url: DOMString, _async: bool,
_username: Option<DOMString>, _password: Option<DOMString>) {
}
pub fn SetRequestHeader(&self, _name: DOMString, _value: DOMString) {
}
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) -> DOMString {
self.status_text.clone()
}
pub fn GetResponseHeader(&self, _name: DOMString) -> Option<DOMString> {
None
}
pub fn GetAllResponseHeaders(&self) -> DOMString {
~""
}
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
}
}
}

View file

@ -0,0 +1,40 @@
/* 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::InheritTypes::XMLHttpRequestEventTargetDerived;
use dom::bindings::utils::{Reflectable, Reflector};
use dom::eventtarget::{EventTarget, XMLHttpRequestTargetTypeId};
use dom::xmlhttprequest::XMLHttpRequestId;
#[deriving(Encodable)]
pub struct XMLHttpRequestEventTarget {
pub eventtarget: EventTarget,
}
impl XMLHttpRequestEventTarget {
pub fn new_inherited(type_id: XMLHttpRequestId) -> XMLHttpRequestEventTarget {
XMLHttpRequestEventTarget {
eventtarget: EventTarget::new_inherited(XMLHttpRequestTargetTypeId(type_id))
}
}
}
impl XMLHttpRequestEventTargetDerived for EventTarget {
fn is_xmlhttprequesteventtarget(&self) -> bool {
match self.type_id {
XMLHttpRequestTargetTypeId(_) => true,
_ => false
}
}
}
impl Reflectable for XMLHttpRequestEventTarget {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.eventtarget.reflector()
}
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
self.eventtarget.mut_reflector()
}
}

View file

@ -0,0 +1,48 @@
/* 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::InheritTypes::XMLHttpRequestUploadDerived;
use dom::bindings::codegen::BindingDeclarations::XMLHttpRequestUploadBinding;
use dom::bindings::js::JS;
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::eventtarget::{EventTarget, XMLHttpRequestTargetTypeId};
use dom::window::Window;
use dom::xmlhttprequest::{XMLHttpRequestUploadTypeId};
use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTarget;
#[deriving(Encodable)]
pub struct XMLHttpRequestUpload {
eventtarget: XMLHttpRequestEventTarget
}
impl XMLHttpRequestUpload {
pub fn new_inherited() -> XMLHttpRequestUpload {
XMLHttpRequestUpload {
eventtarget:XMLHttpRequestEventTarget::new_inherited(XMLHttpRequestUploadTypeId)
}
}
pub fn new(window: &JS<Window>) -> JS<XMLHttpRequestUpload> {
reflect_dom_object(~XMLHttpRequestUpload::new_inherited(),
window,
XMLHttpRequestUploadBinding::Wrap)
}
}
impl Reflectable for XMLHttpRequestUpload {
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 XMLHttpRequestUploadDerived for EventTarget {
fn is_xmlhttprequestupload(&self) -> bool {
match self.type_id {
XMLHttpRequestTargetTypeId(XMLHttpRequestUploadTypeId) => true,
_ => false
}
}
}