1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 //! This module contains traits in script used generically in the rest of Servo.
6 //! The traits are here instead of in script so that these modules won't have
7 //! to depend on script.
8 
9 #![deny(unsafe_code)]
10 #![feature(associated_type_defaults)]
11 
12 extern crate app_units;
13 extern crate atomic_refcell;
14 extern crate canvas_traits;
15 extern crate cssparser;
16 extern crate euclid;
17 extern crate gfx_traits;
18 #[macro_use] extern crate html5ever;
19 extern crate ipc_channel;
20 extern crate libc;
21 #[macro_use]
22 extern crate log;
23 extern crate malloc_size_of;
24 #[macro_use] extern crate malloc_size_of_derive;
25 extern crate metrics;
26 extern crate msg;
27 extern crate net_traits;
28 extern crate profile_traits;
29 extern crate range;
30 extern crate script_traits;
31 extern crate selectors;
32 extern crate servo_arc;
33 extern crate servo_atoms;
34 extern crate servo_url;
35 extern crate style;
36 extern crate webrender_api;
37 
38 pub mod message;
39 pub mod reporter;
40 pub mod rpc;
41 pub mod wrapper_traits;
42 
43 use atomic_refcell::AtomicRefCell;
44 use canvas_traits::canvas::CanvasMsg;
45 use ipc_channel::ipc::IpcSender;
46 use libc::c_void;
47 use net_traits::image_cache::PendingImageId;
48 use script_traits::UntrustedNodeAddress;
49 use servo_url::ServoUrl;
50 use std::ptr::NonNull;
51 use std::sync::atomic::AtomicIsize;
52 use style::data::ElementData;
53 
54 #[repr(C)]
55 pub struct StyleData {
56     /// Data that the style system associates with a node. When the
57     /// style system is being used standalone, this is all that hangs
58     /// off the node. This must be first to permit the various
59     /// transmutations between ElementData and PersistentLayoutData.
60     pub element_data: AtomicRefCell<ElementData>,
61 
62     /// Information needed during parallel traversals.
63     pub parallel: DomParallelInfo,
64 }
65 
66 impl StyleData {
new() -> Self67     pub fn new() -> Self {
68         Self {
69             element_data: AtomicRefCell::new(ElementData::default()),
70             parallel: DomParallelInfo::new(),
71         }
72     }
73 }
74 
75 #[derive(Clone, Copy, MallocSizeOf)]
76 pub struct OpaqueStyleAndLayoutData {
77     // NB: We really store a `StyleAndLayoutData` here, so be careful!
78     #[ignore_malloc_size_of = "TODO(#6910) Box value that should be counted but \
79                                the type lives in layout"]
80     pub ptr: NonNull<StyleData>,
81 }
82 
83 #[allow(unsafe_code)]
84 unsafe impl Send for OpaqueStyleAndLayoutData {}
85 
86 /// Information that we need stored in each DOM node.
87 #[derive(MallocSizeOf)]
88 pub struct DomParallelInfo {
89     /// The number of children remaining to process during bottom-up traversal.
90     pub children_to_process: AtomicIsize,
91 }
92 
93 impl DomParallelInfo {
new() -> DomParallelInfo94     pub fn new() -> DomParallelInfo {
95         DomParallelInfo {
96             children_to_process: AtomicIsize::new(0),
97         }
98     }
99 }
100 
101 
102 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
103 pub enum LayoutNodeType {
104     Element(LayoutElementType),
105     Text,
106 }
107 
108 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
109 pub enum LayoutElementType {
110     Element,
111     HTMLBRElement,
112     HTMLCanvasElement,
113     HTMLIFrameElement,
114     HTMLImageElement,
115     HTMLInputElement,
116     HTMLObjectElement,
117     HTMLParagraphElement,
118     HTMLTableCellElement,
119     HTMLTableColElement,
120     HTMLTableElement,
121     HTMLTableRowElement,
122     HTMLTableSectionElement,
123     HTMLTextAreaElement,
124     SVGSVGElement,
125 }
126 
127 pub enum HTMLCanvasDataSource {
128     WebGL(webrender_api::ImageKey),
129     Image(Option<IpcSender<CanvasMsg>>)
130 }
131 
132 pub struct HTMLCanvasData {
133     pub source: HTMLCanvasDataSource,
134     pub width: u32,
135     pub height: u32,
136 }
137 
138 pub struct SVGSVGData {
139     pub width: u32,
140     pub height: u32,
141 }
142 
143 /// The address of a node known to be valid. These are sent from script to layout.
144 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
145 pub struct TrustedNodeAddress(pub *const c_void);
146 
147 #[allow(unsafe_code)]
148 unsafe impl Send for TrustedNodeAddress {}
149 
is_image_data(uri: &str) -> bool150 pub fn is_image_data(uri: &str) -> bool {
151     static TYPES: &'static [&'static str] = &["data:image/png", "data:image/gif", "data:image/jpeg"];
152     TYPES.iter().any(|&type_| uri.starts_with(type_))
153 }
154 
155 /// Whether the pending image needs to be fetched or is waiting on an existing fetch.
156 pub enum PendingImageState {
157     Unrequested(ServoUrl),
158     PendingResponse,
159 }
160 
161 /// The data associated with an image that is not yet present in the image cache.
162 /// Used by the script thread to hold on to DOM elements that need to be repainted
163 /// when an image fetch is complete.
164 pub struct PendingImage {
165     pub state: PendingImageState,
166     pub node: UntrustedNodeAddress,
167     pub id: PendingImageId,
168 }
169