diff options
Diffstat (limited to 'components/util')
-rw-r--r-- | components/util/Cargo.toml | 2 | ||||
-rw-r--r-- | components/util/lib.rs | 2 | ||||
-rw-r--r-- | components/util/panicking.rs | 7 | ||||
-rw-r--r-- | components/util/resource_files.rs | 1 | ||||
-rw-r--r-- | components/util/str.rs | 22 | ||||
-rw-r--r-- | components/util/thread.rs | 100 |
6 files changed, 37 insertions, 97 deletions
diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index 7a4bdf9379c..ac7bc43b1dd 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -45,7 +45,7 @@ serde = "0.7" serde_macros = "0.7" smallvec = "0.1" string_cache = {version = "0.2.12", features = ["heap_size"]} -url = {version = "0.5.7", features = ["heap_size", "serde_serialization"]} +url = {version = "1.0.0", features = ["heap_size", "serde"]} [target.x86_64-pc-windows-gnu.dependencies] kernel32-sys = "0.2" diff --git a/components/util/lib.rs b/components/util/lib.rs index 5eb530f1b19..b4998b0580c 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -18,6 +18,7 @@ #![deny(unsafe_code)] extern crate app_units; +#[allow(unused_extern_crates)] #[macro_use] extern crate bitflags; extern crate deque; @@ -27,6 +28,7 @@ extern crate heapsize; extern crate ipc_channel; #[cfg(feature = "non-geckolib")] extern crate js; +#[allow(unused_extern_crates)] #[macro_use] extern crate lazy_static; extern crate libc; diff --git a/components/util/panicking.rs b/components/util/panicking.rs index eecfd74b97f..95abf897e19 100644 --- a/components/util/panicking.rs +++ b/components/util/panicking.rs @@ -17,11 +17,16 @@ static HOOK_SET: Once = ONCE_INIT; /// TLS data pertaining to how failures should be reported pub struct PanicHandlerLocal { /// failure handler passed through spawn_named_with_send_on_failure - pub fail: Box<(FnBox(&(Any + Send))) + Send + 'static> + pub fail: Box<FnBox(&Any)> } thread_local!(pub static LOCAL_INFO: RefCell<Option<PanicHandlerLocal>> = RefCell::new(None)); +/// Set the thread-local panic hook +pub fn set_thread_local_hook(local: Box<FnBox(&Any)>) { + LOCAL_INFO.with(|i| *i.borrow_mut() = Some(PanicHandlerLocal { fail: local })); +} + /// Initiates the custom panic hook /// Should be called in main() after arguments have been parsed pub fn initiate_panic_hook() { diff --git a/components/util/resource_files.rs b/components/util/resource_files.rs index 06e8a428361..58d6a17441c 100644 --- a/components/util/resource_files.rs +++ b/components/util/resource_files.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#[cfg(not(target_os = "android"))] use std::env; use std::fs::File; use std::io::{self, Read}; diff --git a/components/util/str.rs b/components/util/str.rs index e7aa933cecf..b0a3d6c2c9e 100644 --- a/components/util/str.rs +++ b/components/util/str.rs @@ -124,19 +124,6 @@ impl Extend<char> for DOMString { pub type StaticCharVec = &'static [char]; pub type StaticStringVec = &'static [&'static str]; -/// Whitespace as defined by HTML5 § 2.4.1. -// TODO(SimonSapin) Maybe a custom Pattern can be more efficient? -pub const WHITESPACE: &'static [char] = &[' ', '\t', '\x0a', '\x0c', '\x0d']; - -pub fn is_whitespace(s: &str) -> bool { - s.chars().all(char_is_whitespace) -} - -#[inline] -pub fn char_is_whitespace(c: char) -> bool { - WHITESPACE.contains(&c) -} - /// A "space character" according to: /// /// https://html.spec.whatwg.org/multipage/#space-character @@ -148,6 +135,15 @@ pub static HTML_SPACE_CHARACTERS: StaticCharVec = &[ '\u{000d}', ]; +#[inline] +pub fn char_is_whitespace(c: char) -> bool { + HTML_SPACE_CHARACTERS.contains(&c) +} + +pub fn is_whitespace(s: &str) -> bool { + s.chars().all(char_is_whitespace) +} + pub fn split_html_space_chars<'a>(s: &'a str) -> Filter<Split<'a, StaticCharVec>, fn(&&str) -> bool> { fn not_empty(&split: &&str) -> bool { !split.is_empty() } diff --git a/components/util/thread.rs b/components/util/thread.rs index 85a7c52adf8..f6bb72e20a3 100644 --- a/components/util/thread.rs +++ b/components/util/thread.rs @@ -6,97 +6,33 @@ use ipc_channel::ipc::IpcSender; use panicking; use serde::Serialize; use std::any::Any; -use std::borrow::ToOwned; -use std::sync::mpsc::Sender; use std::thread; use thread_state; -pub type PanicReason = Option<String>; - pub fn spawn_named<F>(name: String, f: F) where F: FnOnce() + Send + 'static { - spawn_named_with_send_on_failure_maybe(name, None, f, |_| {}); -} - -pub trait AddFailureDetails { - fn add_panic_message(&mut self, message: String); - fn add_panic_object(&mut self, object: &Any) { - if let Some(message) = object.downcast_ref::<String>() { - self.add_panic_message(message.to_owned()); - } else if let Some(&message) = object.downcast_ref::<&'static str>() { - self.add_panic_message(message.to_owned()); - } - } -} - -/// An abstraction over `Sender<T>` and `IpcSender<T>`, for use in -/// `spawn_named_with_send_on_failure`. -pub trait SendOnFailure { - type Value; - fn send_on_failure(&mut self, value: Self::Value); -} - -impl<T> SendOnFailure for Sender<T> where T: Send + 'static { - type Value = T; - fn send_on_failure(&mut self, value: T) { - // Discard any errors to avoid double-panic - let _ = self.send(value); - } -} - -impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static { - type Value = T; - fn send_on_failure(&mut self, value: T) { - // Discard any errors to avoid double-panic - let _ = self.send(value); - } + thread::Builder::new().name(name).spawn(f).expect("Thread spawn failed"); } /// Arrange to send a particular message to a channel if the thread fails. -pub fn spawn_named_with_send_on_failure<F, T, S>(name: String, - state: thread_state::ThreadState, - f: F, - mut msg: T, - mut dest: S) +pub fn spawn_named_with_send_on_panic<F, Id>(name: String, + state: thread_state::ThreadState, + f: F, + id: Id, + panic_chan: IpcSender<(Id, String)>) where F: FnOnce() + Send + 'static, - T: Send + AddFailureDetails + 'static, - S: Send + SendOnFailure + 'static, - S::Value: From<T>, + Id: Copy + Send + Serialize + 'static, { - spawn_named_with_send_on_failure_maybe(name, Some(state), f, - move |err| { - msg.add_panic_object(err); - dest.send_on_failure(S::Value::from(msg)); - }); -} - -fn spawn_named_with_send_on_failure_maybe<F, G>(name: String, - state: Option<thread_state::ThreadState>, - f: F, - fail: G) - where F: FnOnce() + Send + 'static, - G: FnOnce(&(Any + Send)) + Send + 'static { - - - let builder = thread::Builder::new().name(name.clone()); - - let local = panicking::PanicHandlerLocal { - fail: Box::new(fail), - }; - - let f_with_state = move || { - if let Some(state) = state { - thread_state::initialize(state); - } - - // set the handler - panicking::LOCAL_INFO.with(|i| { - *i.borrow_mut() = Some(local); - }); - f(); - }; - - - builder.spawn(f_with_state).unwrap(); + thread::Builder::new().name(name).spawn(move || { + thread_state::initialize(state); + panicking::set_thread_local_hook(Box::new(move |payload: &Any| { + debug!("Thread failed, notifying constellation"); + let reason = payload.downcast_ref::<String>().map(|s| String::from(&**s)) + .or(payload.downcast_ref::<&'static str>().map(|s| String::from(*s))) + .unwrap_or_else(|| String::from("<unknown reason>")); + let _ = panic_chan.send((id, reason)); + })); + f() + }).expect("Thread spawn failed"); } |