Implement trait-based ResourceThreads and clean up related naming issues

Changes include:

- Introduce an IpcSend trait to abstract over a collection of IpcSenders
- Implement ResourceThreads collection to abstract the resource-related
  sub threads across the component
- Rename original ResourceThread and ControlMsg into an unifed CoreResource__
  to accommodate above changes and avoid confusions
This commit is contained in:
Zhen Zhang 2016-05-18 00:07:42 +08:00
parent 051a749e0d
commit a51db4cfa8
22 changed files with 213 additions and 179 deletions

View file

@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use ipc_channel::ipc;
use net::resource_thread::new_resource_thread;
use net::resource_thread::new_core_resource_thread;
use net_traits::hosts::{parse_hostsfile, host_replacement};
use net_traits::{ControlMsg, LoadData, LoadConsumer, LoadContext, NetworkError, ProgressMsg};
use net_traits::{CoreResourceMsg, LoadData, LoadConsumer, LoadContext, NetworkError, ProgressMsg};
use profile_traits::time::ProfilerChan;
use std::borrow::ToOwned;
use std::collections::HashMap;
@ -20,17 +20,17 @@ fn ip(s: &str) -> IpAddr {
#[test]
fn test_exit() {
let (tx, _rx) = ipc::channel().unwrap();
let resource_thread = new_resource_thread("".to_owned(), None, ProfilerChan(tx));
resource_thread.send(ControlMsg::Exit).unwrap();
let resource_thread = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx));
resource_thread.send(CoreResourceMsg::Exit).unwrap();
}
#[test]
fn test_bad_scheme() {
let (tx, _rx) = ipc::channel().unwrap();
let resource_thread = new_resource_thread("".to_owned(), None, ProfilerChan(tx));
let resource_thread = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx));
let (start_chan, start) = ipc::channel().unwrap();
let url = Url::parse("bogus://whatever").unwrap();
resource_thread.send(ControlMsg::Load(LoadData::new(LoadContext::Browsing, url, None, None, None),
resource_thread.send(CoreResourceMsg::Load(LoadData::new(LoadContext::Browsing, url, None, None, None),
LoadConsumer::Channel(start_chan), None)).unwrap();
let response = start.recv().unwrap();
@ -38,7 +38,7 @@ fn test_bad_scheme() {
ProgressMsg::Done(result) => { assert!(result.is_err()) }
_ => panic!("bleh")
}
resource_thread.send(ControlMsg::Exit).unwrap();
resource_thread.send(CoreResourceMsg::Exit).unwrap();
}
#[test]
@ -204,20 +204,20 @@ fn test_cancelled_listener() {
});
let (tx, _rx) = ipc::channel().unwrap();
let resource_thread = new_resource_thread("".to_owned(), None, ProfilerChan(tx));
let resource_thread = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx));
let (sender, receiver) = ipc::channel().unwrap();
let (id_sender, id_receiver) = ipc::channel().unwrap();
let (sync_sender, sync_receiver) = ipc::channel().unwrap();
let url = Url::parse(&format!("http://127.0.0.1:{}", port)).unwrap();
resource_thread.send(ControlMsg::Load(LoadData::new(LoadContext::Browsing, url, None, None, None),
resource_thread.send(CoreResourceMsg::Load(LoadData::new(LoadContext::Browsing, url, None, None, None),
LoadConsumer::Channel(sender),
Some(id_sender))).unwrap();
// get the `ResourceId` and send a cancel message, which should stop the loading loop
let res_id = id_receiver.recv().unwrap();
resource_thread.send(ControlMsg::Cancel(res_id)).unwrap();
resource_thread.send(CoreResourceMsg::Cancel(res_id)).unwrap();
// synchronize with the resource_thread loop, so that we don't simply send everything at once!
resource_thread.send(ControlMsg::Synchronize(sync_sender)).unwrap();
resource_thread.send(CoreResourceMsg::Synchronize(sync_sender)).unwrap();
let _ = sync_receiver.recv();
// now, let's send the body, because the connection is still active and data would be loaded
// (but, the loading has been cancelled)
@ -225,5 +225,5 @@ fn test_cancelled_listener() {
let response = receiver.recv().unwrap();
assert_eq!(response.progress_port.recv().unwrap(),
ProgressMsg::Done(Err(NetworkError::LoadCancelled)));
resource_thread.send(ControlMsg::Exit).unwrap();
resource_thread.send(CoreResourceMsg::Exit).unwrap();
}