Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow embedders to provide user agent string #25672

Closed
wants to merge 8 commits into from
Next

Make default_user_agent_string a method on the EmbedderMethods trait …

…so that embedder can decide what user agent to report in network requests
  • Loading branch information
mediremi committed Feb 1, 2020
commit 88f48d4507566e05158a667b62400ef8331619a1
@@ -182,6 +182,9 @@ pub trait EmbedderMethods {

/// Register services with a WebXR Registry.
fn register_webxr(&mut self, _: &mut webxr::MainThreadRegistry) {}

/// Returns the default user agent string
fn default_user_agent_string(&self) -> &'static str;

This comment has been minimized.

@paulrouget

paulrouget Feb 7, 2020

Contributor

Make that a verb for consistency.

}

#[derive(Clone, Copy, Debug)]
@@ -10,7 +10,6 @@ use euclid::Size2D;
use getopts::{Matches, Options};
use servo_geometry::DeviceIndependentPixel;
use servo_url::ServoUrl;
use std::borrow::Cow;
use std::default::Default;
use std::env;
use std::fs::{self, File};
@@ -130,9 +129,6 @@ pub struct Opts {
/// The initial requested size of the window.
pub initial_window_size: Size2D<u32, DeviceIndependentPixel>,

/// An optional string allowing the user agent to be set for testing.
pub user_agent: Cow<'static, str>,

/// Whether we're running in multiprocess mode.
pub multiprocess: bool,

@@ -470,51 +466,6 @@ pub fn multiprocess() -> bool {
MULTIPROCESS.load(Ordering::Relaxed)
}

enum UserAgent {
Desktop,
Android,
#[allow(non_camel_case_types)]
iOS,
}

fn default_user_agent_string(agent: UserAgent) -> &'static str {
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
const DESKTOP_UA_STRING: &'static str =
"Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Servo/1.0 Firefox/72.0";
#[cfg(all(target_os = "linux", not(target_arch = "x86_64")))]
const DESKTOP_UA_STRING: &'static str =
"Mozilla/5.0 (X11; Linux i686; rv:72.0) Servo/1.0 Firefox/72.0";

#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
const DESKTOP_UA_STRING: &'static str =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Servo/1.0 Firefox/72.0";
#[cfg(all(target_os = "windows", not(target_arch = "x86_64")))]
const DESKTOP_UA_STRING: &'static str =
"Mozilla/5.0 (Windows NT 10.0; rv:72.0) Servo/1.0 Firefox/72.0";

#[cfg(not(any(target_os = "linux", target_os = "windows")))]
// Neither Linux nor Windows, so maybe OS X, and if not then OS X is an okay fallback.
const DESKTOP_UA_STRING: &'static str =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Servo/1.0 Firefox/72.0";

match agent {
UserAgent::Desktop => DESKTOP_UA_STRING,
UserAgent::Android => "Mozilla/5.0 (Android; Mobile; rv:68.0) Servo/1.0 Firefox/68.0",
UserAgent::iOS => {
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X; rv:72.0) Servo/1.0 Firefox/72.0"
},
}
}

#[cfg(target_os = "android")]
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Android;

#[cfg(target_os = "ios")]
const DEFAULT_USER_AGENT: UserAgent = UserAgent::iOS;

#[cfg(not(any(target_os = "android", target_os = "ios")))]
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Desktop;

pub fn default_opts() -> Opts {
Opts {
is_running_problem_test: false,
@@ -543,7 +494,6 @@ pub fn default_opts() -> Opts {
devtools_port: None,
webdriver_port: None,
initial_window_size: Size2D::new(1024, 740),
user_agent: default_user_agent_string(DEFAULT_USER_AGENT).into(),
multiprocess: false,
random_pipeline_closure_probability: None,
random_pipeline_closure_seed: None,
@@ -911,14 +861,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
MULTIPROCESS.store(true, Ordering::SeqCst)
}

let user_agent = match opt_match.opt_str("u") {
Some(ref ua) if ua == "ios" => default_user_agent_string(UserAgent::iOS).into(),
Some(ref ua) if ua == "android" => default_user_agent_string(UserAgent::Android).into(),
Some(ref ua) if ua == "desktop" => default_user_agent_string(UserAgent::Desktop).into(),
Some(ua) => ua.into(),
None => default_user_agent_string(DEFAULT_USER_AGENT).into(),
};

This comment has been minimized.

@paulrouget

paulrouget Feb 7, 2020

Contributor

This still needs to be possible.


let user_stylesheets = opt_match
.opt_strs("user-stylesheet")
.iter()
@@ -963,7 +905,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
devtools_port: devtools_port,
webdriver_port: webdriver_port,
initial_window_size: initial_window_size,
user_agent: user_agent,
multiprocess: opt_match.opt_present("M"),
sandbox: opt_match.opt_present("S"),
random_pipeline_closure_probability: random_pipeline_closure_probability,
@@ -506,7 +506,7 @@ where
// pipelines, including the script and layout threads, as well
// as the navigation context.
let (constellation_chan, sw_senders) = create_constellation(
opts.user_agent.clone(),
embedder.default_user_agent_string.clone(),
opts.config_dir.clone(),
embedder_proxy.clone(),
compositor_proxy.clone(),
@@ -687,6 +687,47 @@ impl EmbedderMethods for ServoEmbedderCallbacks {
debug!("EmbedderMethods::create_event_loop_waker");
self.waker.clone()
}

fn default_user_agent_string(&self) -> &'static str {
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
const UA_STRING: &'static str =
"Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Servo/1.0 Firefox/63.0";
#[cfg(all(target_os = "linux", not(target_arch = "x86_64")))]
const UA_STRING: &'static str =
"Mozilla/5.0 (X11; Linux i686; rv:63.0) Servo/1.0 Firefox/63.0";

#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
const UA_STRING: &'static str =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Servo/1.0 Firefox/63.0";
#[cfg(all(target_os = "windows", not(target_arch = "x86_64")))]
const UA_STRING: &'static str =
"Mozilla/5.0 (Windows NT 10.0; rv:63.0) Servo/1.0 Firefox/63.0";

#[cfg(all(target_os = "macos"))]
const UA_STRING: &'static str =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:63.0) Servo/1.0 Firefox/63.0";

#[cfg(target_os = "android")]
const UA_STRING: &'static str =
"Mozilla/5.0 (Android; Mobile; rv:63.0) Servo/1.0 Firefox/63.0";

#[cfg(target_os = "ios")]
const UA_STRING: &'static str =
"Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X; rv:63.0) Servo/1.0 Firefox/63.0";

#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "android",
target_os = "ios"
)))]
// Use OS X user agent as fallback
const UA_STRING: &'static str =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:63.0) Servo/1.0 Firefox/63.0";

UA_STRING
}
}

impl WindowMethods for ServoWindowCallbacks {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.