Allow to specify port with --devtools option (fixes #3597)

Note that using `servo --devtools http://example.org` doesn't work. In
that case either the port must be specified or the option moved to the
end. This is done by getopts and is the same for other such options,
e.g. `--profile`.
This commit is contained in:
Robin Stocker 2014-10-08 23:29:15 +11:00
parent 94731270df
commit 8a5c6a0d3b
4 changed files with 21 additions and 19 deletions

View file

@ -58,20 +58,19 @@ mod actors {
} }
mod protocol; mod protocol;
/// Spin up a devtools server that listens for connections. Defaults to port 6000. /// Spin up a devtools server that listens for connections on the specified port.
/// TODO: allow specifying a port pub fn start_server(port: u16) -> Sender<DevtoolsControlMsg> {
pub fn start_server() -> Sender<DevtoolsControlMsg> { let (sender, receiver) = comm::channel();
let (chan, port) = comm::channel();
TaskBuilder::new().named("devtools").spawn(proc() { TaskBuilder::new().named("devtools").spawn(proc() {
run_server(port) run_server(receiver, port)
}); });
chan sender
} }
static POLL_TIMEOUT: u64 = 300; static POLL_TIMEOUT: u64 = 300;
fn run_server(port: Receiver<DevtoolsControlMsg>) { fn run_server(receiver: Receiver<DevtoolsControlMsg>, port: u16) {
let listener = TcpListener::bind("127.0.0.1", 6000); let listener = TcpListener::bind("127.0.0.1", port);
// bind the listener to the specified address // bind the listener to the specified address
let mut acceptor = listener.listen().unwrap(); let mut acceptor = listener.listen().unwrap();
@ -185,7 +184,7 @@ fn run_server(port: Receiver<DevtoolsControlMsg>) {
loop { loop {
match acceptor.accept() { match acceptor.accept() {
Err(ref e) if e.kind == TimedOut => { Err(ref e) if e.kind == TimedOut => {
match port.try_recv() { match receiver.try_recv() {
Ok(ServerExitMsg) | Err(Disconnected) => break, Ok(ServerExitMsg) | Err(Disconnected) => break,
Ok(NewGlobal(id, sender)) => handle_new_global(actors.clone(), id, sender), Ok(NewGlobal(id, sender)) => handle_new_global(actors.clone(), id, sender),
Err(Empty) => acceptor.set_timeout(Some(POLL_TIMEOUT)), Err(Empty) => acceptor.set_timeout(Some(POLL_TIMEOUT)),

View file

@ -85,8 +85,9 @@ pub struct Opts {
/// and render. /// and render.
pub trace_layout: bool, pub trace_layout: bool,
/// True if we should start a server to listen to remote Firefox devtools connections. /// `None` to disable devtools or `Some` with a port number to start a server to listen to
pub devtools_server: bool, /// remote Firefox devtools connections.
pub devtools_port: Option<u16>,
/// The initial requested size of the window. /// The initial requested size of the window.
pub initial_window_size: TypedSize2D<ScreenPx, uint>, pub initial_window_size: TypedSize2D<ScreenPx, uint>,
@ -127,7 +128,7 @@ pub fn from_cmdline_args(args: &[String]) -> Option<Opts> {
getopts::optflag("", "show-debug-borders", "Show debugging borders on layers and tiles."), getopts::optflag("", "show-debug-borders", "Show debugging borders on layers and tiles."),
getopts::optflag("", "disable-text-aa", "Disable antialiasing for text rendering."), getopts::optflag("", "disable-text-aa", "Disable antialiasing for text rendering."),
getopts::optflag("", "trace-layout", "Write layout trace to external file for debugging."), getopts::optflag("", "trace-layout", "Write layout trace to external file for debugging."),
getopts::optflag("", "devtools", "Start remote devtools server"), getopts::optflagopt("", "devtools", "Start remote devtools server on port", "6000"),
getopts::optopt("", "resolution", "Set window resolution.", "800x600"), getopts::optopt("", "resolution", "Set window resolution.", "800x600"),
getopts::optopt("u", "user-agent", "Set custom user agent string", "NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)"), getopts::optopt("u", "user-agent", "Set custom user agent string", "NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)"),
getopts::optflag("h", "help", "Print this message") getopts::optflag("h", "help", "Print this message")
@ -211,6 +212,10 @@ pub fn from_cmdline_args(args: &[String]) -> Option<Opts> {
bubble_inline_sizes_separately = true; bubble_inline_sizes_separately = true;
} }
let devtools_port = opt_match.opt_default("devtools", "6000").map(|port| {
from_str(port.as_slice()).unwrap()
});
let initial_window_size = match opt_match.opt_str("resolution") { let initial_window_size = match opt_match.opt_str("resolution") {
Some(res_string) => { Some(res_string) => {
let res: Vec<uint> = res_string.as_slice().split('x').map(|r| from_str(r).unwrap()).collect(); let res: Vec<uint> = res_string.as_slice().split('x').map(|r| from_str(r).unwrap()).collect();
@ -240,7 +245,7 @@ pub fn from_cmdline_args(args: &[String]) -> Option<Opts> {
show_debug_borders: opt_match.opt_present("show-debug-borders"), show_debug_borders: opt_match.opt_present("show-debug-borders"),
enable_text_antialiasing: !opt_match.opt_present("disable-text-aa"), enable_text_antialiasing: !opt_match.opt_present("disable-text-aa"),
trace_layout: trace_layout, trace_layout: trace_layout,
devtools_server: opt_match.opt_present("devtools"), devtools_port: devtools_port,
initial_window_size: initial_window_size, initial_window_size: initial_window_size,
user_agent: opt_match.opt_str("u"), user_agent: opt_match.opt_str("u"),
}) })

View file

@ -68,7 +68,7 @@ pub extern "C" fn cef_run_message_loop() {
show_debug_borders: false, show_debug_borders: false,
enable_text_antialiasing: true, enable_text_antialiasing: true,
trace_layout: false, trace_layout: false,
devtools_server: false, devtools_port: None,
initial_window_size: TypedSize2D(800, 600), initial_window_size: TypedSize2D(800, 600),
user_agent: None, user_agent: None,
}; };

View file

@ -95,11 +95,9 @@ pub fn run(opts: opts::Opts) {
let (compositor_port, compositor_chan) = CompositorChan::new(); let (compositor_port, compositor_chan) = CompositorChan::new();
let time_profiler_chan = TimeProfiler::create(opts.time_profiler_period); let time_profiler_chan = TimeProfiler::create(opts.time_profiler_period);
let memory_profiler_chan = MemoryProfiler::create(opts.memory_profiler_period); let memory_profiler_chan = MemoryProfiler::create(opts.memory_profiler_period);
let devtools_chan = if opts.devtools_server { let devtools_chan = opts.devtools_port.map(|port| {
Some(devtools::start_server()) devtools::start_server(port)
} else { });
None
};
let opts_clone = opts.clone(); let opts_clone = opts.clone();
let time_profiler_chan_clone = time_profiler_chan.clone(); let time_profiler_chan_clone = time_profiler_chan.clone();