Skip to content
Permalink
Browse files

adding default config dirs

  • Loading branch information...
creativcoder committed Apr 4, 2016
1 parent aa9f50a commit b4885fef9a88474737f46b43021acc5800b5cf08
@@ -211,17 +211,17 @@ impl ResourceChannelManager {
let _ = sender.send(());
}
CoreResourceMsg::Exit => {
if let Some(ref profile_dir) = opts::get().profile_dir {
if let Some(ref config_dir) = opts::get().config_dir {
match self.resource_manager.auth_cache.read() {
Ok(auth_cache) => write_json_to_file(&*auth_cache, profile_dir, "auth_cache.json"),
Ok(auth_cache) => write_json_to_file(&*auth_cache, config_dir, "auth_cache.json"),
Err(_) => warn!("Error writing auth cache to disk"),
}
match self.resource_manager.cookie_jar.read() {
Ok(jar) => write_json_to_file(&*jar, profile_dir, "cookie_jar.json"),
Ok(jar) => write_json_to_file(&*jar, config_dir, "cookie_jar.json"),
Err(_) => warn!("Error writing cookie jar to disk"),
}
match self.resource_manager.hsts_list.read() {
Ok(hsts) => write_json_to_file(&*hsts, profile_dir, "hsts_list.json"),
Ok(hsts) => write_json_to_file(&*hsts, config_dir, "hsts_list.json"),
Err(_) => warn!("Error writing hsts list to disk"),
}
}
@@ -233,9 +233,9 @@ impl ResourceChannelManager {
}
}

pub fn read_json_from_file<T: Decodable>(data: &mut T, profile_dir: &str, filename: &str) {
pub fn read_json_from_file<T: Decodable>(data: &mut T, config_dir: &str, filename: &str) {

let path = Path::new(profile_dir).join(filename);
let path = Path::new(config_dir).join(filename);
let display = path.display();

let mut file = match File::open(&path) {
@@ -261,13 +261,14 @@ pub fn read_json_from_file<T: Decodable>(data: &mut T, profile_dir: &str, filena
}
}

pub fn write_json_to_file<T: Encodable>(data: &T, profile_dir: &str, filename: &str) {
pub fn write_json_to_file<T: Encodable>(data: &T, config_dir: &str, filename: &str) {

let json_encoded: String;
match json::encode(&data) {
Ok(d) => json_encoded = d,
Err(_) => return,
}
let path = Path::new(profile_dir).join(filename);
let path = Path::new(config_dir).join(filename);
let display = path.display();

let mut file = match File::create(&path) {
@@ -391,10 +392,10 @@ impl CoreResourceManager {
profiler_chan: ProfilerChan) -> CoreResourceManager {
let mut auth_cache = AuthCache::new();
let mut cookie_jar = CookieStorage::new();
if let Some(ref profile_dir) = opts::get().profile_dir {
read_json_from_file(&mut auth_cache, profile_dir, "auth_cache.json");
read_json_from_file(&mut hsts_list, profile_dir, "hsts_list.json");
read_json_from_file(&mut cookie_jar, profile_dir, "cookie_jar.json");
if let Some(ref config_dir) = opts::get().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");
}
CoreResourceManager {
user_agent: user_agent,
@@ -38,8 +38,8 @@ struct StorageManager {
impl StorageManager {
fn new(port: IpcReceiver<StorageThreadMsg>) -> StorageManager {
let mut local_data = HashMap::new();
if let Some(ref profile_dir) = opts::get().profile_dir {
resource_thread::read_json_from_file(&mut local_data, profile_dir, "local_data.json");
if let Some(ref config_dir) = opts::get().config_dir {
resource_thread::read_json_from_file(&mut local_data, config_dir, "local_data.json");
}
StorageManager {
port: port,
@@ -75,8 +75,8 @@ impl StorageManager {
self.clear(sender, url, storage_type)
}
StorageThreadMsg::Exit => {
if let Some(ref profile_dir) = opts::get().profile_dir {
resource_thread::write_json_to_file(&self.local_data, profile_dir, "local_data.json");
if let Some(ref config_dir) = opts::get().config_dir {
resource_thread::write_json_to_file(&self.local_data, config_dir, "local_data.json");
}
break
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -30,6 +30,7 @@ serde = "0.7"
serde_macros = "0.7"
smallvec = "0.1"
url = {version = "1.0.0", features = ["heap_size", "serde"]}
xdg = "2.0"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2"
@@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Contains routines for retrieving default config directories.
//! For linux based platforms, it uses the XDG base directory spec but provides
//! similar abstractions for non-linux platforms.

extern crate xdg;

use std::env;
use std::fs;
use std::path::PathBuf;

#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))]
pub fn default_config_dir() -> Option<PathBuf> {
let xdg_dirs = xdg::BaseDirectories::with_profile("servo", "default").unwrap();
let config_dir = xdg_dirs.get_config_home();
Some(config_dir)
}

#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))]
pub fn default_data_dir() -> Option<PathBuf> {
let xdg_dirs = xdg::BaseDirectories::with_profile("servo", "default").unwrap();
let data_dir = xdg_dirs.get_data_home();
Some(data_dir)
}

#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "windows")))]
pub fn default_cache_dir() -> Option<PathBuf> {
let xdg_dirs = xdg::BaseDirectories::with_profile("servo", "default").unwrap();
let cache_dir = xdg_dirs.get_cache_home();
Some(cache_dir)
}

#[cfg(target_os = "macos")]
pub fn default_config_dir() -> Option<PathBuf> {
let mut config_dir = env::home_dir().unwrap();
config_dir.push("Library");
config_dir.push("Application Support");
config_dir.push("Servo");
Some(config_dir)
}

#[cfg(target_os = "windows")]
pub fn default_config_dir() -> Option<PathBuf> {
let mut config_dir = match env::var("APPDATA") {
Ok(appdata_path) => PathBuf::from(appdata_path),
Err(_) => { let mut dir = env::home_dir().unwrap();
dir.push("Appdata");
dir.push("Roaming");
dir
}
};
config_dir.push("Servo");
Some(config_dir)
}
@@ -38,9 +38,11 @@ extern crate rustc_serialize;
extern crate serde;
extern crate smallvec;
extern crate url;
extern crate xdg;

use std::sync::Arc;

pub mod basedir;
pub mod cache;
#[allow(unsafe_code)]
pub mod debug_utils;
@@ -14,14 +14,14 @@ use resource_files::set_resources_path;
use std::cmp;
use std::default::Default;
use std::env;
use std::fs;
use std::fs::File;
use std::io::{self, Read, Write};
use std::fs::{self, File};
use std::io::{self, Read, Write, stderr};
use std::path::{Path, PathBuf};
use std::process;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use url::{self, Url};


/// Global flags for Servo, currently set on the command line.
#[derive(Clone, Deserialize, Serialize)]
pub struct Opts {
@@ -197,8 +197,8 @@ pub struct Opts {
/// True if WebRender should use multisample antialiasing.
pub use_msaa: bool,

/// Directory path for persistent session
pub profile_dir: Option<String>,
/// Directory for a default config directory
pub config_dir: Option<String>,

// Which rendering API to use.
pub render_api: RenderApi,
@@ -471,6 +471,7 @@ const DEFAULT_USER_AGENT: UserAgent = UserAgent::Gonk;
const DEFAULT_USER_AGENT: UserAgent = UserAgent::Desktop;

pub fn default_opts() -> Opts {

Opts {
is_running_problem_test: false,
url: Some(Url::parse("about:blank").unwrap()),
@@ -524,7 +525,7 @@ pub fn default_opts() -> Opts {
webrender_stats: false,
use_msaa: false,
render_api: DEFAULT_RENDER_API,
profile_dir: None,
config_dir: None,
full_backtraces: false,
}
}
@@ -581,8 +582,9 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
opts.optflag("b", "no-native-titlebar", "Do not use native titlebar");
opts.optflag("w", "webrender", "Use webrender backend");
opts.optopt("G", "graphics", "Select graphics backend (gl or es2)", "gl");
opts.optopt("", "profile-dir",
"optional directory path for user sessions", "");
opts.optopt("", "config-dir",
"config directory following xdg spec on linux platform", "");


let opt_match = match opts.parse(args) {
Ok(m) => m,
@@ -596,12 +598,6 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
process::exit(0);
};

if let Some(ref profile_dir) = opt_match.opt_str("profile-dir") {
if let Err(why) = fs::create_dir_all(profile_dir) {
error!("Couldn't create/open {:?}: {:?}", Path::new(profile_dir).to_string_lossy(), why);
}
}

// If this is the content process, we'll receive the real options over IPC. So just fill in
// some dummy options for now.
if let Some(content_process) = opt_match.opt_str("content-process") {
@@ -833,7 +829,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
use_webrender: use_webrender,
webrender_stats: debug_options.webrender_stats,
use_msaa: debug_options.use_msaa,
profile_dir: opt_match.opt_str("profile-dir"),
config_dir: opt_match.opt_str("config-dir"),
full_backtraces: debug_options.full_backtraces,
};

@@ -842,9 +838,9 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
// These must happen after setting the default options, since the prefs rely on
// on the resource path.
// Note that command line preferences have the highest precedence
if get().profile_dir.is_some() {
prefs::add_user_prefs();
}

prefs::add_user_prefs();

for pref in opt_match.opt_strs("pref").iter() {
let split: Vec<&str> = pref.splitn(2, '=').collect();
let pref_name = split[0];
@@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use basedir::default_config_dir;
use opts;
use resource_files::resources_dir_path;
use rustc_serialize::json::{Json, ToJson};
@@ -166,20 +167,32 @@ pub fn extend_prefs(extension: HashMap<String, Pref>) {
}

pub fn add_user_prefs() {
if let Some(ref dir) = opts::get().profile_dir {
let mut path = PathBuf::from(dir);
path.push("prefs.json");
if let Ok(file) = File::open(path) {
if let Ok(prefs) = read_prefs_from_file(file) {
extend_prefs(prefs);
match opts::get().config_dir {
Some(ref config_path) => {
let mut path = PathBuf::from(config_path);
init_user_prefs(&mut path);
}
None => {
let mut path = default_config_dir().unwrap();
if path.join("prefs.json").exists() {
init_user_prefs(&mut path);
}
} else {
writeln!(&mut stderr(), "Error opening prefs.json from profile_dir")
.expect("failed printing to stderr");
}
}
}

fn init_user_prefs(path: &mut PathBuf) {
path.push("prefs.json");
if let Ok(file) = File::open(path) {
if let Ok(prefs) = read_prefs_from_file(file) {
extend_prefs(prefs);
}
} else {
writeln!(&mut stderr(), "Error opening prefs.json from config directory")
.expect("failed printing to stderr");
}
}

fn read_prefs() -> Result<HashMap<String, Pref>, ()> {
let mut path = resources_dir_path();
path.push("prefs.json");

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -10,3 +10,4 @@ doctest = false

[dependencies]
util = {path = "../../../components/util"}

Oops, something went wrong.

0 comments on commit b4885fe

Please sign in to comment.
You can’t perform that action at this time.