Skip to content

Commit 91a2e76

Browse files
author
bors-servo
authored
Auto merge of #14260 - asajeffrey:constellation-event-loops, r=Ms2ger
Rename ScriptChan in constellation to EventLoop <!-- Please describe your changes on the following line: --> We currently have a type `ScriptChan` in the constellation, which is named after its implementation rather than its semantics. In the spec, the nearest concept seems to be event loop https://html.spec.whatwg.org/multipage/#event-loop. --- <!-- 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 - [X] These changes do not require tests because renaming. <!-- 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/14260) <!-- Reviewable:end -->
2 parents 1b2daae + e945e38 commit 91a2e76

File tree

4 files changed

+100
-77
lines changed

4 files changed

+100
-77
lines changed

components/constellation/constellation.rs

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use debugger;
2121
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg};
2222
use euclid::scale_factor::ScaleFactor;
2323
use euclid::size::{Size2D, TypedSize2D};
24+
use event_loop::EventLoop;
2425
use gfx::font_cache_thread::FontCacheThread;
2526
use gfx_traits::Epoch;
2627
use ipc_channel::ipc::{self, IpcSender};
@@ -133,10 +134,10 @@ pub struct Constellation<Message, LTF, STF> {
133134
/// to receive sw manager message
134135
swmanager_receiver: Receiver<SWManagerMsg>,
135136

136-
/// A map from top-level frame id and registered domain name to script channels.
137-
/// This double indirection ensures that separate tabs do not share script threads,
137+
/// A map from top-level frame id and registered domain name to event loops.
138+
/// This double indirection ensures that separate tabs do not share event loops,
138139
/// even if the same domain is loaded in each.
139-
script_channels: HashMap<FrameId, HashMap<String, Weak<ScriptChan>>>,
140+
event_loops: HashMap<FrameId, HashMap<String, Weak<EventLoop>>>,
140141

141142
/// A list of all the pipelines. (See the `pipeline` module for more details.)
142143
pipelines: HashMap<PipelineId, Pipeline>,
@@ -367,30 +368,6 @@ enum ExitPipelineMode {
367368
Force,
368369
}
369370

370-
/// A script channel, that closes the script thread down when it is dropped
371-
pub struct ScriptChan {
372-
chan: IpcSender<ConstellationControlMsg>,
373-
dont_send_or_sync: PhantomData<Rc<()>>,
374-
}
375-
376-
impl Drop for ScriptChan {
377-
fn drop(&mut self) {
378-
let _ = self.chan.send(ConstellationControlMsg::ExitScriptThread);
379-
}
380-
}
381-
382-
impl ScriptChan {
383-
pub fn send(&self, msg: ConstellationControlMsg) -> Result<(), IOError> {
384-
self.chan.send(msg)
385-
}
386-
pub fn new(chan: IpcSender<ConstellationControlMsg>) -> Rc<ScriptChan> {
387-
Rc::new(ScriptChan { chan: chan, dont_send_or_sync: PhantomData })
388-
}
389-
pub fn sender(&self) -> IpcSender<ConstellationControlMsg> {
390-
self.chan.clone()
391-
}
392-
}
393-
394371
/// A logger directed at the constellation from content processes
395372
#[derive(Clone)]
396373
pub struct FromScriptLogger {
@@ -532,7 +509,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
532509
swmanager_chan: None,
533510
swmanager_receiver: swmanager_receiver,
534511
swmanager_sender: sw_mgr_clone,
535-
script_channels: HashMap::new(),
512+
event_loops: HashMap::new(),
536513
pipelines: HashMap::new(),
537514
frames: HashMap::new(),
538515
pending_frames: vec!(),
@@ -608,17 +585,17 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
608585
None => self.root_frame_id,
609586
};
610587

611-
let (script_channel, host) = match sandbox {
588+
let (event_loop, host) = match sandbox {
612589
IFrameSandboxState::IFrameSandboxed => (None, None),
613590
IFrameSandboxState::IFrameUnsandboxed => match reg_host(&load_data.url) {
614591
None => (None, None),
615592
Some(host) => {
616-
let script_channel = self.script_channels.get(&top_level_frame_id)
593+
let event_loop = self.event_loops.get(&top_level_frame_id)
617594
.and_then(|map| map.get(host))
618595
.and_then(|weak| weak.upgrade());
619-
match script_channel {
596+
match event_loop {
620597
None => (None, Some(String::from(host))),
621-
Some(script_channel) => (Some(script_channel.clone()), None),
598+
Some(event_loop) => (Some(event_loop.clone()), None),
622599
}
623600
},
624601
},
@@ -665,7 +642,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
665642
time_profiler_chan: self.time_profiler_chan.clone(),
666643
mem_profiler_chan: self.mem_profiler_chan.clone(),
667644
window_size: initial_window_size,
668-
script_chan: script_channel,
645+
event_loop: event_loop,
669646
load_data: load_data,
670647
device_pixel_ratio: self.window_size.device_pixel_ratio,
671648
pipeline_namespace_id: self.next_pipeline_namespace_id(),
@@ -680,9 +657,9 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
680657
};
681658

682659
if let Some(host) = host {
683-
self.script_channels.entry(top_level_frame_id)
660+
self.event_loops.entry(top_level_frame_id)
684661
.or_insert_with(HashMap::new)
685-
.insert(host, Rc::downgrade(&pipeline.script_chan));
662+
.insert(host, Rc::downgrade(&pipeline.event_loop));
686663
}
687664

688665
assert!(!self.pipelines.contains_key(&pipeline_id));
@@ -970,7 +947,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
970947
let msg = ConstellationControlMsg::SendEvent(pipeline_id, event);
971948
let result = match self.pipelines.get(&pipeline_id) {
972949
None => { debug!("Pipeline {:?} got event after closure.", pipeline_id); return; }
973-
Some(pipeline) => pipeline.script_chan.send(msg),
950+
Some(pipeline) => pipeline.event_loop.send(msg),
974951
};
975952
if let Err(e) = result {
976953
self.handle_send_error(pipeline_id, e);
@@ -1118,7 +1095,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
11181095
let msg = ConstellationControlMsg::DispatchStorageEvent(
11191096
pipeline.id, storage, url.clone(), key.clone(), old_value.clone(), new_value.clone()
11201097
);
1121-
if let Err(err) = pipeline.script_chan.send(msg) {
1098+
if let Err(err) = pipeline.event_loop.send(msg) {
11221099
warn!("Failed to broadcast storage event to pipeline {} ({:?}).", pipeline.id, err);
11231100
}
11241101
}
@@ -1327,7 +1304,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
13271304
match self.pipelines.get_mut(&pipeline_id) {
13281305
Some(pipeline) => {
13291306
pipeline.size = Some(*size);
1330-
pipeline.script_chan.send(msg)
1307+
pipeline.event_loop.send(msg)
13311308
}
13321309
None => return,
13331310
}
@@ -1352,7 +1329,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
13521329
child: pipeline_id,
13531330
};
13541331
let result = match self.pipelines.get(&parent_id) {
1355-
Some(parent) => parent.script_chan.send(msg),
1332+
Some(parent) => parent.event_loop.send(msg),
13561333
None => return warn!("Parent {} frame loaded after closure.", parent_id),
13571334
};
13581335
if let Err(e) = result {
@@ -1432,7 +1409,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
14321409
None => return warn!("Script loaded url in closed iframe {}.", parent_pipeline_id),
14331410
};
14341411

1435-
let script_sender = parent_pipeline.script_chan.clone();
1412+
let script_sender = parent_pipeline.event_loop.clone();
14361413

14371414
let url = ServoUrl::parse("about:blank").expect("infallible");
14381415
Pipeline::new(new_pipeline_id,
@@ -1475,7 +1452,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
14751452
AnimationTickType::Script => {
14761453
let msg = ConstellationControlMsg::TickAllAnimations(pipeline_id);
14771454
match self.pipelines.get(&pipeline_id) {
1478-
Some(pipeline) => pipeline.script_chan.send(msg),
1455+
Some(pipeline) => pipeline.event_loop.send(msg),
14791456
None => return warn!("Pipeline {:?} got script tick after closure.", pipeline_id),
14801457
}
14811458
}
@@ -1548,7 +1525,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
15481525
// and issue an iframe load through there.
15491526
let msg = ConstellationControlMsg::Navigate(parent_pipeline_id, frame_id, load_data, replace);
15501527
let result = match self.pipelines.get(&parent_pipeline_id) {
1551-
Some(parent_pipeline) => parent_pipeline.script_chan.send(msg),
1528+
Some(parent_pipeline) => parent_pipeline.event_loop.send(msg),
15521529
None => {
15531530
warn!("Pipeline {:?} child loaded after closure", parent_pipeline_id);
15541531
return None;
@@ -1694,7 +1671,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
16941671
let event = CompositorEvent::KeyEvent(ch, key, state, mods);
16951672
let msg = ConstellationControlMsg::SendEvent(pipeline_id, event);
16961673
let result = match self.pipelines.get(&pipeline_id) {
1697-
Some(pipeline) => pipeline.script_chan.send(msg),
1674+
Some(pipeline) => pipeline.event_loop.send(msg),
16981675
None => return debug!("Pipeline {:?} got key event after closure.", pipeline_id),
16991676
};
17001677
if let Err(e) = result {
@@ -1716,7 +1693,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
17161693
if let Some(pipeline_id) = root_pipeline_id {
17171694
let msg = ConstellationControlMsg::Reload(pipeline_id);
17181695
let result = match self.pipelines.get(&pipeline_id) {
1719-
Some(pipeline) => pipeline.script_chan.send(msg),
1696+
Some(pipeline) => pipeline.event_loop.send(msg),
17201697
None => return debug!("Pipeline {:?} got reload event after closure.", pipeline_id),
17211698
};
17221699
if let Err(e) = result {
@@ -1728,7 +1705,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
17281705
fn handle_get_pipeline_title_msg(&mut self, pipeline_id: PipelineId) {
17291706
let result = match self.pipelines.get(&pipeline_id) {
17301707
None => return self.compositor_proxy.send(ToCompositorMsg::ChangePageTitle(pipeline_id, None)),
1731-
Some(pipeline) => pipeline.script_chan.send(ConstellationControlMsg::GetTitle(pipeline_id)),
1708+
Some(pipeline) => pipeline.event_loop.send(ConstellationControlMsg::GetTitle(pipeline_id)),
17321709
};
17331710
if let Err(e) = result {
17341711
self.handle_send_error(pipeline_id, e);
@@ -1791,7 +1768,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
17911768
// telling it to mark the iframe element as focused.
17921769
let msg = ConstellationControlMsg::FocusIFrame(parent_pipeline_id, frame_id);
17931770
let result = match self.pipelines.get(&parent_pipeline_id) {
1794-
Some(pipeline) => pipeline.script_chan.send(msg),
1771+
Some(pipeline) => pipeline.event_loop.send(msg),
17951772
None => return warn!("Pipeline {:?} focus after closure.", parent_pipeline_id),
17961773
};
17971774
if let Err(e) = result {
@@ -1853,7 +1830,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
18531830
visibility);
18541831
let result = match self.pipelines.get(&parent_pipeline_id) {
18551832
None => return warn!("Parent pipeline {:?} closed", parent_pipeline_id),
1856-
Some(parent_pipeline) => parent_pipeline.script_chan.send(visibility_msg),
1833+
Some(parent_pipeline) => parent_pipeline.event_loop.send(visibility_msg),
18571834
};
18581835

18591836
if let Err(e) = result {
@@ -1911,22 +1888,22 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
19111888
WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd) => {
19121889
let control_msg = ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, cmd);
19131890
let result = match self.pipelines.get(&pipeline_id) {
1914-
Some(pipeline) => pipeline.script_chan.send(control_msg),
1891+
Some(pipeline) => pipeline.event_loop.send(control_msg),
19151892
None => return warn!("Pipeline {:?} ScriptCommand after closure.", pipeline_id),
19161893
};
19171894
if let Err(e) = result {
19181895
self.handle_send_error(pipeline_id, e);
19191896
}
19201897
},
19211898
WebDriverCommandMsg::SendKeys(pipeline_id, cmd) => {
1922-
let script_channel = match self.pipelines.get(&pipeline_id) {
1923-
Some(pipeline) => pipeline.script_chan.clone(),
1899+
let event_loop = match self.pipelines.get(&pipeline_id) {
1900+
Some(pipeline) => pipeline.event_loop.clone(),
19241901
None => return warn!("Pipeline {:?} SendKeys after closure.", pipeline_id),
19251902
};
19261903
for (key, mods, state) in cmd {
19271904
let event = CompositorEvent::KeyEvent(None, key, state, mods);
19281905
let control_msg = ConstellationControlMsg::SendEvent(pipeline_id, event);
1929-
if let Err(e) = script_channel.send(control_msg) {
1906+
if let Err(e) = event_loop.send(control_msg) {
19301907
return self.handle_send_error(pipeline_id, e);
19311908
}
19321909
}
@@ -2015,7 +1992,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
20151992
next_pipeline_id);
20161993
let result = match self.pipelines.get(&parent_pipeline_id) {
20171994
None => return warn!("Pipeline {:?} child traversed after closure.", parent_pipeline_id),
2018-
Some(pipeline) => pipeline.script_chan.send(msg),
1995+
Some(pipeline) => pipeline.event_loop.send(msg),
20191996
};
20201997
if let Err(e) = result {
20211998
self.handle_send_error(parent_pipeline_id, e);
@@ -2112,7 +2089,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
21122089
if let Some((parent_pipeline_id, _)) = pipeline.parent_info {
21132090
if let Some(parent_pipeline) = self.pipelines.get(&parent_pipeline_id) {
21142091
let msg = ConstellationControlMsg::FramedContentChanged(parent_pipeline_id, pipeline.frame_id);
2115-
let _ = parent_pipeline.script_chan.send(msg);
2092+
let _ = parent_pipeline.event_loop.send(msg);
21162093
}
21172094
}
21182095
}
@@ -2143,7 +2120,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
21432120
None => return warn!("Pipeline {:?} resized after closing.", pipeline_id),
21442121
Some(pipeline) => pipeline,
21452122
};
2146-
let _ = pipeline.script_chan.send(ConstellationControlMsg::Resize(
2123+
let _ = pipeline.event_loop.send(ConstellationControlMsg::Resize(
21472124
pipeline.id,
21482125
new_size,
21492126
size_type
@@ -2156,7 +2133,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
21562133
},
21572134
Some(pipeline) => pipeline,
21582135
};
2159-
let _ = pipeline.script_chan.send(ConstellationControlMsg::ResizeInactive(
2136+
let _ = pipeline.event_loop.send(ConstellationControlMsg::ResizeInactive(
21602137
pipeline.id,
21612138
new_size
21622139
));
@@ -2171,7 +2148,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
21712148
Some(pipeline) => pipeline,
21722149
};
21732150
if pipeline.parent_info.is_none() {
2174-
let _ = pipeline.script_chan.send(ConstellationControlMsg::Resize(
2151+
let _ = pipeline.event_loop.send(ConstellationControlMsg::Resize(
21752152
pipeline.id,
21762153
new_size,
21772154
size_type
@@ -2340,7 +2317,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
23402317

23412318
self.close_frame_children(frame_id, exit_mode);
23422319

2343-
self.script_channels.remove(&frame_id);
2320+
self.event_loops.remove(&frame_id);
23442321
if self.frames.remove(&frame_id).is_none() {
23452322
warn!("Closing frame {:?} twice.", frame_id);
23462323
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 the `EventLoop` type, which is the constellation's
6+
//! view of a script thread. When an `EventLoop` is dropped, an `ExitScriptThread`
7+
//! message is sent to the script thread, asking it to shut down.
8+
9+
use ipc_channel::ipc::IpcSender;
10+
use script_traits::ConstellationControlMsg;
11+
use std::io::Error as IOError;
12+
use std::marker::PhantomData;
13+
use std::rc::Rc;
14+
15+
/// https://html.spec.whatwg.org/multipage/#event-loop
16+
pub struct EventLoop {
17+
script_chan: IpcSender<ConstellationControlMsg>,
18+
dont_send_or_sync: PhantomData<Rc<()>>,
19+
}
20+
21+
impl Drop for EventLoop {
22+
fn drop(&mut self) {
23+
let _ = self.script_chan.send(ConstellationControlMsg::ExitScriptThread);
24+
}
25+
}
26+
27+
impl EventLoop {
28+
/// Create a new event loop from the channel to its script thread.
29+
pub fn new(script_chan: IpcSender<ConstellationControlMsg>) -> Rc<EventLoop> {
30+
Rc::new(EventLoop {
31+
script_chan: script_chan,
32+
dont_send_or_sync: PhantomData,
33+
})
34+
}
35+
36+
/// Send a message to the event loop.
37+
pub fn send(&self, msg: ConstellationControlMsg) -> Result<(), IOError> {
38+
self.script_chan.send(msg)
39+
}
40+
41+
/// The underlying channel to the script thread.
42+
pub fn sender(&self) -> IpcSender<ConstellationControlMsg> {
43+
self.script_chan.clone()
44+
}
45+
}
46+

components/constellation/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern crate style_traits;
4343
extern crate webrender_traits;
4444

4545
mod constellation;
46+
mod event_loop;
4647
mod pipeline;
4748
#[cfg(not(target_os = "windows"))]
4849
mod sandboxing;

0 commit comments

Comments
 (0)