diff --git a/Cargo.lock b/Cargo.lock index f0d87c941cc..514d2d93fe7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -766,10 +766,13 @@ dependencies = [ "crossbeam-channel", "ipc-channel", "libc", + "log", "mach2", "malloc_size_of_derive", "parking_lot", + "rayon", "serde", + "serde_json", "servo_config", "servo_malloc_size_of", "time", diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index 815a4583131..63abab36d8f 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -104,7 +104,7 @@ use base::id::{ BrowsingContextGroupId, BrowsingContextId, HistoryStateId, MessagePortId, MessagePortRouterId, PipelineId, PipelineNamespace, PipelineNamespaceId, PipelineNamespaceRequest, WebViewId, }; -use base::{Epoch, generic_channel}; +use base::{Epoch, IpcSend, generic_channel}; #[cfg(feature = "bluetooth")] use bluetooth_traits::BluetoothRequest; use canvas::canvas_paint_thread::CanvasPaintThread; @@ -153,8 +153,7 @@ use net_traits::pub_domains::reg_host; use net_traits::request::Referrer; use net_traits::storage_thread::{StorageThreadMsg, StorageType}; use net_traits::{ - self, AsyncRuntime, IpcSend, ReferrerPolicy, ResourceThreads, exit_fetch_thread, - start_fetch_thread, + self, AsyncRuntime, ReferrerPolicy, ResourceThreads, exit_fetch_thread, start_fetch_thread, }; use profile_traits::mem::ProfilerMsg; use profile_traits::{mem, time}; diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index b1810cd36ae..f356f4da673 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -7,8 +7,7 @@ use std::borrow::ToOwned; use std::collections::HashMap; use std::fs::File; -use std::io::prelude::*; -use std::io::{self, BufReader, BufWriter}; +use std::io::{self, BufReader}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, RwLock, Weak}; use std::thread; @@ -22,7 +21,7 @@ use devtools_traits::DevtoolsControlMsg; use embedder_traits::EmbedderProxy; use hyper_serde::Serde; use ipc_channel::ipc::{self, IpcReceiver, IpcReceiverSet, IpcSender}; -use log::{debug, trace, warn}; +use log::{debug, warn}; use net_traits::blob_url_store::parse_blob_url; use net_traits::filemanager_thread::FileTokenCheck; use net_traits::indexeddb_thread::IndexedDBThreadMsg; @@ -198,9 +197,9 @@ fn create_http_states( let http_cache = HttpCache::default(); let mut cookie_jar = CookieStorage::new(150); if let Some(config_dir) = config_dir { - read_json_from_file(&mut auth_cache, config_dir, "auth_cache.json"); - read_json_from_file(&mut hsts_list, config_dir, "hsts_list.json"); - read_json_from_file(&mut cookie_jar, config_dir, "cookie_jar.json"); + base::read_json_from_file(&mut auth_cache, config_dir, "auth_cache.json"); + base::read_json_from_file(&mut hsts_list, config_dir, "hsts_list.json"); + base::read_json_from_file(&mut cookie_jar, config_dir, "cookie_jar.json"); } let override_manager = CertificateErrorOverrideManager::new(); @@ -532,16 +531,16 @@ impl ResourceChannelManager { if let Some(ref config_dir) = self.config_dir { match http_state.auth_cache.read() { Ok(auth_cache) => { - write_json_to_file(&*auth_cache, config_dir, "auth_cache.json") + base::write_json_to_file(&*auth_cache, config_dir, "auth_cache.json") }, Err(_) => warn!("Error writing auth cache to disk"), } match http_state.cookie_jar.read() { - Ok(jar) => write_json_to_file(&*jar, config_dir, "cookie_jar.json"), + Ok(jar) => base::write_json_to_file(&*jar, config_dir, "cookie_jar.json"), Err(_) => warn!("Error writing cookie jar to disk"), } match http_state.hsts_list.read() { - Ok(hsts) => write_json_to_file(&*hsts, config_dir, "hsts_list.json"), + Ok(hsts) => base::write_json_to_file(&*hsts, config_dir, "hsts_list.json"), Err(_) => warn!("Error writing hsts list to disk"), } } @@ -554,49 +553,6 @@ impl ResourceChannelManager { } } -pub fn read_json_from_file(data: &mut T, config_dir: &Path, filename: &str) -where - T: for<'de> Deserialize<'de>, -{ - let path = config_dir.join(filename); - let display = path.display(); - - let mut file = match File::open(&path) { - Err(why) => { - warn!("couldn't open {}: {}", display, why); - return; - }, - Ok(file) => file, - }; - - let mut string_buffer: String = String::new(); - match file.read_to_string(&mut string_buffer) { - Err(why) => panic!("couldn't read from {}: {}", display, why), - Ok(_) => trace!("successfully read from {}", display), - } - - match serde_json::from_str(&string_buffer) { - Ok(decoded_buffer) => *data = decoded_buffer, - Err(why) => warn!("Could not decode buffer{}", why), - } -} - -pub fn write_json_to_file(data: &T, config_dir: &Path, filename: &str) -where - T: Serialize, -{ - let path = config_dir.join(filename); - let display = path.display(); - - let mut file = match File::create(&path) { - Err(why) => panic!("couldn't create {}: {}", display, why), - Ok(file) => file, - }; - let mut writer = BufWriter::new(&mut file); - serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file"); - trace!("successfully wrote to {display}"); -} - #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AuthCacheEntry { pub user_name: String, diff --git a/components/net/storage_thread.rs b/components/net/storage_thread.rs index 55a77f590b5..f1485afb549 100644 --- a/components/net/storage_thread.rs +++ b/components/net/storage_thread.rs @@ -18,8 +18,6 @@ use profile_traits::path; use rustc_hash::FxHashMap; use servo_url::ServoUrl; -use crate::resource_thread; - const QUOTA_SIZE_LIMIT: usize = 5 * 1024 * 1024; pub trait StorageThreadFactory { @@ -62,7 +60,7 @@ impl StorageManager { fn new(port: GenericReceiver, config_dir: Option) -> StorageManager { let mut local_data = HashMap::new(); if let Some(ref config_dir) = config_dir { - resource_thread::read_json_from_file(&mut local_data, config_dir, "local_data.json"); + base::read_json_from_file(&mut local_data, config_dir, "local_data.json"); } StorageManager { port, @@ -142,7 +140,7 @@ impl StorageManager { fn save_state(&self) { if let Some(ref config_dir) = self.config_dir { - resource_thread::write_json_to_file(&self.local_data, config_dir, "local_data.json"); + base::write_json_to_file(&self.local_data, config_dir, "local_data.json"); } } diff --git a/components/script/dom/cookiestore.rs b/components/script/dom/cookiestore.rs index 065901bd7bd..db4e5ded857 100644 --- a/components/script/dom/cookiestore.rs +++ b/components/script/dom/cookiestore.rs @@ -6,6 +6,7 @@ use std::borrow::Cow; use std::collections::VecDeque; use std::rc::Rc; +use base::IpcSend; use base::id::CookieStoreId; use cookie::{Cookie, SameSite}; use dom_struct::dom_struct; @@ -16,7 +17,7 @@ use ipc_channel::router::ROUTER; use itertools::Itertools; use js::jsval::NullValue; use net_traits::CookieSource::NonHTTP; -use net_traits::{CookieAsyncResponse, CookieData, CoreResourceMsg, IpcSend}; +use net_traits::{CookieAsyncResponse, CookieData, CoreResourceMsg}; use script_bindings::script_runtime::CanGc; use servo_url::ServoUrl; diff --git a/components/script/dom/dedicatedworkerglobalscope.rs b/components/script/dom/dedicatedworkerglobalscope.rs index 7ce86e7b412..e330d3b154f 100644 --- a/components/script/dom/dedicatedworkerglobalscope.rs +++ b/components/script/dom/dedicatedworkerglobalscope.rs @@ -6,6 +6,7 @@ use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::thread::{self, JoinHandle}; +use base::IpcSend; use base::id::{BrowsingContextId, PipelineId, WebViewId}; use constellation_traits::{WorkerGlobalScopeInit, WorkerScriptLoadOrigin}; use crossbeam_channel::{Receiver, Sender, unbounded}; @@ -18,13 +19,13 @@ use ipc_channel::router::ROUTER; use js::jsapi::{Heap, JS_AddInterruptCallback, JSContext, JSObject}; use js::jsval::UndefinedValue; use js::rust::{CustomAutoRooter, CustomAutoRooterGuard, HandleValue}; +use net_traits::Metadata; use net_traits::image_cache::ImageCache; use net_traits::policy_container::PolicyContainer; use net_traits::request::{ CredentialsMode, Destination, InsecureRequestsPolicy, ParserMetadata, Referrer, RequestBuilder, RequestMode, }; -use net_traits::{IpcSend, Metadata}; use servo_rand::random; use servo_url::{ImmutableOrigin, ServoUrl}; use style::thread_state::{self, ThreadState}; diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 6e3da9a088a..a9c7f946ced 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -14,7 +14,7 @@ use std::time::Duration; use base::cross_process_instant::CrossProcessInstant; use base::id::WebViewId; -use base::{Epoch, generic_channel}; +use base::{Epoch, IpcSend, generic_channel}; use canvas_traits::canvas::CanvasId; use canvas_traits::webgl::{WebGLContextId, WebGLMsg}; use chrono::Local; @@ -41,7 +41,7 @@ use net_traits::policy_container::PolicyContainer; use net_traits::pub_domains::is_pub_domain; use net_traits::request::{InsecureRequestsPolicy, RequestBuilder}; use net_traits::response::HttpsState; -use net_traits::{FetchResponseListener, IpcSend, ReferrerPolicy}; +use net_traits::{FetchResponseListener, ReferrerPolicy}; use percent_encoding::percent_decode; use profile_traits::ipc as profile_ipc; use profile_traits::time::TimerMetadataFrameType; diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index e97506836df..fe6615ebd30 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -14,6 +14,7 @@ use std::thread::JoinHandle; use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use std::{mem, ptr}; +use base::IpcSend; use base::id::{ BlobId, BroadcastChannelRouterId, MessagePortId, MessagePortRouterId, PipelineId, ServiceWorkerId, ServiceWorkerRegistrationId, WebViewId, @@ -52,8 +53,8 @@ use net_traits::policy_container::PolicyContainer; use net_traits::request::{InsecureRequestsPolicy, Referrer, RequestBuilder}; use net_traits::response::HttpsState; use net_traits::{ - CoreResourceMsg, CoreResourceThread, FetchResponseListener, IpcSend, ReferrerPolicy, - ResourceThreads, fetch_async, + CoreResourceMsg, CoreResourceThread, FetchResponseListener, ReferrerPolicy, ResourceThreads, + fetch_async, }; use profile_traits::{ipc as profile_ipc, mem as profile_mem, time as profile_time}; use rustc_hash::{FxBuildHasher, FxHashMap}; diff --git a/components/script/dom/history.rs b/components/script/dom/history.rs index dc773b842b7..d146ec6c1d7 100644 --- a/components/script/dom/history.rs +++ b/components/script/dom/history.rs @@ -5,6 +5,7 @@ use std::cell::Cell; use std::cmp::Ordering; +use base::IpcSend; use base::id::HistoryStateId; use constellation_traits::{ ScriptToConstellationMessage, StructuredSerializedData, TraversalDirection, @@ -13,7 +14,7 @@ use dom_struct::dom_struct; use js::jsapi::Heap; use js::jsval::{JSVal, NullValue, UndefinedValue}; use js::rust::{HandleValue, MutableHandleValue}; -use net_traits::{CoreResourceMsg, IpcSend}; +use net_traits::CoreResourceMsg; use profile_traits::ipc; use profile_traits::ipc::channel; use servo_url::ServoUrl; diff --git a/components/script/dom/html/htmlinputelement.rs b/components/script/dom/html/htmlinputelement.rs index 26c60c5231f..da32508db4f 100644 --- a/components/script/dom/html/htmlinputelement.rs +++ b/components/script/dom/html/htmlinputelement.rs @@ -11,7 +11,7 @@ use std::ptr::NonNull; use std::str::FromStr; use std::{f64, ptr}; -use base::generic_channel; +use base::{IpcSend, generic_channel}; use dom_struct::dom_struct; use embedder_traits::{ EmbedderMsg, FilterPattern, FormControl as EmbedderFormControl, InputMethodType, RgbColor, @@ -26,9 +26,9 @@ use js::jsapi::{ use js::jsval::UndefinedValue; use js::rust::wrappers::{CheckRegExpSyntax, ExecuteRegExpNoStatics, ObjectIsRegExp}; use js::rust::{HandleObject, MutableHandleObject}; +use net_traits::CoreResourceMsg; use net_traits::blob_url_store::get_blob_origin; use net_traits::filemanager_thread::{FileManagerResult, FileManagerThreadMsg}; -use net_traits::{CoreResourceMsg, IpcSend}; use script_bindings::codegen::GenericBindings::CharacterDataBinding::CharacterDataMethods; use script_bindings::codegen::GenericBindings::DocumentBinding::DocumentMethods; use servo_config::pref; diff --git a/components/script/dom/idbdatabase.rs b/components/script/dom/idbdatabase.rs index 7ded6ffef7f..5fb80e22e3b 100644 --- a/components/script/dom/idbdatabase.rs +++ b/components/script/dom/idbdatabase.rs @@ -4,9 +4,9 @@ use std::cell::Cell; +use base::IpcSend; use dom_struct::dom_struct; use ipc_channel::ipc::IpcSender; -use net_traits::IpcSend; use net_traits::indexeddb_thread::{IndexedDBThreadMsg, KeyPath, SyncOperation}; use profile_traits::ipc; use stylo_atoms::Atom; diff --git a/components/script/dom/idbobjectstore.rs b/components/script/dom/idbobjectstore.rs index 5b8ad5b01ec..8d0a6344b29 100644 --- a/components/script/dom/idbobjectstore.rs +++ b/components/script/dom/idbobjectstore.rs @@ -2,11 +2,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use base::IpcSend; use dom_struct::dom_struct; use js::gc::MutableHandleValue; use js::jsval::NullValue; use js::rust::HandleValue; -use net_traits::IpcSend; use net_traits::indexeddb_thread::{ AsyncOperation, AsyncReadOnlyOperation, AsyncReadWriteOperation, IndexedDBKeyType, IndexedDBThreadMsg, SyncOperation, diff --git a/components/script/dom/idbopendbrequest.rs b/components/script/dom/idbopendbrequest.rs index 62fe1b648b8..81685fa1e94 100644 --- a/components/script/dom/idbopendbrequest.rs +++ b/components/script/dom/idbopendbrequest.rs @@ -2,11 +2,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use base::IpcSend; use dom_struct::dom_struct; use ipc_channel::router::ROUTER; use js::jsval::UndefinedValue; use js::rust::HandleValue; -use net_traits::IpcSend; use net_traits::indexeddb_thread::{BackendResult, IndexedDBThreadMsg, SyncOperation}; use profile_traits::ipc; use script_bindings::conversions::SafeToJSValConvertible; diff --git a/components/script/dom/idbrequest.rs b/components/script/dom/idbrequest.rs index 0890588b307..aeb20f6f66a 100644 --- a/components/script/dom/idbrequest.rs +++ b/components/script/dom/idbrequest.rs @@ -5,12 +5,12 @@ use std::cell::Cell; use std::iter::repeat_n; +use base::IpcSend; use dom_struct::dom_struct; use ipc_channel::router::ROUTER; use js::jsapi::Heap; use js::jsval::{DoubleValue, JSVal, ObjectValue, UndefinedValue}; use js::rust::HandleValue; -use net_traits::IpcSend; use net_traits::indexeddb_thread::{ AsyncOperation, AsyncReadOnlyOperation, BackendError, BackendResult, IndexedDBKeyType, IndexedDBRecord, IndexedDBThreadMsg, IndexedDBTxnMode, PutItemResult, diff --git a/components/script/dom/idbtransaction.rs b/components/script/dom/idbtransaction.rs index 75b1e6a4686..3dc7a368409 100644 --- a/components/script/dom/idbtransaction.rs +++ b/components/script/dom/idbtransaction.rs @@ -5,9 +5,9 @@ use std::cell::Cell; use std::collections::HashMap; +use base::IpcSend; use dom_struct::dom_struct; use ipc_channel::ipc::IpcSender; -use net_traits::IpcSend; use net_traits::indexeddb_thread::{IndexedDBThreadMsg, KeyPath, SyncOperation}; use profile_traits::ipc; use script_bindings::codegen::GenericUnionTypes::StringOrStringSequence; diff --git a/components/script/dom/serviceworkerglobalscope.rs b/components/script/dom/serviceworkerglobalscope.rs index 03444aef9c0..f3adcf448af 100644 --- a/components/script/dom/serviceworkerglobalscope.rs +++ b/components/script/dom/serviceworkerglobalscope.rs @@ -7,6 +7,7 @@ use std::sync::atomic::AtomicBool; use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant}; +use base::IpcSend; use base::generic_channel::GenericSender; use base::id::PipelineId; use constellation_traits::{ @@ -20,10 +21,10 @@ use ipc_channel::ipc::IpcReceiver; use ipc_channel::router::ROUTER; use js::jsapi::{JS_AddInterruptCallback, JSContext}; use js::jsval::UndefinedValue; +use net_traits::CustomResponseMediator; use net_traits::request::{ CredentialsMode, Destination, InsecureRequestsPolicy, ParserMetadata, Referrer, RequestBuilder, }; -use net_traits::{CustomResponseMediator, IpcSend}; use servo_config::pref; use servo_rand::random; use servo_url::ServoUrl; diff --git a/components/script/dom/servoparser/prefetch.rs b/components/script/dom/servoparser/prefetch.rs index fb7b869ebc2..9a46070674f 100644 --- a/components/script/dom/servoparser/prefetch.rs +++ b/components/script/dom/servoparser/prefetch.rs @@ -5,6 +5,7 @@ use std::cell::{Cell, RefCell}; use std::ops::Deref; +use base::IpcSend; use base::id::{PipelineId, WebViewId}; use html5ever::buffer_queue::BufferQueue; use html5ever::tokenizer::states::RawKind; @@ -18,7 +19,7 @@ use net_traits::policy_container::PolicyContainer; use net_traits::request::{ CorsSettings, CredentialsMode, Destination, InsecureRequestsPolicy, ParserMetadata, Referrer, }; -use net_traits::{CoreResourceMsg, FetchChannels, IpcSend, ReferrerPolicy, ResourceThreads}; +use net_traits::{CoreResourceMsg, FetchChannels, ReferrerPolicy, ResourceThreads}; use servo_url::{ImmutableOrigin, ServoUrl}; use crate::dom::bindings::reflector::DomGlobal; diff --git a/components/script/dom/url.rs b/components/script/dom/url.rs index 8486de3de0d..553aec8e442 100644 --- a/components/script/dom/url.rs +++ b/components/script/dom/url.rs @@ -4,11 +4,12 @@ use std::default::Default; +use base::IpcSend; use dom_struct::dom_struct; use js::rust::HandleObject; +use net_traits::CoreResourceMsg; use net_traits::blob_url_store::{get_blob_origin, parse_blob_url}; use net_traits::filemanager_thread::FileManagerThreadMsg; -use net_traits::{CoreResourceMsg, IpcSend}; use profile_traits::ipc; use servo_url::ServoUrl; use uuid::Uuid; diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs index 595a7d62e44..c2a6fd54039 100644 --- a/components/script/dom/workerglobalscope.rs +++ b/components/script/dom/workerglobalscope.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; +use base::IpcSend; use base::cross_process_instant::CrossProcessInstant; use base::id::{PipelineId, PipelineNamespace}; use constellation_traits::WorkerGlobalScopeInit; @@ -21,12 +22,12 @@ use ipc_channel::ipc::IpcSender; use js::jsval::UndefinedValue; use js::panic::maybe_resume_unwind; use js::rust::{HandleValue, MutableHandleValue, ParentRuntime}; +use net_traits::ReferrerPolicy; use net_traits::policy_container::PolicyContainer; use net_traits::request::{ CredentialsMode, Destination, InsecureRequestsPolicy, ParserMetadata, RequestBuilder as NetRequestInit, }; -use net_traits::{IpcSend, ReferrerPolicy}; use profile_traits::mem::{ProcessReports, perform_memory_report}; use servo_url::{MutableOrigin, ServoUrl}; use timers::TimerScheduler; diff --git a/components/script/dom/worklet.rs b/components/script/dom/worklet.rs index ba3fd866a9f..373026f355a 100644 --- a/components/script/dom/worklet.rs +++ b/components/script/dom/worklet.rs @@ -18,12 +18,12 @@ use std::sync::Arc; use std::sync::atomic::{AtomicIsize, Ordering}; use std::thread; +use base::IpcSend; use base::id::PipelineId; use crossbeam_channel::{Receiver, Sender, unbounded}; use dom_struct::dom_struct; use js::jsapi::{GCReason, JS_GC, JS_GetGCParameter, JSGCParamKey, JSTracer}; use malloc_size_of::malloc_size_of_is_0; -use net_traits::IpcSend; use net_traits::request::{Destination, RequestBuilder, RequestMode}; use rustc_hash::FxHashMap; use servo_url::{ImmutableOrigin, ServoUrl}; diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index e05a5b467b5..c72f08c746a 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -6,6 +6,7 @@ use std::collections::{HashMap, HashSet}; use std::ffi::CString; use std::ptr::NonNull; +use base::IpcSend; use base::generic_channel::GenericSender; use base::id::{BrowsingContextId, PipelineId}; use cookie::Cookie; @@ -27,7 +28,6 @@ use net_traits::CookieSource::{HTTP, NonHTTP}; use net_traits::CoreResourceMsg::{ DeleteCookie, DeleteCookies, GetCookiesDataForUrl, SetCookieForUrl, }; -use net_traits::IpcSend; use script_bindings::codegen::GenericBindings::ShadowRootBinding::ShadowRootMethods; use script_bindings::conversions::is_array_like; use script_bindings::num::Finite; diff --git a/components/shared/base/Cargo.toml b/components/shared/base/Cargo.toml index 34dcfc7fbf7..2afcf2698f4 100644 --- a/components/shared/base/Cargo.toml +++ b/components/shared/base/Cargo.toml @@ -19,10 +19,13 @@ ipc-channel = { workspace = true } malloc_size_of = { workspace = true } malloc_size_of_derive = { workspace = true } parking_lot = { workspace = true } +rayon = { workspace = true } serde = { workspace = true } +serde_json = { workspace = true } servo_config = { path = "../../config" } time = { workspace = true } webrender_api = { workspace = true } +log = { workspace = true } [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] mach2 = { workspace = true } diff --git a/components/shared/base/lib.rs b/components/shared/base/lib.rs index 5855eaee71d..a340a87f77c 100644 --- a/components/shared/base/lib.rs +++ b/components/shared/base/lib.rs @@ -14,12 +14,62 @@ pub mod generic_channel; pub mod id; pub mod print_tree; pub mod text; +pub mod threadpool; mod unicode_block; +use std::fs::File; +use std::io::{BufWriter, Read}; +use std::path::Path; + +use ipc_channel::ipc::{IpcError, IpcSender}; +use log::{trace, warn}; use malloc_size_of_derive::MallocSizeOf; use serde::{Deserialize, Serialize}; use webrender_api::Epoch as WebRenderEpoch; +pub fn read_json_from_file(data: &mut T, config_dir: &Path, filename: &str) +where + T: for<'de> Deserialize<'de>, +{ + let path = config_dir.join(filename); + let display = path.display(); + + let mut file = match File::open(&path) { + Err(why) => { + warn!("couldn't open {}: {}", display, why); + return; + }, + Ok(file) => file, + }; + + let mut string_buffer: String = String::new(); + match file.read_to_string(&mut string_buffer) { + Err(why) => panic!("couldn't read from {}: {}", display, why), + Ok(_) => trace!("successfully read from {}", display), + } + + match serde_json::from_str(&string_buffer) { + Ok(decoded_buffer) => *data = decoded_buffer, + Err(why) => warn!("Could not decode buffer{}", why), + } +} + +pub fn write_json_to_file(data: &T, config_dir: &Path, filename: &str) +where + T: Serialize, +{ + let path = config_dir.join(filename); + let display = path.display(); + + let mut file = match File::create(&path) { + Err(why) => panic!("couldn't create {}: {}", display, why), + Ok(file) => file, + }; + let mut writer = BufWriter::new(&mut file); + serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file"); + trace!("successfully wrote to {display}"); +} + /// A struct for denoting the age of messages; prevents race conditions. #[derive( Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, MallocSizeOf, @@ -50,3 +100,18 @@ impl WebRenderEpochToU16 for WebRenderEpoch { (self.0 % u16::MAX as u32) as u16 } } + +pub type IpcSendResult = Result<(), IpcError>; + +/// Abstraction of the ability to send a particular type of message, +/// used by net_traits::ResourceThreads to ease the use its IpcSender sub-fields +/// XXX: If this trait will be used more in future, some auto derive might be appealing +pub trait IpcSend +where + T: serde::Serialize + for<'de> serde::Deserialize<'de>, +{ + /// send message T + fn send(&self, _: T) -> IpcSendResult; + /// get underlying sender + fn sender(&self) -> IpcSender; +} diff --git a/components/shared/base/threadpool.rs b/components/shared/base/threadpool.rs new file mode 100644 index 00000000000..aab5ddca6cc --- /dev/null +++ b/components/shared/base/threadpool.rs @@ -0,0 +1,139 @@ +/* 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 https://mozilla.org/MPL/2.0/. */ + +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; + +use log::debug; + +/// The state of the thread-pool used by CoreResource. +struct ThreadPoolState { + /// The number of active workers. + active_workers: u32, + /// Whether the pool can spawn additional work. + active: bool, +} + +impl ThreadPoolState { + pub fn new() -> ThreadPoolState { + ThreadPoolState { + active_workers: 0, + active: true, + } + } + + /// Is the pool still able to spawn new work? + pub fn is_active(&self) -> bool { + self.active + } + + /// How many workers are currently active? + pub fn active_workers(&self) -> u32 { + self.active_workers + } + + /// Prevent additional work from being spawned. + pub fn switch_to_inactive(&mut self) { + self.active = false; + } + + /// Add to the count of active workers. + pub fn increment_active(&mut self) { + self.active_workers += 1; + } + + /// Substract from the count of active workers. + pub fn decrement_active(&mut self) { + self.active_workers -= 1; + } +} + +/// Threadpool used by Fetch and file operations. +pub struct ThreadPool { + pool: rayon::ThreadPool, + state: Arc>, +} + +impl ThreadPool { + pub fn new(num_threads: usize, pool_name: String) -> Self { + debug!("Creating new ThreadPool with {num_threads} threads!"); + let pool = rayon::ThreadPoolBuilder::new() + .thread_name(move |i| format!("{pool_name}#{i}")) + .num_threads(num_threads) + .build() + .unwrap(); + let state = Arc::new(Mutex::new(ThreadPoolState::new())); + Self { pool, state } + } + + /// Spawn work on the thread-pool, if still active. + /// + /// There is no need to give feedback to the caller, + /// because if we do not perform work, + /// it is because the system as a whole is exiting. + pub fn spawn(&self, work: OP) + where + OP: FnOnce() + Send + 'static, + { + { + let mut state = self.state.lock().unwrap(); + if state.is_active() { + state.increment_active(); + } else { + // Don't spawn any work. + return; + } + } + + let state = self.state.clone(); + + self.pool.spawn(move || { + { + let mut state = state.lock().unwrap(); + if !state.is_active() { + // Decrement number of active workers and return, + // without doing any work. + return state.decrement_active(); + } + } + // Perform work. + work(); + { + // Decrement number of active workers. + let mut state = state.lock().unwrap(); + state.decrement_active(); + } + }); + } + + /// Prevent further work from being spawned, + /// and wait until all workers are done, + /// or a timeout of roughly one second has been reached. + pub fn exit(&self) { + { + let mut state = self.state.lock().unwrap(); + state.switch_to_inactive(); + } + let mut rounds = 0; + loop { + rounds += 1; + { + let state = self.state.lock().unwrap(); + let still_active = state.active_workers(); + + if still_active == 0 || rounds == 10 { + if still_active > 0 { + debug!( + "Exiting ThreadPool with {:?} still working(should be zero)", + still_active + ); + } + break; + } + } + thread::sleep(Duration::from_millis(100)); + } + } +} diff --git a/components/shared/net/lib.rs b/components/shared/net/lib.rs index 650b2140744..d33bf9ffcf9 100644 --- a/components/shared/net/lib.rs +++ b/components/shared/net/lib.rs @@ -11,6 +11,7 @@ use std::thread::{self, JoinHandle}; use base::cross_process_instant::CrossProcessInstant; use base::generic_channel::{GenericSend, GenericSender, SendResult}; use base::id::{CookieStoreId, HistoryStateId}; +use base::{IpcSend, IpcSendResult}; use content_security_policy::{self as csp}; use cookie::Cookie; use crossbeam_channel::{Receiver, Sender, unbounded}; @@ -18,8 +19,7 @@ use headers::{ContentType, HeaderMapExt, ReferrerPolicy as ReferrerPolicyHeader} use http::{Error as HttpError, HeaderMap, HeaderValue, StatusCode, header}; use hyper_serde::Serde; use hyper_util::client::legacy::Error as HyperError; -use ipc_channel::Error as IpcError; -use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; +use ipc_channel::ipc::{self, IpcError, IpcReceiver, IpcSender}; use ipc_channel::router::ROUTER; use malloc_size_of::malloc_size_of_is_0; use malloc_size_of_derive::MallocSizeOf; @@ -415,21 +415,6 @@ pub trait AsyncRuntime: Send { /// Handle to a resource thread pub type CoreResourceThread = IpcSender; -pub type IpcSendResult = Result<(), IpcError>; - -/// Abstraction of the ability to send a particular type of message, -/// used by net_traits::ResourceThreads to ease the use its IpcSender sub-fields -/// XXX: If this trait will be used more in future, some auto derive might be appealing -pub trait IpcSend -where - T: serde::Serialize + for<'de> serde::Deserialize<'de>, -{ - /// send message T - fn send(&self, _: T) -> IpcSendResult; - /// get underlying sender - fn sender(&self) -> IpcSender; -} - // FIXME: Originally we will construct an Arc from ResourceThread // in script_thread to avoid some performance pitfall. Now we decide to deal with // the "Arc" hack implicitly in future. @@ -462,7 +447,7 @@ impl ResourceThreads { impl IpcSend for ResourceThreads { fn send(&self, msg: CoreResourceMsg) -> IpcSendResult { - self.core_thread.send(msg) + self.core_thread.send(msg).map_err(IpcError::Bincode) } fn sender(&self) -> IpcSender { @@ -472,7 +457,7 @@ impl IpcSend for ResourceThreads { impl IpcSend for ResourceThreads { fn send(&self, msg: IndexedDBThreadMsg) -> IpcSendResult { - self.idb_thread.send(msg) + self.idb_thread.send(msg).map_err(IpcError::Bincode) } fn sender(&self) -> IpcSender {