Move user input logic into servoshell (#30238)

* cleanup and move user input logix into servoshell

* fix fmt

* moves test from servoshell file

* move command-line args into servoshell

* remove feature media-gstreamer

* fix fmt

* move user input logic code into lib to make it more testable

* remove opts_matches in fn instead get it from main2

* remove pub and fix import

* add licence in new file

* revert passing Matches, instead pass Option String

* review update, also move sanitize fn to parser file

* fmt fix

* review fix: remove extra line
This commit is contained in:
Atbrakhi 2023-09-06 13:45:56 +02:00 committed by GitHub
parent f137b2f2c3
commit 3df284cf54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 66 deletions

1
Cargo.lock generated
View file

@ -5535,6 +5535,7 @@ dependencies = [
"sig",
"surfman",
"tinyfiledialogs",
"url",
"vergen",
"webxr",
"winapi",

View file

@ -24,9 +24,6 @@ use url::{self, Url};
pub struct Opts {
pub is_running_problem_test: bool,
/// The initial URL to load.
pub url: Option<ServoUrl>,
/// Whether or not the legacy layout system is enabled.
pub legacy_layout: bool,
@ -386,7 +383,6 @@ pub fn multiprocess() -> bool {
pub fn default_opts() -> Opts {
Opts {
is_running_problem_test: false,
url: None,
legacy_layout: false,
tile_size: 512,
time_profiling: None,
@ -598,15 +594,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
url.starts_with("http://web-platform.test:8000/_mozilla/css/canvas_over_area.html")
});
let url_opt = url_opt.and_then(|url_string| {
parse_url_or_filename(&cwd, url_string)
.map_err(|error| {
warn!("URL parsing failed ({:?}).", error);
error
})
.ok()
});
let tile_size: usize = match opt_match.opt_str("s") {
Some(tile_size_str) => tile_size_str
.parse()
@ -752,7 +739,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
let opts = Opts {
debug: debug_options.clone(),
is_running_problem_test,
url: url_opt,
legacy_layout,
tile_size,
time_profiling,
@ -815,13 +801,3 @@ pub fn set_options(opts: Opts) {
pub fn get() -> RwLockReadGuard<'static, Opts> {
OPTIONS.read().unwrap()
}
pub fn parse_url_or_filename(cwd: &Path, input: &str) -> Result<ServoUrl, ()> {
match ServoUrl::parse(input) {
Ok(url) => Ok(url),
Err(url::ParseError::RelativeUrlWithoutBase) => {
Url::from_file_path(&*cwd.join(input)).map(ServoUrl::from_url)
},
Err(_) => Err(()),
}
}

View file

@ -65,6 +65,7 @@ surfman = { workspace = true, features = ["sm-x11", "sm-raw-window-handle"] }
tinyfiledialogs = "3.0"
webxr = { git = "https://github.com/servo/webxr", features = ["ipc", "glwindow", "headless"] }
winit = "0.28.6"
url = { workspace = true }
[target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies]
image = { workspace = true }

View file

@ -8,23 +8,21 @@ use crate::browser::Browser;
use crate::embedder::EmbedderCallbacks;
use crate::events_loop::{EventsLoop, WakerEvent};
use crate::minibrowser::Minibrowser;
use crate::parser::get_default_url;
use crate::window_trait::WindowPortsMethods;
use crate::{headed_window, headless_window};
use gleam::gl;
use winit::window::WindowId;
use winit::event_loop::EventLoopWindowTarget;
use servo::compositing::windowing::EmbedderEvent;
use servo::config::opts::{self, parse_url_or_filename};
use servo::config::opts;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use servo::Servo;
use std::cell::{Cell, RefCell, RefMut};
use std::collections::HashMap;
use std::env;
use std::rc::Rc;
use surfman::GLApi;
use webxr::glwindow::GlWindowDiscovery;
use winit::window::WindowId;
use winit::event_loop::EventLoopWindowTarget;
pub struct App {
servo: Option<Servo<dyn WindowPortsMethods>>,
@ -46,6 +44,7 @@ impl App {
no_native_titlebar: bool,
device_pixels_per_px: Option<f32>,
user_agent: Option<String>,
url: Option<String>,
) {
let events_loop = EventsLoop::new(opts::get().headless, opts::get().output_file.is_some());
@ -63,6 +62,7 @@ impl App {
// Handle browser state.
let browser = Browser::new(window.clone());
let initial_url = get_default_url(url);
let mut app = App {
event_queue: RefCell::new(vec![]),
@ -139,7 +139,7 @@ impl App {
// is ready to present, so that we can paint the minibrowser then present.
servo.set_external_present(app.minibrowser.is_some());
servo.handle_events(vec![EmbedderEvent::NewBrowser(get_default_url(), servo_data.browser_id)]);
servo.handle_events(vec![EmbedderEvent::NewBrowser(initial_url.to_owned(), servo_data.browser_id)]);
servo.setup_logging();
app.windows.insert(window.id(), window.clone());
@ -341,17 +341,3 @@ impl App {
self.minibrowser.as_ref().map(|x| x.borrow_mut())
}
}
fn get_default_url() -> ServoUrl {
// If the url is not provided, we fallback to the homepage in prefs,
// or a blank page in case the homepage is not set either.
let cwd = env::current_dir().unwrap();
let cmdline_url = opts::get().url.clone();
let pref_url = {
let homepage_url = pref!(shell.homepage);
parse_url_or_filename(&cwd, &homepage_url).ok()
};
let blank_url = ServoUrl::parse("about:blank").ok();
cmdline_url.or(pref_url).or(blank_url).unwrap()
}

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use crate::keyutils::{CMD_OR_ALT, CMD_OR_CONTROL};
use crate::parser::sanitize_url;
use crate::window_trait::{WindowPortsMethods, LINE_HEIGHT};
use arboard::Clipboard;
use euclid::{Point2D, Vector2D};
@ -14,10 +15,8 @@ use servo::embedder_traits::{
};
use servo::msg::constellation_msg::TopLevelBrowsingContextId as BrowserId;
use servo::msg::constellation_msg::TraversalDirection;
use servo::net_traits::pub_domains::is_reg_domain;
use servo::script_traits::TouchEventType;
use servo::servo_config::opts;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use servo::webrender_api::ScrollLocation;
use std::env;
@ -634,23 +633,6 @@ fn get_selected_files(patterns: Vec<FilterPattern>, multiple_files: bool) -> Opt
.expect("Thread spawning failed")
}
fn sanitize_url(request: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(request)
.ok()
.or_else(|| {
if request.contains('/') || is_reg_domain(request) {
ServoUrl::parse(&format!("https://{}", request)).ok()
} else {
None
}
})
.or_else(|| {
let url = pref!(shell.searchpage).replace("%s", request);
ServoUrl::parse(&url).ok()
})
}
// This is a mitigation for #25498, not a verified solution.
// There may be codepaths in tinyfiledialog.c that this is
// inadquate against, as it passes the string via shell to

View file

@ -10,6 +10,9 @@ extern crate log;
#[macro_use]
extern crate sig;
#[cfg(test)]
mod test;
mod app;
mod backtrace;
mod browser;
@ -21,6 +24,7 @@ mod headed_window;
mod headless_window;
mod keyutils;
mod minibrowser;
mod parser;
mod prefs;
mod resources;
mod window_trait;
@ -164,7 +168,13 @@ pub fn main() {
let user_agent = opts_matches.opt_str("u");
App::run(do_not_use_native_titlebar, device_pixels_per_px, user_agent);
let url_opt = if !opts_matches.free.is_empty() {
Some(&opts_matches.free[0][..])
} else {
None
};
App::run(do_not_use_native_titlebar, device_pixels_per_px, user_agent, url_opt.map(|s| s.to_string()));
platform::deinit(clean_shutdown)
}

View file

@ -0,0 +1,61 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::env;
use std::path::Path;
use log::warn;
use servo::net_traits::pub_domains::is_reg_domain;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
use url::{self, Url};
pub fn parse_url_or_filename(cwd: &Path, input: &str) -> Result<ServoUrl, ()> {
match ServoUrl::parse(input) {
Ok(url) => Ok(url),
Err(url::ParseError::RelativeUrlWithoutBase) => {
Url::from_file_path(&*cwd.join(input)).map(ServoUrl::from_url)
},
Err(_) => Err(()),
}
}
pub fn get_default_url(url_opt: Option<String>) -> ServoUrl {
// If the url is not provided, we fallback to the homepage in prefs,
// or a blank page in case the homepage is not set either.
let cwd = env::current_dir().unwrap();
let cmdline_url = url_opt.map(|s| s.to_string()).and_then(|url_string| {
parse_url_or_filename(&cwd, &url_string)
.map_err(|error| {
warn!("URL parsing failed ({:?}).", error);
error
})
.ok()
});
let pref_url = {
let homepage_url = pref!(shell.homepage);
parse_url_or_filename(&cwd, &homepage_url).ok()
};
let blank_url = ServoUrl::parse("about:blank").ok();
cmdline_url.or(pref_url).or(blank_url).unwrap()
}
pub fn sanitize_url(request: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(request)
.ok()
.or_else(|| {
if request.contains('/') || is_reg_domain(request) {
ServoUrl::parse(&format!("https://{}", request)).ok()
} else {
None
}
})
.or_else(|| {
let url = pref!(shell.searchpage).replace("%s", request);
ServoUrl::parse(&url).ok()
})
}

View file

@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use servo_config::opts::parse_url_or_filename;
use std::path::Path;
use crate::parser::parse_url_or_filename;
#[cfg(not(target_os = "windows"))]
const FAKE_CWD: &'static str = "/fake/cwd";

View file

@ -197,6 +197,7 @@ class MachCommands(CommandBase):
test_patterns.append(test)
self_contained_tests = [
"servoshell",
"background_hang_monitor",
"gfx",
"layout_2013",