mirror of
https://github.com/servo/servo.git
synced 2025-08-02 04:00:32 +01:00
Auto merge of #14559 - asajeffrey:script-track-document-and-bc-discarding, r=cbrewster
Implement browsing context discarding <!-- Please describe your changes on the following line: --> Implement browsing context discarding (https://html.spec.whatwg.org/multipage/browsers.html#discard-a-document). * When a pipeline is closed, inform the script thread whether the browsing context is to be discarded. * In script threads, synchronously discard any similar-origin documents and browsing contexts. * When a browsing context is discarded, it loses the reference to the active document, but the window keeps it, so we need to move the `Document` pointer from `BrowsingContext` to `Window`. * Fix the webIDL for Window to make parent and top optional (the spec says they can return null when the browsing context is discarded). --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #14411 - [X] There are tests for these changes <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14559) <!-- Reviewable:end -->
This commit is contained in:
commit
8b274f25d3
12 changed files with 196 additions and 160 deletions
|
@ -3,16 +3,13 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::conversions::{ToJSValConvertible, root_from_handleobject};
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::{JS, MutNullableJS, Root, RootedReference};
|
||||
use dom::bindings::js::{JS, Root, RootedReference};
|
||||
use dom::bindings::proxyhandler::{fill_property_descriptor, get_property_descriptor};
|
||||
use dom::bindings::reflector::{DomObject, MutDomObject, Reflector};
|
||||
use dom::bindings::trace::JSTraceable;
|
||||
use dom::bindings::utils::WindowProxyHandler;
|
||||
use dom::bindings::utils::get_array_index_from_id;
|
||||
use dom::document::Document;
|
||||
use dom::element::Element;
|
||||
use dom::globalscope::GlobalScope;
|
||||
use dom::window::Window;
|
||||
use js::JSCLASS_IS_GLOBAL;
|
||||
use js::glue::{CreateWrapperProxyHandler, ProxyTraps, NewWindowProxy};
|
||||
|
@ -26,7 +23,6 @@ use js::jsapi::{MutableHandle, MutableHandleObject, MutableHandleValue};
|
|||
use js::jsapi::{ObjectOpResult, PropertyDescriptor};
|
||||
use js::jsval::{UndefinedValue, PrivateValue};
|
||||
use js::rust::get_object_class;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use std::cell::Cell;
|
||||
|
||||
#[dom_struct]
|
||||
|
@ -40,10 +36,8 @@ pub struct BrowsingContext {
|
|||
/// Indicates if reflow is required when reloading.
|
||||
needs_reflow: Cell<bool>,
|
||||
|
||||
/// The current active document.
|
||||
/// Note that the session history is stored in the constellation,
|
||||
/// in the script thread we just track the current active document.
|
||||
active_document: MutNullableJS<Document>,
|
||||
/// Has this browsing context been discarded?
|
||||
discarded: Cell<bool>,
|
||||
|
||||
/// The containing iframe element, if this is a same-origin iframe
|
||||
frame_element: Option<JS<Element>>,
|
||||
|
@ -54,7 +48,7 @@ impl BrowsingContext {
|
|||
BrowsingContext {
|
||||
reflector: Reflector::new(),
|
||||
needs_reflow: Cell::new(true),
|
||||
active_document: Default::default(),
|
||||
discarded: Cell::new(false),
|
||||
frame_element: frame_element.map(JS::from_ref),
|
||||
}
|
||||
}
|
||||
|
@ -84,20 +78,12 @@ impl BrowsingContext {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_active_document(&self, document: &Document) {
|
||||
self.active_document.set(Some(document))
|
||||
pub fn discard(&self) {
|
||||
self.discarded.set(true);
|
||||
}
|
||||
|
||||
pub fn active_document(&self) -> Root<Document> {
|
||||
self.active_document.get().expect("No active document.")
|
||||
}
|
||||
|
||||
pub fn maybe_active_document(&self) -> Option<Root<Document>> {
|
||||
self.active_document.get()
|
||||
}
|
||||
|
||||
pub fn active_window(&self) -> Root<Window> {
|
||||
Root::from_ref(self.active_document().window())
|
||||
pub fn is_discarded(&self) -> bool {
|
||||
self.discarded.get()
|
||||
}
|
||||
|
||||
pub fn frame_element(&self) -> Option<&Element> {
|
||||
|
@ -115,15 +101,6 @@ impl BrowsingContext {
|
|||
self.needs_reflow.set(status);
|
||||
old
|
||||
}
|
||||
|
||||
pub fn active_pipeline_id(&self) -> Option<PipelineId> {
|
||||
self.active_document.get()
|
||||
.map(|doc| doc.window().upcast::<GlobalScope>().pipeline_id())
|
||||
}
|
||||
|
||||
pub fn unset_active_document(&self) {
|
||||
self.active_document.set(None)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
|
|
|
@ -190,6 +190,7 @@ pub struct Document {
|
|||
last_modified: Option<String>,
|
||||
encoding: Cell<EncodingRef>,
|
||||
is_html_document: bool,
|
||||
is_fully_active: Cell<bool>,
|
||||
url: DOMRefCell<ServoUrl>,
|
||||
quirks_mode: Cell<QuirksMode>,
|
||||
/// Caches for the getElement methods
|
||||
|
@ -385,20 +386,17 @@ impl Document {
|
|||
self.trigger_mozbrowser_event(MozBrowserEvent::SecurityChange(https_state));
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#active-document
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.browsing_context().map_or(false, |context| {
|
||||
self == &*context.active_document()
|
||||
})
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#fully-active
|
||||
pub fn is_fully_active(&self) -> bool {
|
||||
if !self.is_active() {
|
||||
return false;
|
||||
}
|
||||
// FIXME: It should also check whether the browser context is top-level or not
|
||||
true
|
||||
self.is_fully_active.get()
|
||||
}
|
||||
|
||||
pub fn fully_activate(&self) {
|
||||
self.is_fully_active.set(true)
|
||||
}
|
||||
|
||||
pub fn fully_deactivate(&self) {
|
||||
self.is_fully_active.set(false)
|
||||
}
|
||||
|
||||
pub fn origin(&self) -> &Origin {
|
||||
|
@ -1693,11 +1691,16 @@ impl Document {
|
|||
self.current_parser.get()
|
||||
}
|
||||
|
||||
/// Find an iframe element in the document.
|
||||
pub fn find_iframe(&self, frame_id: FrameId) -> Option<Root<HTMLIFrameElement>> {
|
||||
/// Iterate over all iframes in the document.
|
||||
pub fn iter_iframes(&self) -> impl Iterator<Item=Root<HTMLIFrameElement>> {
|
||||
self.upcast::<Node>()
|
||||
.traverse_preorder()
|
||||
.filter_map(Root::downcast::<HTMLIFrameElement>)
|
||||
}
|
||||
|
||||
/// Find an iframe element in the document.
|
||||
pub fn find_iframe(&self, frame_id: FrameId) -> Option<Root<HTMLIFrameElement>> {
|
||||
self.iter_iframes()
|
||||
.find(|node| node.frame_id() == frame_id)
|
||||
}
|
||||
|
||||
|
@ -1864,6 +1867,7 @@ impl Document {
|
|||
// https://dom.spec.whatwg.org/#concept-document-encoding
|
||||
encoding: Cell::new(UTF_8),
|
||||
is_html_document: is_html_document == IsHTMLDocument::HTMLDocument,
|
||||
is_fully_active: Cell::new(false),
|
||||
id_map: DOMRefCell::new(HashMap::new()),
|
||||
tag_map: DOMRefCell::new(HashMap::new()),
|
||||
tagns_map: DOMRefCell::new(HashMap::new()),
|
||||
|
@ -2261,19 +2265,12 @@ impl DocumentMethods for Document {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-document-hasfocus
|
||||
fn HasFocus(&self) -> bool {
|
||||
match self.browsing_context() {
|
||||
Some(browsing_context) => {
|
||||
// Step 2.
|
||||
let candidate = browsing_context.active_document();
|
||||
// Step 3.
|
||||
if &*candidate == self {
|
||||
true
|
||||
} else {
|
||||
false //TODO Step 4.
|
||||
}
|
||||
}
|
||||
None => false,
|
||||
// Step 1-2.
|
||||
if self.window().parent_info().is_none() && self.is_fully_active() {
|
||||
return true;
|
||||
}
|
||||
// TODO Step 3.
|
||||
false
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#relaxing-the-same-origin-restriction
|
||||
|
@ -3190,8 +3187,8 @@ impl DocumentMethods for Document {
|
|||
|
||||
// Step 2.
|
||||
// TODO: handle throw-on-dynamic-markup-insertion counter.
|
||||
|
||||
if !self.is_active() {
|
||||
// FIXME: this should check for being active rather than fully active
|
||||
if !self.is_fully_active() {
|
||||
// Step 3.
|
||||
return Ok(());
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ use dom::eventtarget::EventTarget;
|
|||
use dom::globalscope::GlobalScope;
|
||||
use dom::htmlelement::HTMLElement;
|
||||
use dom::node::{Node, NodeDamage, UnbindContext, document_from_node, window_from_node};
|
||||
use dom::urlhelper::UrlHelper;
|
||||
use dom::virtualmethods::VirtualMethods;
|
||||
use dom::window::{ReflowReason, Window};
|
||||
use html5ever_atoms::LocalName;
|
||||
|
@ -709,42 +708,34 @@ impl VirtualMethods for HTMLIFrameElement {
|
|||
LoadBlocker::terminate(&mut blocker);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#a-browsing-context-is-discarded
|
||||
if let Some(pipeline_id) = self.pipeline_id.get() {
|
||||
debug!("Unbinding pipeline {} from frame {}.", pipeline_id, self.frame_id);
|
||||
let window = window_from_node(self);
|
||||
debug!("Unbinding frame {}.", self.frame_id);
|
||||
let window = window_from_node(self);
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
|
||||
// The only reason we're waiting for the iframe to be totally
|
||||
// removed is to ensure the script thread can't add iframes faster
|
||||
// than the compositor can remove them.
|
||||
//
|
||||
// Since most of this cleanup doesn't happen on same-origin
|
||||
// iframes, and since that would cause a deadlock, don't do it.
|
||||
let same_origin = {
|
||||
// FIXME(#10968): this should probably match the origin check in
|
||||
// HTMLIFrameElement::contentDocument.
|
||||
let self_url = self.get_url();
|
||||
let win_url = window_from_node(self).get_url();
|
||||
UrlHelper::SameOrigin(&self_url, &win_url) || self_url.as_str() == "about:blank"
|
||||
};
|
||||
let (sender, receiver) = if same_origin {
|
||||
(None, None)
|
||||
} else {
|
||||
let (sender, receiver) = ipc::channel().unwrap();
|
||||
(Some(sender), Some(receiver))
|
||||
};
|
||||
let msg = ConstellationMsg::RemoveIFrame(pipeline_id, sender);
|
||||
window.upcast::<GlobalScope>().constellation_chan().send(msg).unwrap();
|
||||
if let Some(receiver) = receiver {
|
||||
receiver.recv().unwrap()
|
||||
// Ask the constellation to remove the iframe, and tell us the
|
||||
// pipeline ids of the closed pipelines.
|
||||
let msg = ConstellationMsg::RemoveIFrame(self.frame_id, sender);
|
||||
window.upcast::<GlobalScope>().constellation_chan().send(msg).unwrap();
|
||||
let exited_pipeline_ids = receiver.recv().unwrap();
|
||||
|
||||
// The spec for discarding is synchronous,
|
||||
// so we need to discard the browsing contexts now, rather than
|
||||
// when the `PipelineExit` message arrives.
|
||||
for exited_pipeline_id in exited_pipeline_ids {
|
||||
if let Some(exited_document) = ScriptThread::find_document(exited_pipeline_id) {
|
||||
exited_document.window().browsing_context().discard();
|
||||
for exited_iframe in exited_document.iter_iframes() {
|
||||
exited_iframe.pipeline_id.set(None);
|
||||
}
|
||||
}
|
||||
|
||||
// Resetting the pipeline_id to None is required here so that
|
||||
// if this iframe is subsequently re-added to the document
|
||||
// the load doesn't think that it's a navigation, but instead
|
||||
// a new iframe. Without this, the constellation gets very
|
||||
// confused.
|
||||
self.pipeline_id.set(None);
|
||||
}
|
||||
|
||||
// Resetting the pipeline_id to None is required here so that
|
||||
// if this iframe is subsequently re-added to the document
|
||||
// the load doesn't think that it's a navigation, but instead
|
||||
// a new iframe. Without this, the constellation gets very
|
||||
// confused.
|
||||
self.pipeline_id.set(None);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,9 +31,13 @@
|
|||
// other browsing contexts
|
||||
[Replaceable] readonly attribute WindowProxy frames;
|
||||
//[Replaceable] readonly attribute unsigned long length;
|
||||
[Unforgeable] readonly attribute WindowProxy top;
|
||||
// Note that this can return null in the case that the browsing context has been discarded.
|
||||
// https://github.com/whatwg/html/issues/2115
|
||||
[Unforgeable] readonly attribute WindowProxy? top;
|
||||
// attribute any opener;
|
||||
readonly attribute WindowProxy parent;
|
||||
// Note that this can return null in the case that the browsing context has been discarded.
|
||||
// https://github.com/whatwg/html/issues/2115
|
||||
readonly attribute WindowProxy? parent;
|
||||
readonly attribute Element? frameElement;
|
||||
//WindowProxy open(optional DOMString url = "about:blank", optional DOMString target = "_blank",
|
||||
// optional DOMString features = "", optional boolean replace = false);
|
||||
|
|
|
@ -163,6 +163,7 @@ pub struct Window {
|
|||
#[ignore_heap_size_of = "channels are hard"]
|
||||
image_cache_chan: ImageCacheChan,
|
||||
browsing_context: MutNullableJS<BrowsingContext>,
|
||||
document: MutNullableJS<Document>,
|
||||
history: MutNullableJS<History>,
|
||||
performance: MutNullableJS<Performance>,
|
||||
navigation_start: u64,
|
||||
|
@ -443,7 +444,7 @@ impl WindowMethods for Window {
|
|||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-document-2
|
||||
fn Document(&self) -> Root<Document> {
|
||||
self.browsing_context().active_document()
|
||||
self.document.get().expect("Document accessed before initialization.")
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-history
|
||||
|
@ -551,21 +552,32 @@ impl WindowMethods for Window {
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-parent
|
||||
fn Parent(&self) -> Root<BrowsingContext> {
|
||||
match self.parent() {
|
||||
Some(window) => window.browsing_context(),
|
||||
None => self.Window()
|
||||
fn GetParent(&self) -> Option<Root<BrowsingContext>> {
|
||||
// Steps 1. and 2.
|
||||
if self.browsing_context().is_discarded() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
match self.parent() {
|
||||
// Step 4.
|
||||
Some(parent) => Some(parent.Window()),
|
||||
// Step 5.
|
||||
None => Some(self.Window())
|
||||
}
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-top
|
||||
fn Top(&self) -> Root<BrowsingContext> {
|
||||
fn GetTop(&self) -> Option<Root<BrowsingContext>> {
|
||||
// Steps 1. and 2.
|
||||
if self.browsing_context().is_discarded() {
|
||||
return None;
|
||||
}
|
||||
// Step 5.
|
||||
let mut window = Root::from_ref(self);
|
||||
while let Some(parent) = window.parent() {
|
||||
window = parent;
|
||||
}
|
||||
window.browsing_context()
|
||||
}
|
||||
Some(window.Window())
|
||||
}
|
||||
|
||||
// https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/
|
||||
// NavigationTiming/Overview.html#sec-window.performance-attribute
|
||||
|
@ -1351,6 +1363,13 @@ impl Window {
|
|||
unsafe { SetWindowProxy(cx, window, browsing_context.reflector().get_jsobject()); }
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn init_document(&self, document: &Document) {
|
||||
assert!(self.document.get().is_none());
|
||||
assert!(document.window() == self);
|
||||
self.document.set(Some(&document));
|
||||
}
|
||||
|
||||
/// Commence a new URL load which will either replace this window or scroll to a fragment.
|
||||
pub fn load_url(&self, url: ServoUrl, replace: bool, force_reload: bool,
|
||||
referrer_policy: Option<ReferrerPolicy>) {
|
||||
|
@ -1518,13 +1537,8 @@ impl Window {
|
|||
return None;
|
||||
}
|
||||
|
||||
let browsing_context = self.browsing_context();
|
||||
|
||||
browsing_context.frame_element().map(|frame_element| {
|
||||
let window = window_from_node(frame_element);
|
||||
let context = window.browsing_context();
|
||||
context.active_window()
|
||||
})
|
||||
self.browsing_context().frame_element()
|
||||
.map(|frame_element| window_from_node(frame_element))
|
||||
}
|
||||
|
||||
/// Returns whether this window is mozbrowser.
|
||||
|
@ -1610,6 +1624,7 @@ impl Window {
|
|||
image_cache_thread: image_cache_thread,
|
||||
history: Default::default(),
|
||||
browsing_context: Default::default(),
|
||||
document: Default::default(),
|
||||
performance: Default::default(),
|
||||
navigation_start: (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as u64,
|
||||
navigation_start_precise: time::precise_time_ns() as f64,
|
||||
|
|
|
@ -83,7 +83,7 @@ use profile_traits::time::{self, ProfilerCategory, profile};
|
|||
use script_layout_interface::message::{self, NewLayoutThreadInfo, ReflowQueryType};
|
||||
use script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory, EnqueuedPromiseCallback};
|
||||
use script_runtime::{ScriptPort, StackRootTLS, get_reports, new_rt_and_cx, PromiseJobQueue};
|
||||
use script_traits::{CompositorEvent, ConstellationControlMsg, EventResult};
|
||||
use script_traits::{CompositorEvent, ConstellationControlMsg, DiscardBrowsingContext, EventResult};
|
||||
use script_traits::{InitialScriptState, LayoutMsg, LoadData, MouseButton, MouseEventType, MozBrowserEvent};
|
||||
use script_traits::{NewLayoutInfo, ScriptMsg as ConstellationMsg};
|
||||
use script_traits::{ScriptThreadFactory, TimerEvent, TimerEventRequest, TimerSource};
|
||||
|
@ -1007,8 +1007,8 @@ impl ScriptThread {
|
|||
self.handle_css_error_reporting(pipeline_id, filename, line, column, msg),
|
||||
ConstellationControlMsg::Reload(pipeline_id) =>
|
||||
self.handle_reload(pipeline_id),
|
||||
ConstellationControlMsg::ExitPipeline(pipeline_id) =>
|
||||
self.handle_exit_pipeline_msg(pipeline_id),
|
||||
ConstellationControlMsg::ExitPipeline(pipeline_id, discard_browsing_context) =>
|
||||
self.handle_exit_pipeline_msg(pipeline_id, discard_browsing_context),
|
||||
msg @ ConstellationControlMsg::AttachLayout(..) |
|
||||
msg @ ConstellationControlMsg::Viewport(..) |
|
||||
msg @ ConstellationControlMsg::SetScrollState(..) |
|
||||
|
@ -1368,6 +1368,7 @@ impl ScriptThread {
|
|||
}
|
||||
}
|
||||
document.window().thaw();
|
||||
document.fully_activate();
|
||||
return;
|
||||
}
|
||||
let mut loads = self.incomplete_loads.borrow_mut();
|
||||
|
@ -1565,7 +1566,7 @@ impl ScriptThread {
|
|||
}
|
||||
|
||||
/// Handles a request to exit a pipeline and shut down layout.
|
||||
fn handle_exit_pipeline_msg(&self, id: PipelineId) {
|
||||
fn handle_exit_pipeline_msg(&self, id: PipelineId, discard_bc: DiscardBrowsingContext) {
|
||||
debug!("Exiting pipeline {}.", id);
|
||||
|
||||
self.closed_pipelines.borrow_mut().insert(id);
|
||||
|
@ -1591,6 +1592,11 @@ impl ScriptThread {
|
|||
|
||||
if let Some(document) = self.documents.borrow_mut().remove(id) {
|
||||
shut_down_layout(document.window());
|
||||
if discard_bc == DiscardBrowsingContext::Yes {
|
||||
if let Some(context) = document.browsing_context() {
|
||||
context.discard();
|
||||
}
|
||||
}
|
||||
let _ = self.constellation_chan.send(ConstellationMsg::PipelineExited(id));
|
||||
}
|
||||
|
||||
|
@ -1606,7 +1612,7 @@ impl ScriptThread {
|
|||
pipeline_ids.extend(self.documents.borrow().iter().next().map(|(pipeline_id, _)| pipeline_id));
|
||||
|
||||
for pipeline_id in pipeline_ids {
|
||||
self.handle_exit_pipeline_msg(pipeline_id);
|
||||
self.handle_exit_pipeline_msg(pipeline_id, DiscardBrowsingContext::Yes);
|
||||
}
|
||||
|
||||
debug!("Exited script thread.");
|
||||
|
@ -1819,9 +1825,13 @@ impl ScriptThread {
|
|||
referrer_policy);
|
||||
document.set_ready_state(DocumentReadyState::Loading);
|
||||
|
||||
if !incomplete.is_frozen {
|
||||
document.fully_activate();
|
||||
}
|
||||
|
||||
self.documents.borrow_mut().insert(incomplete.pipeline_id, &*document);
|
||||
|
||||
browsing_context.set_active_document(&document);
|
||||
window.init_document(&document);
|
||||
|
||||
self.constellation_chan
|
||||
.send(ConstellationMsg::ActivateDocument(incomplete.pipeline_id))
|
||||
|
@ -2253,8 +2263,8 @@ fn shut_down_layout(window: &Window) {
|
|||
// Drop our references to the JSContext and DOM objects.
|
||||
window.clear_js_runtime();
|
||||
|
||||
// Sever the connection between the global and the DOM tree
|
||||
browsing_context.unset_active_document();
|
||||
// Discard the browsing context.
|
||||
browsing_context.discard();
|
||||
|
||||
// Destroy the layout thread. If there were node leaks, layout will now crash safely.
|
||||
chan.send(message::Msg::ExitNow).ok();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue