Auto merge of #12612 - nox:die-opts-die, r=Ms2ger

Remove use of util::opts from net crate

<!-- 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/12612)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-27 03:31:03 -05:00 committed by GitHub
commit d8991496c6
4 changed files with 50 additions and 27 deletions

View file

@ -42,13 +42,13 @@ use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, RwLock};
use storage_thread::StorageThreadFactory;
use url::Url;
use util::opts;
use util::prefs::PREFS;
use util::thread::spawn_named;
use websocket_loader;
@ -165,11 +165,17 @@ fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata,
/// Returns a tuple of (public, private) senders to the new threads.
pub fn new_resource_threads(user_agent: String,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
profiler_chan: ProfilerChan) -> (ResourceThreads, ResourceThreads) {
profiler_chan: ProfilerChan,
config_dir: Option<PathBuf>)
-> (ResourceThreads, ResourceThreads) {
let filemanager_chan: IpcSender<FileManagerThreadMsg> = FileManagerThreadFactory::new(TFD_PROVIDER);
let (public_core, private_core) = new_core_resource_thread(user_agent, devtools_chan,
profiler_chan, filemanager_chan.clone());
let storage: IpcSender<StorageThreadMsg> = StorageThreadFactory::new();
let (public_core, private_core) = new_core_resource_thread(
user_agent,
devtools_chan,
profiler_chan,
filemanager_chan.clone(),
config_dir.clone());
let storage: IpcSender<StorageThreadMsg> = StorageThreadFactory::new(config_dir);
(ResourceThreads::new(public_core, storage.clone(), filemanager_chan.clone()),
ResourceThreads::new(private_core, storage, filemanager_chan))
}
@ -179,7 +185,8 @@ pub fn new_resource_threads(user_agent: String,
pub fn new_core_resource_thread(user_agent: String,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
profiler_chan: ProfilerChan,
filemanager_chan: IpcSender<FileManagerThreadMsg>)
filemanager_chan: IpcSender<FileManagerThreadMsg>,
config_dir: Option<PathBuf>)
-> (CoreResourceThread, CoreResourceThread) {
let (public_setup_chan, public_setup_port) = ipc::channel().unwrap();
let (private_setup_chan, private_setup_port) = ipc::channel().unwrap();
@ -192,6 +199,7 @@ pub fn new_core_resource_thread(user_agent: String,
let mut channel_manager = ResourceChannelManager {
resource_manager: resource_manager,
config_dir: config_dir,
};
channel_manager.start(public_setup_chan_clone,
private_setup_chan_clone,
@ -202,14 +210,16 @@ pub fn new_core_resource_thread(user_agent: String,
}
struct ResourceChannelManager {
resource_manager: CoreResourceManager
resource_manager: CoreResourceManager,
config_dir: Option<PathBuf>,
}
fn create_resource_groups() -> (ResourceGroup, ResourceGroup) {
fn create_resource_groups(config_dir: Option<&Path>)
-> (ResourceGroup, ResourceGroup) {
let mut hsts_list = HstsList::from_servo_preload();
let mut auth_cache = AuthCache::new();
let mut cookie_jar = CookieStorage::new();
if let Some(ref config_dir) = opts::get().config_dir {
if let Some(config_dir) = config_dir {
read_json_from_file(&mut auth_cache, config_dir, "auth_cache.json");
read_json_from_file(&mut hsts_list, config_dir, "hsts_list.json");
read_json_from_file(&mut cookie_jar, config_dir, "cookie_jar.json");
@ -236,7 +246,8 @@ impl ResourceChannelManager {
private_control_sender: CoreResourceThread,
public_receiver: IpcReceiver<CoreResourceMsg>,
private_receiver: IpcReceiver<CoreResourceMsg>) {
let (public_resource_group, private_resource_group) = create_resource_groups();
let (public_resource_group, private_resource_group) =
create_resource_groups(self.config_dir.as_ref().map(Deref::deref));
let mut rx_set = IpcReceiverSet::new().unwrap();
let private_id = rx_set.add(private_receiver).unwrap();
@ -297,7 +308,7 @@ impl ResourceChannelManager {
let _ = sender.send(());
}
CoreResourceMsg::Exit(sender) => {
if let Some(ref config_dir) = opts::get().config_dir {
if let Some(ref config_dir) = self.config_dir {
match group.auth_cache.read() {
Ok(auth_cache) => write_json_to_file(&*auth_cache, config_dir, "auth_cache.json"),
Err(_) => warn!("Error writing auth cache to disk"),
@ -319,8 +330,10 @@ impl ResourceChannelManager {
}
}
pub fn read_json_from_file<T: Decodable>(data: &mut T, config_dir: &str, filename: &str) {
let path = Path::new(config_dir).join(filename);
pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
where T: Decodable
{
let path = config_dir.join(filename);
let display = path.display();
let mut file = match File::open(&path) {
@ -346,13 +359,15 @@ pub fn read_json_from_file<T: Decodable>(data: &mut T, config_dir: &str, filenam
}
}
pub fn write_json_to_file<T: Encodable>(data: &T, config_dir: &str, filename: &str) {
pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
where T: Encodable
{
let json_encoded: String;
match json::encode(&data) {
Ok(d) => json_encoded = d,
Err(_) => return,
}
let path = Path::new(config_dir).join(filename);
let path = config_dir.join(filename);
let display = path.display();
let mut file = match File::create(&path) {

View file

@ -8,22 +8,22 @@ use resource_thread;
use std::borrow::ToOwned;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::path::PathBuf;
use url::Url;
use util::opts;
use util::thread::spawn_named;
const QUOTA_SIZE_LIMIT: usize = 5 * 1024 * 1024;
pub trait StorageThreadFactory {
fn new() -> Self;
fn new(config_dir: Option<PathBuf>) -> Self;
}
impl StorageThreadFactory for IpcSender<StorageThreadMsg> {
/// Create a storage thread
fn new() -> IpcSender<StorageThreadMsg> {
fn new(config_dir: Option<PathBuf>) -> IpcSender<StorageThreadMsg> {
let (chan, port) = ipc::channel().unwrap();
spawn_named("StorageManager".to_owned(), move || {
StorageManager::new(port).start();
StorageManager::new(port, config_dir).start();
});
chan
}
@ -33,18 +33,22 @@ struct StorageManager {
port: IpcReceiver<StorageThreadMsg>,
session_data: HashMap<String, (usize, BTreeMap<String, String>)>,
local_data: HashMap<String, (usize, BTreeMap<String, String>)>,
config_dir: Option<PathBuf>,
}
impl StorageManager {
fn new(port: IpcReceiver<StorageThreadMsg>) -> StorageManager {
fn new(port: IpcReceiver<StorageThreadMsg>,
config_dir: Option<PathBuf>)
-> StorageManager {
let mut local_data = HashMap::new();
if let Some(ref config_dir) = opts::get().config_dir {
if let Some(ref config_dir) = config_dir {
resource_thread::read_json_from_file(&mut local_data, config_dir, "local_data.json");
}
StorageManager {
port: port,
session_data: HashMap::new(),
local_data: local_data,
config_dir: config_dir,
}
}
}
@ -75,7 +79,7 @@ impl StorageManager {
self.clear(sender, url, storage_type)
}
StorageThreadMsg::Exit(sender) => {
if let Some(ref config_dir) = opts::get().config_dir {
if let Some(ref config_dir) = self.config_dir {
resource_thread::write_json_to_file(&self.local_data, config_dir, "local_data.json");
}
let _ = sender.send(());

View file

@ -239,7 +239,8 @@ fn create_constellation(opts: opts::Opts,
let (public_resource_threads, private_resource_threads) =
new_resource_threads(opts.user_agent.clone(),
devtools_chan.clone(),
time_profiler_chan.clone());
time_profiler_chan.clone(),
opts.config_dir.map(Into::into));
let image_cache_thread = new_image_cache_thread(public_resource_threads.sender(),
webrender_api_sender.as_ref().map(|wr| wr.create_api()));
let font_cache_thread = FontCacheThread::new(public_resource_threads.sender(),

View file

@ -41,7 +41,8 @@ fn test_exit() {
let (tx, _rx) = ipc::channel().unwrap();
let (sender, receiver) = ipc::channel().unwrap();
let filemanager_chan = FileManagerThreadFactory::new(TFD_PROVIDER);
let (resource_thread, _) = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx), filemanager_chan);
let (resource_thread, _) = new_core_resource_thread(
"".to_owned(), None, ProfilerChan(tx), filemanager_chan, None);
resource_thread.send(CoreResourceMsg::Exit(sender)).unwrap();
receiver.recv().unwrap();
}
@ -51,7 +52,8 @@ fn test_bad_scheme() {
let (tx, _rx) = ipc::channel().unwrap();
let (sender, receiver) = ipc::channel().unwrap();
let filemanager_chan = FileManagerThreadFactory::new(TFD_PROVIDER);
let (resource_thread, _) = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx), filemanager_chan);
let (resource_thread, _) = new_core_resource_thread(
"".to_owned(), None, ProfilerChan(tx), filemanager_chan, None);
let (start_chan, start) = ipc::channel().unwrap();
let url = Url::parse("bogus://whatever").unwrap();
resource_thread.send(CoreResourceMsg::Load(LoadData::new(LoadContext::Browsing, url, &ResourceTest),
@ -231,7 +233,8 @@ fn test_cancelled_listener() {
let (tx, _rx) = ipc::channel().unwrap();
let (exit_sender, exit_receiver) = ipc::channel().unwrap();
let filemanager_chan = FileManagerThreadFactory::new(TFD_PROVIDER);
let (resource_thread, _) = new_core_resource_thread("".to_owned(), None, ProfilerChan(tx), filemanager_chan);
let (resource_thread, _) = new_core_resource_thread(
"".to_owned(), None, ProfilerChan(tx), filemanager_chan, None);
let (sender, receiver) = ipc::channel().unwrap();
let (id_sender, id_receiver) = ipc::channel().unwrap();
let (sync_sender, sync_receiver) = ipc::channel().unwrap();