Introduce GlobalScope::get_url

This commit is contained in:
Anthony Ramine 2016-10-02 14:29:01 +02:00
parent 092504b4e3
commit f38159b7d3
10 changed files with 39 additions and 39 deletions

View file

@ -32,7 +32,6 @@ use std::ffi::CString;
use std::panic;
use task_source::file_reading::FileReadingTaskSource;
use timers::{OneshotTimerCallback, OneshotTimerHandle};
use url::Url;
/// A freely-copyable reference to a rooted global object.
#[derive(Copy, Clone)]
@ -91,14 +90,6 @@ impl<'a> GlobalRef<'a> {
self.resource_threads().sender()
}
/// Get the URL for this global scope.
pub fn get_url(&self) -> Url {
match *self {
GlobalRef::Window(ref window) => window.get_url(),
GlobalRef::Worker(ref worker) => worker.get_url().clone(),
}
}
/// `ScriptChan` used to send messages to the event loop of this global's
/// thread.
pub fn script_chan(&self) -> Box<ScriptChan + Send> {
@ -158,7 +149,7 @@ impl<'a> GlobalRef<'a> {
&self, code: &str, filename: &str, rval: MutableHandleValue) {
let metadata = time::TimerMetadata {
url: if filename.is_empty() {
self.get_url().as_str().into()
self.as_global_scope().get_url().as_str().into()
} else {
filename.into()
},

View file

@ -190,6 +190,7 @@ impl Blob {
/// valid or invalid Blob URL.
fn promote(&self, set_valid: bool) -> Uuid {
let mut bytes = vec![];
let global_url = self.global_scope().get_url();
match *self.blob_impl.borrow_mut() {
BlobImpl::Sliced(_, _) => {
@ -199,8 +200,7 @@ impl Blob {
}
BlobImpl::File(ref f) => {
if set_valid {
let global = self.global();
let origin = get_blob_origin(&global.r().get_url());
let origin = get_blob_origin(&global_url);
let (tx, rx) = ipc::channel().unwrap();
let msg = FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone());
@ -219,8 +219,7 @@ impl Blob {
BlobImpl::Memory(ref mut bytes_in) => mem::swap(bytes_in, &mut bytes),
};
let global = self.global();
let origin = get_blob_origin(&global.r().get_url());
let origin = get_blob_origin(&global_url);
let blob_buf = BlobBuf {
filename: None,
@ -251,9 +250,7 @@ impl Blob {
/// Get a FileID representing sliced parent-blob content
fn create_sliced_url_id(&self, parent_id: &Uuid,
rel_pos: &RelativePos, parent_len: u64) -> Uuid {
let global = self.global();
let origin = get_blob_origin(&global.r().get_url());
let origin = get_blob_origin(&self.global_scope().get_url());
let (tx, rx) = ipc::channel().unwrap();
let msg = FileManagerThreadMsg::AddSlicedURLEntry(parent_id.clone(),
@ -282,8 +279,7 @@ impl Blob {
/// Cleanups at the time of destruction/closing
fn clean_up_file_resource(&self) {
if let BlobImpl::File(ref f) = *self.blob_impl.borrow() {
let global = self.global();
let origin = get_blob_origin(&global.r().get_url());
let origin = get_blob_origin(&self.global_scope().get_url());
let (tx, rx) = ipc::channel().unwrap();
@ -311,7 +307,7 @@ impl Drop for Blob {
fn read_file(global: GlobalRef, id: Uuid) -> Result<Vec<u8>, ()> {
let resource_threads = global.resource_threads();
let (chan, recv) = ipc::channel().map_err(|_|())?;
let origin = get_blob_origin(&global.get_url());
let origin = get_blob_origin(&global.as_global_scope().get_url());
let check_url_validity = false;
let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin);
let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));

View file

@ -52,14 +52,15 @@ impl EventSource {
pub fn Constructor(global: GlobalRef,
url_str: DOMString,
event_source_init: &EventSourceInit) -> Fallible<Root<EventSource>> {
let global_scope = global.as_global_scope();
// Steps 1-2
let base_url = global.get_url();
let base_url = global_scope.get_url();
let url = match base_url.join(&*url_str) {
Ok(u) => u,
Err(_) => return Err(Error::Syntax)
};
// Step 3
let event_source = EventSource::new(global.as_global_scope(), url, event_source_init.withCredentials);
let event_source = EventSource::new(global_scope, url, event_source_init.withCredentials);
// Step 4
// Step 5
// Step 6

View file

@ -179,6 +179,17 @@ impl GlobalScope {
}
unreachable!();
}
/// Get the URL for this global scope.
pub fn get_url(&self) -> Url {
if let Some(window) = self.downcast::<Window>() {
return window.get_url();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.get_url().clone();
}
unreachable!();
}
}
fn timestamp_in_ms(time: Timespec) -> u64 {

View file

@ -92,7 +92,7 @@ impl Request {
// Step 4
// TODO: `entry settings object` is not implemented in Servo yet.
let base_url = global.get_url();
let base_url = global.as_global_scope().get_url();
match input {
// Step 5
@ -130,7 +130,7 @@ impl Request {
// Step 7
// TODO: `entry settings object` is not implemented yet.
let origin = global.get_url().origin();
let origin = base_url.origin();
// Step 8
let mut window = Window::Client;
@ -450,8 +450,9 @@ impl Request {
fn net_request_from_global(global: GlobalRef,
url: Url,
is_service_worker_global_scope: bool) -> NetTraitsRequest {
let origin = Origin::Origin(global.get_url().origin());
let pipeline_id = global.as_global_scope().pipeline_id();
let global_scope = global.as_global_scope();
let origin = Origin::Origin(global_scope.get_url().origin());
let pipeline_id = global_scope.pipeline_id();
NetTraitsRequest::new(url,
Some(origin),
is_service_worker_global_scope,

View file

@ -149,9 +149,10 @@ impl Response {
// https://fetch.spec.whatwg.org/#dom-response-redirect
pub fn Redirect(global: GlobalRef, url: USVString, status: u16) -> Fallible<Root<Response>> {
let global_scope = global.as_global_scope();
// Step 1
// TODO: `entry settings object` is not implemented in Servo yet.
let base_url = global.get_url();
let base_url = global_scope.get_url();
let parsed_url = base_url.join(&url.0);
// Step 2
@ -167,7 +168,7 @@ impl Response {
// Step 4
// see Step 4 continued
let r = Response::new(global.as_global_scope());
let r = Response::new(global_scope);
// Step 5
*r.status.borrow_mut() = Some(StatusCode::from_u16(status));

View file

@ -40,9 +40,7 @@ impl Storage {
}
fn get_url(&self) -> Url {
let global_root = self.global();
let global_ref = global_root.r();
global_ref.get_url()
self.global_scope().get_url()
}
fn get_storage_thread(&self) -> IpcSender<StorageThreadMsg> {

View file

@ -118,7 +118,7 @@ impl URL {
pub fn CreateObjectURL(global: GlobalRef, blob: &Blob) -> DOMString {
/// XXX: Second field is an unicode-serialized Origin, it is a temporary workaround
/// and should not be trusted. See issue https://github.com/servo/servo/issues/11722
let origin = get_blob_origin(&global.get_url());
let origin = get_blob_origin(&global.as_global_scope().get_url());
if blob.IsClosed() {
// Generate a dummy id
@ -142,7 +142,7 @@ impl URL {
NOTE: The first step is unnecessary, since closed blobs do not exist in the store
*/
let origin = get_blob_origin(&global.get_url());
let origin = get_blob_origin(&global.as_global_scope().get_url());
if let Ok(url) = Url::parse(&url) {
if let Ok((id, _, _)) = parse_blob_url(&url) {

View file

@ -239,10 +239,11 @@ impl WebSocket {
}
// Step 6: Origin.
let origin = UrlHelper::Origin(&global.get_url()).0;
let global_scope = global.as_global_scope();
let origin = UrlHelper::Origin(&global_scope.get_url()).0;
// Step 7.
let ws = WebSocket::new(global.as_global_scope(), resource_url.clone());
let ws = WebSocket::new(global_scope, resource_url.clone());
let address = Trusted::new(ws.r());
let connect_data = WebSocketConnectData {

View file

@ -595,7 +595,7 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
use_cors_preflight: has_handlers,
credentials_mode: credentials_mode,
use_url_credentials: use_url_credentials,
origin: self.global().r().get_url(),
origin: self.global_scope().get_url(),
referrer_url: self.referrer_url.clone(),
referrer_policy: self.referrer_policy.clone(),
pipeline_id: self.pipeline_id(),
@ -1204,7 +1204,7 @@ impl XMLHttpRequest {
// TODO: Disable scripting while parsing
parse_html(document.r(),
DOMString::from(decoded),
wr.get_url(),
wr.as_global_scope().get_url(),
ParseContext::Owner(Some(wr.as_global_scope().pipeline_id())));
document
}
@ -1218,7 +1218,7 @@ impl XMLHttpRequest {
// TODO: Disable scripting while parsing
parse_xml(document.r(),
DOMString::from(decoded),
wr.get_url(),
wr.as_global_scope().get_url(),
xml::ParseContext::Owner(Some(wr.as_global_scope().pipeline_id())));
document
}
@ -1230,7 +1230,7 @@ impl XMLHttpRequest {
let doc = win.Document();
let doc = doc.r();
let docloader = DocumentLoader::new(&*doc.loader());
let base = self.global().r().get_url();
let base = self.global_scope().get_url();
let parsed_url = match base.join(&self.ResponseURL().0) {
Ok(parsed) => Some(parsed),
Err(_) => None // Step 7