use std::borrow::Cow;
use std::cell::{Cell, OnceCell};
use std::collections::hash_map::Entry;
use std::collections::{HashMap, VecDeque};
use std::ops::Index;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use std::{mem, ptr};
use base::id::{
BlobId, BroadcastChannelRouterId, MessagePortId, MessagePortRouterId, PipelineId,
ServiceWorkerId, ServiceWorkerRegistrationId,
};
use content_security_policy::{CheckResult, CspList, PolicyDisposition};
use crossbeam_channel::Sender;
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg};
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use js::glue::{IsWrapper, UnwrapObjectDynamic};
use js::jsapi::{
Compile1, CurrentGlobalOrNull, GetNonCCWObjectGlobal, HandleObject, Heap,
InstantiateGlobalStencil, InstantiateOptions, JSContext, JSObject, JSScript, RuntimeCode,
SetScriptPrivate,
};
use js::jsval::{PrivateValue, UndefinedValue};
use js::panic::maybe_resume_unwind;
use js::rust::wrappers::{JS_ExecuteScript, JS_GetScriptPrivate};
use js::rust::{
describe_scripted_caller, get_object_class, transform_str_to_source_text,
CompileOptionsWrapper, CustomAutoRooter, CustomAutoRooterGuard, HandleValue,
MutableHandleValue, ParentRuntime, Runtime,
};
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
use net_traits::blob_url_store::{get_blob_origin, BlobBuf};
use net_traits::filemanager_thread::{
FileManagerResult, FileManagerThreadMsg, ReadFileProgress, RelativePos,
};
use net_traits::image_cache::ImageCache;
use net_traits::policy_container::PolicyContainer;
use net_traits::request::{Referrer, RequestBuilder};
use net_traits::response::HttpsState;
use net_traits::{
fetch_async, CoreResourceMsg, CoreResourceThread, FetchResponseListener, IpcSend,
ReferrerPolicy, ResourceThreads,
};
use profile_traits::{ipc as profile_ipc, mem as profile_mem, time as profile_time};
use script_traits::serializable::{BlobData, BlobImpl, FileBlob};
use script_traits::transferable::MessagePortImpl;
use script_traits::{
BroadcastMsg, GamepadEvent, GamepadSupportedHapticEffects, GamepadUpdateType, MessagePortMsg,
PortMessageTask, ScriptMsg, ScriptToConstellationChan, TimerEvent, TimerEventId,
TimerSchedulerMsg, TimerSource,
};
use servo_url::{ImmutableOrigin, MutableOrigin, ServoUrl};
use uuid::Uuid;
#[cfg(feature = "webgpu")]
use webgpu::{DeviceLostReason, WebGPUDevice};
use super::bindings::codegen::Bindings::MessagePortBinding::StructuredSerializeOptions;
#[cfg(feature = "webgpu")]
use super::bindings::codegen::Bindings::WebGPUBinding::GPUDeviceLostReason;
use super::bindings::error::Fallible;
use super::bindings::trace::{HashMapTracedValues, RootedTraceableBox};
use crate::dom::bindings::cell::{DomRefCell, RefMut};
use crate::dom::bindings::codegen::Bindings::BroadcastChannelBinding::BroadcastChannelMethods;
use crate::dom::bindings::codegen::Bindings::EventSourceBinding::EventSource_Binding::EventSourceMethods;
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::{
ImageBitmapOptions, ImageBitmapSource,
};
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use crate::dom::bindings::codegen::Bindings::PerformanceBinding::Performance_Binding::PerformanceMethods;
use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionState;
use crate::dom::bindings::codegen::Bindings::VoidFunctionBinding::VoidFunction;
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use crate::dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
use crate::dom::bindings::conversions::{root_from_object, root_from_object_static};
use crate::dom::bindings::error::{report_pending_exception, Error, ErrorInfo};
use crate::dom::bindings::frozenarray::CachedFrozenArray;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::settings_stack::{entry_global, incumbent_global, AutoEntryScript};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone;
use crate::dom::bindings::trace::CustomTraceable;
use crate::dom::bindings::weakref::{DOMTracker, WeakRef};
use crate::dom::blob::Blob;
use crate::dom::broadcastchannel::BroadcastChannel;
use crate::dom::crypto::Crypto;
use crate::dom::dedicatedworkerglobalscope::{
DedicatedWorkerControlMsg, DedicatedWorkerGlobalScope,
};
use crate::dom::errorevent::ErrorEvent;
use crate::dom::event::{Event, EventBubbles, EventCancelable, EventStatus};
use crate::dom::eventsource::EventSource;
use crate::dom::eventtarget::EventTarget;
use crate::dom::file::File;
use crate::dom::gamepad::{contains_user_gesture, Gamepad};
use crate::dom::gamepadevent::GamepadEventType;
use crate::dom::htmlscriptelement::{ScriptId, SourceCode};
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::messageevent::MessageEvent;
use crate::dom::messageport::MessagePort;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::dom::performance::Performance;
use crate::dom::performanceobserver::VALID_ENTRY_TYPES;
use crate::dom::promise::Promise;
use crate::dom::readablestream::ReadableStream;
use crate::dom::serviceworker::ServiceWorker;
use crate::dom::serviceworkerregistration::ServiceWorkerRegistration;
use crate::dom::underlyingsourcecontainer::UnderlyingSourceType;
#[cfg(feature = "webgpu")]
use crate::dom::webgpu::gpudevice::GPUDevice;
#[cfg(feature = "webgpu")]
use crate::dom::webgpu::identityhub::IdentityHub;
use crate::dom::window::Window;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::dom::workletglobalscope::WorkletGlobalScope;
use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
use crate::network_listener::{NetworkListener, PreInvoke};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
use crate::script_module::{DynamicModuleList, ModuleScript, ModuleTree, ScriptFetchOptions};
use crate::script_runtime::{
CanGc, CommonScriptMsg, JSContext as SafeJSContext, ScriptChan, ScriptPort, ThreadSafeJSContext,
};
use crate::script_thread::{MainThreadScriptChan, ScriptThread};
use crate::security_manager::CSPViolationReporter;
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::file_reading::FileReadingTaskSource;
use crate::task_source::gamepad::GamepadTaskSource;
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::timer::TimerTaskSource;
use crate::task_source::websocket::WebsocketTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};
use crate::timers::{
IsInterval, OneshotTimerCallback, OneshotTimerHandle, OneshotTimers, TimerCallback,
};
use crate::unminify::unminified_path;
#[derive(JSTraceable)]
pub struct AutoCloseWorker {
closing: Arc<AtomicBool>,
join_handle: Option<JoinHandle<()>>,
#[no_trace]
control_sender: Sender<DedicatedWorkerControlMsg>,
#[no_trace]
context: ThreadSafeJSContext,
}
impl Drop for AutoCloseWorker {
fn drop(&mut self) {
self.closing.store(true, Ordering::SeqCst);
if self
.control_sender
.send(DedicatedWorkerControlMsg::Exit)
.is_err()
{
warn!("Couldn't send an exit message to a dedicated worker.");
}
self.context.request_interrupt_callback();
if self
.join_handle
.take()
.expect("No handle to join on worker.")
.join()
.is_err()
{
warn!("Failed to join on dedicated worker thread.");
}
}
}
#[dom_struct]
pub struct GlobalScope {
eventtarget: EventTarget,
crypto: MutNullableDom<Crypto>,
message_port_state: DomRefCell<MessagePortState>,
broadcast_channel_state: DomRefCell<BroadcastChannelState>,
blob_state: DomRefCell<BlobState>,
registration_map: DomRefCell<
HashMapTracedValues<ServiceWorkerRegistrationId, Dom<ServiceWorkerRegistration>>,
>,
worker_map: DomRefCell<HashMapTracedValues<ServiceWorkerId, Dom<ServiceWorker>>>,
#[no_trace]
pipeline_id: PipelineId,
devtools_wants_updates: Cell<bool>,
console_timers: DomRefCell<HashMap<DOMString, Instant>>,
#[ignore_malloc_size_of = "mozjs"]
module_map: DomRefCell<HashMapTracedValues<ServoUrl, Rc<ModuleTree>>>,
#[ignore_malloc_size_of = "mozjs"]
inline_module_map: DomRefCell<HashMap<ScriptId, Rc<ModuleTree>>>,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
mem_profiler_chan: profile_mem::ProfilerChan,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
time_profiler_chan: profile_time::ProfilerChan,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
script_to_constellation_chan: ScriptToConstellationChan,
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
scheduler_chan: IpcSender<TimerSchedulerMsg>,
in_error_reporting_mode: Cell<bool>,
#[no_trace]
resource_threads: ResourceThreads,
timers: OneshotTimers,
init_timers: Cell<bool>,
#[no_trace]
origin: MutableOrigin,
#[no_trace]
creation_url: Option<ServoUrl>,
permission_state_invocation_results: DomRefCell<HashMap<String, PermissionState>>,
#[ignore_malloc_size_of = "Rc<T> is hard"]
microtask_queue: Rc<MicrotaskQueue>,
#[ignore_malloc_size_of = "Arc"]
list_auto_close_worker: DomRefCell<Vec<AutoCloseWorker>>,
event_source_tracker: DOMTracker<EventSource>,
#[ignore_malloc_size_of = "mozjs"]
#[allow(clippy::vec_box)]
uncaught_rejections: DomRefCell<Vec<Box<Heap<*mut JSObject>>>>,
#[ignore_malloc_size_of = "mozjs"]
#[allow(clippy::vec_box)]
consumed_rejections: DomRefCell<Vec<Box<Heap<*mut JSObject>>>>,
is_headless: bool,
user_agent: Cow<'static, str>,
#[ignore_malloc_size_of = "defined in wgpu"]
#[no_trace]
#[cfg(feature = "webgpu")]
gpu_id_hub: Arc<IdentityHub>,
#[cfg(feature = "webgpu")]
gpu_devices: DomRefCell<HashMapTracedValues<WebGPUDevice, WeakRef<GPUDevice>>>,
#[ignore_malloc_size_of = "mozjs"]
frozen_supported_performance_entry_types: CachedFrozenArray,
#[no_trace]
https_state: Cell<HttpsState>,
console_group_stack: DomRefCell<Vec<DOMString>>,
console_count_map: DomRefCell<HashMap<DOMString, usize>>,
dynamic_modules: DomRefCell<DynamicModuleList>,
inherited_secure_context: Option<bool>,
unminified_js_dir: Option<String>,
#[ignore_malloc_size_of = "Rc<T> is hard"]
byte_length_queuing_strategy_size_function: OnceCell<Rc<Function>>,
#[ignore_malloc_size_of = "Rc<T> is hard"]
count_queuing_strategy_size_function: OnceCell<Rc<Function>>,
}
struct MessageListener {
canceller: TaskCanceller,
task_source: PortMessageQueue,
context: Trusted<GlobalScope>,
}
struct BroadcastListener {
canceller: TaskCanceller,
task_source: DOMManipulationTaskSource,
context: Trusted<GlobalScope>,
}
struct TimerListener {
canceller: TaskCanceller,
task_source: TimerTaskSource,
context: Trusted<GlobalScope>,
}
type FileListenerCallback = Box<dyn Fn(Rc<Promise>, Fallible<Vec<u8>>) + Send>;
struct FileListener {
state: Option<FileListenerState>,
task_source: FileReadingTaskSource,
task_canceller: TaskCanceller,
}
enum FileListenerTarget {
Promise(TrustedPromise, FileListenerCallback),
Stream(Trusted<ReadableStream>),
}
enum FileListenerState {
Empty(FileListenerTarget),
Receiving(Vec<u8>, FileListenerTarget),
}
#[derive(JSTraceable, MallocSizeOf)]
pub enum BlobTracker {
File(WeakRef<File>),
Blob(WeakRef<Blob>),
}
#[derive(JSTraceable, MallocSizeOf)]
pub struct BlobInfo {
tracker: BlobTracker,
#[no_trace]
blob_impl: BlobImpl,
has_url: bool,
}
#[derive(JSTraceable, MallocSizeOf)]
pub enum BlobState {
Managed(HashMapTracedValues<BlobId, BlobInfo>),
UnManaged,
}
enum BlobResult {
Bytes(Vec<u8>),
File(Uuid, usize),
}
#[derive(JSTraceable, MallocSizeOf)]
#[crown::unrooted_must_root_lint::must_root]
pub struct ManagedMessagePort {
dom_port: Dom<MessagePort>,
#[no_trace]
port_impl: Option<MessagePortImpl>,
pending: bool,
closed: bool,
}
#[derive(JSTraceable, MallocSizeOf)]
#[crown::unrooted_must_root_lint::must_root]
pub enum BroadcastChannelState {
Managed(
#[no_trace] BroadcastChannelRouterId,
HashMap<DOMString, VecDeque<Dom<BroadcastChannel>>>,
),
UnManaged,
}
#[derive(JSTraceable, MallocSizeOf)]
#[crown::unrooted_must_root_lint::must_root]
pub enum MessagePortState {
Managed(
#[no_trace] MessagePortRouterId,
HashMapTracedValues<MessagePortId, ManagedMessagePort>,
),
UnManaged,
}
impl BroadcastListener {
fn handle(&self, event: BroadcastMsg) {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(broadcast_message_event: move || {
let global = context.root();
global.broadcast_message_event(event, None);
}),
&self.canceller,
);
}
}
impl TimerListener {
fn handle(&self, event: TimerEvent) {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(timer_event: move || {
let global = context.root();
let TimerEvent(source, id) = event;
match source {
TimerSource::FromWorker => {
global.downcast::<WorkerGlobalScope>().expect("Window timer delivered to worker");
},
TimerSource::FromWindow(pipeline) => {
assert_eq!(pipeline, global.pipeline_id());
global.downcast::<Window>().expect("Worker timer delivered to window");
},
};
global.fire_timer(id, CanGc::note());
}),
&self.canceller,
);
}
}
impl MessageListener {
fn notify(&self, msg: MessagePortMsg) {
match msg {
MessagePortMsg::CompleteTransfer(ports) => {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(process_complete_transfer: move || {
let global = context.root();
let router_id = match global.port_router_id() {
Some(router_id) => router_id,
None => {
let _ = global.script_to_constellation_chan().send(
ScriptMsg::MessagePortTransferResult(None, vec![], ports),
);
return;
}
};
let mut succeeded = vec![];
let mut failed = HashMap::new();
for (id, buffer) in ports.into_iter() {
if global.is_managing_port(&id) {
succeeded.push(id);
global.complete_port_transfer(id, buffer);
} else {
failed.insert(id, buffer);
}
}
let _ = global.script_to_constellation_chan().send(
ScriptMsg::MessagePortTransferResult(Some(router_id), succeeded, failed),
);
}),
&self.canceller,
);
},
MessagePortMsg::CompletePendingTransfer(port_id, buffer) => {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(complete_pending: move || {
let global = context.root();
global.complete_port_transfer(port_id, buffer);
}),
&self.canceller,
);
},
MessagePortMsg::NewTask(port_id, task) => {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(process_new_task: move || {
let global = context.root();
global.route_task_to_port(port_id, task, CanGc::note());
}),
&self.canceller,
);
},
MessagePortMsg::RemoveMessagePort(port_id) => {
let context = self.context.clone();
let _ = self.task_source.queue_with_canceller(
task!(process_remove_message_port: move || {
let global = context.root();
global.note_entangled_port_removed(&port_id);
}),
&self.canceller,
);
},
}
}
}
fn stream_handle_incoming(stream: &ReadableStream, bytes: Fallible<Vec<u8>>) {
match bytes {
Ok(b) => {
stream.enqueue_native(b);
},
Err(e) => {
stream.error_native(e);
},
}
}
fn stream_handle_eof(stream: &ReadableStream) {
stream.controller_close_native();
}
impl FileListener {
fn handle(&mut self, msg: FileManagerResult<ReadFileProgress>) {
match msg {
Ok(ReadFileProgress::Meta(blob_buf)) => match self.state.take() {
Some(FileListenerState::Empty(target)) => {
let bytes = if let FileListenerTarget::Stream(ref trusted_stream) = target {
let trusted = trusted_stream.clone();
let task = task!(enqueue_stream_chunk: move || {
let stream = trusted.root();
stream_handle_incoming(&stream, Ok(blob_buf.bytes));
});
let _ = self
.task_source
.queue_with_canceller(task, &self.task_canceller);
Vec::with_capacity(0)
} else {
blob_buf.bytes
};
self.state = Some(FileListenerState::Receiving(bytes, target));
},
_ => panic!(
"Unexpected FileListenerState when receiving ReadFileProgress::Meta msg."
),
},
Ok(ReadFileProgress::Partial(mut bytes_in)) => match self.state.take() {
Some(FileListenerState::Receiving(mut bytes, target)) => {
if let FileListenerTarget::Stream(ref trusted_stream) = target {
let trusted = trusted_stream.clone();
let task = task!(enqueue_stream_chunk: move || {
let stream = trusted.root();
stream_handle_incoming(&stream, Ok(bytes_in));
});
let _ = self
.task_source
.queue_with_canceller(task, &self.task_canceller);
} else {
bytes.append(&mut bytes_in);
};
self.state = Some(FileListenerState::Receiving(bytes, target));
},
_ => panic!(
"Unexpected FileListenerState when receiving ReadFileProgress::Partial msg."
),
},
Ok(ReadFileProgress::EOF) => match self.state.take() {
Some(FileListenerState::Receiving(bytes, target)) => match target {
FileListenerTarget::Promise(trusted_promise, callback) => {
let task = task!(resolve_promise: move || {
let promise = trusted_promise.root();
let _ac = enter_realm(&*promise.global());
callback(promise, Ok(bytes));
});
let _ = self
.task_source
.queue_with_canceller(task, &self.task_canceller);
},
FileListenerTarget::Stream(trusted_stream) => {
let trusted = trusted_stream.clone();
let task = task!(enqueue_stream_chunk: move || {
let stream = trusted.root();
stream_handle_eof(&stream);
});
let _ = self
.task_source
.queue_with_canceller(task, &self.task_canceller);
},
},
_ => {
panic!("Unexpected FileListenerState when receiving ReadFileProgress::EOF msg.")
},
},
Err(_) => match self.state.take() {
Some(FileListenerState::Receiving(_, target)) |
Some(FileListenerState::Empty(target)) => {
let error = Err(Error::Network);
match target {
FileListenerTarget::Promise(trusted_promise, callback) => {
let _ = self.task_source.queue_with_canceller(
task!(reject_promise: move || {
let promise = trusted_promise.root();
let _ac = enter_realm(&*promise.global());
callback(promise, error);
}),
&self.task_canceller,
);
},
FileListenerTarget::Stream(trusted_stream) => {
let _ = self.task_source.queue_with_canceller(
task!(error_stream: move || {
let stream = trusted_stream.root();
stream_handle_incoming(&stream, error);
}),
&self.task_canceller,
);
},
}
},
_ => panic!("Unexpected FileListenerState when receiving Err msg."),
},
}
}
}
impl GlobalScope {
#[allow(clippy::too_many_arguments)]
pub fn new_inherited(
pipeline_id: PipelineId,
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
mem_profiler_chan: profile_mem::ProfilerChan,
time_profiler_chan: profile_time::ProfilerChan,
script_to_constellation_chan: ScriptToConstellationChan,
scheduler_chan: IpcSender<TimerSchedulerMsg>,
resource_threads: ResourceThreads,
origin: MutableOrigin,
creation_url: Option<ServoUrl>,
microtask_queue: Rc<MicrotaskQueue>,
is_headless: bool,
user_agent: Cow<'static, str>,
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
inherited_secure_context: Option<bool>,
unminify_js: bool,
) -> Self {
Self {
message_port_state: DomRefCell::new(MessagePortState::UnManaged),
broadcast_channel_state: DomRefCell::new(BroadcastChannelState::UnManaged),
blob_state: DomRefCell::new(BlobState::UnManaged),
eventtarget: EventTarget::new_inherited(),
crypto: Default::default(),
registration_map: DomRefCell::new(HashMapTracedValues::new()),
worker_map: DomRefCell::new(HashMapTracedValues::new()),
pipeline_id,
devtools_wants_updates: Default::default(),
console_timers: DomRefCell::new(Default::default()),
module_map: DomRefCell::new(Default::default()),
inline_module_map: DomRefCell::new(Default::default()),
devtools_chan,
mem_profiler_chan,
time_profiler_chan,
script_to_constellation_chan,
scheduler_chan: scheduler_chan.clone(),
in_error_reporting_mode: Default::default(),
resource_threads,
timers: OneshotTimers::new(scheduler_chan),
init_timers: Default::default(),
origin,
creation_url,
permission_state_invocation_results: Default::default(),
microtask_queue,
list_auto_close_worker: Default::default(),
event_source_tracker: DOMTracker::new(),
uncaught_rejections: Default::default(),
consumed_rejections: Default::default(),
is_headless,
user_agent,
#[cfg(feature = "webgpu")]
gpu_id_hub,
#[cfg(feature = "webgpu")]
gpu_devices: DomRefCell::new(HashMapTracedValues::new()),
frozen_supported_performance_entry_types: CachedFrozenArray::new(),
https_state: Cell::new(HttpsState::None),
console_group_stack: DomRefCell::new(Vec::new()),
console_count_map: Default::default(),
dynamic_modules: DomRefCell::new(DynamicModuleList::new()),
inherited_secure_context,
unminified_js_dir: unminify_js.then(|| unminified_path("unminified-js")),
byte_length_queuing_strategy_size_function: OnceCell::new(),
count_queuing_strategy_size_function: OnceCell::new(),
}
}
fn port_router_id(&self) -> Option<MessagePortRouterId> {
if let MessagePortState::Managed(id, _message_ports) = &*self.message_port_state.borrow() {
Some(*id)
} else {
None
}
}
fn is_managing_port(&self, port_id: &MessagePortId) -> bool {
if let MessagePortState::Managed(_router_id, message_ports) =
&*self.message_port_state.borrow()
{
return message_ports.contains_key(port_id);
}
false
}
fn setup_timers(&self) {
if self.init_timers.get() {
return;
}
self.init_timers.set(true);
let (timer_ipc_chan, timer_ipc_port) = ipc::channel().unwrap();
self.timers.setup_scheduling(timer_ipc_chan);
let context = Trusted::new(self);
let (task_source, canceller) = (
self.timer_task_source(),
self.task_canceller(TaskSourceName::Timer),
);
let timer_listener = TimerListener {
context,
task_source,
canceller,
};
ROUTER.add_typed_route(
timer_ipc_port,
Box::new(move |message| {
let event = message.unwrap();
timer_listener.handle(event);
}),
);
}
pub fn get_serviceworker_registration(
&self,
script_url: &ServoUrl,
scope: &ServoUrl,
registration_id: ServiceWorkerRegistrationId,
installing_worker: Option<ServiceWorkerId>,
_waiting_worker: Option<ServiceWorkerId>,
_active_worker: Option<ServiceWorkerId>,
) -> DomRoot<ServiceWorkerRegistration> {
let mut registrations = self.registration_map.borrow_mut();
if let Some(registration) = registrations.get(®istration_id) {
return DomRoot::from_ref(&**registration);
}
let new_registration = ServiceWorkerRegistration::new(self, scope.clone(), registration_id);
if let Some(worker_id) = installing_worker {
let worker = self.get_serviceworker(script_url, scope, worker_id);
new_registration.set_installing(&worker);
}
registrations.insert(registration_id, Dom::from_ref(&*new_registration));
new_registration
}
pub fn get_serviceworker(
&self,
script_url: &ServoUrl,
scope: &ServoUrl,
worker_id: ServiceWorkerId,
) -> DomRoot<ServiceWorker> {
let mut workers = self.worker_map.borrow_mut();
if let Some(worker) = workers.get(&worker_id) {
DomRoot::from_ref(&**worker)
} else {
let new_worker = ServiceWorker::new(self, script_url.clone(), scope.clone(), worker_id);
workers.insert(worker_id, Dom::from_ref(&*new_worker));
new_worker
}
}
fn complete_port_transfer(&self, port_id: MessagePortId, tasks: VecDeque<PortMessageTask>) {
let should_start = if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
match message_ports.get_mut(&port_id) {
None => {
panic!("complete_port_transfer called for an unknown port.");
},
Some(managed_port) => {
if managed_port.pending {
panic!("CompleteTransfer msg received for a pending port.");
}
if let Some(port_impl) = managed_port.port_impl.as_mut() {
port_impl.complete_transfer(tasks);
port_impl.enabled()
} else {
panic!("managed-port has no port-impl.");
}
},
}
} else {
panic!("complete_port_transfer called for an unknown port.");
};
if should_start {
self.start_message_port(&port_id);
}
}
pub fn perform_a_dom_garbage_collection_checkpoint(&self) {
self.perform_a_message_port_garbage_collection_checkpoint();
self.perform_a_blob_garbage_collection_checkpoint();
self.perform_a_broadcast_channel_garbage_collection_checkpoint();
}
pub fn remove_web_messaging_and_dedicated_workers_infra(&self) {
self.remove_message_ports_router();
self.remove_broadcast_channel_router();
self.list_auto_close_worker
.borrow_mut()
.drain(0..)
.for_each(drop);
}
fn remove_message_ports_router(&self) {
if let MessagePortState::Managed(router_id, _message_ports) =
&*self.message_port_state.borrow()
{
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::RemoveMessagePortRouter(*router_id));
}
*self.message_port_state.borrow_mut() = MessagePortState::UnManaged;
}
fn remove_broadcast_channel_router(&self) {
if let BroadcastChannelState::Managed(router_id, _channels) =
&*self.broadcast_channel_state.borrow()
{
let _ =
self.script_to_constellation_chan()
.send(ScriptMsg::RemoveBroadcastChannelRouter(
*router_id,
self.origin().immutable().clone(),
));
}
*self.broadcast_channel_state.borrow_mut() = BroadcastChannelState::UnManaged;
}
pub fn entangle_ports(&self, port1: MessagePortId, port2: MessagePortId) {
if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
for (port_id, entangled_id) in &[(port1, port2), (port2, port1)] {
match message_ports.get_mut(port_id) {
None => {
return warn!("entangled_ports called on a global not managing the port.");
},
Some(managed_port) => {
if let Some(port_impl) = managed_port.port_impl.as_mut() {
managed_port.dom_port.entangle(*entangled_id);
port_impl.entangle(*entangled_id);
} else {
panic!("managed-port has no port-impl.");
}
},
}
}
} else {
panic!("entangled_ports called on a global not managing any ports.");
}
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::EntanglePorts(port1, port2));
}
pub fn note_entangled_port_removed(&self, port_id: &MessagePortId) {
warn!(
"Entangled port of {:?} has been removed in another global",
port_id
);
}
pub fn mark_port_as_transferred(&self, port_id: &MessagePortId) -> MessagePortImpl {
if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
let mut port_impl = message_ports
.remove(port_id)
.map(|ref mut managed_port| {
managed_port
.port_impl
.take()
.expect("Managed port doesn't have a port-impl.")
})
.expect("mark_port_as_transferred called on a global not managing the port.");
port_impl.set_has_been_shipped();
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::MessagePortShipped(*port_id));
port_impl
} else {
panic!("mark_port_as_transferred called on a global not managing any ports.");
}
}
pub fn start_message_port(&self, port_id: &MessagePortId) {
if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
let message_buffer = match message_ports.get_mut(port_id) {
None => panic!("start_message_port called on a unknown port."),
Some(managed_port) => {
if let Some(port_impl) = managed_port.port_impl.as_mut() {
port_impl.start()
} else {
panic!("managed-port has no port-impl.");
}
},
};
if let Some(message_buffer) = message_buffer {
for task in message_buffer {
let port_id = *port_id;
let this = Trusted::new(self);
let _ = self.port_message_queue().queue(
task!(process_pending_port_messages: move || {
let target_global = this.root();
target_global.route_task_to_port(port_id, task, CanGc::note());
}),
self,
);
}
}
} else {
warn!("start_message_port called on a global not managing any ports.")
}
}
pub fn close_message_port(&self, port_id: &MessagePortId) {
if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
match message_ports.get_mut(port_id) {
None => panic!("close_message_port called on an unknown port."),
Some(managed_port) => {
if let Some(port_impl) = managed_port.port_impl.as_mut() {
port_impl.close();
managed_port.closed = true;
} else {
panic!("managed-port has no port-impl.");
}
},
};
} else {
warn!("close_message_port called on a global not managing any ports.")
}
}
pub fn post_messageport_msg(&self, port_id: MessagePortId, task: PortMessageTask) {
if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
let entangled_port = match message_ports.get_mut(&port_id) {
None => panic!("post_messageport_msg called on an unknown port."),
Some(managed_port) => {
if let Some(port_impl) = managed_port.port_impl.as_mut() {
port_impl.entangled_port_id()
} else {
panic!("managed-port has no port-impl.");
}
},
};
if let Some(entangled_id) = entangled_port {
let this = Trusted::new(self);
let _ = self.port_message_queue().queue(
task!(post_message: move || {
let global = this.root();
global.route_task_to_port(entangled_id, task, CanGc::note());
}),
self,
);
}
} else {
warn!("post_messageport_msg called on a global not managing any ports.");
}
}
fn re_route_port_task(&self, port_id: MessagePortId, task: PortMessageTask) {
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::RerouteMessagePort(port_id, task));
}
pub fn schedule_broadcast(&self, msg: BroadcastMsg, channel_id: &Uuid) {
self.broadcast_message_event(msg.clone(), Some(channel_id));
if let BroadcastChannelState::Managed(router_id, _) =
&*self.broadcast_channel_state.borrow()
{
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::ScheduleBroadcast(*router_id, msg));
} else {
panic!("Attemps to broadcast a message via global not managing any channels.");
}
}
pub fn broadcast_message_event(&self, event: BroadcastMsg, channel_id: Option<&Uuid>) {
if let BroadcastChannelState::Managed(_, channels) = &*self.broadcast_channel_state.borrow()
{
let BroadcastMsg {
data,
origin,
channel_name,
} = event;
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
if worker.is_closing() {
return;
}
}
if let Some(window) = self.downcast::<Window>() {
if !window.Document().is_fully_active() {
return;
}
}
let channel_name = DOMString::from_string(channel_name);
if let Some(channels) = channels.get(&channel_name) {
channels
.iter()
.filter(|channel| {
if let Some(id) = channel_id {
channel.id() != id
} else {
true
}
})
.map(|channel| DomRoot::from_ref(&**channel))
.for_each(|channel| {
let data = data.clone_for_broadcast();
let origin = origin.clone();
let channel = Trusted::new(&*channel);
let global = Trusted::new(self);
let _ = self.dom_manipulation_task_source().queue(
task!(process_pending_port_messages: move || {
let destination = channel.root();
let global = global.root();
if destination.closed() {
return;
}
rooted!(in(*GlobalScope::get_cx()) let mut message = UndefinedValue());
if let Ok(ports) = structuredclone::read(&global, data, message.handle_mut()) {
MessageEvent::dispatch_jsval(
destination.upcast(),
&global,
message.handle(),
Some(&origin.ascii_serialization()),
None,
ports,
CanGc::note()
);
} else {
MessageEvent::dispatch_error(destination.upcast(), &global, CanGc::note());
}
}),
self,
);
});
}
}
}
pub fn route_task_to_port(&self, port_id: MessagePortId, task: PortMessageTask, can_gc: CanGc) {
let should_dispatch = if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
if !message_ports.contains_key(&port_id) {
self.re_route_port_task(port_id, task);
return;
}
match message_ports.get_mut(&port_id) {
None => panic!("route_task_to_port called for an unknown port."),
Some(managed_port) => {
if let Some(port_impl) = managed_port.port_impl.as_mut() {
port_impl.handle_incoming(task).map(|to_dispatch| {
(DomRoot::from_ref(&*managed_port.dom_port), to_dispatch)
})
} else {
panic!("managed-port has no port-impl.");
}
},
}
} else {
self.re_route_port_task(port_id, task);
return;
};
if let Some((dom_port, PortMessageTask { origin, data })) = should_dispatch {
rooted!(in(*GlobalScope::get_cx()) let mut message_clone = UndefinedValue());
if let Ok(ports) = structuredclone::read(self, data, message_clone.handle_mut()) {
MessageEvent::dispatch_jsval(
dom_port.upcast(),
self,
message_clone.handle(),
Some(&origin.ascii_serialization()),
None,
ports,
can_gc,
);
} else {
MessageEvent::dispatch_error(dom_port.upcast(), self, can_gc);
}
}
}
pub fn maybe_add_pending_ports(&self) {
if let MessagePortState::Managed(router_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
let to_be_added: Vec<MessagePortId> = message_ports
.iter()
.filter_map(|(id, managed_port)| {
if managed_port.pending {
Some(*id)
} else {
None
}
})
.collect();
for id in to_be_added.iter() {
let managed_port = message_ports
.get_mut(id)
.expect("Collected port-id to match an entry");
if !managed_port.pending {
panic!("Only pending ports should be found in to_be_added")
}
managed_port.pending = false;
}
let _ =
self.script_to_constellation_chan()
.send(ScriptMsg::CompleteMessagePortTransfer(
*router_id,
to_be_added,
));
} else {
warn!("maybe_add_pending_ports called on a global not managing any ports.");
}
}
pub fn perform_a_message_port_garbage_collection_checkpoint(&self) {
let is_empty = if let MessagePortState::Managed(_id, message_ports) =
&mut *self.message_port_state.borrow_mut()
{
let to_be_removed: Vec<MessagePortId> = message_ports
.iter()
.filter_map(|(id, managed_port)| {
if managed_port.closed {
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::RemoveMessagePort(*id));
Some(*id)
} else {
None
}
})
.collect();
for id in to_be_removed {
message_ports.remove(&id);
}
message_ports.is_empty()
} else {
false
};
if is_empty {
self.remove_message_ports_router();
}
}
pub fn perform_a_broadcast_channel_garbage_collection_checkpoint(&self) {
let is_empty = if let BroadcastChannelState::Managed(router_id, ref mut channels) =
&mut *self.broadcast_channel_state.borrow_mut()
{
channels.retain(|name, ref mut channels| {
channels.retain(|chan| !chan.closed());
if channels.is_empty() {
let _ = self.script_to_constellation_chan().send(
ScriptMsg::RemoveBroadcastChannelNameInRouter(
*router_id,
name.to_string(),
self.origin().immutable().clone(),
),
);
false
} else {
true
}
});
channels.is_empty()
} else {
false
};
if is_empty {
self.remove_broadcast_channel_router();
}
}
pub fn track_broadcast_channel(&self, dom_channel: &BroadcastChannel) {
let mut current_state = self.broadcast_channel_state.borrow_mut();
if let BroadcastChannelState::UnManaged = &*current_state {
let (broadcast_control_sender, broadcast_control_receiver) =
ipc::channel().expect("ipc channel failure");
let context = Trusted::new(self);
let (task_source, canceller) = (
self.dom_manipulation_task_source(),
self.task_canceller(TaskSourceName::DOMManipulation),
);
let listener = BroadcastListener {
canceller,
task_source,
context,
};
ROUTER.add_typed_route(
broadcast_control_receiver,
Box::new(move |message| match message {
Ok(msg) => listener.handle(msg),
Err(err) => warn!("Error receiving a BroadcastMsg: {:?}", err),
}),
);
let router_id = BroadcastChannelRouterId::new();
*current_state = BroadcastChannelState::Managed(router_id, HashMap::new());
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::NewBroadcastChannelRouter(
router_id,
broadcast_control_sender,
self.origin().immutable().clone(),
));
}
if let BroadcastChannelState::Managed(router_id, channels) = &mut *current_state {
let entry = channels.entry(dom_channel.Name()).or_insert_with(|| {
let _ = self.script_to_constellation_chan().send(
ScriptMsg::NewBroadcastChannelNameInRouter(
*router_id,
dom_channel.Name().to_string(),
self.origin().immutable().clone(),
),
);
VecDeque::new()
});
entry.push_back(Dom::from_ref(dom_channel));
} else {
panic!("track_broadcast_channel should have first switched the state to managed.");
}
}
pub fn track_message_port(&self, dom_port: &MessagePort, port_impl: Option<MessagePortImpl>) {
let mut current_state = self.message_port_state.borrow_mut();
if let MessagePortState::UnManaged = &*current_state {
let (port_control_sender, port_control_receiver) =
ipc::channel().expect("ipc channel failure");
let context = Trusted::new(self);
let (task_source, canceller) = (
self.port_message_queue(),
self.task_canceller(TaskSourceName::PortMessage),
);
let listener = MessageListener {
canceller,
task_source,
context,
};
ROUTER.add_typed_route(
port_control_receiver,
Box::new(move |message| match message {
Ok(msg) => listener.notify(msg),
Err(err) => warn!("Error receiving a MessagePortMsg: {:?}", err),
}),
);
let router_id = MessagePortRouterId::new();
*current_state = MessagePortState::Managed(router_id, HashMapTracedValues::new());
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::NewMessagePortRouter(
router_id,
port_control_sender,
));
}
if let MessagePortState::Managed(router_id, message_ports) = &mut *current_state {
if let Some(port_impl) = port_impl {
message_ports.insert(
*dom_port.message_port_id(),
ManagedMessagePort {
port_impl: Some(port_impl),
dom_port: Dom::from_ref(dom_port),
pending: true,
closed: false,
},
);
let this = Trusted::new(self);
let _ = self.port_message_queue().queue(
task!(process_pending_port_messages: move || {
let target_global = this.root();
target_global.maybe_add_pending_ports();
}),
self,
);
} else {
let port_impl = MessagePortImpl::new(*dom_port.message_port_id());
message_ports.insert(
*dom_port.message_port_id(),
ManagedMessagePort {
port_impl: Some(port_impl),
dom_port: Dom::from_ref(dom_port),
pending: false,
closed: false,
},
);
let _ = self
.script_to_constellation_chan()
.send(ScriptMsg::NewMessagePort(
*router_id,
*dom_port.message_port_id(),
));
};
} else {
panic!("track_message_port should have first switched the state to managed.");
}
}
pub fn serialize_blob(&self, blob_id: &BlobId) -> BlobImpl {
let bytes = self
.get_blob_bytes(blob_id)
.expect("Could not read bytes from blob as part of serialization steps.");
let type_string = self.get_blob_type_string(blob_id);
BlobImpl::new_from_bytes(bytes, type_string)
}
fn track_blob_info(&self, blob_info: BlobInfo, blob_id: BlobId) {
let mut blob_state = self.blob_state.borrow_mut();
match &mut *blob_state {
BlobState::UnManaged => {
let mut blobs_map = HashMapTracedValues::new();
blobs_map.insert(blob_id, blob_info);
*blob_state = BlobState::Managed(blobs_map);
},
BlobState::Managed(blobs_map) => {
blobs_map.insert(blob_id, blob_info);
},
}
}
pub fn track_blob(&self, dom_blob: &Blob, blob_impl: BlobImpl) {
let blob_id = blob_impl.blob_id();
let blob_info = BlobInfo {
blob_impl,
tracker: BlobTracker::Blob(WeakRef::new(dom_blob)),
has_url: false,
};
self.track_blob_info(blob_info, blob_id);
}
pub fn track_file(&self, file: &File, blob_impl: BlobImpl) {
let blob_id = blob_impl.blob_id();
let blob_info = BlobInfo {
blob_impl,
tracker: BlobTracker::File(WeakRef::new(file)),
has_url: false,
};
self.track_blob_info(blob_info, blob_id);
}
fn perform_a_blob_garbage_collection_checkpoint(&self) {
let mut blob_state = self.blob_state.borrow_mut();
if let BlobState::Managed(blobs_map) = &mut *blob_state {
blobs_map.0.retain(|_id, blob_info| {
let garbage_collected = match &blob_info.tracker {
BlobTracker::File(weak) => weak.root().is_none(),
BlobTracker::Blob(weak) => weak.root().is_none(),
};
if garbage_collected && !blob_info.has_url {
if let BlobData::File(ref f) = blob_info.blob_impl.blob_data() {
self.decrement_file_ref(f.get_id());
}
false
} else {
true
}
});
if blobs_map.is_empty() {
*blob_state = BlobState::UnManaged;
}
}
}
pub fn clean_up_all_file_resources(&self) {
let mut blob_state = self.blob_state.borrow_mut();
if let BlobState::Managed(blobs_map) = &mut *blob_state {
blobs_map.drain().for_each(|(_id, blob_info)| {
if let BlobData::File(ref f) = blob_info.blob_impl.blob_data() {
self.decrement_file_ref(f.get_id());
}
});
}
*blob_state = BlobState::UnManaged;
}
fn decrement_file_ref(&self, id: Uuid) {
let origin = get_blob_origin(&self.get_url());
let (tx, rx) = profile_ipc::channel(self.time_profiler_chan().clone()).unwrap();
let msg = FileManagerThreadMsg::DecRef(id, origin, tx);
self.send_to_file_manager(msg);
let _ = rx.recv();
}
pub fn get_blob_bytes(&self, blob_id: &BlobId) -> Result<Vec<u8>, ()> {
let parent = {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_bytes for an unknown blob.");
match blob_info.blob_impl.blob_data() {
BlobData::Sliced(ref parent, ref rel_pos) => Some((*parent, rel_pos.clone())),
_ => None,
}
} else {
panic!("get_blob_bytes called on a global not managing any blobs.");
}
};
match parent {
Some((parent_id, rel_pos)) => self.get_blob_bytes_non_sliced(&parent_id).map(|v| {
let range = rel_pos.to_abs_range(v.len());
v.index(range).to_vec()
}),
None => self.get_blob_bytes_non_sliced(blob_id),
}
}
fn get_blob_bytes_non_sliced(&self, blob_id: &BlobId) -> Result<Vec<u8>, ()> {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_bytes_non_sliced called for a unknown blob.");
match blob_info.blob_impl.blob_data() {
BlobData::File(ref f) => {
let (buffer, is_new_buffer) = match f.get_cache() {
Some(bytes) => (bytes, false),
None => {
let bytes = self.read_file(f.get_id())?;
(bytes, true)
},
};
if is_new_buffer {
f.cache_bytes(buffer.clone());
}
Ok(buffer)
},
BlobData::Memory(ref s) => Ok(s.clone()),
BlobData::Sliced(_, _) => panic!("This blob doesn't have a parent."),
}
} else {
panic!("get_blob_bytes_non_sliced called on a global not managing any blobs.");
}
}
fn get_blob_bytes_or_file_id(&self, blob_id: &BlobId) -> BlobResult {
let parent = {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_bytes_or_file_id for an unknown blob.");
match blob_info.blob_impl.blob_data() {
BlobData::Sliced(ref parent, ref rel_pos) => Some((*parent, rel_pos.clone())),
_ => None,
}
} else {
panic!("get_blob_bytes_or_file_id called on a global not managing any blobs.");
}
};
match parent {
Some((parent_id, rel_pos)) => {
match self.get_blob_bytes_non_sliced_or_file_id(&parent_id) {
BlobResult::Bytes(bytes) => {
let range = rel_pos.to_abs_range(bytes.len());
BlobResult::Bytes(bytes.index(range).to_vec())
},
res => res,
}
},
None => self.get_blob_bytes_non_sliced_or_file_id(blob_id),
}
}
fn get_blob_bytes_non_sliced_or_file_id(&self, blob_id: &BlobId) -> BlobResult {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_bytes_non_sliced_or_file_id called for a unknown blob.");
match blob_info.blob_impl.blob_data() {
BlobData::File(ref f) => match f.get_cache() {
Some(bytes) => BlobResult::Bytes(bytes.clone()),
None => BlobResult::File(f.get_id(), f.get_size() as usize),
},
BlobData::Memory(ref s) => BlobResult::Bytes(s.clone()),
BlobData::Sliced(_, _) => panic!("This blob doesn't have a parent."),
}
} else {
panic!(
"get_blob_bytes_non_sliced_or_file_id called on a global not managing any blobs."
);
}
}
pub fn get_blob_type_string(&self, blob_id: &BlobId) -> String {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_type_string called for a unknown blob.");
blob_info.blob_impl.type_string()
} else {
panic!("get_blob_type_string called on a global not managing any blobs.");
}
}
pub fn get_blob_size(&self, blob_id: &BlobId) -> u64 {
let blob_state = self.blob_state.borrow();
if let BlobState::Managed(blobs_map) = &*blob_state {
let parent = {
let blob_info = blobs_map
.get(blob_id)
.expect("get_blob_size called for a unknown blob.");
match blob_info.blob_impl.blob_data() {
BlobData::Sliced(ref parent, ref rel_pos) => Some((*parent, rel_pos.clone())),
_ => None,
}
};
match parent {
Some((parent_id, rel_pos)) => {
let parent_info = blobs_map
.get(&parent_id)
.expect("Parent of blob whose size is unknown.");
let parent_size = match parent_info.blob_impl.blob_data() {
BlobData::File(ref f) => f.get_size(),
BlobData::Memory(ref v) => v.len() as u64,
BlobData::Sliced(_, _) => panic!("Blob ancestry should be only one level."),
};
rel_pos.to_abs_range(parent_size as usize).len() as u64
},
None => {
let blob_info = blobs_map.get(blob_id).expect("Blob whose size is unknown.");
match blob_info.blob_impl.blob_data() {
BlobData::File(ref f) => f.get_size(),
BlobData::Memory(ref v) => v.len() as u64,
BlobData::Sliced(_, _) => panic!(
"It was previously checked that this blob does not have a parent."
),
}
},
}
} else {
panic!("get_blob_size called on a global not managing any blobs.");
}
}
pub fn get_blob_url_id(&self, blob_id: &BlobId) -> Uuid {
let mut blob_state = self.blob_state.borrow_mut();
if let BlobState::Managed(blobs_map) = &mut *blob_state {
let parent = {
let blob_info = blobs_map
.get_mut(blob_id)
.expect("get_blob_url_id called for a unknown blob.");
blob_info.has_url = true;
match blob_info.blob_impl.blob_data() {
BlobData::Sliced(ref parent, ref rel_pos) => Some((*parent, rel_pos.clone())),
_ => None,
}
};
match parent {
Some((parent_id, rel_pos)) => {
let parent_info = blobs_map
.get_mut(&parent_id)
.expect("Parent of blob whose url is requested is unknown.");
let parent_file_id = self.promote(parent_info, false);
let parent_size = match parent_info.blob_impl.blob_data() {
BlobData::File(ref f) => f.get_size(),
BlobData::Memory(ref v) => v.len() as u64,
BlobData::Sliced(_, _) => panic!("Blob ancestry should be only one level."),
};
let parent_size = rel_pos.to_abs_range(parent_size as usize).len() as u64;
let blob_info = blobs_map
.get_mut(blob_id)
.expect("Blob whose url is requested is unknown.");
self.create_sliced_url_id(blob_info, &parent_file_id, &rel_pos, parent_size)
},
None => {
let blob_info = blobs_map
.get_mut(blob_id)
.expect("Blob whose url is requested is unknown.");
self.promote(blob_info, true)
},
}
} else {
panic!("get_blob_url_id called on a global not managing any blobs.");
}
}
fn create_sliced_url_id(
&self,
blob_info: &mut BlobInfo,
parent_file_id: &Uuid,
rel_pos: &RelativePos,
parent_len: u64,
) -> Uuid {
let origin = get_blob_origin(&self.get_url());
let (tx, rx) = profile_ipc::channel(self.time_profiler_chan().clone()).unwrap();
let msg = FileManagerThreadMsg::AddSlicedURLEntry(
*parent_file_id,
rel_pos.clone(),
tx,
origin.clone(),
);
self.send_to_file_manager(msg);
match rx.recv().expect("File manager thread is down.") {
Ok(new_id) => {
*blob_info.blob_impl.blob_data_mut() = BlobData::File(FileBlob::new(
new_id,
None,
None,
rel_pos.to_abs_range(parent_len as usize).len() as u64,
));
new_id
},
Err(_) => {
Uuid::new_v4()
},
}
}
pub fn promote(&self, blob_info: &mut BlobInfo, set_valid: bool) -> Uuid {
let mut bytes = vec![];
let global_url = self.get_url();
match blob_info.blob_impl.blob_data_mut() {
BlobData::Sliced(_, _) => {
panic!("Sliced blobs should use create_sliced_url_id instead of promote.");
},
BlobData::File(ref f) => {
if set_valid {
let origin = get_blob_origin(&global_url);
let (tx, rx) = profile_ipc::channel(self.time_profiler_chan().clone()).unwrap();
let msg = FileManagerThreadMsg::ActivateBlobURL(f.get_id(), tx, origin.clone());
self.send_to_file_manager(msg);
match rx.recv().unwrap() {
Ok(_) => return f.get_id(),
Err(_) => return Uuid::new_v4(),
}
} else {
return f.get_id();
}
},
BlobData::Memory(ref mut bytes_in) => mem::swap(bytes_in, &mut bytes),
};
let origin = get_blob_origin(&global_url);
let blob_buf = BlobBuf {
filename: None,
type_string: blob_info.blob_impl.type_string(),
size: bytes.len() as u64,
bytes: bytes.to_vec(),
};
let id = Uuid::new_v4();
let msg = FileManagerThreadMsg::PromoteMemory(id, blob_buf, set_valid, origin.clone());
self.send_to_file_manager(msg);
*blob_info.blob_impl.blob_data_mut() = BlobData::File(FileBlob::new(
id,
None,
Some(bytes.to_vec()),
bytes.len() as u64,
));
id
}
fn send_to_file_manager(&self, msg: FileManagerThreadMsg) {
let resource_threads = self.resource_threads();
let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));
}
fn read_file(&self, id: Uuid) -> Result<Vec<u8>, ()> {
let recv = self.send_msg(id);
GlobalScope::read_msg(recv)
}
pub fn get_blob_stream(&self, blob_id: &BlobId, can_gc: CanGc) -> DomRoot<ReadableStream> {
let (file_id, size) = match self.get_blob_bytes_or_file_id(blob_id) {
BlobResult::Bytes(bytes) => {
let stream = ReadableStream::new_from_bytes(self, bytes, can_gc);
return stream;
},
BlobResult::File(id, size) => (id, size),
};
let stream = ReadableStream::new_with_external_underlying_source(
self,
UnderlyingSourceType::Blob(size),
can_gc,
);
let recv = self.send_msg(file_id);
let trusted_stream = Trusted::new(&*stream.clone());
let task_canceller = self.task_canceller(TaskSourceName::FileReading);
let task_source = self.file_reading_task_source();
let mut file_listener = FileListener {
state: Some(FileListenerState::Empty(FileListenerTarget::Stream(
trusted_stream,
))),
task_source,
task_canceller,
};
ROUTER.add_typed_route(
recv.to_ipc_receiver(),
Box::new(move |msg| {
file_listener.handle(msg.expect("Deserialization of file listener msg failed."));
}),
);
stream
}
pub fn read_file_async(&self, id: Uuid, promise: Rc<Promise>, callback: FileListenerCallback) {
let recv = self.send_msg(id);
let trusted_promise = TrustedPromise::new(promise);
let task_canceller = self.task_canceller(TaskSourceName::FileReading);
let task_source = self.file_reading_task_source();
let mut file_listener = FileListener {
state: Some(FileListenerState::Empty(FileListenerTarget::Promise(
trusted_promise,
callback,
))),
task_source,
task_canceller,
};
ROUTER.add_typed_route(
recv.to_ipc_receiver(),
Box::new(move |msg| {
file_listener.handle(msg.expect("Deserialization of file listener msg failed."));
}),
);
}
fn send_msg(&self, id: Uuid) -> profile_ipc::IpcReceiver<FileManagerResult<ReadFileProgress>> {
let resource_threads = self.resource_threads();
let (chan, recv) = profile_ipc::channel(self.time_profiler_chan().clone()).unwrap();
let origin = get_blob_origin(&self.get_url());
let msg = FileManagerThreadMsg::ReadFile(chan, id, origin);
let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));
recv
}
fn read_msg(
receiver: profile_ipc::IpcReceiver<FileManagerResult<ReadFileProgress>>,
) -> Result<Vec<u8>, ()> {
let mut bytes = vec![];
loop {
match receiver.recv().unwrap() {
Ok(ReadFileProgress::Meta(mut blob_buf)) => {
bytes.append(&mut blob_buf.bytes);
},
Ok(ReadFileProgress::Partial(mut bytes_in)) => {
bytes.append(&mut bytes_in);
},
Ok(ReadFileProgress::EOF) => {
return Ok(bytes);
},
Err(_) => return Err(()),
}
}
}
pub fn permission_state_invocation_results(
&self,
) -> &DomRefCell<HashMap<String, PermissionState>> {
&self.permission_state_invocation_results
}
pub fn track_worker(
&self,
closing: Arc<AtomicBool>,
join_handle: JoinHandle<()>,
control_sender: Sender<DedicatedWorkerControlMsg>,
context: ThreadSafeJSContext,
) {
self.list_auto_close_worker
.borrow_mut()
.push(AutoCloseWorker {
closing,
join_handle: Some(join_handle),
control_sender,
context,
});
}
pub fn track_event_source(&self, event_source: &EventSource) {
self.event_source_tracker.track(event_source);
}
pub fn close_event_sources(&self) -> bool {
let mut canceled_any_fetch = false;
self.event_source_tracker
.for_each(
|event_source: DomRoot<EventSource>| match event_source.ReadyState() {
2 => {},
_ => {
event_source.cancel();
canceled_any_fetch = true;
},
},
);
canceled_any_fetch
}
#[allow(unsafe_code)]
pub fn from_reflector<T: DomObject>(reflector: &T, _realm: &AlreadyInRealm) -> DomRoot<Self> {
unsafe { GlobalScope::from_object(*reflector.reflector().get_jsobject()) }
}
#[allow(unsafe_code)]
pub unsafe fn from_object(obj: *mut JSObject) -> DomRoot<Self> {
assert!(!obj.is_null());
let global = GetNonCCWObjectGlobal(obj);
global_scope_from_global_static(global)
}
#[allow(unsafe_code)]
pub unsafe fn from_context(cx: *mut JSContext, _realm: InRealm) -> DomRoot<Self> {
let global = CurrentGlobalOrNull(cx);
assert!(!global.is_null());
global_scope_from_global(global, cx)
}
#[allow(unsafe_code)]
pub fn from_safe_context(cx: SafeJSContext, realm: InRealm) -> DomRoot<Self> {
unsafe { Self::from_context(*cx, realm) }
}
#[allow(unsafe_code)]
pub unsafe fn from_object_maybe_wrapped(
mut obj: *mut JSObject,
cx: *mut JSContext,
) -> DomRoot<Self> {
if IsWrapper(obj) {
obj = UnwrapObjectDynamic(obj, cx, false);
assert!(!obj.is_null());
}
GlobalScope::from_object(obj)
}
pub fn add_uncaught_rejection(&self, rejection: HandleObject) {
self.uncaught_rejections
.borrow_mut()
.push(Heap::boxed(rejection.get()));
}
pub fn remove_uncaught_rejection(&self, rejection: HandleObject) {
let mut uncaught_rejections = self.uncaught_rejections.borrow_mut();
if let Some(index) = uncaught_rejections
.iter()
.position(|promise| *promise == Heap::boxed(rejection.get()))
{
uncaught_rejections.remove(index);
}
}
#[allow(clippy::vec_box)]
pub fn get_uncaught_rejections(&self) -> &DomRefCell<Vec<Box<Heap<*mut JSObject>>>> {
&self.uncaught_rejections
}
pub fn add_consumed_rejection(&self, rejection: HandleObject) {
self.consumed_rejections
.borrow_mut()
.push(Heap::boxed(rejection.get()));
}
pub fn remove_consumed_rejection(&self, rejection: HandleObject) {
let mut consumed_rejections = self.consumed_rejections.borrow_mut();
if let Some(index) = consumed_rejections
.iter()
.position(|promise| *promise == Heap::boxed(rejection.get()))
{
consumed_rejections.remove(index);
}
}
#[allow(clippy::vec_box)]
pub fn get_consumed_rejections(&self) -> &DomRefCell<Vec<Box<Heap<*mut JSObject>>>> {
&self.consumed_rejections
}
pub fn set_module_map(&self, url: ServoUrl, module: ModuleTree) {
self.module_map.borrow_mut().insert(url, Rc::new(module));
}
pub fn get_module_map(&self) -> &DomRefCell<HashMapTracedValues<ServoUrl, Rc<ModuleTree>>> {
&self.module_map
}
pub fn set_inline_module_map(&self, script_id: ScriptId, module: ModuleTree) {
self.inline_module_map
.borrow_mut()
.insert(script_id, Rc::new(module));
}
pub fn get_inline_module_map(&self) -> &DomRefCell<HashMap<ScriptId, Rc<ModuleTree>>> {
&self.inline_module_map
}
#[allow(unsafe_code)]
pub fn get_cx() -> SafeJSContext {
unsafe { SafeJSContext::from_ptr(Runtime::get()) }
}
pub fn crypto(&self) -> DomRoot<Crypto> {
self.crypto.or_init(|| Crypto::new(self))
}
pub fn live_devtools_updates(&self) -> bool {
self.devtools_wants_updates.get()
}
pub fn set_devtools_wants_updates(&self, value: bool) {
self.devtools_wants_updates.set(value);
}
pub fn time(&self, label: DOMString) -> Result<(), ()> {
let mut timers = self.console_timers.borrow_mut();
if timers.len() >= 10000 {
return Err(());
}
match timers.entry(label) {
Entry::Vacant(entry) => {
entry.insert(Instant::now());
Ok(())
},
Entry::Occupied(_) => Err(()),
}
}
pub fn time_log(&self, label: &str) -> Result<u64, ()> {
self.console_timers
.borrow()
.get(label)
.ok_or(())
.map(|&start| (Instant::now() - start).as_millis() as u64)
}
pub fn time_end(&self, label: &str) -> Result<u64, ()> {
self.console_timers
.borrow_mut()
.remove(label)
.ok_or(())
.map(|start| (Instant::now() - start).as_millis() as u64)
}
pub fn devtools_chan(&self) -> Option<&IpcSender<ScriptToDevtoolsControlMsg>> {
self.devtools_chan.as_ref()
}
pub fn issue_page_warning(&self, warning: &str) {
if let Some(ref chan) = self.devtools_chan {
let _ = chan.send(ScriptToDevtoolsControlMsg::ReportPageError(
self.pipeline_id,
PageError {
type_: "PageError".to_string(),
error_message: warning.to_string(),
source_name: self.get_url().to_string(),
line_text: "".to_string(),
line_number: 0,
column_number: 0,
category: "script".to_string(),
time_stamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
error: false,
warning: true,
exception: true,
strict: false,
private: false,
},
));
}
}
pub fn mem_profiler_chan(&self) -> &profile_mem::ProfilerChan {
&self.mem_profiler_chan
}
pub fn time_profiler_chan(&self) -> &profile_time::ProfilerChan {
&self.time_profiler_chan
}
pub fn script_to_constellation_chan(&self) -> &ScriptToConstellationChan {
&self.script_to_constellation_chan
}
pub fn send_to_embedder(&self, msg: EmbedderMsg) {
self.send_to_constellation(ScriptMsg::ForwardToEmbedder(msg));
}
pub fn send_to_constellation(&self, msg: ScriptMsg) {
self.script_to_constellation_chan().send(msg).unwrap();
}
pub fn scheduler_chan(&self) -> &IpcSender<TimerSchedulerMsg> {
&self.scheduler_chan
}
pub fn pipeline_id(&self) -> PipelineId {
self.pipeline_id
}
pub fn origin(&self) -> &MutableOrigin {
&self.origin
}
pub fn creation_url(&self) -> &Option<ServoUrl> {
&self.creation_url
}
pub fn image_cache(&self) -> Arc<dyn ImageCache> {
if let Some(window) = self.downcast::<Window>() {
return window.image_cache();
}
if let Some(worker) = self.downcast::<DedicatedWorkerGlobalScope>() {
return worker.image_cache();
}
if let Some(worker) = self.downcast::<PaintWorkletGlobalScope>() {
return worker.image_cache();
}
unreachable!();
}
pub fn policy_container(&self) -> PolicyContainer {
if let Some(window) = self.downcast::<Window>() {
return window.Document().policy_container().to_owned();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.policy_container().to_owned();
}
unreachable!();
}
pub fn api_base_url(&self) -> ServoUrl {
if let Some(window) = self.downcast::<Window>() {
return window.Document().base_url();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.get_url().clone();
}
if let Some(worklet) = self.downcast::<WorkletGlobalScope>() {
return worklet.base_url();
}
unreachable!();
}
pub fn get_url(&self) -> ServoUrl {
if let Some(window) = self.downcast::<Window>() {
return window.get_url();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.get_url().clone();
}
if let Some(worklet) = self.downcast::<WorkletGlobalScope>() {
return worklet.base_url();
}
unreachable!();
}
pub fn get_referrer_policy(&self) -> ReferrerPolicy {
if let Some(window) = self.downcast::<Window>() {
let document = window.Document();
return document.get_referrer_policy();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
let policy_container = worker.policy_container().to_owned();
return policy_container.get_referrer_policy();
}
unreachable!();
}
pub fn get_referrer(&self) -> Referrer {
if let Some(window) = self.downcast::<Window>() {
let mut document = window.Document();
if let ImmutableOrigin::Opaque(_) = document.origin().immutable() {
return Referrer::NoReferrer;
}
let mut url = document.url();
while url.as_str() == "about:srcdoc" {
let Some(parent_document) =
document.browsing_context().and_then(|browsing_context| {
browsing_context
.parent()
.and_then(|parent| parent.document())
})
else {
return Referrer::NoReferrer;
};
document = parent_document;
url = document.url();
}
Referrer::Client(url)
} else {
Referrer::Client(self.get_url())
}
}
pub fn as_window(&self) -> &Window {
self.downcast::<Window>().expect("expected a Window scope")
}
pub fn report_an_error(&self, error_info: ErrorInfo, value: HandleValue, can_gc: CanGc) {
if self.in_error_reporting_mode.get() {
return;
}
self.in_error_reporting_mode.set(true);
let event = ErrorEvent::new(
self,
atom!("error"),
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable,
error_info.message.as_str().into(),
error_info.filename.as_str().into(),
error_info.lineno,
error_info.column,
value,
can_gc,
);
let event_status = event
.upcast::<Event>()
.fire(self.upcast::<EventTarget>(), can_gc);
self.in_error_reporting_mode.set(false);
if event_status == EventStatus::NotCanceled {
if let Some(dedicated) = self.downcast::<DedicatedWorkerGlobalScope>() {
dedicated.forward_error_to_worker_object(error_info);
} else if self.is::<Window>() {
if let Some(ref chan) = self.devtools_chan {
let _ = chan.send(ScriptToDevtoolsControlMsg::ReportPageError(
self.pipeline_id,
PageError {
type_: "PageError".to_string(),
error_message: error_info.message.clone(),
source_name: error_info.filename.clone(),
line_text: "".to_string(), line_number: error_info.lineno,
column_number: error_info.column,
category: "script".to_string(),
time_stamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64,
error: true,
warning: false,
exception: true,
strict: false,
private: false,
},
));
}
}
}
}
pub fn resource_threads(&self) -> &ResourceThreads {
&self.resource_threads
}
pub fn core_resource_thread(&self) -> CoreResourceThread {
self.resource_threads().sender()
}
pub fn script_chan(&self) -> Box<dyn ScriptChan + Send> {
if let Some(window) = self.downcast::<Window>() {
return MainThreadScriptChan(window.main_thread_script_chan().clone()).clone();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.script_chan();
}
unreachable!();
}
pub fn gamepad_task_source(&self) -> GamepadTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().gamepad_task_source();
}
unreachable!();
}
pub fn networking_task_source(&self) -> NetworkingTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().networking_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.networking_task_source();
}
unreachable!();
}
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!();
}
pub fn timer_task_source(&self) -> TimerTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().timer_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.timer_task_source();
}
unreachable!();
}
pub fn remote_event_task_source(&self) -> RemoteEventTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().remote_event_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.remote_event_task_source();
}
unreachable!();
}
pub fn websocket_task_source(&self) -> WebsocketTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().websocket_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.websocket_task_source();
}
unreachable!();
}
pub fn evaluate_js_on_global_with_result(
&self,
code: &str,
rval: MutableHandleValue,
fetch_options: ScriptFetchOptions,
script_base_url: ServoUrl,
can_gc: CanGc,
) -> bool {
let source_code = SourceCode::Text(Rc::new(DOMString::from_string((*code).to_string())));
self.evaluate_script_on_global_with_result(
&source_code,
"",
rval,
1,
fetch_options,
script_base_url,
can_gc,
)
}
#[allow(unsafe_code)]
#[allow(clippy::too_many_arguments)]
pub fn evaluate_script_on_global_with_result(
&self,
code: &SourceCode,
filename: &str,
rval: MutableHandleValue,
line_number: u32,
fetch_options: ScriptFetchOptions,
script_base_url: ServoUrl,
can_gc: CanGc,
) -> bool {
let cx = GlobalScope::get_cx();
let ar = enter_realm(self);
let _aes = AutoEntryScript::new(self);
unsafe {
rooted!(in(*cx) let mut compiled_script = std::ptr::null_mut::<JSScript>());
match code {
SourceCode::Text(text_code) => {
let options = CompileOptionsWrapper::new(*cx, filename, line_number);
debug!("compiling dom string");
compiled_script.set(Compile1(
*cx,
options.ptr,
&mut transform_str_to_source_text(text_code),
));
if compiled_script.is_null() {
debug!("error compiling Dom string");
report_pending_exception(*cx, true, InRealm::Entered(&ar), can_gc);
return false;
}
},
SourceCode::Compiled(pre_compiled_script) => {
let options = InstantiateOptions {
skipFilenameValidation: false,
hideScriptFromDebugger: false,
deferDebugMetadata: false,
};
let script = InstantiateGlobalStencil(
*cx,
&options,
*pre_compiled_script.source_code,
ptr::null_mut(),
);
compiled_script.set(script);
},
};
assert!(!compiled_script.is_null());
rooted!(in(*cx) let mut script_private = UndefinedValue());
JS_GetScriptPrivate(*compiled_script, script_private.handle_mut());
if script_private.is_undefined() {
debug!("Set script private for {}", script_base_url);
let module_script_data = Rc::new(ModuleScript::new(
script_base_url,
fetch_options,
None,
));
SetScriptPrivate(
*compiled_script,
&PrivateValue(Rc::into_raw(module_script_data) as *const _),
);
}
let result = JS_ExecuteScript(*cx, compiled_script.handle(), rval);
if !result {
debug!("error evaluating Dom string");
report_pending_exception(*cx, true, InRealm::Entered(&ar), can_gc);
}
maybe_resume_unwind();
result
}
}
pub fn schedule_callback(
&self,
callback: OneshotTimerCallback,
duration: Duration,
) -> OneshotTimerHandle {
self.setup_timers();
self.timers
.schedule_callback(callback, duration, self.timer_source())
}
pub fn unschedule_callback(&self, handle: OneshotTimerHandle) {
self.timers.unschedule_callback(handle);
}
pub fn set_timeout_or_interval(
&self,
callback: TimerCallback,
arguments: Vec<HandleValue>,
timeout: Duration,
is_interval: IsInterval,
) -> i32 {
self.setup_timers();
self.timers.set_timeout_or_interval(
self,
callback,
arguments,
timeout,
is_interval,
self.timer_source(),
)
}
pub fn clear_timeout_or_interval(&self, handle: i32) {
self.timers.clear_timeout_or_interval(self, handle);
}
pub fn queue_function_as_microtask(&self, callback: Rc<VoidFunction>) {
self.enqueue_microtask(Microtask::User(UserMicrotask {
callback,
pipeline: self.pipeline_id(),
}))
}
#[allow(unsafe_code)]
pub fn is_js_evaluation_allowed(&self, cx: SafeJSContext) -> bool {
let Some(csp_list) = self.get_csp_list() else {
return true;
};
let scripted_caller = unsafe { describe_scripted_caller(*cx) }.unwrap_or_default();
let is_js_evaluation_allowed = csp_list.is_js_evaluation_allowed() == CheckResult::Allowed;
if !is_js_evaluation_allowed {
for policy in csp_list.0 {
let task = CSPViolationReporter::new(
self,
None,
policy.disposition == PolicyDisposition::Report,
RuntimeCode::JS,
scripted_caller.filename.clone(),
scripted_caller.line,
scripted_caller.col,
);
self.dom_manipulation_task_source()
.queue(task, self)
.unwrap();
}
}
is_js_evaluation_allowed
}
pub fn create_image_bitmap(
&self,
image: ImageBitmapSource,
options: &ImageBitmapOptions,
can_gc: CanGc,
) -> Rc<Promise> {
let in_realm_proof = AlreadyInRealm::assert();
let p = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
if options.resizeWidth.is_some_and(|w| w == 0) {
p.reject_error(Error::InvalidState);
return p;
}
if options.resizeHeight.is_some_and(|w| w == 0) {
p.reject_error(Error::InvalidState);
return p;
}
match image {
ImageBitmapSource::HTMLCanvasElement(ref canvas) => {
if !canvas.is_valid() {
p.reject_error(Error::InvalidState);
return p;
}
if let Some((data, size)) = canvas.fetch_all_data() {
let data = data
.map(|data| data.to_vec())
.unwrap_or_else(|| vec![0; size.area() as usize * 4]);
let image_bitmap = ImageBitmap::new(self, size.width, size.height).unwrap();
image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
ImageBitmapSource::OffscreenCanvas(ref canvas) => {
if !canvas.is_valid() {
p.reject_error(Error::InvalidState);
return p;
}
if let Some((data, size)) = canvas.fetch_all_data() {
let data = data
.map(|data| data.to_vec())
.unwrap_or_else(|| vec![0; size.area() as usize * 4]);
let image_bitmap = ImageBitmap::new(self, size.width, size.height).unwrap();
image_bitmap.set_bitmap_data(data);
image_bitmap.set_origin_clean(canvas.origin_is_clean());
p.resolve_native(&(image_bitmap));
}
p
},
_ => {
p.reject_error(Error::NotSupported);
p
},
}
}
pub fn fire_timer(&self, handle: TimerEventId, can_gc: CanGc) {
self.timers.fire_timer(handle, self, can_gc);
}
pub fn resume(&self) {
self.timers.resume();
}
pub fn suspend(&self) {
self.timers.suspend();
}
pub fn slow_down_timers(&self) {
self.timers.slow_down();
}
pub fn speed_up_timers(&self) {
self.timers.speed_up();
}
fn timer_source(&self) -> TimerSource {
if self.is::<Window>() {
return TimerSource::FromWindow(self.pipeline_id());
}
if self.is::<WorkerGlobalScope>() {
return TimerSource::FromWorker;
}
unreachable!();
}
pub fn can_continue_running(&self) -> bool {
if self.is::<Window>() {
return ScriptThread::can_continue_running();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return !worker.is_closing();
}
true
}
pub fn task_canceller(&self, name: TaskSourceName) -> TaskCanceller {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().task_canceller(name);
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.task_canceller();
}
unreachable!();
}
pub fn perform_a_microtask_checkpoint(&self, can_gc: CanGc) {
if self.can_continue_running() {
self.microtask_queue.checkpoint(
GlobalScope::get_cx(),
|_| Some(DomRoot::from_ref(self)),
vec![DomRoot::from_ref(self)],
can_gc,
);
}
}
pub fn enqueue_microtask(&self, job: Microtask) {
self.microtask_queue.enqueue(job, GlobalScope::get_cx());
}
pub fn new_script_pair(&self) -> (Box<dyn ScriptChan + Send>, Box<dyn ScriptPort + Send>) {
if let Some(window) = self.downcast::<Window>() {
return window.new_script_pair();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.new_script_pair();
}
unreachable!();
}
pub fn microtask_queue(&self) -> &Rc<MicrotaskQueue> {
&self.microtask_queue
}
pub fn process_event(&self, msg: CommonScriptMsg) -> bool {
if self.is::<Window>() {
return ScriptThread::process_event(msg);
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.process_event(msg);
}
unreachable!();
}
pub fn dom_manipulation_task_source(&self) -> DOMManipulationTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().dom_manipulation_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.dom_manipulation_task_source();
}
unreachable!();
}
pub fn file_reading_task_source(&self) -> FileReadingTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().file_reading_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.file_reading_task_source();
}
unreachable!();
}
pub fn runtime_handle(&self) -> ParentRuntime {
if self.is::<Window>() {
ScriptThread::runtime_handle()
} else if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
worker.runtime_handle()
} else {
unreachable!()
}
}
#[allow(unsafe_code)]
pub fn current() -> Option<DomRoot<Self>> {
unsafe {
let cx = Runtime::get();
assert!(!cx.is_null());
let global = CurrentGlobalOrNull(cx);
if global.is_null() {
None
} else {
Some(global_scope_from_global(global, cx))
}
}
}
pub fn entry() -> DomRoot<Self> {
entry_global()
}
pub fn incumbent() -> Option<DomRoot<Self>> {
incumbent_global()
}
pub fn performance(&self) -> DomRoot<Performance> {
if let Some(window) = self.downcast::<Window>() {
return window.Performance();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.Performance();
}
unreachable!();
}
pub fn performance_timeline_task_source(&self) -> PerformanceTimelineTaskSource {
if let Some(window) = self.downcast::<Window>() {
return window.task_manager().performance_timeline_task_source();
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.performance_timeline_task_source();
}
unreachable!();
}
pub fn supported_performance_entry_types(&self, cx: SafeJSContext, retval: MutableHandleValue) {
self.frozen_supported_performance_entry_types.get_or_init(
|| {
VALID_ENTRY_TYPES
.iter()
.map(|t| DOMString::from(t.to_string()))
.collect()
},
cx,
retval,
);
}
pub fn is_headless(&self) -> bool {
self.is_headless
}
pub fn get_user_agent(&self) -> Cow<'static, str> {
self.user_agent.clone()
}
pub fn get_https_state(&self) -> HttpsState {
self.https_state.get()
}
pub fn set_https_state(&self, https_state: HttpsState) {
self.https_state.set(https_state);
}
pub fn is_secure_context(&self) -> bool {
if Some(false) == self.inherited_secure_context {
return false;
}
if let Some(creation_url) = self.creation_url() {
if creation_url.scheme() == "blob" && Some(true) == self.inherited_secure_context {
return true;
}
return creation_url.is_potentially_trustworthy();
}
false
}
pub fn get_csp_list(&self) -> Option<CspList> {
if self.downcast::<Window>().is_some() {
return self.policy_container().csp_list;
}
None
}
pub fn status_code(&self) -> Option<u16> {
if let Some(window) = self.downcast::<Window>() {
return window.Document().status_code();
}
None
}
#[cfg(feature = "webgpu")]
pub fn wgpu_id_hub(&self) -> Arc<IdentityHub> {
self.gpu_id_hub.clone()
}
#[cfg(feature = "webgpu")]
pub fn add_gpu_device(&self, device: &GPUDevice) {
self.gpu_devices
.borrow_mut()
.insert(device.id(), WeakRef::new(device));
}
#[cfg(feature = "webgpu")]
pub fn remove_gpu_device(&self, device: WebGPUDevice) {
let device = self
.gpu_devices
.borrow_mut()
.remove(&device)
.expect("GPUDevice should still be in devices hashmap");
assert!(device.root().is_none())
}
#[cfg(feature = "webgpu")]
pub fn gpu_device_lost(&self, device: WebGPUDevice, reason: DeviceLostReason, msg: String) {
let reason = match reason {
DeviceLostReason::Unknown => GPUDeviceLostReason::Unknown,
DeviceLostReason::Destroyed => GPUDeviceLostReason::Destroyed,
};
let _ac = enter_realm(self);
if let Some(device) = self
.gpu_devices
.borrow_mut()
.get_mut(&device)
.expect("GPUDevice should still be in devices hashmap")
.root()
{
device.lose(reason, msg);
}
}
#[cfg(feature = "webgpu")]
pub fn handle_uncaptured_gpu_error(
&self,
device: WebGPUDevice,
error: webgpu::Error,
can_gc: CanGc,
) {
if let Some(gpu_device) = self
.gpu_devices
.borrow()
.get(&device)
.and_then(|device| device.root())
{
gpu_device.fire_uncaptured_error(error, can_gc);
} else {
warn!("Recived error for lost GPUDevice!")
}
}
pub fn handle_gamepad_event(&self, gamepad_event: GamepadEvent) {
match gamepad_event {
GamepadEvent::Connected(index, name, bounds, supported_haptic_effects) => {
self.handle_gamepad_connect(
index.0,
name,
bounds.axis_bounds,
bounds.button_bounds,
supported_haptic_effects,
);
},
GamepadEvent::Disconnected(index) => {
self.handle_gamepad_disconnect(index.0);
},
GamepadEvent::Updated(index, update_type) => {
self.receive_new_gamepad_button_or_axis(index.0, update_type);
},
};
}
fn handle_gamepad_connect(
&self,
_index: usize,
name: String,
axis_bounds: (f64, f64),
button_bounds: (f64, f64),
supported_haptic_effects: GamepadSupportedHapticEffects,
) {
let this = Trusted::new(self);
self.gamepad_task_source()
.queue_with_canceller(
task!(gamepad_connected: move || {
let global = this.root();
if let Some(window) = global.downcast::<Window>() {
let navigator = window.Navigator();
let selected_index = navigator.select_gamepad_index();
let gamepad = Gamepad::new(
&global,
selected_index,
name,
"standard".into(),
axis_bounds,
button_bounds,
supported_haptic_effects,
false,
CanGc::note(),
);
navigator.set_gamepad(selected_index as usize, &gamepad, CanGc::note());
}
}),
&self.task_canceller(TaskSourceName::Gamepad),
)
.expect("Failed to queue gamepad connected task.");
}
pub fn handle_gamepad_disconnect(&self, index: usize) {
let this = Trusted::new(self);
self.gamepad_task_source()
.queue_with_canceller(
task!(gamepad_disconnected: move || {
let global = this.root();
if let Some(window) = global.downcast::<Window>() {
let navigator = window.Navigator();
if let Some(gamepad) = navigator.get_gamepad(index) {
if window.Document().is_fully_active() {
gamepad.update_connected(false, gamepad.exposed(), CanGc::note());
navigator.remove_gamepad(index);
}
}
}
}),
&self.task_canceller(TaskSourceName::Gamepad),
)
.expect("Failed to queue gamepad disconnected task.");
}
pub fn receive_new_gamepad_button_or_axis(&self, index: usize, update_type: GamepadUpdateType) {
let this = Trusted::new(self);
self.gamepad_task_source()
.queue_with_canceller(
task!(update_gamepad_state: move || {
let global = this.root();
if let Some(window) = global.downcast::<Window>() {
let navigator = window.Navigator();
if let Some(gamepad) = navigator.get_gamepad(index) {
let current_time = global.performance().Now();
gamepad.update_timestamp(*current_time);
match update_type {
GamepadUpdateType::Axis(index, value) => {
gamepad.map_and_normalize_axes(index, value);
},
GamepadUpdateType::Button(index, value) => {
gamepad.map_and_normalize_buttons(index, value);
}
};
if !navigator.has_gamepad_gesture() && contains_user_gesture(update_type) {
navigator.set_has_gamepad_gesture(true);
navigator.GetGamepads()
.iter()
.filter_map(|g| g.as_ref())
.for_each(|gamepad| {
gamepad.set_exposed(true);
gamepad.update_timestamp(*current_time);
let new_gamepad = Trusted::new(&**gamepad);
if window.Document().is_fully_active() {
window.task_manager().gamepad_task_source().queue_with_canceller(
task!(update_gamepad_connect: move || {
let gamepad = new_gamepad.root();
gamepad.notify_event(GamepadEventType::Connected, CanGc::note());
}),
&window.upcast::<GlobalScope>()
.task_canceller(TaskSourceName::Gamepad),
)
.expect("Failed to queue update gamepad connect task.");
}
});
}
}
}
}),
&self.task_canceller(TaskSourceName::Gamepad),
)
.expect("Failed to queue update gamepad state task.");
}
pub(crate) fn current_group_label(&self) -> Option<DOMString> {
self.console_group_stack
.borrow()
.last()
.map(|label| DOMString::from(format!("[{}]", label)))
}
pub(crate) fn push_console_group(&self, group: DOMString) {
self.console_group_stack.borrow_mut().push(group);
}
pub(crate) fn pop_console_group(&self) {
let _ = self.console_group_stack.borrow_mut().pop();
}
pub(crate) fn increment_console_count(&self, label: &DOMString) -> usize {
*self
.console_count_map
.borrow_mut()
.entry(label.clone())
.and_modify(|e| *e += 1)
.or_insert(1)
}
pub(crate) fn reset_console_count(&self, label: &DOMString) -> Result<(), ()> {
match self.console_count_map.borrow_mut().get_mut(label) {
Some(value) => {
*value = 0;
Ok(())
},
None => Err(()),
}
}
pub(crate) fn dynamic_module_list(&self) -> RefMut<DynamicModuleList> {
self.dynamic_modules.borrow_mut()
}
pub(crate) fn structured_clone(
&self,
cx: SafeJSContext,
value: HandleValue,
options: RootedTraceableBox<StructuredSerializeOptions>,
retval: MutableHandleValue,
) -> Fallible<()> {
let mut rooted = CustomAutoRooter::new(
options
.transfer
.iter()
.map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
.collect(),
);
let guard = CustomAutoRooterGuard::new(*cx, &mut rooted);
let data = structuredclone::write(cx, value, Some(guard))?;
structuredclone::read(self, data, retval).map_err(|_| Error::DataClone)?;
Ok(())
}
pub(crate) fn fetch<Listener: FetchResponseListener + PreInvoke + Send + 'static>(
&self,
request_builder: RequestBuilder,
context: Arc<Mutex<Listener>>,
task_source: NetworkingTaskSource,
cancellation_sender: Option<ipc::IpcReceiver<()>>,
) {
let canceller = Some(self.task_canceller(TaskSourceName::Networking));
let network_listener = NetworkListener {
context,
task_source,
canceller,
};
self.fetch_with_network_listener(request_builder, network_listener, cancellation_sender);
}
pub(crate) fn fetch_with_network_listener<
Listener: FetchResponseListener + PreInvoke + Send + 'static,
>(
&self,
request_builder: RequestBuilder,
network_listener: NetworkListener<Listener>,
cancellation_receiver: Option<ipc::IpcReceiver<()>>,
) {
fetch_async(
&self.core_resource_thread(),
request_builder,
cancellation_receiver,
network_listener.into_callback(),
);
}
pub fn unminified_js_dir(&self) -> Option<String> {
self.unminified_js_dir.clone()
}
pub(crate) fn set_byte_length_queuing_strategy_size(&self, function: Rc<Function>) {
if self
.byte_length_queuing_strategy_size_function
.set(function)
.is_err()
{
warn!("byte length queuing strategy size function is set twice.");
};
}
pub(crate) fn get_byte_length_queuing_strategy_size(&self) -> Option<Rc<Function>> {
self.byte_length_queuing_strategy_size_function
.get()
.cloned()
}
pub(crate) fn set_count_queuing_strategy_size(&self, function: Rc<Function>) {
if self
.count_queuing_strategy_size_function
.set(function)
.is_err()
{
warn!("count queuing strategy size function is set twice.");
};
}
pub(crate) fn get_count_queuing_strategy_size(&self) -> Option<Rc<Function>> {
self.count_queuing_strategy_size_function.get().cloned()
}
}
#[allow(unsafe_code)]
unsafe fn global_scope_from_global(
global: *mut JSObject,
cx: *mut JSContext,
) -> DomRoot<GlobalScope> {
assert!(!global.is_null());
let clasp = get_object_class(global);
assert_ne!(
((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)),
0
);
root_from_object(global, cx).unwrap()
}
#[allow(unsafe_code)]
unsafe fn global_scope_from_global_static(global: *mut JSObject) -> DomRoot<GlobalScope> {
assert!(!global.is_null());
let clasp = get_object_class(global);
assert_ne!(
((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)),
0
);
root_from_object_static(global).unwrap()
}