mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Add a simple method to emulate a platform UA (e.g. pretend a desktop build is an android UA).
This commit is contained in:
parent
3bb930661d
commit
6d9fccbae7
5 changed files with 44 additions and 21 deletions
|
@ -135,7 +135,7 @@ pub struct Opts {
|
|||
pub initial_window_size: TypedSize2D<ScreenPx, u32>,
|
||||
|
||||
/// An optional string allowing the user agent to be set for testing.
|
||||
pub user_agent: Option<String>,
|
||||
pub user_agent: String,
|
||||
|
||||
/// Whether to run in multiprocess mode.
|
||||
pub multiprocess: bool,
|
||||
|
@ -337,17 +337,36 @@ static FORCE_CPU_PAINTING: bool = true;
|
|||
#[cfg(not(target_os="android"))]
|
||||
static FORCE_CPU_PAINTING: bool = false;
|
||||
|
||||
enum UserAgent {
|
||||
Desktop,
|
||||
Android,
|
||||
Gonk,
|
||||
}
|
||||
|
||||
fn default_user_agent_string(agent: UserAgent) -> String {
|
||||
match agent {
|
||||
UserAgent::Desktop => {
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Servo/1.0 Firefox/37.0"
|
||||
}
|
||||
UserAgent::Android => {
|
||||
"Mozilla/5.0 (Android; Mobile; rv:37.0) Servo/1.0 Firefox/37.0"
|
||||
}
|
||||
UserAgent::Gonk => {
|
||||
"Mozilla/5.0 (Mobile; rv:37.0) Servo/1.0 Firefox/37.0"
|
||||
}
|
||||
}.to_owned()
|
||||
}
|
||||
|
||||
#[cfg(target_os="android")]
|
||||
const DEFAULT_USER_AGENT: &'static str = "Mozilla/5.0 (Android; Mobile; rv:37.0) Servo/1.0 Firefox/37.0";
|
||||
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Android;
|
||||
|
||||
// FIXME: This requires https://github.com/servo/servo/issues/7138 to provide the
|
||||
// correct string in Gonk builds (i.e., it will never be chosen today).
|
||||
#[cfg(target_os="gonk")]
|
||||
const DEFAULT_USER_AGENT: &'static str = "Mozilla/5.0 (Mobile; rv:37.0) Servo/1.0 Firefox/37.0";
|
||||
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Gonk;
|
||||
|
||||
#[cfg(not(any(target_os="android", target_os="gonk")))]
|
||||
const DEFAULT_USER_AGENT: &'static str =
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Servo/1.0 Firefox/37.0";
|
||||
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Desktop;
|
||||
|
||||
pub fn default_opts() -> Opts {
|
||||
Opts {
|
||||
|
@ -381,7 +400,7 @@ pub fn default_opts() -> Opts {
|
|||
devtools_port: None,
|
||||
webdriver_port: None,
|
||||
initial_window_size: Size2D::typed(800, 600),
|
||||
user_agent: None,
|
||||
user_agent: default_user_agent_string(DEFAULT_USER_AGENT),
|
||||
multiprocess: false,
|
||||
dump_flow_tree: false,
|
||||
dump_display_list: false,
|
||||
|
@ -424,7 +443,10 @@ pub fn from_cmdline_args(args: &[String]) {
|
|||
opts.optflagopt("", "devtools", "Start remote devtools server on port", "6000");
|
||||
opts.optflagopt("", "webdriver", "Start remote WebDriver server on port", "7000");
|
||||
opts.optopt("", "resolution", "Set window resolution.", "800x600");
|
||||
opts.optopt("u", "user-agent", "Set custom user agent string", "NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)");
|
||||
opts.optopt("u",
|
||||
"user-agent",
|
||||
"Set custom user agent string (or android / gonk / desktop for platform default)",
|
||||
"NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)");
|
||||
opts.optflag("M", "multiprocess", "Run in multiprocess mode");
|
||||
opts.optopt("Z", "debug",
|
||||
"A comma-separated string of debug options. Pass help to show available options.", "");
|
||||
|
@ -532,7 +554,13 @@ pub fn from_cmdline_args(args: &[String]) {
|
|||
}
|
||||
};
|
||||
|
||||
let user_agent = opt_match.opt_str("u").or(Some(DEFAULT_USER_AGENT.to_string()));
|
||||
let user_agent = match opt_match.opt_str("u") {
|
||||
Some(ref ua) if ua == "android" => default_user_agent_string(UserAgent::Android),
|
||||
Some(ref ua) if ua == "gonk" => default_user_agent_string(UserAgent::Gonk),
|
||||
Some(ref ua) if ua == "desktop" => default_user_agent_string(UserAgent::Desktop),
|
||||
Some(ua) => ua,
|
||||
None => default_user_agent_string(DEFAULT_USER_AGENT),
|
||||
};
|
||||
|
||||
let user_stylesheets = opt_match.opt_strs("user-stylesheet").iter().map(|filename| {
|
||||
let path = cwd.join(filename);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue