mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Abstract out ResourceManager messaging from impl
De-coupling makes testing a bit easier.
This commit is contained in:
parent
ff1777e446
commit
f284181781
2 changed files with 80 additions and 35 deletions
|
@ -184,7 +184,16 @@ pub fn new_resource_task(user_agent: Option<String>,
|
||||||
let (setup_chan, setup_port) = channel();
|
let (setup_chan, setup_port) = channel();
|
||||||
let setup_chan_clone = setup_chan.clone();
|
let setup_chan_clone = setup_chan.clone();
|
||||||
spawn_named("ResourceManager".to_owned(), move || {
|
spawn_named("ResourceManager".to_owned(), move || {
|
||||||
ResourceManager::new(setup_port, user_agent, setup_chan_clone, hsts_preload, devtools_chan).start();
|
let resource_manager = ResourceManager::new(
|
||||||
|
user_agent, setup_chan_clone, hsts_preload, devtools_chan
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut channel_manager = ResourceChannelManager {
|
||||||
|
from_client: setup_port,
|
||||||
|
resource_manager: resource_manager
|
||||||
|
};
|
||||||
|
|
||||||
|
channel_manager.start();
|
||||||
});
|
});
|
||||||
setup_chan
|
setup_chan
|
||||||
}
|
}
|
||||||
|
@ -294,20 +303,16 @@ impl HSTSList {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn secure_load_data(load_data: &LoadData) -> LoadData {
|
pub fn secure_load_data(load_data: &LoadData) -> LoadData {
|
||||||
if let Some(h) = load_data.url.domain() {
|
match &*load_data.url.scheme {
|
||||||
match &*load_data.url.scheme {
|
"http" => {
|
||||||
"http" => {
|
let mut secure_load_data = load_data.clone();
|
||||||
let mut secure_load_data = load_data.clone();
|
let mut secure_url = load_data.url.clone();
|
||||||
let mut secure_url = load_data.url.clone();
|
secure_url.scheme = "https".to_string();
|
||||||
secure_url.scheme = "https".to_string();
|
secure_load_data.url = secure_url;
|
||||||
secure_load_data.url = secure_url;
|
|
||||||
|
|
||||||
secure_load_data
|
secure_load_data
|
||||||
},
|
},
|
||||||
_ => load_data.clone()
|
_ => load_data.clone()
|
||||||
}
|
|
||||||
} else {
|
|
||||||
load_data.clone()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,10 +348,36 @@ pub fn replace_hosts(mut load_data: LoadData, host_table: *mut HashMap<String, S
|
||||||
return load_data;
|
return load_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ResourceManager {
|
struct ResourceChannelManager {
|
||||||
from_client: Receiver<ControlMsg>,
|
from_client: Receiver<ControlMsg>,
|
||||||
|
resource_manager: ResourceManager
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResourceChannelManager {
|
||||||
|
fn start(&mut self) {
|
||||||
|
loop {
|
||||||
|
match self.from_client.recv().unwrap() {
|
||||||
|
ControlMsg::Load(load_data, consumer) => {
|
||||||
|
self.resource_manager.load(load_data, consumer)
|
||||||
|
}
|
||||||
|
ControlMsg::SetCookiesForUrl(request, cookie_list, source) => {
|
||||||
|
self.resource_manager.set_cookies_for_url(request, cookie_list, source)
|
||||||
|
}
|
||||||
|
ControlMsg::GetCookiesForUrl(url, consumer, source) => {
|
||||||
|
consumer.send(self.resource_manager.cookie_storage.cookies_for_url(&url, source)).unwrap();
|
||||||
|
}
|
||||||
|
ControlMsg::Exit => {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ResourceManager {
|
||||||
user_agent: Option<String>,
|
user_agent: Option<String>,
|
||||||
cookie_storage: CookieStorage,
|
cookie_storage: CookieStorage,
|
||||||
|
// TODO: Can this be de-coupled?
|
||||||
resource_task: Sender<ControlMsg>,
|
resource_task: Sender<ControlMsg>,
|
||||||
mime_classifier: Arc<MIMEClassifier>,
|
mime_classifier: Arc<MIMEClassifier>,
|
||||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||||
|
@ -354,13 +385,11 @@ struct ResourceManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResourceManager {
|
impl ResourceManager {
|
||||||
fn new(from_client: Receiver<ControlMsg>,
|
pub fn new(user_agent: Option<String>,
|
||||||
user_agent: Option<String>,
|
|
||||||
resource_task: Sender<ControlMsg>,
|
resource_task: Sender<ControlMsg>,
|
||||||
hsts_list: Option<HSTSList>,
|
hsts_list: Option<HSTSList>,
|
||||||
devtools_channel: Option<Sender<DevtoolsControlMsg>>) -> ResourceManager {
|
devtools_channel: Option<Sender<DevtoolsControlMsg>>) -> ResourceManager {
|
||||||
ResourceManager {
|
ResourceManager {
|
||||||
from_client: from_client,
|
|
||||||
user_agent: user_agent,
|
user_agent: user_agent,
|
||||||
cookie_storage: CookieStorage::new(),
|
cookie_storage: CookieStorage::new(),
|
||||||
resource_task: resource_task,
|
resource_task: resource_task,
|
||||||
|
@ -384,22 +413,17 @@ impl ResourceManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start(&mut self) {
|
pub fn is_host_sts(&self, host: &str) -> bool {
|
||||||
loop {
|
match self.hsts_list.as_ref() {
|
||||||
match self.from_client.recv().unwrap() {
|
Some(list) => list.is_host_secure(host),
|
||||||
ControlMsg::Load(load_data, consumer) => {
|
None => false
|
||||||
self.load(load_data, consumer)
|
}
|
||||||
}
|
}
|
||||||
ControlMsg::SetCookiesForUrl(request, cookie_list, source) => {
|
|
||||||
self.set_cookies_for_url(request, cookie_list, source)
|
pub fn add_hsts_entry(&mut self, entry: HSTSEntry) {
|
||||||
}
|
match self.hsts_list.as_mut() {
|
||||||
ControlMsg::GetCookiesForUrl(url, consumer, source) => {
|
Some(list) => list.push(entry),
|
||||||
consumer.send(self.cookie_storage.cookies_for_url(&url, source)).unwrap();
|
None => ()
|
||||||
}
|
|
||||||
ControlMsg::Exit => {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use net::resource_task::{
|
use net::resource_task::{
|
||||||
new_resource_task, parse_hostsfile, replace_hosts, HSTSList, HSTSEntry, secure_load_data
|
new_resource_task, parse_hostsfile, replace_hosts, HSTSList, HSTSEntry, secure_load_data,
|
||||||
|
ResourceManager
|
||||||
};
|
};
|
||||||
use net_traits::{ControlMsg, LoadData, LoadConsumer};
|
use net_traits::{ControlMsg, LoadData, LoadConsumer};
|
||||||
use net_traits::ProgressMsg;
|
use net_traits::ProgressMsg;
|
||||||
|
@ -20,6 +21,26 @@ fn test_exit() {
|
||||||
resource_task.send(ControlMsg::Exit).unwrap();
|
resource_task.send(ControlMsg::Exit).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add_hsts_entry_to_resource_manager_adds_an_hsts_entry() {
|
||||||
|
let list = HSTSList {
|
||||||
|
entries: Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
let (tx, _) = channel();
|
||||||
|
let mut manager = ResourceManager::new(None, tx, Some(list), None);
|
||||||
|
|
||||||
|
let entry = HSTSEntry::new(
|
||||||
|
"mozilla.org".to_string(), false, None
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(!manager.is_host_sts("mozilla.org"));
|
||||||
|
|
||||||
|
manager.add_hsts_entry(entry.unwrap());
|
||||||
|
|
||||||
|
assert!(manager.is_host_sts("mozilla.org"))
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hsts_entry_is_not_expired_when_it_has_no_timestamp() {
|
fn test_hsts_entry_is_not_expired_when_it_has_no_timestamp() {
|
||||||
let entry = HSTSEntry {
|
let entry = HSTSEntry {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue