mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Create easy common interface for off-thread network listeners, and remove the CORS-specific reimplementation of async networking.
This commit is contained in:
parent
f7ac1f1876
commit
1ca9ff56c8
4 changed files with 110 additions and 78 deletions
|
@ -26,6 +26,7 @@ use dom::urlsearchparams::URLSearchParamsHelpers;
|
|||
use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTarget;
|
||||
use dom::xmlhttprequesteventtarget::XMLHttpRequestEventTargetTypeId;
|
||||
use dom::xmlhttprequestupload::XMLHttpRequestUpload;
|
||||
use network_listener::{NetworkListener, PreInvoke};
|
||||
use script_task::{ScriptChan, ScriptMsg, Runnable, ScriptPort};
|
||||
|
||||
use encoding::all::UTF_8;
|
||||
|
@ -43,10 +44,10 @@ use js::jsapi::JS_ClearPendingException;
|
|||
use js::jsval::{JSVal, NullValue, UndefinedValue};
|
||||
|
||||
use net_traits::ControlMsg::Load;
|
||||
use net_traits::{ResourceTask, ResourceCORSData, LoadData, LoadConsumer, AsyncResponseTarget};
|
||||
use net_traits::{AsyncResponseListener, ResponseAction, Metadata};
|
||||
use net_traits::{ResourceTask, ResourceCORSData, LoadData, LoadConsumer};
|
||||
use net_traits::{AsyncResponseListener, Metadata};
|
||||
use cors::{allow_cross_origin_request, CORSRequest, RequestMode, AsyncCORSResponseListener};
|
||||
use cors::{AsyncCORSResponseTarget, CORSResponse};
|
||||
use cors::CORSResponse;
|
||||
use util::str::DOMString;
|
||||
use util::task::spawn_named;
|
||||
|
||||
|
@ -224,45 +225,15 @@ impl XMLHttpRequest {
|
|||
}
|
||||
}
|
||||
|
||||
struct CORSListener {
|
||||
context: Arc<Mutex<CORSContext>>,
|
||||
script_chan: Box<ScriptChan+Send>,
|
||||
}
|
||||
|
||||
impl AsyncCORSResponseTarget for CORSListener {
|
||||
fn invoke_with_listener(&self, response: CORSResponse) {
|
||||
self.script_chan.send(ScriptMsg::RunnableMsg(box CORSRunnable {
|
||||
context: self.context.clone(),
|
||||
response: response,
|
||||
})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
struct CORSRunnable {
|
||||
context: Arc<Mutex<CORSContext>>,
|
||||
response: CORSResponse,
|
||||
}
|
||||
|
||||
impl Runnable for CORSRunnable {
|
||||
fn handler(self: Box<CORSRunnable>) {
|
||||
let this = *self;
|
||||
let context = this.context.lock().unwrap();
|
||||
context.response_available(this.response);
|
||||
}
|
||||
}
|
||||
|
||||
let cors_context = Arc::new(Mutex::new(CORSContext {
|
||||
let cors_context = CORSContext {
|
||||
xhr: context,
|
||||
load_data: RefCell::new(Some(load_data)),
|
||||
req: req.clone(),
|
||||
script_chan: script_chan.clone(),
|
||||
resource_task: resource_task,
|
||||
}));
|
||||
};
|
||||
|
||||
req.http_fetch_async(box CORSListener {
|
||||
context: cors_context,
|
||||
script_chan: script_chan
|
||||
});
|
||||
req.http_fetch_async(box cors_context, script_chan);
|
||||
}
|
||||
|
||||
fn initiate_async_xhr(context: Arc<Mutex<XHRContext>>,
|
||||
|
@ -293,40 +264,14 @@ impl XMLHttpRequest {
|
|||
}
|
||||
}
|
||||
|
||||
struct XHRRunnable {
|
||||
context: Arc<Mutex<XHRContext>>,
|
||||
action: ResponseAction,
|
||||
}
|
||||
|
||||
impl Runnable for XHRRunnable {
|
||||
fn handler(self: Box<XHRRunnable>) {
|
||||
let this = *self;
|
||||
|
||||
let context = this.context.lock().unwrap();
|
||||
let xhr = context.xhr.to_temporary().root();
|
||||
if xhr.r().generation_id.get() != context.gen_id {
|
||||
return;
|
||||
}
|
||||
|
||||
this.action.process(&*context);
|
||||
impl PreInvoke for XHRContext {
|
||||
fn should_invoke(&self) -> bool {
|
||||
let xhr = self.xhr.to_temporary().root();
|
||||
xhr.r().generation_id.get() == self.gen_id
|
||||
}
|
||||
}
|
||||
|
||||
struct XHRListener {
|
||||
context: Arc<Mutex<XHRContext>>,
|
||||
script_chan: Box<ScriptChan+Send>,
|
||||
}
|
||||
|
||||
impl AsyncResponseTarget for XHRListener {
|
||||
fn invoke_with_listener(&self, action: ResponseAction) {
|
||||
self.script_chan.send(ScriptMsg::RunnableMsg(box XHRRunnable {
|
||||
context: self.context.clone(),
|
||||
action: action
|
||||
})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let listener = box XHRListener {
|
||||
let listener = box NetworkListener {
|
||||
context: context,
|
||||
script_chan: script_chan,
|
||||
};
|
||||
|
@ -635,7 +580,7 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
|
|||
debug!("request_headers = {:?}", *self.request_headers.borrow());
|
||||
|
||||
self.fetch_time.set(time::now().to_timespec().sec);
|
||||
let rv = self.fetch2(load_data, cors_request, global.r());
|
||||
let rv = self.fetch(load_data, cors_request, global.r());
|
||||
if self.sync.get() {
|
||||
return rv;
|
||||
}
|
||||
|
@ -798,8 +743,8 @@ trait PrivateXMLHttpRequestHelpers {
|
|||
fn cancel_timeout(self);
|
||||
fn filter_response_headers(self) -> Headers;
|
||||
fn discard_subsequent_responses(self);
|
||||
fn fetch2(self, load_data: LoadData, cors_request: Result<Option<CORSRequest>,()>,
|
||||
global: GlobalRef) -> ErrorResult;
|
||||
fn fetch(self, load_data: LoadData, cors_request: Result<Option<CORSRequest>,()>,
|
||||
global: GlobalRef) -> ErrorResult;
|
||||
}
|
||||
|
||||
impl<'a> PrivateXMLHttpRequestHelpers for JSRef<'a, XMLHttpRequest> {
|
||||
|
@ -1119,8 +1064,7 @@ impl<'a> PrivateXMLHttpRequestHelpers for JSRef<'a, XMLHttpRequest> {
|
|||
self.response_status.set(Err(()));
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn fetch2(self,
|
||||
fn fetch(self,
|
||||
load_data: LoadData,
|
||||
cors_request: Result<Option<CORSRequest>,()>,
|
||||
global: GlobalRef) -> ErrorResult {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue