From e67adfc290cdde1dfc7239c0c5de1b63f628f1a9 Mon Sep 17 00:00:00 2001 From: Gregory Terzian Date: Fri, 27 Jul 2018 13:41:10 +0800 Subject: [PATCH] use DOMTracker instead of WeakMediaQueryListVec --- components/script/dom/mediaquerylist.rs | 54 +------------------------ components/script/dom/window.rs | 32 ++++++++++++--- 2 files changed, 28 insertions(+), 58 deletions(-) diff --git a/components/script/dom/mediaquerylist.rs b/components/script/dom/mediaquerylist.rs index 4c6be0091b1..63e743fde2e 100644 --- a/components/script/dom/mediaquerylist.rs +++ b/components/script/dom/mediaquerylist.rs @@ -2,24 +2,17 @@ * 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/. */ -use dom::bindings::cell::DomRefCell; use dom::bindings::codegen::Bindings::EventListenerBinding::EventListener; use dom::bindings::codegen::Bindings::EventTargetBinding::AddEventListenerOptions; use dom::bindings::codegen::Bindings::EventTargetBinding::EventListenerOptions; use dom::bindings::codegen::Bindings::MediaQueryListBinding::{self, MediaQueryListMethods}; use dom::bindings::inheritance::Castable; -use dom::bindings::reflector::DomObject; use dom::bindings::reflector::reflect_dom_object; use dom::bindings::root::{Dom, DomRoot}; use dom::bindings::str::DOMString; -use dom::bindings::trace::JSTraceable; -use dom::bindings::weakref::{WeakRef, WeakRefVec}; use dom::document::Document; -use dom::event::Event; use dom::eventtarget::EventTarget; -use dom::mediaquerylistevent::MediaQueryListEvent; use dom_struct::dom_struct; -use js::jsapi::JSTracer; use std::cell::Cell; use std::rc::Rc; use style::media_queries::MediaList; @@ -56,7 +49,7 @@ impl MediaQueryList { } impl MediaQueryList { - fn evaluate_changes(&self) -> MediaQueryListMatchState { + pub fn evaluate_changes(&self) -> MediaQueryListMatchState { let matches = self.evaluate(); let result = if let Some(old_matches) = self.last_match_state.get() { @@ -115,48 +108,3 @@ impl MediaQueryListMethods for MediaQueryList { // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-onchange event_handler!(change, GetOnchange, SetOnchange); } - -#[derive(MallocSizeOf)] -pub struct WeakMediaQueryListVec { - cell: DomRefCell>, -} - -impl WeakMediaQueryListVec { - /// Create a new vector of weak references to MediaQueryList - pub fn new() -> Self { - WeakMediaQueryListVec { cell: DomRefCell::new(WeakRefVec::new()) } - } - - pub fn push(&self, mql: &MediaQueryList) { - self.cell.borrow_mut().push(WeakRef::new(mql)); - } - - /// Evaluate media query lists and report changes - /// - pub fn evaluate_and_report_changes(&self) { - rooted_vec!(let mut mql_list); - self.cell.borrow_mut().update(|mql| { - let mql = mql.root().unwrap(); - if let MediaQueryListMatchState::Changed(_) = mql.evaluate_changes() { - // Recording list of changed Media Queries - mql_list.push(Dom::from_ref(&*mql)); - } - }); - // Sending change events for all changed Media Queries - for mql in mql_list.iter() { - let event = MediaQueryListEvent::new(&mql.global(), - atom!("change"), - false, false, - mql.Media(), - mql.Matches()); - event.upcast::().fire(mql.upcast::()); - } - } -} - -#[allow(unsafe_code)] -unsafe impl JSTraceable for WeakMediaQueryListVec { - unsafe fn trace(&self, _: *mut JSTracer) { - self.cell.borrow_mut().retain_alive() - } -} diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 5e1829f0c62..43d7719b76b 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -12,6 +12,7 @@ use dom::bindings::cell::DomRefCell; use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, DocumentReadyState}; use dom::bindings::codegen::Bindings::FunctionBinding::Function; use dom::bindings::codegen::Bindings::HistoryBinding::HistoryBinding::HistoryMethods; +use dom::bindings::codegen::Bindings::MediaQueryListBinding::MediaQueryListBinding::MediaQueryListMethods; use dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionState; use dom::bindings::codegen::Bindings::RequestBinding::RequestInit; use dom::bindings::codegen::Bindings::WindowBinding::{self, FrameRequestCallback, WindowMethods}; @@ -27,16 +28,20 @@ use dom::bindings::str::{DOMString, USVString}; use dom::bindings::structuredclone::StructuredCloneData; use dom::bindings::trace::RootedTraceableBox; use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler}; +use dom::bindings::weakref::DOMTracker; use dom::bluetooth::BluetoothExtraPermissionData; use dom::crypto::Crypto; use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner}; use dom::customelementregistry::CustomElementRegistry; use dom::document::{AnimationFrameCallback, Document}; use dom::element::Element; +use dom::event::Event; +use dom::eventtarget::EventTarget; use dom::globalscope::GlobalScope; use dom::history::History; use dom::location::Location; -use dom::mediaquerylist::{MediaQueryList, WeakMediaQueryListVec}; +use dom::mediaquerylist::{MediaQueryList, MediaQueryListMatchState}; +use dom::mediaquerylistevent::MediaQueryListEvent; use dom::messageevent::MessageEvent; use dom::navigator::Navigator; use dom::node::{Node, NodeDamage, document_from_node, from_untrusted_node_address}; @@ -260,7 +265,7 @@ pub struct Window { scroll_offsets: DomRefCell>>, /// All the MediaQueryLists we need to update - media_query_lists: WeakMediaQueryListVec, + media_query_lists: DOMTracker, test_runner: MutNullableDom, @@ -1052,7 +1057,7 @@ impl WindowMethods for Window { media_queries::MediaList::parse(&context, &mut parser); let document = self.Document(); let mql = MediaQueryList::new(&document, media_query_list); - self.media_query_lists.push(&*mql); + self.media_query_lists.track(&*mql); mql } @@ -1771,8 +1776,25 @@ impl Window { self.parent_info.is_none() } + /// Evaluate media query lists and report changes + /// pub fn evaluate_media_queries_and_report_changes(&self) { - self.media_query_lists.evaluate_and_report_changes(); + rooted_vec!(let mut mql_list); + self.media_query_lists.for_each(|mql| { + if let MediaQueryListMatchState::Changed(_) = mql.evaluate_changes() { + // Recording list of changed Media Queries + mql_list.push(Dom::from_ref(&*mql)); + } + }); + // Sending change events for all changed Media Queries + for mql in mql_list.iter() { + let event = MediaQueryListEvent::new(&mql.global(), + atom!("change"), + false, false, + mql.Media(), + mql.Matches()); + event.upcast::().fire(mql.upcast::()); + } } /// Slow down/speed up timers based on visibility. @@ -1911,7 +1933,7 @@ impl Window { ignore_further_async_events: Default::default(), error_reporter, scroll_offsets: Default::default(), - media_query_lists: WeakMediaQueryListVec::new(), + media_query_lists: DOMTracker::new(), test_runner: Default::default(), webgl_chan, webvr_chan,