2015-05-11 19:35:33 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
//! Tracking of pending loads in a document.
|
|
|
|
//! https://html.spec.whatwg.org/multipage/#the-end
|
|
|
|
|
2016-02-10 14:20:29 +00:00
|
|
|
use dom::bindings::js::JS;
|
|
|
|
use dom::document::Document;
|
2016-09-21 23:49:33 +00:00
|
|
|
use ipc_channel::ipc::IpcSender;
|
2016-10-13 14:55:00 +00:00
|
|
|
use net_traits::{CoreResourceMsg, FetchResponseMsg, ResourceThreads, IpcSend};
|
2016-09-21 23:49:33 +00:00
|
|
|
use net_traits::request::RequestInit;
|
2016-11-17 21:34:47 +00:00
|
|
|
use servo_url::ServoUrl;
|
2016-02-10 14:20:29 +00:00
|
|
|
use std::thread;
|
2015-05-11 19:35:33 +00:00
|
|
|
|
2015-08-04 03:24:41 +00:00
|
|
|
#[derive(JSTraceable, PartialEq, Clone, Debug, HeapSizeOf)]
|
2015-05-11 19:35:33 +00:00
|
|
|
pub enum LoadType {
|
2016-11-17 21:34:47 +00:00
|
|
|
Image(ServoUrl),
|
|
|
|
Script(ServoUrl),
|
|
|
|
Subframe(ServoUrl),
|
|
|
|
Stylesheet(ServoUrl),
|
|
|
|
PageSource(ServoUrl),
|
|
|
|
Media(ServoUrl),
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LoadType {
|
2016-11-17 21:34:47 +00:00
|
|
|
fn url(&self) -> &ServoUrl {
|
2015-05-11 19:35:33 +00:00
|
|
|
match *self {
|
|
|
|
LoadType::Image(ref url) |
|
|
|
|
LoadType::Script(ref url) |
|
|
|
|
LoadType::Subframe(ref url) |
|
|
|
|
LoadType::Stylesheet(ref url) |
|
2016-05-03 23:42:46 +00:00
|
|
|
LoadType::Media(ref url) |
|
2015-05-11 19:35:33 +00:00
|
|
|
LoadType::PageSource(ref url) => url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 14:20:29 +00:00
|
|
|
/// Canary value ensuring that manually added blocking loads (ie. ones that weren't
|
2016-10-13 14:55:00 +00:00
|
|
|
/// created via DocumentLoader::fetch_async) are always removed by the time
|
2016-02-10 14:20:29 +00:00
|
|
|
/// that the owner is destroyed.
|
|
|
|
#[derive(JSTraceable, HeapSizeOf)]
|
|
|
|
#[must_root]
|
|
|
|
pub struct LoadBlocker {
|
|
|
|
/// The document whose load event is blocked by this object existing.
|
|
|
|
doc: JS<Document>,
|
|
|
|
/// The load that is blocking the document's load event.
|
|
|
|
load: Option<LoadType>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LoadBlocker {
|
|
|
|
/// Mark the document's load event as blocked on this new load.
|
|
|
|
pub fn new(doc: &Document, load: LoadType) -> LoadBlocker {
|
2016-10-04 13:05:44 +00:00
|
|
|
doc.mut_loader().add_blocking_load(load.clone());
|
2016-02-10 14:20:29 +00:00
|
|
|
LoadBlocker {
|
|
|
|
doc: JS::from_ref(doc),
|
|
|
|
load: Some(load),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove this load from the associated document's list of blocking loads.
|
|
|
|
pub fn terminate(blocker: &mut Option<LoadBlocker>) {
|
|
|
|
if let Some(this) = blocker.as_mut() {
|
|
|
|
this.doc.finish_load(this.load.take().unwrap());
|
|
|
|
}
|
|
|
|
*blocker = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the url associated with this load.
|
2016-11-17 21:34:47 +00:00
|
|
|
pub fn url(&self) -> Option<&ServoUrl> {
|
2016-02-10 14:20:29 +00:00
|
|
|
self.load.as_ref().map(LoadType::url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LoadBlocker {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if !thread::panicking() {
|
2016-07-01 13:04:38 +00:00
|
|
|
debug_assert!(self.load.is_none());
|
2016-02-10 14:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 03:24:41 +00:00
|
|
|
#[derive(JSTraceable, HeapSizeOf)]
|
2015-05-11 19:35:33 +00:00
|
|
|
pub struct DocumentLoader {
|
2016-06-01 10:09:21 +00:00
|
|
|
resource_threads: ResourceThreads,
|
2015-05-11 19:35:33 +00:00
|
|
|
blocking_loads: Vec<LoadType>,
|
2015-10-26 20:48:23 +00:00
|
|
|
events_inhibited: bool,
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentLoader {
|
|
|
|
pub fn new(existing: &DocumentLoader) -> DocumentLoader {
|
2016-10-13 14:55:00 +00:00
|
|
|
DocumentLoader::new_with_threads(existing.resource_threads.clone(), None)
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 10:09:21 +00:00
|
|
|
pub fn new_with_threads(resource_threads: ResourceThreads,
|
2016-11-17 21:34:47 +00:00
|
|
|
initial_load: Option<ServoUrl>) -> DocumentLoader {
|
2016-11-03 23:11:05 +00:00
|
|
|
debug!("Initial blocking load {:?}.", initial_load);
|
2015-05-11 19:35:33 +00:00
|
|
|
let initial_loads = initial_load.into_iter().map(LoadType::PageSource).collect();
|
|
|
|
|
|
|
|
DocumentLoader {
|
2016-06-01 10:09:21 +00:00
|
|
|
resource_threads: resource_threads,
|
2015-05-11 19:35:33 +00:00
|
|
|
blocking_loads: initial_loads,
|
2015-10-26 20:48:23 +00:00
|
|
|
events_inhibited: false,
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 14:20:29 +00:00
|
|
|
/// Add a load to the list of blocking loads.
|
2016-10-04 13:05:44 +00:00
|
|
|
fn add_blocking_load(&mut self, load: LoadType) {
|
2016-11-03 23:11:05 +00:00
|
|
|
debug!("Adding blocking load {:?} ({}).", load, self.blocking_loads.len());
|
2016-02-10 14:20:29 +00:00
|
|
|
self.blocking_loads.push(load);
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:49:33 +00:00
|
|
|
/// Initiate a new fetch.
|
|
|
|
pub fn fetch_async(&mut self,
|
|
|
|
load: LoadType,
|
|
|
|
request: RequestInit,
|
2016-10-04 13:05:44 +00:00
|
|
|
fetch_target: IpcSender<FetchResponseMsg>) {
|
|
|
|
self.add_blocking_load(load);
|
2017-02-23 01:50:48 +00:00
|
|
|
self.fetch_async_background(request, fetch_target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initiate a new fetch that does not block the document load event.
|
2017-03-08 12:28:58 +00:00
|
|
|
pub fn fetch_async_background(&self,
|
2017-02-23 01:50:48 +00:00
|
|
|
request: RequestInit,
|
|
|
|
fetch_target: IpcSender<FetchResponseMsg>) {
|
2016-10-04 13:05:44 +00:00
|
|
|
self.resource_threads.sender().send(CoreResourceMsg::Fetch(request, fetch_target)).unwrap();
|
2016-09-21 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:35:33 +00:00
|
|
|
/// Mark an in-progress network request complete.
|
2016-04-29 13:33:34 +00:00
|
|
|
pub fn finish_load(&mut self, load: &LoadType) {
|
2016-11-03 23:11:05 +00:00
|
|
|
debug!("Removing blocking load {:?} ({}).", load, self.blocking_loads.len());
|
2016-04-29 13:33:34 +00:00
|
|
|
let idx = self.blocking_loads.iter().position(|unfinished| *unfinished == *load);
|
2015-05-21 17:37:06 +00:00
|
|
|
self.blocking_loads.remove(idx.expect(&format!("unknown completed load {:?}", load)));
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_blocked(&self) -> bool {
|
2015-11-18 10:14:30 +00:00
|
|
|
// TODO: Ensure that we report blocked if parsing is still ongoing.
|
2015-05-11 19:35:33 +00:00
|
|
|
!self.blocking_loads.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn inhibit_events(&mut self) {
|
2015-10-26 20:48:23 +00:00
|
|
|
self.events_inhibited = true;
|
|
|
|
}
|
2016-06-01 10:09:21 +00:00
|
|
|
|
2015-10-26 20:48:23 +00:00
|
|
|
pub fn events_inhibited(&self) -> bool {
|
|
|
|
self.events_inhibited
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|
2016-06-01 10:09:21 +00:00
|
|
|
|
|
|
|
pub fn resource_threads(&self) -> &ResourceThreads {
|
|
|
|
&self.resource_threads
|
|
|
|
}
|
2015-05-11 19:35:33 +00:00
|
|
|
}
|