Auto merge of #16698 - bradwerth:cloneRight, r=heycam

Change StyleSet to track stylesheets by unique ID.

MozReview-Commit-ID: Ky3P53o4Euw

https://bugzilla.mozilla.org/show_bug.cgi?id=1348481
https://reviewboard.mozilla.org/r/128850/

---
<!-- 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
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- 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/16698)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-05-02 18:15:05 -05:00 committed by GitHub
commit 536d63c670
4 changed files with 62 additions and 37 deletions

View file

@ -18,7 +18,7 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::sync::mpsc::{Receiver, Sender, channel}; use std::sync::mpsc::{Receiver, Sender, channel};
use stylesheet_set::StylesheetSet; use stylesheet_set::StylesheetSet;
use stylesheets::{FontFaceRule, Origin}; use stylesheets::{FontFaceRule, Origin, Stylesheet};
use stylist::{ExtraStyleData, Stylist}; use stylist::{ExtraStyleData, Stylist};
/// The container for data that a Servo-backed Gecko document needs to style /// The container for data that a Servo-backed Gecko document needs to style
@ -106,8 +106,9 @@ impl PerDocumentStyleDataImpl {
}; };
let author_style_disabled = self.stylesheets.author_style_disabled(); let author_style_disabled = self.stylesheets.author_style_disabled();
let stylesheets = self.stylesheets.flush(); let mut stylesheets = Vec::<Arc<Stylesheet>>::new();
stylist.update(stylesheets, self.stylesheets.flush(&mut stylesheets);
stylist.update(stylesheets.as_slice(),
&StylesheetGuards::same(guard), &StylesheetGuards::same(guard),
/* ua_sheets = */ None, /* ua_sheets = */ None,
/* stylesheets_changed = */ true, /* stylesheets_changed = */ true,

View file

@ -1574,16 +1574,18 @@ extern "C" {
extern "C" { extern "C" {
pub fn Servo_StyleSet_AppendStyleSheet(set: RawServoStyleSetBorrowed, pub fn Servo_StyleSet_AppendStyleSheet(set: RawServoStyleSetBorrowed,
sheet: RawServoStyleSheetBorrowed, sheet: RawServoStyleSheetBorrowed,
unique_id: u32,
flush: bool); flush: bool);
} }
extern "C" { extern "C" {
pub fn Servo_StyleSet_PrependStyleSheet(set: RawServoStyleSetBorrowed, pub fn Servo_StyleSet_PrependStyleSheet(set: RawServoStyleSetBorrowed,
sheet: RawServoStyleSheetBorrowed, sheet: RawServoStyleSheetBorrowed,
unique_id: u32,
flush: bool); flush: bool);
} }
extern "C" { extern "C" {
pub fn Servo_StyleSet_RemoveStyleSheet(set: RawServoStyleSetBorrowed, pub fn Servo_StyleSet_RemoveStyleSheet(set: RawServoStyleSetBorrowed,
sheet: RawServoStyleSheetBorrowed, unique_id: u32,
flush: bool); flush: bool);
} }
extern "C" { extern "C" {
@ -1591,8 +1593,8 @@ extern "C" {
RawServoStyleSetBorrowed, RawServoStyleSetBorrowed,
sheet: sheet:
RawServoStyleSheetBorrowed, RawServoStyleSheetBorrowed,
reference: unique_id: u32,
RawServoStyleSheetBorrowed, before_unique_id: u32,
flush: bool); flush: bool);
} }
extern "C" { extern "C" {

View file

@ -4,19 +4,26 @@
//! A centralized set of stylesheets for a document. //! A centralized set of stylesheets for a document.
use arc_ptr_eq;
use std::sync::Arc; use std::sync::Arc;
use stylesheets::Stylesheet; use stylesheets::Stylesheet;
/// Entry for a StylesheetSet. We don't bother creating a constructor, because
/// there's no sensible defaults for the member variables.
pub struct StylesheetSetEntry {
unique_id: u32,
sheet: Arc<Stylesheet>,
}
/// The set of stylesheets effective for a given document. /// The set of stylesheets effective for a given document.
pub struct StylesheetSet { pub struct StylesheetSet {
/// The actual list of all the stylesheets that apply to the given document. /// The actual list of all the stylesheets that apply to the given document,
/// each stylesheet associated with a unique ID.
/// ///
/// This is only a list of top-level stylesheets, and as such it doesn't /// This is only a list of top-level stylesheets, and as such it doesn't
/// include recursive `@import` rules. /// include recursive `@import` rules.
stylesheets: Vec<Arc<Stylesheet>>, entries: Vec<StylesheetSetEntry>,
/// Whether the stylesheets list above has changed since the last restyle. /// Whether the entries list above has changed since the last restyle.
dirty: bool, dirty: bool,
/// Has author style been disabled? /// Has author style been disabled?
@ -27,7 +34,7 @@ impl StylesheetSet {
/// Create a new empty StylesheetSet. /// Create a new empty StylesheetSet.
pub fn new() -> Self { pub fn new() -> Self {
StylesheetSet { StylesheetSet {
stylesheets: vec![], entries: vec![],
dirty: false, dirty: false,
author_style_disabled: false, author_style_disabled: false,
} }
@ -39,39 +46,51 @@ impl StylesheetSet {
self.author_style_disabled self.author_style_disabled
} }
fn remove_stylesheet_if_present(&mut self, sheet: &Arc<Stylesheet>) { fn remove_stylesheet_if_present(&mut self, unique_id: u32) {
self.stylesheets.retain(|x| !arc_ptr_eq(x, sheet)); self.entries.retain(|x| x.unique_id != unique_id);
} }
/// Appends a new stylesheet to the current set. /// Appends a new stylesheet to the current set.
pub fn append_stylesheet(&mut self, sheet: &Arc<Stylesheet>) { pub fn append_stylesheet(&mut self, sheet: &Arc<Stylesheet>,
self.remove_stylesheet_if_present(sheet); unique_id: u32) {
self.stylesheets.push(sheet.clone()); self.remove_stylesheet_if_present(unique_id);
self.entries.push(StylesheetSetEntry {
unique_id: unique_id,
sheet: sheet.clone(),
});
self.dirty = true; self.dirty = true;
} }
/// Prepend a new stylesheet to the current set. /// Prepend a new stylesheet to the current set.
pub fn prepend_stylesheet(&mut self, sheet: &Arc<Stylesheet>) { pub fn prepend_stylesheet(&mut self, sheet: &Arc<Stylesheet>,
self.remove_stylesheet_if_present(sheet); unique_id: u32) {
self.stylesheets.insert(0, sheet.clone()); self.remove_stylesheet_if_present(unique_id);
self.entries.insert(0, StylesheetSetEntry {
unique_id: unique_id,
sheet: sheet.clone(),
});
self.dirty = true; self.dirty = true;
} }
/// Insert a given stylesheet before another stylesheet in the document. /// Insert a given stylesheet before another stylesheet in the document.
pub fn insert_stylesheet_before(&mut self, pub fn insert_stylesheet_before(&mut self,
sheet: &Arc<Stylesheet>, sheet: &Arc<Stylesheet>,
before: &Arc<Stylesheet>) { unique_id: u32,
self.remove_stylesheet_if_present(sheet); before_unique_id: u32) {
let index = self.stylesheets.iter().position(|x| { self.remove_stylesheet_if_present(unique_id);
arc_ptr_eq(x, before) let index = self.entries.iter().position(|x| {
}).expect("`before` stylesheet not found"); x.unique_id == before_unique_id
self.stylesheets.insert(index, sheet.clone()); }).expect("`before_unique_id` stylesheet not found");
self.entries.insert(index, StylesheetSetEntry {
unique_id: unique_id,
sheet: sheet.clone(),
});
self.dirty = true; self.dirty = true;
} }
/// Remove a given stylesheet from the set. /// Remove a given stylesheet from the set.
pub fn remove_stylesheet(&mut self, sheet: &Arc<Stylesheet>) { pub fn remove_stylesheet(&mut self, unique_id: u32) {
self.remove_stylesheet_if_present(sheet); self.remove_stylesheet_if_present(unique_id);
self.dirty = true; self.dirty = true;
} }
@ -90,9 +109,11 @@ impl StylesheetSet {
} }
/// Flush the current set, unmarking it as dirty. /// Flush the current set, unmarking it as dirty.
pub fn flush(&mut self) -> &[Arc<Stylesheet>] { pub fn flush(&mut self, sheets: &mut Vec<Arc<Stylesheet>>) {
self.dirty = false; self.dirty = false;
&self.stylesheets for entry in &self.entries {
sheets.push(entry.sheet.clone())
}
} }
/// Mark the stylesheets as dirty, because something external may have /// Mark the stylesheets as dirty, because something external may have

View file

@ -593,12 +593,13 @@ pub extern "C" fn Servo_StyleSheet_ClearAndUpdate(stylesheet: RawServoStyleSheet
#[no_mangle] #[no_mangle]
pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: RawServoStyleSetBorrowed, pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: RawServoStyleSetBorrowed,
raw_sheet: RawServoStyleSheetBorrowed, raw_sheet: RawServoStyleSheetBorrowed,
unique_id: u32,
flush: bool) { flush: bool) {
let global_style_data = &*GLOBAL_STYLE_DATA; let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read(); let guard = global_style_data.shared_lock.read();
let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut(); let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
let sheet = HasArcFFI::as_arc(&raw_sheet); let sheet = HasArcFFI::as_arc(&raw_sheet);
data.stylesheets.append_stylesheet(sheet); data.stylesheets.append_stylesheet(sheet, unique_id);
if flush { if flush {
data.flush_stylesheets(&guard); data.flush_stylesheets(&guard);
} }
@ -607,12 +608,13 @@ pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: RawServoStyleSetBorr
#[no_mangle] #[no_mangle]
pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: RawServoStyleSetBorrowed, pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: RawServoStyleSetBorrowed,
raw_sheet: RawServoStyleSheetBorrowed, raw_sheet: RawServoStyleSheetBorrowed,
unique_id: u32,
flush: bool) { flush: bool) {
let global_style_data = &*GLOBAL_STYLE_DATA; let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read(); let guard = global_style_data.shared_lock.read();
let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut(); let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
let sheet = HasArcFFI::as_arc(&raw_sheet); let sheet = HasArcFFI::as_arc(&raw_sheet);
data.stylesheets.prepend_stylesheet(sheet); data.stylesheets.prepend_stylesheet(sheet, unique_id);
if flush { if flush {
data.flush_stylesheets(&guard); data.flush_stylesheets(&guard);
} }
@ -621,14 +623,14 @@ pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: RawServoStyleSetBor
#[no_mangle] #[no_mangle]
pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: RawServoStyleSetBorrowed, pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: RawServoStyleSetBorrowed,
raw_sheet: RawServoStyleSheetBorrowed, raw_sheet: RawServoStyleSheetBorrowed,
raw_reference: RawServoStyleSheetBorrowed, unique_id: u32,
before_unique_id: u32,
flush: bool) { flush: bool) {
let global_style_data = &*GLOBAL_STYLE_DATA; let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read(); let guard = global_style_data.shared_lock.read();
let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut(); let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
let sheet = HasArcFFI::as_arc(&raw_sheet); let sheet = HasArcFFI::as_arc(&raw_sheet);
let reference = HasArcFFI::as_arc(&raw_reference); data.stylesheets.insert_stylesheet_before(sheet, unique_id, before_unique_id);
data.stylesheets.insert_stylesheet_before(sheet, reference);
if flush { if flush {
data.flush_stylesheets(&guard); data.flush_stylesheets(&guard);
} }
@ -636,13 +638,12 @@ pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: RawServoStyleS
#[no_mangle] #[no_mangle]
pub extern "C" fn Servo_StyleSet_RemoveStyleSheet(raw_data: RawServoStyleSetBorrowed, pub extern "C" fn Servo_StyleSet_RemoveStyleSheet(raw_data: RawServoStyleSetBorrowed,
raw_sheet: RawServoStyleSheetBorrowed, unique_id: u32,
flush: bool) { flush: bool) {
let global_style_data = &*GLOBAL_STYLE_DATA; let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read(); let guard = global_style_data.shared_lock.read();
let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut(); let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
let sheet = HasArcFFI::as_arc(&raw_sheet); data.stylesheets.remove_stylesheet(unique_id);
data.stylesheets.remove_stylesheet(sheet);
if flush { if flush {
data.flush_stylesheets(&guard); data.flush_stylesheets(&guard);
} }