libservo: Remove a couple EmbedderMethods (#36276)

- Remove `EmbedderMethods::get_user_agent_string`. This is now part of
  the `Preferences` data structure, which should allow it to be
  per-`WebView` in the future.
- Remove `EmbedderMethods::get_version_string`. This was used to include
  some data along with WebRender captures about the Servo version. This
  isn't really necessary and it was done to replace code in the past
  that output the WebRender version, so also isn't what the original
  code did. I think we can just remove this entirely.

The idea with these changes is that `EmbedderMethods` can be removed
in a followup and the rest of the methods can be added to
`ServoDelegate`. These two methods are ones that cannot be added to a
delegate as they are used during `Servo` initialization.

Testing: There is currently no testing for libservo. These changes are
meant
as preparation for adding a suite of `WebView` unit tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-04-02 00:59:50 +02:00 committed by GitHub
parent 0d38d6239a
commit 4402b7cf8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 95 additions and 206 deletions

View file

@ -9,7 +9,7 @@ use servo_config_macro::ServoPreferences;
pub use crate::pref_util::PrefValue;
static PREFERENCES: RwLock<Preferences> = RwLock::new(Preferences::new());
static PREFERENCES: RwLock<Preferences> = RwLock::new(Preferences::const_default());
#[inline]
/// Get the current set of global preferences for Servo.
@ -230,10 +230,13 @@ pub struct Preferences {
pub threadpools_resource_workers_max: i64,
/// Maximum number of workers for webrender
pub threadpools_webrender_workers_max: i64,
/// The user-agent to use for Servo. This can also be set via [`UserAgentPlatform`] in
/// order to set the value to the default value for the given platform.
pub user_agent: String,
}
impl Preferences {
const fn new() -> Self {
const fn const_default() -> Self {
Self {
css_animations_testing_enabled: false,
devtools_server_enabled: false,
@ -389,12 +392,87 @@ impl Preferences {
threadpools_resource_workers_max: 4,
threadpools_webrender_workers_max: 4,
webgl_testing_context_creation_error: false,
user_agent: String::new(),
}
}
}
impl Default for Preferences {
fn default() -> Self {
Self::new()
let mut preferences = Self::const_default();
preferences.user_agent = UserAgentPlatform::default().to_user_agent_string();
preferences
}
}
pub enum UserAgentPlatform {
Desktop,
Android,
OpenHarmony,
Ios,
}
impl UserAgentPlatform {
/// Return the default `UserAgentPlatform` for this platform. This is
/// not an implementation of `Default` so that it can be `const`.
const fn default() -> Self {
if cfg!(target_os = "android") {
Self::Android
} else if cfg!(target_env = "ohos") {
Self::OpenHarmony
} else if cfg!(target_os = "ios") {
Self::Ios
} else {
Self::Desktop
}
}
}
impl UserAgentPlatform {
/// Convert this [`UserAgentPlatform`] into its corresponding `String` value, ie the
/// default user-agent to use for this platform.
pub fn to_user_agent_string(&self) -> String {
const SERVO_VERSION: &str = env!("CARGO_PKG_VERSION");
match self {
UserAgentPlatform::Desktop
if cfg!(all(target_os = "windows", target_arch = "x86_64")) =>
{
#[cfg(target_arch = "x86_64")]
const ARCHITECTURE: &str = "x86; ";
#[cfg(not(target_arch = "x86_64"))]
const ARCHITECTURE: &str = "";
format!(
"Mozilla/5.0 (Windows NT 10.0; Win64; {ARCHITECTURE}rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
)
},
UserAgentPlatform::Desktop if cfg!(target_os = "macos") => {
format!(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
)
},
UserAgentPlatform::Desktop => {
#[cfg(target_arch = "x86_64")]
const ARCHITECTURE: &str = "x86_64";
// TODO: This is clearly wrong for other platforms.
#[cfg(not(target_arch = "x86_64"))]
const ARCHITECTURE: &str = "i686";
format!(
"Mozilla/5.0 (X11; Linux {ARCHITECTURE}; rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
)
},
UserAgentPlatform::Android => {
format!(
"Mozilla/5.0 (Android; Mobile; rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
)
},
UserAgentPlatform::OpenHarmony => format!(
"Mozilla/5.0 (OpenHarmony; Mobile; rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
),
UserAgentPlatform::Ios => format!(
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X; rv:128.0) Servo/{SERVO_VERSION} Firefox/128.0"
),
}
}
}