From 41df45bd18439a32e3eaa65df4eabf52fd549976 Mon Sep 17 00:00:00 2001 From: Eddy Bruel Date: Mon, 12 Sep 2016 10:19:17 -0500 Subject: [PATCH] servo: Merge #13204 - Implement a WebSocket server for debugging (from ejpbruel:debugger); r=jdm This pull request adds a very simple WebSocket server to Servo, that we intend to use for debugging. It currently only echoes back messages, but eventually we want this server to implement the Chrome Debugging Protocol. --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). - [ ] There are tests for these changes OR - [X] These changes do not require tests because this is a prototype. Source-Repo: https://github.com/servo/servo Source-Revision: f834c893c7cfde631e458564499d85e8895b4482 --- servo/components/debugger/Cargo.toml | 15 +++++++++ servo/components/debugger/lib.rs | 50 ++++++++++++++++++++++++++++ servo/components/servo/Cargo.lock | 9 +++++ servo/components/servo/Cargo.toml | 1 + servo/components/servo/lib.rs | 4 +++ servo/components/util/opts.rs | 12 +++++++ servo/ports/cef/Cargo.lock | 9 +++++ 7 files changed, 100 insertions(+) create mode 100644 servo/components/debugger/Cargo.toml create mode 100644 servo/components/debugger/lib.rs diff --git a/servo/components/debugger/Cargo.toml b/servo/components/debugger/Cargo.toml new file mode 100644 index 000000000000..9a12e74b5bb7 --- /dev/null +++ b/servo/components/debugger/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "debugger" +version = "0.0.1" +authors = ["The Servo Project Developers"] +license = "MPL-2.0" +publish = false + +[lib] +name = "debugger" +path = "lib.rs" +crate_type = ["rlib"] + +[dependencies] +util = { path = "../util" } +websocket = "0.17.1" diff --git a/servo/components/debugger/lib.rs b/servo/components/debugger/lib.rs new file mode 100644 index 000000000000..d2f9713c4f93 --- /dev/null +++ b/servo/components/debugger/lib.rs @@ -0,0 +1,50 @@ +/* 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 http://mozilla.org/MPL/2.0/. */ + +extern crate util; +extern crate websocket; + +use util::thread::spawn_named; +use websocket::{Message, Receiver, Sender, Server}; +use websocket::message::Type; + +pub fn start_server(port: u16) { + println!("Starting debugger server."); + spawn_named("debugger-server".to_owned(), move || { + run_server(port) + }); +} + +fn run_server(port: u16) { + let server = Server::bind(("127.0.0.1", port)).unwrap(); + for connection in server { + spawn_named("debugger-connection".to_owned(), move || { + let connection = connection.unwrap(); + let request = connection.read_request().unwrap(); + let response = request.accept(); + let client = response.send().unwrap(); + let (mut sender, mut receiver) = client.split(); + for message in receiver.incoming_messages() { + let message: Message = message.unwrap(); + match message.opcode { + Type::Close => { + let message = Message::close(); + sender.send_message(&message).unwrap(); + break; + } + Type::Ping => { + let message = Message::pong(message.payload); + sender.send_message(&message).unwrap(); + } + Type::Text => { + sender.send_message(&message).unwrap(); + } + _ => { + panic!("Unexpected message type."); + } + } + } + }); + } +} diff --git a/servo/components/servo/Cargo.lock b/servo/components/servo/Cargo.lock index e76e13236f0f..17b6d7feec4f 100644 --- a/servo/components/servo/Cargo.lock +++ b/servo/components/servo/Cargo.lock @@ -10,6 +10,7 @@ dependencies = [ "compiletest_helper 0.0.1", "compositing 0.0.1", "constellation 0.0.1", + "debugger 0.0.1", "devtools 0.0.1", "devtools_traits 0.0.1", "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -481,6 +482,14 @@ dependencies = [ "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "debugger" +version = "0.0.1" +dependencies = [ + "util 0.0.1", + "websocket 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "deque" version = "0.3.1" diff --git a/servo/components/servo/Cargo.toml b/servo/components/servo/Cargo.toml index 40ebdcd1bf58..4943a01d98d1 100644 --- a/servo/components/servo/Cargo.toml +++ b/servo/components/servo/Cargo.toml @@ -49,6 +49,7 @@ canvas = {path = "../canvas"} canvas_traits = {path = "../canvas_traits"} compositing = {path = "../compositing"} constellation = {path = "../constellation"} +debugger = {path = "../debugger"} devtools = {path = "../devtools"} devtools_traits = {path = "../devtools_traits"} env_logger = "0.3" diff --git a/servo/components/servo/lib.rs b/servo/components/servo/lib.rs index 11a13d6c1ccd..47038533fe42 100644 --- a/servo/components/servo/lib.rs +++ b/servo/components/servo/lib.rs @@ -28,6 +28,7 @@ pub extern crate canvas; pub extern crate canvas_traits; pub extern crate compositing; pub extern crate constellation; +pub extern crate debugger; pub extern crate devtools; pub extern crate devtools_traits; pub extern crate euclid; @@ -125,6 +126,9 @@ impl Browser where Window: WindowMethods + 'static { let time_profiler_chan = profile_time::Profiler::create(&opts.time_profiling, opts.time_profiler_trace_path.clone()); let mem_profiler_chan = profile_mem::Profiler::create(opts.mem_profiler_period); + if let Some(port) = opts.debugger_port { + debugger::start_server(port) + } let devtools_chan = opts.devtools_port.map(|port| { devtools::start_server(port) }); diff --git a/servo/components/util/opts.rs b/servo/components/util/opts.rs index 209fe4bb500a..ec7c0235fd9b 100644 --- a/servo/components/util/opts.rs +++ b/servo/components/util/opts.rs @@ -123,6 +123,10 @@ pub struct Opts { /// Enable all heartbeats for profiling. pub profile_heartbeats: bool, + /// `None` to disable debugger or `Some` with a port number to start a server to listen to + /// remote Firefox debugger connections. + pub debugger_port: Option, + /// `None` to disable devtools or `Some` with a port number to start a server to listen to /// remote Firefox devtools connections. pub devtools_port: Option, @@ -502,6 +506,7 @@ pub fn default_opts() -> Opts { enable_text_antialiasing: false, enable_canvas_antialiasing: false, trace_layout: false, + debugger_port: None, devtools_port: None, webdriver_port: None, initial_window_size: TypedSize2D::new(1024, 740), @@ -562,6 +567,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult { opts.optflag("z", "headless", "Headless mode"); opts.optflag("f", "hard-fail", "Exit on thread failure instead of displaying about:failure"); opts.optflag("F", "soft-fail", "Display about:failure on thread failure instead of exiting"); + opts.optflagopt("", "remote-debugging-port", "Start remote debugger server on port", "2794"); 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.", "1024x740"); @@ -721,6 +727,11 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult { bubble_inline_sizes_separately = true; } + let debugger_port = opt_match.opt_default("remote-debugging-port", "2794").map(|port| { + port.parse() + .unwrap_or_else(|err| args_fail(&format!("Error parsing option: --remote-debugging-port ({})", err))) + }); + let devtools_port = opt_match.opt_default("devtools", "6000").map(|port| { port.parse().unwrap_or_else(|err| args_fail(&format!("Error parsing option: --devtools ({})", err))) }); @@ -802,6 +813,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult { profile_script_events: debug_options.profile_script_events, profile_heartbeats: debug_options.profile_heartbeats, trace_layout: debug_options.trace_layout, + debugger_port: debugger_port, devtools_port: devtools_port, webdriver_port: webdriver_port, initial_window_size: initial_window_size, diff --git a/servo/ports/cef/Cargo.lock b/servo/ports/cef/Cargo.lock index bd787629d5bf..f2cb44d3a939 100644 --- a/servo/ports/cef/Cargo.lock +++ b/servo/ports/cef/Cargo.lock @@ -439,6 +439,14 @@ dependencies = [ "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "debugger" +version = "0.0.1" +dependencies = [ + "util 0.0.1", + "websocket 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "deque" version = "0.3.1" @@ -1971,6 +1979,7 @@ dependencies = [ "canvas_traits 0.0.1", "compositing 0.0.1", "constellation 0.0.1", + "debugger 0.0.1", "devtools 0.0.1", "devtools_traits 0.0.1", "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",