From f8c7235f73865705f2f1104619ea3c753fbb3c9b Mon Sep 17 00:00:00 2001 From: Mariot Chauvin Date: Fri, 10 Mar 2017 10:27:58 +0000 Subject: [PATCH] Access browsing context without panic Current browsing context accessor in Document unwrap the browsing context option which prevents document to handle correctly the case when there is no browsing context. --- components/script/dom/document.rs | 2 +- components/script/dom/window.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 29d7227dee9..14786820ffc 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -390,7 +390,7 @@ impl Document { #[inline] pub fn browsing_context(&self) -> Option> { if self.has_browsing_context { - Some(self.window.browsing_context()) + self.window.maybe_browsing_context() } else { None } diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 64a303c9412..571cf907a71 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -319,10 +319,15 @@ impl Window { &self.image_cache_thread } + /// This can panic if it is called after the browsing context has been discarded pub fn browsing_context(&self) -> Root { self.browsing_context.get().unwrap() } + pub fn maybe_browsing_context(&self) -> Option> { + self.browsing_context.get() + } + pub fn bluetooth_thread(&self) -> IpcSender { self.bluetooth_thread.clone() } @@ -608,7 +613,7 @@ impl WindowMethods for Window { // https://html.spec.whatwg.org/multipage/#dom-parent fn GetParent(&self) -> Option> { // Steps 1. and 2. - if self.browsing_context().is_discarded() { + if self.maybe_browsing_context().map_or(true, |c| c.is_discarded()) { return None; } match self.parent() { @@ -622,7 +627,7 @@ impl WindowMethods for Window { // https://html.spec.whatwg.org/multipage/#dom-top fn GetTop(&self) -> Option> { // Steps 1. and 2. - if self.browsing_context().is_discarded() { + if self.maybe_browsing_context().map_or(true, |c| c.is_discarded()) { return None; } // Step 5.