use std::borrow::Cow;
use std::sync::Arc;
use base::id::PipelineId;
use crossbeam_channel::Sender;
use devtools_traits::ScriptToDevtoolsControlMsg;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
use js::rust::Runtime;
use net_traits::image_cache::ImageCache;
use net_traits::ResourceThreads;
use profile_traits::{mem, time};
use script_traits::{Painter, ScriptMsg, ScriptToConstellationChan, TimerSchedulerMsg};
use servo_atoms::Atom;
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::IdentityHub;
use crate::dom::paintworkletglobalscope::{PaintWorkletGlobalScope, PaintWorkletTask};
use crate::dom::testworkletglobalscope::{TestWorkletGlobalScope, TestWorkletTask};
use crate::dom::worklet::WorkletExecutor;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::{CanGc, JSContext};
use crate::script_thread::MainThreadScriptMsg;
#[dom_struct]
pub struct WorkletGlobalScope {
globalscope: GlobalScope,
#[no_trace]
base_url: ServoUrl,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
to_script_thread_sender: Sender<MainThreadScriptMsg>,
executor: WorkletExecutor,
}
impl WorkletGlobalScope {
pub fn new(
scope_type: WorkletGlobalScopeType,
runtime: &Runtime,
pipeline_id: PipelineId,
base_url: ServoUrl,
executor: WorkletExecutor,
init: &WorkletGlobalScopeInit,
) -> DomRoot<WorkletGlobalScope> {
match scope_type {
WorkletGlobalScopeType::Test => DomRoot::upcast(TestWorkletGlobalScope::new(
runtime,
pipeline_id,
base_url,
executor,
init,
)),
WorkletGlobalScopeType::Paint => DomRoot::upcast(PaintWorkletGlobalScope::new(
runtime,
pipeline_id,
base_url,
executor,
init,
)),
}
}
pub fn new_inherited(
pipeline_id: PipelineId,
base_url: ServoUrl,
executor: WorkletExecutor,
init: &WorkletGlobalScopeInit,
) -> Self {
let script_to_constellation_chan = ScriptToConstellationChan {
sender: init.to_constellation_sender.clone(),
pipeline_id,
};
Self {
globalscope: GlobalScope::new_inherited(
pipeline_id,
init.devtools_chan.clone(),
init.mem_profiler_chan.clone(),
init.time_profiler_chan.clone(),
script_to_constellation_chan,
init.scheduler_chan.clone(),
init.resource_threads.clone(),
MutableOrigin::new(ImmutableOrigin::new_opaque()),
None,
Default::default(),
init.is_headless,
init.user_agent.clone(),
init.gpu_id_hub.clone(),
init.inherited_secure_context,
false,
),
base_url,
to_script_thread_sender: init.to_script_thread_sender.clone(),
executor,
}
}
pub fn get_cx() -> JSContext {
GlobalScope::get_cx()
}
pub fn evaluate_js(&self, script: &str, can_gc: CanGc) -> bool {
debug!("Evaluating Dom in a worklet.");
rooted!(in (*GlobalScope::get_cx()) let mut rval = UndefinedValue());
self.globalscope.evaluate_js_on_global_with_result(
script,
rval.handle_mut(),
ScriptFetchOptions::default_classic_script(&self.globalscope),
self.globalscope.api_base_url(),
can_gc,
)
}
pub fn register_paint_worklet(
&self,
name: Atom,
properties: Vec<Atom>,
painter: Box<dyn Painter>,
) {
self.to_script_thread_sender
.send(MainThreadScriptMsg::RegisterPaintWorklet {
pipeline_id: self.globalscope.pipeline_id(),
name,
properties,
painter,
})
.expect("Worklet thread outlived script thread.");
}
pub fn base_url(&self) -> ServoUrl {
self.base_url.clone()
}
pub fn executor(&self) -> WorkletExecutor {
self.executor.clone()
}
pub fn perform_a_worklet_task(&self, task: WorkletTask) {
match task {
WorkletTask::Test(task) => match self.downcast::<TestWorkletGlobalScope>() {
Some(global) => global.perform_a_worklet_task(task),
None => warn!("This is not a test worklet."),
},
WorkletTask::Paint(task) => match self.downcast::<PaintWorkletGlobalScope>() {
Some(global) => global.perform_a_worklet_task(task),
None => warn!("This is not a paint worklet."),
},
}
}
}
#[derive(Clone)]
pub struct WorkletGlobalScopeInit {
pub to_script_thread_sender: Sender<MainThreadScriptMsg>,
pub resource_threads: ResourceThreads,
pub mem_profiler_chan: mem::ProfilerChan,
pub time_profiler_chan: time::ProfilerChan,
pub devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
pub to_constellation_sender: IpcSender<(PipelineId, ScriptMsg)>,
pub scheduler_chan: IpcSender<TimerSchedulerMsg>,
pub image_cache: Arc<dyn ImageCache>,
pub is_headless: bool,
pub user_agent: Cow<'static, str>,
pub gpu_id_hub: Arc<IdentityHub>,
pub inherited_secure_context: Option<bool>,
}
#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf)]
pub enum WorkletGlobalScopeType {
Test,
Paint,
}
pub enum WorkletTask {
Test(TestWorkletTask),
Paint(PaintWorkletTask),
}