Auto merge of #24007 - gterzian:use_router_in_webdriver, r=asajeffrey

Use ipc router in webdriver

<!-- Please describe your changes on the following line: -->

Fix for https://github.com/servo/servo/issues/23905#issuecomment-522588119

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [ ] `./mach build -d` does not report any errors
- [ ] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/24007)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-08-21 05:54:45 -04:00 committed by GitHub
commit 47aa1ccaa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,8 @@
#![crate_type = "rlib"]
#![deny(unsafe_code)]
#[macro_use]
extern crate crossbeam_channel;
#[macro_use]
extern crate log;
#[macro_use]
@ -19,11 +21,12 @@ mod capabilities;
use crate::actions::InputSourceState;
use base64;
use capabilities::ServoCapabilities;
use crossbeam_channel::Sender;
use crossbeam_channel::{after, unbounded, Receiver, Sender};
use euclid::{Rect, Size2D};
use hyper::Method;
use image::{DynamicImage, ImageFormat, RgbImage};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use keyboard_types::webdriver::send_keys;
use msg::constellation_msg::{BrowsingContextId, TopLevelBrowsingContextId, TraversalDirection};
use pixels::PixelFormat;
@ -171,6 +174,14 @@ impl WebDriverSession {
}
struct Handler {
/// The threaded receiver on which we can block for a load-status.
/// It will receive messages sent on the load_status_sender,
/// and forwarded by the IPC router.
load_status_receiver: Receiver<LoadStatus>,
/// The IPC sender which we can clone and pass along to the constellation,
/// for it to send us a load-status. Messages sent on it
/// will be forwarded to the load_status_receiver.
load_status_sender: IpcSender<LoadStatus>,
session: Option<WebDriverSession>,
constellation_chan: Sender<ConstellationMsg>,
resize_timeout: u32,
@ -371,7 +382,17 @@ impl<'de> Visitor<'de> for TupleVecMapVisitor {
impl Handler {
pub fn new(constellation_chan: Sender<ConstellationMsg>) -> Handler {
// Create a pair of both an IPC and a threaded channel,
// keep the IPC sender to clone and pass to the constellation for each load,
// and keep a threaded receiver to block on an incoming load-status.
// Pass the others to the IPC router so that IPC messages are forwarded to the threaded receiver.
// We need to use the router because IPC does not come with a timeout on receive/select.
let (load_status_sender, receiver) = ipc::channel().unwrap();
let (sender, load_status_receiver) = unbounded();
ROUTER.route_ipc_receiver_to_crossbeam_sender(receiver, sender);
Handler {
load_status_sender,
load_status_receiver,
session: None,
constellation_chan: constellation_chan,
resize_timeout: 500,
@ -613,35 +634,26 @@ impl Handler {
let top_level_browsing_context_id = self.session()?.top_level_browsing_context_id;
let (sender, receiver) = ipc::channel().unwrap();
let load_data = LoadData::new(LoadOrigin::WebDriver, url, None, None, None);
let cmd_msg =
WebDriverCommandMsg::LoadUrl(top_level_browsing_context_id, load_data, sender.clone());
let cmd_msg = WebDriverCommandMsg::LoadUrl(
top_level_browsing_context_id,
load_data,
self.load_status_sender.clone(),
);
self.constellation_chan
.send(ConstellationMsg::WebDriverCommand(cmd_msg))
.unwrap();
self.wait_for_load(sender, receiver)
self.wait_for_load()
}
fn wait_for_load(
&self,
sender: IpcSender<LoadStatus>,
receiver: IpcReceiver<LoadStatus>,
) -> WebDriverResult<WebDriverResponse> {
fn wait_for_load(&self) -> WebDriverResult<WebDriverResponse> {
let timeout = self.session()?.load_timeout;
thread::spawn(move || {
thread::sleep(Duration::from_millis(timeout));
let _ = sender.send(LoadStatus::LoadTimeout);
});
// wait to get a load event
match receiver.recv().unwrap() {
LoadStatus::LoadComplete => Ok(WebDriverResponse::Void),
LoadStatus::LoadTimeout => {
Err(WebDriverError::new(ErrorStatus::Timeout, "Load timed out"))
},
select! {
recv(self.load_status_receiver) -> _ => Ok(WebDriverResponse::Void),
recv(after(Duration::from_millis(timeout))) -> _ => Err(
WebDriverError::new(ErrorStatus::Timeout, "Load timed out")
),
}
}
@ -775,14 +787,15 @@ impl Handler {
fn handle_refresh(&self) -> WebDriverResult<WebDriverResponse> {
let top_level_browsing_context_id = self.session()?.top_level_browsing_context_id;
let (sender, receiver) = ipc::channel().unwrap();
let cmd_msg = WebDriverCommandMsg::Refresh(top_level_browsing_context_id, sender.clone());
let cmd_msg = WebDriverCommandMsg::Refresh(
top_level_browsing_context_id,
self.load_status_sender.clone(),
);
self.constellation_chan
.send(ConstellationMsg::WebDriverCommand(cmd_msg))
.unwrap();
self.wait_for_load(sender, receiver)
self.wait_for_load()
}
fn handle_title(&self) -> WebDriverResult<WebDriverResponse> {