pub mod chrome_linux;
pub mod chrome_macos;
pub mod chrome_windows;
pub const BUILT_IN_CHROME_VERSION: &str = "151.0.7922.138";
pub const BUILT_IN_CHROME_MAJOR: u32 = 151;
const CHROME_VERSION: &str = BUILT_IN_CHROME_VERSION;
const CHROME_MAJOR: &str = "151";
pub(crate) fn chrome_brands(full: bool) -> Vec<BrandVersion> {
let chrome_version = if full { CHROME_VERSION } else { CHROME_MAJOR };
let grease_version = if full { "24.0.0.0" } else { "24" };
vec![
BrandVersion {
brand: "Chromium".to_string(),
version: chrome_version.to_string(),
},
BrandVersion {
brand: "Google Chrome".to_string(),
version: chrome_version.to_string(),
},
BrandVersion {
brand: "Not.A/Brand".to_string(),
version: grease_version.to_string(),
},
]
}
pub(crate) fn desktop_screen() -> ScreenProfile {
ScreenProfile {
width: 1920,
height: 1080,
available_width: 1920,
available_height: 1040,
device_scale_factor: 1.0,
}
}
pub(crate) fn desktop_environment() -> DeviceEnvironmentProfile {
DeviceEnvironmentProfile {
color_scheme: "dark".to_string(),
reduced_motion: "no-preference".to_string(),
forced_colors: "none".to_string(),
color_gamut: "srgb".to_string(),
monochrome: "0".to_string(),
touch_enabled: false,
max_touch_points: 0,
}
}
pub(crate) fn us_eastern_locale() -> LocaleProfile {
LocaleProfile {
locale: "en-US".to_string(),
timezone: "America/New_York".to_string(),
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct BrowserProfile {
pub(crate) name: String,
pub(crate) navigator: NavigatorProfile,
pub(crate) screen: ScreenProfile,
pub(crate) device_environment: DeviceEnvironmentProfile,
pub(crate) locale: LocaleProfile,
pub(crate) version: Option<ProfileVersion>,
}
impl BrowserProfile {
pub fn name(&self) -> &str {
&self.name
}
pub fn navigator(&self) -> &NavigatorProfile {
&self.navigator
}
pub fn screen(&self) -> &ScreenProfile {
&self.screen
}
pub fn device_environment(&self) -> &DeviceEnvironmentProfile {
&self.device_environment
}
pub fn locale(&self) -> &LocaleProfile {
&self.locale
}
pub const fn version(&self) -> Option<&ProfileVersion> {
self.version.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProfileVersion {
pub chrome_major: u32,
pub chrome_version: String,
}
impl ProfileVersion {
pub fn new(chrome_major: u32, chrome_version: impl Into<String>) -> Self {
Self {
chrome_major,
chrome_version: chrome_version.into(),
}
}
pub fn built_in() -> Self {
Self::new(BUILT_IN_CHROME_MAJOR, BUILT_IN_CHROME_VERSION)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NavigatorProfile {
pub user_agent: String,
pub platform: String,
pub languages: Vec<String>,
pub client_hints: Option<UserAgentClientHintsProfile>,
}
pub type IdentityConfig = NavigatorProfile;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserAgentClientHintsProfile {
pub brands: Vec<BrandVersion>,
pub full_version_list: Vec<BrandVersion>,
pub platform: String,
pub platform_version: String,
pub architecture: String,
pub bitness: String,
pub model: String,
pub mobile: bool,
pub wow64: Option<bool>,
pub form_factors: Option<Vec<String>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BrandVersion {
pub brand: String,
pub version: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ScreenProfile {
pub width: u32,
pub height: u32,
pub available_width: u32,
pub available_height: u32,
pub device_scale_factor: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ScreenConfig {
pub width: u32,
pub height: u32,
pub device_scale_factor: f64,
}
impl ScreenConfig {
pub const fn new(width: u32, height: u32, device_scale_factor: f64) -> Self {
Self {
width,
height,
device_scale_factor,
}
}
pub const fn dimensions(mut self, width: u32, height: u32) -> Self {
self.width = width;
self.height = height;
self
}
pub const fn scale_factor(mut self, value: f64) -> Self {
self.device_scale_factor = value;
self
}
}
impl From<&ScreenProfile> for ScreenConfig {
fn from(profile: &ScreenProfile) -> Self {
Self {
width: profile.width,
height: profile.height,
device_scale_factor: profile.device_scale_factor,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceEnvironmentProfile {
pub color_scheme: String,
pub reduced_motion: String,
pub forced_colors: String,
pub color_gamut: String,
pub monochrome: String,
pub touch_enabled: bool,
pub max_touch_points: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorScheme {
Light,
Dark,
#[default]
NoPreference,
}
impl ColorScheme {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Light => "light",
Self::Dark => "dark",
Self::NoPreference => "no-preference",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReducedMotion {
Reduce,
#[default]
NoPreference,
}
impl ReducedMotion {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Reduce => "reduce",
Self::NoPreference => "no-preference",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ForcedColors {
Active,
#[default]
None,
}
impl ForcedColors {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Active => "active",
Self::None => "none",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorGamut {
#[default]
Srgb,
P3,
Rec2020,
}
impl ColorGamut {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Srgb => "srgb",
Self::P3 => "p3",
Self::Rec2020 => "rec2020",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct MediaFeaturesConfig {
pub color_scheme: ColorScheme,
pub reduced_motion: ReducedMotion,
pub forced_colors: ForcedColors,
pub color_gamut: ColorGamut,
pub monochrome: u32,
}
impl MediaFeaturesConfig {
pub const fn desktop() -> Self {
Self {
color_scheme: ColorScheme::NoPreference,
reduced_motion: ReducedMotion::NoPreference,
forced_colors: ForcedColors::None,
color_gamut: ColorGamut::Srgb,
monochrome: 0,
}
}
pub const fn color_scheme(mut self, value: ColorScheme) -> Self {
self.color_scheme = value;
self
}
pub const fn reduced_motion(mut self, value: ReducedMotion) -> Self {
self.reduced_motion = value;
self
}
pub const fn forced_colors(mut self, value: ForcedColors) -> Self {
self.forced_colors = value;
self
}
pub const fn color_gamut(mut self, value: ColorGamut) -> Self {
self.color_gamut = value;
self
}
pub const fn monochrome(mut self, value: u32) -> Self {
self.monochrome = value;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct TouchConfig {
pub enabled: bool,
pub max_touch_points: u32,
}
impl TouchConfig {
pub const fn new(enabled: bool, max_touch_points: u32) -> Self {
Self {
enabled,
max_touch_points,
}
}
}
impl From<&DeviceEnvironmentProfile> for MediaFeaturesConfig {
fn from(profile: &DeviceEnvironmentProfile) -> Self {
Self {
color_scheme: match profile.color_scheme.as_str() {
"light" => ColorScheme::Light,
"dark" => ColorScheme::Dark,
_ => ColorScheme::NoPreference,
},
reduced_motion: if profile.reduced_motion == "reduce" {
ReducedMotion::Reduce
} else {
ReducedMotion::NoPreference
},
forced_colors: if profile.forced_colors == "active" {
ForcedColors::Active
} else {
ForcedColors::None
},
color_gamut: match profile.color_gamut.as_str() {
"p3" => ColorGamut::P3,
"rec2020" => ColorGamut::Rec2020,
_ => ColorGamut::Srgb,
},
monochrome: profile.monochrome.parse().unwrap_or_default(),
}
}
}
impl From<&DeviceEnvironmentProfile> for TouchConfig {
fn from(profile: &DeviceEnvironmentProfile) -> Self {
Self {
enabled: profile.touch_enabled,
max_touch_points: profile.max_touch_points,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LocaleProfile {
pub locale: String,
pub timezone: String,
}
#[derive(Debug, Clone)]
pub struct BrowserProfileBuilder {
profile: BrowserProfile,
}
impl BrowserProfileBuilder {
pub fn new(profile: BrowserProfile) -> Self {
Self { profile }
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.profile.name = name.into();
self
}
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.profile.navigator.user_agent = user_agent.into();
self.profile.version = None;
self
}
pub fn navigator_platform(mut self, platform: impl Into<String>) -> Self {
self.profile.navigator.platform = platform.into();
self
}
pub fn client_hints(mut self, hints: Option<UserAgentClientHintsProfile>) -> Self {
self.profile.navigator.client_hints = hints;
self.profile.version = None;
self
}
pub fn version(mut self, version: Option<ProfileVersion>) -> Self {
self.profile.version = version;
self
}
pub fn languages<I, S>(mut self, languages: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.profile.navigator.languages = languages.into_iter().map(Into::into).collect();
self
}
pub fn locale(mut self, locale: impl Into<String>) -> Self {
let locale = locale.into();
self.profile.locale.locale = locale.clone();
if self.profile.navigator.languages.is_empty() {
self.profile.navigator.languages.push(locale);
} else {
self.profile.navigator.languages[0] = locale;
}
self
}
pub fn timezone(mut self, timezone: impl Into<String>) -> Self {
self.profile.locale.timezone = timezone.into();
self
}
pub fn screen(mut self, width: u32, height: u32) -> Self {
self.profile.screen.width = width;
self.profile.screen.height = height;
self
}
pub fn available_screen(mut self, width: u32, height: u32) -> Self {
self.profile.screen.available_width = width;
self.profile.screen.available_height = height;
self
}
pub fn device_scale_factor(mut self, factor: f64) -> Self {
self.profile.screen.device_scale_factor = factor;
self
}
pub fn color_scheme(mut self, scheme: ColorScheme) -> Self {
self.profile.device_environment.color_scheme = scheme.as_str().to_string();
self
}
pub fn reduced_motion(mut self, preference: ReducedMotion) -> Self {
self.profile.device_environment.reduced_motion = preference.as_str().to_string();
self
}
pub fn forced_colors(mut self, value: ForcedColors) -> Self {
self.profile.device_environment.forced_colors = value.as_str().to_string();
self
}
pub fn color_gamut(mut self, value: ColorGamut) -> Self {
self.profile.device_environment.color_gamut = value.as_str().to_string();
self
}
pub fn monochrome(mut self, value: u32) -> Self {
self.profile.device_environment.monochrome = value.to_string();
self
}
pub fn touch(mut self, enabled: bool, max_touch_points: u32) -> Self {
self.profile.device_environment.touch_enabled = enabled;
self.profile.device_environment.max_touch_points = max_touch_points;
self
}
pub fn build(self) -> crate::Result<BrowserProfile> {
crate::validation::validate_profile(&self.profile)?;
Ok(self.profile)
}
}