Skip to content

Commit

Permalink
use DOMTracker instead of WeakMediaQueryListVec
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Jul 29, 2018
1 parent 8155cc6 commit e67adfc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 58 deletions.
54 changes: 1 addition & 53 deletions components/script/dom/mediaquerylist.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<WeakRefVec<MediaQueryList>>,
}

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
/// <https://drafts.csswg.org/cssom-view/#evaluate-media-queries-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::<Event>().fire(mql.upcast::<EventTarget>());
}
}
}

#[allow(unsafe_code)]
unsafe impl JSTraceable for WeakMediaQueryListVec {
unsafe fn trace(&self, _: *mut JSTracer) {
self.cell.borrow_mut().retain_alive()
}
}
32 changes: 27 additions & 5 deletions components/script/dom/window.rs
Expand Up @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -260,7 +265,7 @@ pub struct Window {
scroll_offsets: DomRefCell<HashMap<UntrustedNodeAddress, Vector2D<f32>>>,

/// All the MediaQueryLists we need to update
media_query_lists: WeakMediaQueryListVec,
media_query_lists: DOMTracker<MediaQueryList>,

test_runner: MutNullableDom<TestRunner>,

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -1771,8 +1776,25 @@ impl Window {
self.parent_info.is_none()
}

/// Evaluate media query lists and report changes
/// <https://drafts.csswg.org/cssom-view/#evaluate-media-queries-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::<Event>().fire(mql.upcast::<EventTarget>());
}
}

/// Slow down/speed up timers based on visibility.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e67adfc

Please sign in to comment.