Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MessagePort and MessageChannel #16622

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Add PortMessageQueue

  • Loading branch information
KiChjang committed Jun 9, 2019
commit 9edd4ec2dd07f640d4867b7ad3979b48c790cb55
@@ -337,6 +337,7 @@ pub enum ScriptHangAnnotation {
ExitFullscreen,
WebVREvent,
PerformanceTimelineTask,
PortMessage,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
@@ -136,6 +136,7 @@ impl Formattable for ProfilerCategory {
ProfilerCategory::ScriptParseHTML => "Script Parse HTML",
ProfilerCategory::ScriptParseXML => "Script Parse XML",
ProfilerCategory::ScriptPlannedNavigation => "Script Planned Navigation",
ProfilerCategory::ScriptPortMessage => "Script Port Message",
ProfilerCategory::ScriptResize => "Script Resize",
ProfilerCategory::ScriptEvent => "Script Event",
ProfilerCategory::ScriptUpdateReplacedElement => "Script Update Replaced Element",
@@ -108,6 +108,7 @@ pub enum ProfilerCategory {
ScriptWorkletEvent = 0x7a,
ScriptPerformanceEvent = 0x7b,
ScriptHistoryEvent = 0x7c,
ScriptPortMessage = 0x7d,
TimeToFirstPaint = 0x80,
TimeToFirstContentfulPaint = 0x81,
TimeToInteractive = 0x82,
@@ -33,6 +33,7 @@ use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::file_reading::FileReadingTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
use crate::task_source::TaskSourceName;
@@ -489,7 +490,7 @@ impl GlobalScope {
unreachable!();
}

/// `ScriptChan` to send messages to the networking task source of
/// `TaskSource` to send messages to the networking task source of
/// this global scope.
pub fn networking_task_source(&self) -> NetworkingTaskSource {
if let Some(window) = self.downcast::<Window>() {
@@ -501,7 +502,19 @@ impl GlobalScope {
unreachable!();
}

/// `ScriptChan` to send messages to the remote-event task source of
/// `TaskSource` to send messages to the port message queue of
/// this global scope.
pub fn port_message_queue(&self) -> PortMessageQueue {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().port_message_queue();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.port_message_queue();
}
unreachable!();
}

/// `TaskSource` to send messages to the remote-event task source of
/// this global scope.
pub fn remote_event_task_source(&self) -> RemoteEventTaskSource {
if let Some(window) = self.downcast::<Window>() {
@@ -513,7 +526,7 @@ impl GlobalScope {
unreachable!();
}

/// `ScriptChan` to send messages to the websocket task source of
/// `TaskSource` to send messages to the websocket task source of
/// this global scope.
pub fn websocket_task_source(&self) -> WebsocketTaskSource {
if let Some(window) = self.downcast::<Window>() {
@@ -522,7 +535,7 @@ impl GlobalScope {
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.websocket_task_source();
}
unreachable!();
unreachable!()
}

/// Evaluate JS code on this global scope.
@@ -32,6 +32,7 @@ use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::file_reading::FileReadingTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
use crate::timers::{IsInterval, TimerCallback};
@@ -453,6 +454,10 @@ impl WorkerGlobalScope {
PerformanceTimelineTaskSource(self.script_chan(), self.pipeline_id())
}

pub fn port_message_queue(&self) -> PortMessageQueue {
PortMessageQueue(self.script_chan(), self.pipeline_id())
}

pub fn remote_event_task_source(&self) -> RemoteEventTaskSource {
RemoteEventTaskSource(self.script_chan(), self.pipeline_id())
}
@@ -110,6 +110,7 @@ pub enum ScriptThreadEventCategory {
ImageCacheMsg,
InputEvent,
NetworkEvent,
PortMessage,
Resize,
ScriptEvent,
SetScrollState,
@@ -79,6 +79,7 @@ use crate::task_source::history_traversal::HistoryTraversalTaskSource;
use crate::task_source::media_element::MediaElementTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::user_interaction::UserInteractionTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
@@ -562,6 +563,8 @@ pub struct ScriptThread {

performance_timeline_task_sender: Box<dyn ScriptChan>,

port_message_sender: Box<dyn ScriptChan>,

remote_event_task_sender: Box<dyn ScriptChan>,

/// A channel to hand out to threads that need to respond to a message from the script thread.
@@ -1107,6 +1110,7 @@ impl ScriptThread {
media_element_task_sender: chan.clone(),
user_interaction_task_sender: chan.clone(),
networking_task_sender: boxed_script_sender.clone(),
port_message_sender: boxed_script_sender.clone(),
file_reading_task_sender: boxed_script_sender.clone(),
performance_timeline_task_sender: boxed_script_sender.clone(),
remote_event_task_sender: boxed_script_sender.clone(),
@@ -1448,6 +1452,7 @@ impl ScriptThread {
ScriptThreadEventCategory::PerformanceTimelineTask => {
ScriptHangAnnotation::PerformanceTimelineTask
},
ScriptThreadEventCategory::PortMessage => ScriptHangAnnotation::PortMessage,
};
self.background_hang_monitor
.notify_activity(HangAnnotation::Script(hang_annotation));
@@ -1549,6 +1554,7 @@ impl ScriptThread {
ScriptThreadEventCategory::ImageCacheMsg => ProfilerCategory::ScriptImageCacheMsg,
ScriptThreadEventCategory::InputEvent => ProfilerCategory::ScriptInputEvent,
ScriptThreadEventCategory::NetworkEvent => ProfilerCategory::ScriptNetworkEvent,
ScriptThreadEventCategory::PortMessage => ProfilerCategory::ScriptPortMessage,
ScriptThreadEventCategory::Resize => ProfilerCategory::ScriptResize,
ScriptThreadEventCategory::ScriptEvent => ProfilerCategory::ScriptEvent,
ScriptThreadEventCategory::SetScrollState => ProfilerCategory::ScriptSetScrollState,
@@ -2453,6 +2459,10 @@ impl ScriptThread {
NetworkingTaskSource(self.networking_task_sender.clone(), pipeline_id)
}

pub fn port_message_queue(&self, pipeline_id: PipelineId) -> PortMessageQueue {
PortMessageQueue(self.port_message_sender.clone(), pipeline_id)
}

pub fn file_reading_task_source(&self, pipeline_id: PipelineId) -> FileReadingTaskSource {
FileReadingTaskSource(self.file_reading_task_sender.clone(), pipeline_id)
}
@@ -2850,6 +2860,7 @@ impl ScriptThread {
self.networking_task_source(incomplete.pipeline_id),
self.performance_timeline_task_source(incomplete.pipeline_id)
.clone(),
self.port_message_queue(incomplete.pipeline_id),
self.user_interaction_task_source(incomplete.pipeline_id),
self.remote_event_task_source(incomplete.pipeline_id),
self.websocket_task_source(incomplete.pipeline_id),
@@ -10,6 +10,7 @@ use crate::task_source::history_traversal::HistoryTraversalTaskSource;
use crate::task_source::media_element::MediaElementTaskSource;
use crate::task_source::networking::NetworkingTaskSource;
use crate::task_source::performance_timeline::PerformanceTimelineTaskSource;
use crate::task_source::port_message::PortMessageQueue;
use crate::task_source::remote_event::RemoteEventTaskSource;
use crate::task_source::user_interaction::UserInteractionTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
@@ -47,6 +48,8 @@ pub struct TaskManager {
#[ignore_malloc_size_of = "task sources are hard"]
performance_timeline_task_source: PerformanceTimelineTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
port_message_queue: PortMessageQueue,
#[ignore_malloc_size_of = "task sources are hard"]
user_interaction_task_source: UserInteractionTaskSource,
#[ignore_malloc_size_of = "task sources are hard"]
remote_event_task_source: RemoteEventTaskSource,
@@ -62,6 +65,7 @@ impl TaskManager {
media_element_task_source: MediaElementTaskSource,
networking_task_source: NetworkingTaskSource,
performance_timeline_task_source: PerformanceTimelineTaskSource,
port_message_queue: PortMessageQueue,
user_interaction_task_source: UserInteractionTaskSource,
remote_event_task_source: RemoteEventTaskSource,
websocket_task_source: WebsocketTaskSource,
@@ -73,6 +77,7 @@ impl TaskManager {
media_element_task_source,
networking_task_source,
performance_timeline_task_source,
port_message_queue,
user_interaction_task_source,
remote_event_task_source,
websocket_task_source,
@@ -136,6 +141,14 @@ impl TaskManager {
PerformanceTimeline
);

task_source_functions!(
self,
port_message_queue_with_canceller,
port_message_queue,
PortMessageQueue,
PortMessage
);

task_source_functions!(
self,
remote_event_task_source_with_canceller,
@@ -8,6 +8,7 @@ pub mod history_traversal;
pub mod media_element;
pub mod networking;
pub mod performance_timeline;
pub mod port_message;
pub mod remote_event;
pub mod user_interaction;
pub mod websocket;
@@ -28,6 +29,7 @@ pub enum TaskSourceName {
HistoryTraversal,
Networking,
PerformanceTimeline,
PortMessage,
UserInteraction,
RemoteEvent,
MediaElement,
@@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 crate::script_runtime::{CommonScriptMsg, ScriptChan, ScriptThreadEventCategory};
use crate::task::{TaskCanceller, TaskOnce};
use crate::task_source::{TaskSource, TaskSourceName};
use msg::constellation_msg::PipelineId;
use std::fmt;

#[derive(JSTraceable)]
pub struct PortMessageQueue(pub Box<ScriptChan + Send + 'static>, pub PipelineId);

impl Clone for PortMessageQueue {
fn clone(&self) -> PortMessageQueue {
PortMessageQueue(self.0.clone(), self.1.clone())
}
}

impl fmt::Debug for PortMessageQueue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PortMessageQueue(...)")
}
}

impl TaskSource for PortMessageQueue {
const NAME: TaskSourceName = TaskSourceName::PortMessage;

fn queue_with_canceller<T>(
&self,
task: T,
canceller: &TaskCanceller,
) -> Result<(), ()>
where
T: TaskOnce + 'static,
{
let msg = CommonScriptMsg::Task(
ScriptThreadEventCategory::PortMessage,
Box::new(canceller.wrap_task(task)),
Some(self.1),
Self::NAME,
);
self.0.send(msg).map_err(|_| ())
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.