mirror of
https://github.com/servo/servo.git
synced 2025-09-27 15:20:09 +01:00
storage: Move shared functionality to base (#39419)
Part of #39418. See that PR for a full description. Moves: - `read_json_from_file` - `write_json_to_file` - `IpcSendResult` - `IpcSend` Renames: - `CoreResourceThreadPool` to `ThreadPool` (shorter and more descriptive, as we use it for more than the core resource thread now) Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
parent
ba208b19cc
commit
f766b66a97
25 changed files with 254 additions and 98 deletions
|
@ -7,8 +7,7 @@
|
|||
use std::borrow::ToOwned;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{self, BufReader, BufWriter};
|
||||
use std::io::{self, BufReader};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex, RwLock, Weak};
|
||||
use std::thread;
|
||||
|
@ -22,7 +21,7 @@ use devtools_traits::DevtoolsControlMsg;
|
|||
use embedder_traits::EmbedderProxy;
|
||||
use hyper_serde::Serde;
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcReceiverSet, IpcSender};
|
||||
use log::{debug, trace, warn};
|
||||
use log::{debug, warn};
|
||||
use net_traits::blob_url_store::parse_blob_url;
|
||||
use net_traits::filemanager_thread::FileTokenCheck;
|
||||
use net_traits::indexeddb_thread::IndexedDBThreadMsg;
|
||||
|
@ -198,9 +197,9 @@ fn create_http_states(
|
|||
let http_cache = HttpCache::default();
|
||||
let mut cookie_jar = CookieStorage::new(150);
|
||||
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");
|
||||
base::read_json_from_file(&mut auth_cache, config_dir, "auth_cache.json");
|
||||
base::read_json_from_file(&mut hsts_list, config_dir, "hsts_list.json");
|
||||
base::read_json_from_file(&mut cookie_jar, config_dir, "cookie_jar.json");
|
||||
}
|
||||
|
||||
let override_manager = CertificateErrorOverrideManager::new();
|
||||
|
@ -532,16 +531,16 @@ impl ResourceChannelManager {
|
|||
if let Some(ref config_dir) = self.config_dir {
|
||||
match http_state.auth_cache.read() {
|
||||
Ok(auth_cache) => {
|
||||
write_json_to_file(&*auth_cache, config_dir, "auth_cache.json")
|
||||
base::write_json_to_file(&*auth_cache, config_dir, "auth_cache.json")
|
||||
},
|
||||
Err(_) => warn!("Error writing auth cache to disk"),
|
||||
}
|
||||
match http_state.cookie_jar.read() {
|
||||
Ok(jar) => write_json_to_file(&*jar, config_dir, "cookie_jar.json"),
|
||||
Ok(jar) => base::write_json_to_file(&*jar, config_dir, "cookie_jar.json"),
|
||||
Err(_) => warn!("Error writing cookie jar to disk"),
|
||||
}
|
||||
match http_state.hsts_list.read() {
|
||||
Ok(hsts) => write_json_to_file(&*hsts, config_dir, "hsts_list.json"),
|
||||
Ok(hsts) => base::write_json_to_file(&*hsts, config_dir, "hsts_list.json"),
|
||||
Err(_) => warn!("Error writing hsts list to disk"),
|
||||
}
|
||||
}
|
||||
|
@ -554,49 +553,6 @@ impl ResourceChannelManager {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
let path = config_dir.join(filename);
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match File::open(&path) {
|
||||
Err(why) => {
|
||||
warn!("couldn't open {}: {}", display, why);
|
||||
return;
|
||||
},
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let mut string_buffer: String = String::new();
|
||||
match file.read_to_string(&mut string_buffer) {
|
||||
Err(why) => panic!("couldn't read from {}: {}", display, why),
|
||||
Ok(_) => trace!("successfully read from {}", display),
|
||||
}
|
||||
|
||||
match serde_json::from_str(&string_buffer) {
|
||||
Ok(decoded_buffer) => *data = decoded_buffer,
|
||||
Err(why) => warn!("Could not decode buffer{}", why),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let path = config_dir.join(filename);
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match File::create(&path) {
|
||||
Err(why) => panic!("couldn't create {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
let mut writer = BufWriter::new(&mut file);
|
||||
serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
|
||||
trace!("successfully wrote to {display}");
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct AuthCacheEntry {
|
||||
pub user_name: String,
|
||||
|
|
|
@ -18,8 +18,6 @@ use profile_traits::path;
|
|||
use rustc_hash::FxHashMap;
|
||||
use servo_url::ServoUrl;
|
||||
|
||||
use crate::resource_thread;
|
||||
|
||||
const QUOTA_SIZE_LIMIT: usize = 5 * 1024 * 1024;
|
||||
|
||||
pub trait StorageThreadFactory {
|
||||
|
@ -62,7 +60,7 @@ impl StorageManager {
|
|||
fn new(port: GenericReceiver<StorageThreadMsg>, config_dir: Option<PathBuf>) -> StorageManager {
|
||||
let mut local_data = HashMap::new();
|
||||
if let Some(ref config_dir) = config_dir {
|
||||
resource_thread::read_json_from_file(&mut local_data, config_dir, "local_data.json");
|
||||
base::read_json_from_file(&mut local_data, config_dir, "local_data.json");
|
||||
}
|
||||
StorageManager {
|
||||
port,
|
||||
|
@ -142,7 +140,7 @@ impl StorageManager {
|
|||
|
||||
fn save_state(&self) {
|
||||
if let Some(ref config_dir) = self.config_dir {
|
||||
resource_thread::write_json_to_file(&self.local_data, config_dir, "local_data.json");
|
||||
base::write_json_to_file(&self.local_data, config_dir, "local_data.json");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue