2014-09-19 17:15:03 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#![crate_name = "devtools"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
|
|
|
|
#![comment = "The Servo Parallel Browser Project"]
|
|
|
|
#![license = "MPL"]
|
|
|
|
|
2014-09-24 00:20:44 +04:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
2014-09-19 17:15:03 +04:00
|
|
|
#![feature(phase)]
|
|
|
|
|
|
|
|
#![feature(phase)]
|
|
|
|
#[phase(plugin, link)]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
/// An actor-based remote devtools server implementation. Only tested with nightly Firefox
|
|
|
|
/// versions at time of writing. Largely based on reverse-engineering of Firefox chrome
|
|
|
|
/// devtool logs and reading of [code](http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/).
|
|
|
|
|
|
|
|
extern crate collections;
|
|
|
|
extern crate core;
|
|
|
|
extern crate devtools_traits;
|
|
|
|
extern crate serialize;
|
|
|
|
extern crate sync;
|
2014-09-21 02:35:08 +04:00
|
|
|
extern crate "msg" as servo_msg;
|
2014-10-28 20:24:43 +03:00
|
|
|
extern crate "util" as servo_util;
|
2014-09-19 17:15:03 +04:00
|
|
|
|
|
|
|
use actor::{Actor, ActorRegistry};
|
|
|
|
use actors::console::ConsoleActor;
|
|
|
|
use actors::inspector::InspectorActor;
|
|
|
|
use actors::root::RootActor;
|
|
|
|
use actors::tab::TabActor;
|
2014-11-13 00:42:35 +03:00
|
|
|
use protocol::JsonPacketStream;
|
2014-09-19 17:15:03 +04:00
|
|
|
|
|
|
|
use devtools_traits::{ServerExitMsg, DevtoolsControlMsg, NewGlobal, DevtoolScriptControlMsg};
|
|
|
|
use servo_msg::constellation_msg::PipelineId;
|
2014-10-28 20:24:43 +03:00
|
|
|
use servo_util::task::spawn_named;
|
2014-09-19 17:15:03 +04:00
|
|
|
|
|
|
|
use std::cell::RefCell;
|
2014-12-04 08:48:59 +03:00
|
|
|
use std::collections::HashMap;
|
2014-09-19 17:15:03 +04:00
|
|
|
use std::comm;
|
|
|
|
use std::comm::{Disconnected, Empty};
|
|
|
|
use std::io::{TcpListener, TcpStream};
|
2014-11-13 00:42:35 +03:00
|
|
|
use std::io::{Acceptor, Listener, TimedOut};
|
2014-09-19 17:15:03 +04:00
|
|
|
use sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
mod actor;
|
|
|
|
/// Corresponds to http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/
|
|
|
|
mod actors {
|
|
|
|
pub mod console;
|
|
|
|
pub mod inspector;
|
|
|
|
pub mod root;
|
|
|
|
pub mod tab;
|
|
|
|
}
|
|
|
|
mod protocol;
|
|
|
|
|
2014-10-08 18:24:36 +04:00
|
|
|
/// Spin up a devtools server that listens for connections on the specified port.
|
|
|
|
pub fn start_server(port: u16) -> Sender<DevtoolsControlMsg> {
|
|
|
|
let (sender, receiver) = comm::channel();
|
2014-10-28 20:24:43 +03:00
|
|
|
spawn_named("Devtools", proc() {
|
2014-10-08 18:24:36 +04:00
|
|
|
run_server(receiver, port)
|
2014-09-19 17:15:03 +04:00
|
|
|
});
|
2014-10-08 18:24:36 +04:00
|
|
|
sender
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static POLL_TIMEOUT: u64 = 300;
|
|
|
|
|
2014-10-08 18:24:36 +04:00
|
|
|
fn run_server(receiver: Receiver<DevtoolsControlMsg>, port: u16) {
|
2014-11-13 06:48:31 +03:00
|
|
|
let listener = TcpListener::bind(format!("{}:{}", "127.0.0.1", port).as_slice());
|
2014-09-19 17:15:03 +04:00
|
|
|
|
|
|
|
// bind the listener to the specified address
|
|
|
|
let mut acceptor = listener.listen().unwrap();
|
|
|
|
acceptor.set_timeout(Some(POLL_TIMEOUT));
|
|
|
|
|
|
|
|
let mut registry = ActorRegistry::new();
|
|
|
|
|
|
|
|
let root = box RootActor {
|
|
|
|
tabs: vec!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
registry.register(root);
|
|
|
|
registry.find::<RootActor>("root");
|
|
|
|
|
|
|
|
let actors = Arc::new(Mutex::new(registry));
|
|
|
|
|
2014-11-17 16:15:30 +03:00
|
|
|
let mut accepted_connections: Vec<TcpStream> = Vec::new();
|
|
|
|
|
2014-12-04 08:48:59 +03:00
|
|
|
let mut actor_pipelines: HashMap<PipelineId, String> = HashMap::new();
|
|
|
|
|
2014-09-19 17:15:03 +04:00
|
|
|
/// Process the input from a single devtools client until EOF.
|
|
|
|
fn handle_client(actors: Arc<Mutex<ActorRegistry>>, mut stream: TcpStream) {
|
2014-11-13 06:48:31 +03:00
|
|
|
println!("connection established to {}", stream.peer_name().unwrap());
|
2014-09-19 17:15:03 +04:00
|
|
|
{
|
2014-11-13 06:48:31 +03:00
|
|
|
let actors = actors.lock();
|
2014-09-19 17:15:03 +04:00
|
|
|
let msg = actors.find::<RootActor>("root").encodable();
|
|
|
|
stream.write_json_packet(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
'outer: loop {
|
2014-11-13 00:42:35 +03:00
|
|
|
match stream.read_json_packet() {
|
2014-12-05 04:04:06 +03:00
|
|
|
Ok(json_packet) => {
|
|
|
|
match actors.lock().handle_message(json_packet.as_object().unwrap(),
|
|
|
|
&mut stream) {
|
|
|
|
Ok(()) => {},
|
|
|
|
Err(()) => {
|
|
|
|
println!("error: devtools actor stopped responding");
|
2014-12-08 22:58:09 +03:00
|
|
|
let _ = stream.close_read();
|
|
|
|
let _ = stream.close_write();
|
2014-12-05 04:04:06 +03:00
|
|
|
break 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 00:42:35 +03:00
|
|
|
Err(e) => {
|
|
|
|
println!("error: {}", e.desc);
|
|
|
|
break 'outer
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need separate actor representations for each script global that exists;
|
|
|
|
// clients can theoretically connect to multiple globals simultaneously.
|
|
|
|
// TODO: move this into the root or tab modules?
|
|
|
|
fn handle_new_global(actors: Arc<Mutex<ActorRegistry>>,
|
|
|
|
pipeline: PipelineId,
|
2014-12-04 08:48:59 +03:00
|
|
|
sender: Sender<DevtoolScriptControlMsg>,
|
|
|
|
actor_pipelines: &mut HashMap<PipelineId, String>) {
|
2014-09-19 17:15:03 +04:00
|
|
|
let mut actors = actors.lock();
|
|
|
|
|
|
|
|
//TODO: move all this actor creation into a constructor method on TabActor
|
|
|
|
let (tab, console, inspector) = {
|
|
|
|
let console = ConsoleActor {
|
|
|
|
name: actors.new_name("console"),
|
|
|
|
script_chan: sender.clone(),
|
|
|
|
pipeline: pipeline,
|
2014-12-04 08:48:59 +03:00
|
|
|
streams: RefCell::new(Vec::new()),
|
2014-09-19 17:15:03 +04:00
|
|
|
};
|
|
|
|
let inspector = InspectorActor {
|
|
|
|
name: actors.new_name("inspector"),
|
|
|
|
walker: RefCell::new(None),
|
|
|
|
pageStyle: RefCell::new(None),
|
|
|
|
highlighter: RefCell::new(None),
|
|
|
|
script_chan: sender,
|
|
|
|
pipeline: pipeline,
|
|
|
|
};
|
|
|
|
//TODO: send along the current page title and URL
|
|
|
|
let tab = TabActor {
|
|
|
|
name: actors.new_name("tab"),
|
|
|
|
title: "".to_string(),
|
|
|
|
url: "about:blank".to_string(),
|
|
|
|
console: console.name(),
|
|
|
|
inspector: inspector.name(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let root = actors.find_mut::<RootActor>("root");
|
|
|
|
root.tabs.push(tab.name.clone());
|
|
|
|
(tab, console, inspector)
|
|
|
|
};
|
|
|
|
|
2014-12-04 08:48:59 +03:00
|
|
|
actor_pipelines.insert(pipeline, tab.name.clone());
|
2014-09-19 17:15:03 +04:00
|
|
|
actors.register(box tab);
|
|
|
|
actors.register(box console);
|
|
|
|
actors.register(box inspector);
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: figure out some system that allows us to watch for new connections,
|
|
|
|
// shut down existing ones at arbitrary times, and also watch for messages
|
|
|
|
// from multiple script tasks simultaneously. Polling for new connections
|
|
|
|
// for 300ms and then checking the receiver is not a good compromise
|
|
|
|
// (and makes Servo hang on exit if there's an open connection, no less).
|
|
|
|
// accept connections and process them, spawning a new tasks for each one
|
2014-09-21 02:35:08 +04:00
|
|
|
loop {
|
|
|
|
match acceptor.accept() {
|
2014-09-19 17:15:03 +04:00
|
|
|
Err(ref e) if e.kind == TimedOut => {
|
2014-10-08 18:24:36 +04:00
|
|
|
match receiver.try_recv() {
|
2014-09-19 17:15:03 +04:00
|
|
|
Ok(ServerExitMsg) | Err(Disconnected) => break,
|
2014-12-04 08:48:59 +03:00
|
|
|
Ok(NewGlobal(id, sender)) => handle_new_global(actors.clone(), id, sender, &mut actor_pipelines),
|
2014-09-19 17:15:03 +04:00
|
|
|
Err(Empty) => acceptor.set_timeout(Some(POLL_TIMEOUT)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_e) => { /* connection failed */ }
|
|
|
|
Ok(stream) => {
|
|
|
|
let actors = actors.clone();
|
2014-11-17 16:15:30 +03:00
|
|
|
accepted_connections.push(stream.clone());
|
2014-10-28 20:24:43 +03:00
|
|
|
spawn_named("DevtoolsClientHandler", proc() {
|
2014-09-19 17:15:03 +04:00
|
|
|
// connection succeeded
|
|
|
|
handle_client(actors, stream.clone())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 16:15:30 +03:00
|
|
|
|
|
|
|
for connection in accepted_connections.iter_mut() {
|
|
|
|
let _read = connection.close_read();
|
|
|
|
let _write = connection.close_write();
|
|
|
|
}
|
2014-09-19 17:15:03 +04:00
|
|
|
}
|