mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Rustfmt net crate
This commit is contained in:
parent
ba1ed11ced
commit
2481ad25f8
30 changed files with 4957 additions and 2870 deletions
|
@ -48,59 +48,65 @@ use storage_thread::StorageThreadFactory;
|
|||
use websocket_loader;
|
||||
|
||||
/// Returns a tuple of (public, private) senders to the new threads.
|
||||
pub fn new_resource_threads(user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
time_profiler_chan: ProfilerChan,
|
||||
mem_profiler_chan: MemProfilerChan,
|
||||
embedder_proxy: EmbedderProxy,
|
||||
config_dir: Option<PathBuf>)
|
||||
-> (ResourceThreads, ResourceThreads) {
|
||||
pub fn new_resource_threads(
|
||||
user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
time_profiler_chan: ProfilerChan,
|
||||
mem_profiler_chan: MemProfilerChan,
|
||||
embedder_proxy: EmbedderProxy,
|
||||
config_dir: Option<PathBuf>,
|
||||
) -> (ResourceThreads, ResourceThreads) {
|
||||
let (public_core, private_core) = new_core_resource_thread(
|
||||
user_agent,
|
||||
devtools_chan,
|
||||
time_profiler_chan,
|
||||
mem_profiler_chan,
|
||||
embedder_proxy,
|
||||
config_dir.clone());
|
||||
config_dir.clone(),
|
||||
);
|
||||
let storage: IpcSender<StorageThreadMsg> = StorageThreadFactory::new(config_dir);
|
||||
(ResourceThreads::new(public_core, storage.clone()),
|
||||
ResourceThreads::new(private_core, storage))
|
||||
(
|
||||
ResourceThreads::new(public_core, storage.clone()),
|
||||
ResourceThreads::new(private_core, storage),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/// Create a CoreResourceThread
|
||||
pub fn new_core_resource_thread(user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
time_profiler_chan: ProfilerChan,
|
||||
mem_profiler_chan: MemProfilerChan,
|
||||
embedder_proxy: EmbedderProxy,
|
||||
config_dir: Option<PathBuf>)
|
||||
-> (CoreResourceThread, CoreResourceThread) {
|
||||
pub fn new_core_resource_thread(
|
||||
user_agent: Cow<'static, str>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
time_profiler_chan: ProfilerChan,
|
||||
mem_profiler_chan: MemProfilerChan,
|
||||
embedder_proxy: EmbedderProxy,
|
||||
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();
|
||||
let (report_chan, report_port) = ipc::channel().unwrap();
|
||||
|
||||
thread::Builder::new().name("ResourceManager".to_owned()).spawn(move || {
|
||||
let resource_manager = CoreResourceManager::new(
|
||||
user_agent, devtools_chan, time_profiler_chan, embedder_proxy
|
||||
);
|
||||
thread::Builder::new()
|
||||
.name("ResourceManager".to_owned())
|
||||
.spawn(move || {
|
||||
let resource_manager = CoreResourceManager::new(
|
||||
user_agent,
|
||||
devtools_chan,
|
||||
time_profiler_chan,
|
||||
embedder_proxy,
|
||||
);
|
||||
|
||||
let mut channel_manager = ResourceChannelManager {
|
||||
resource_manager: resource_manager,
|
||||
config_dir: config_dir,
|
||||
};
|
||||
let mut channel_manager = ResourceChannelManager {
|
||||
resource_manager: resource_manager,
|
||||
config_dir: config_dir,
|
||||
};
|
||||
|
||||
mem_profiler_chan.run_with_memory_reporting(|| (
|
||||
channel_manager.start(
|
||||
public_setup_port,
|
||||
private_setup_port,
|
||||
report_port)
|
||||
),
|
||||
String::from("network-cache-reporter"),
|
||||
report_chan,
|
||||
|report_chan| report_chan);
|
||||
|
||||
}).expect("Thread spawning failed");
|
||||
mem_profiler_chan.run_with_memory_reporting(
|
||||
|| (channel_manager.start(public_setup_port, private_setup_port, report_port)),
|
||||
String::from("network-cache-reporter"),
|
||||
report_chan,
|
||||
|report_chan| report_chan,
|
||||
);
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
(public_setup_chan, private_setup_chan)
|
||||
}
|
||||
|
||||
|
@ -121,12 +127,8 @@ fn create_http_states(config_dir: Option<&Path>) -> (Arc<HttpState>, Arc<HttpSta
|
|||
}
|
||||
|
||||
let certs = match opts::get().certificate_path {
|
||||
Some(ref path) => {
|
||||
fs::read_to_string(path).expect("Couldn't not find certificate file")
|
||||
}
|
||||
None => {
|
||||
resources::read_string(Resource::SSLCertificates)
|
||||
},
|
||||
Some(ref path) => fs::read_to_string(path).expect("Couldn't not find certificate file"),
|
||||
None => resources::read_string(Resource::SSLCertificates),
|
||||
};
|
||||
|
||||
let ssl_connector_builder = create_ssl_connector_builder(&certs);
|
||||
|
@ -147,10 +149,12 @@ fn create_http_states(config_dir: Option<&Path>) -> (Arc<HttpState>, Arc<HttpSta
|
|||
|
||||
impl ResourceChannelManager {
|
||||
#[allow(unsafe_code)]
|
||||
fn start(&mut self,
|
||||
public_receiver: IpcReceiver<CoreResourceMsg>,
|
||||
private_receiver: IpcReceiver<CoreResourceMsg>,
|
||||
memory_reporter: IpcReceiver<ReportsChan>) {
|
||||
fn start(
|
||||
&mut self,
|
||||
public_receiver: IpcReceiver<CoreResourceMsg>,
|
||||
private_receiver: IpcReceiver<CoreResourceMsg>,
|
||||
memory_reporter: IpcReceiver<ReportsChan>,
|
||||
) {
|
||||
let (public_http_state, private_http_state) =
|
||||
create_http_states(self.config_dir.as_ref().map(Deref::deref));
|
||||
|
||||
|
@ -164,7 +168,7 @@ impl ResourceChannelManager {
|
|||
// Handles case where profiler thread shuts down before resource thread.
|
||||
match receiver {
|
||||
ipc::IpcSelectionResult::ChannelClosed(..) => continue,
|
||||
_ => {}
|
||||
_ => {},
|
||||
}
|
||||
let (id, data) = receiver.unwrap();
|
||||
// If message is memory report, get the size_of of public and private http caches
|
||||
|
@ -190,10 +194,12 @@ impl ResourceChannelManager {
|
|||
}
|
||||
}
|
||||
|
||||
fn process_report(&mut self,
|
||||
msg: ReportsChan,
|
||||
public_http_state: &Arc<HttpState>,
|
||||
private_http_state: &Arc<HttpState>) {
|
||||
fn process_report(
|
||||
&mut self,
|
||||
msg: ReportsChan,
|
||||
public_http_state: &Arc<HttpState>,
|
||||
private_http_state: &Arc<HttpState>,
|
||||
) {
|
||||
let mut ops = MallocSizeOfOps::new(servo_allocator::usable_size, None, None);
|
||||
let public_cache = public_http_state.http_cache.read().unwrap();
|
||||
let private_cache = private_http_state.http_cache.read().unwrap();
|
||||
|
@ -201,74 +207,95 @@ impl ResourceChannelManager {
|
|||
let public_report = Report {
|
||||
path: path!["memory-cache", "public"],
|
||||
kind: ReportKind::ExplicitJemallocHeapSize,
|
||||
size: public_cache.size_of(&mut ops)
|
||||
size: public_cache.size_of(&mut ops),
|
||||
};
|
||||
|
||||
let private_report = Report {
|
||||
path: path!["memory-cache", "private"],
|
||||
kind: ReportKind::ExplicitJemallocHeapSize,
|
||||
size: private_cache.size_of(&mut ops)
|
||||
size: private_cache.size_of(&mut ops),
|
||||
};
|
||||
|
||||
msg.send(vec!(public_report, private_report));
|
||||
msg.send(vec![public_report, private_report]);
|
||||
}
|
||||
|
||||
/// Returns false if the thread should exit.
|
||||
fn process_msg(&mut self,
|
||||
msg: CoreResourceMsg,
|
||||
http_state: &Arc<HttpState>) -> bool {
|
||||
fn process_msg(&mut self, msg: CoreResourceMsg, http_state: &Arc<HttpState>) -> bool {
|
||||
match msg {
|
||||
CoreResourceMsg::Fetch(req_init, channels) => {
|
||||
match channels {
|
||||
FetchChannels::ResponseMsg(sender, cancel_chan) =>
|
||||
self.resource_manager.fetch(req_init, None, sender, http_state, cancel_chan),
|
||||
FetchChannels::WebSocket { event_sender, action_receiver } =>
|
||||
self.resource_manager.websocket_connect(req_init, event_sender, action_receiver, http_state),
|
||||
}
|
||||
}
|
||||
CoreResourceMsg::FetchRedirect(req_init, res_init, sender, cancel_chan) =>
|
||||
self.resource_manager.fetch(req_init, Some(res_init), sender, http_state, cancel_chan),
|
||||
CoreResourceMsg::SetCookieForUrl(request, cookie, source) =>
|
||||
self.resource_manager.set_cookie_for_url(&request, cookie.into_inner(), source, http_state),
|
||||
CoreResourceMsg::Fetch(req_init, channels) => match channels {
|
||||
FetchChannels::ResponseMsg(sender, cancel_chan) => {
|
||||
self.resource_manager
|
||||
.fetch(req_init, None, sender, http_state, cancel_chan)
|
||||
},
|
||||
FetchChannels::WebSocket {
|
||||
event_sender,
|
||||
action_receiver,
|
||||
} => self.resource_manager.websocket_connect(
|
||||
req_init,
|
||||
event_sender,
|
||||
action_receiver,
|
||||
http_state,
|
||||
),
|
||||
},
|
||||
CoreResourceMsg::FetchRedirect(req_init, res_init, sender, cancel_chan) => self
|
||||
.resource_manager
|
||||
.fetch(req_init, Some(res_init), sender, http_state, cancel_chan),
|
||||
CoreResourceMsg::SetCookieForUrl(request, cookie, source) => self
|
||||
.resource_manager
|
||||
.set_cookie_for_url(&request, cookie.into_inner(), source, http_state),
|
||||
CoreResourceMsg::SetCookiesForUrl(request, cookies, source) => {
|
||||
for cookie in cookies {
|
||||
self.resource_manager.set_cookie_for_url(&request, cookie.into_inner(), source, http_state);
|
||||
self.resource_manager.set_cookie_for_url(
|
||||
&request,
|
||||
cookie.into_inner(),
|
||||
source,
|
||||
http_state,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::GetCookiesForUrl(url, consumer, source) => {
|
||||
let mut cookie_jar = http_state.cookie_jar.write().unwrap();
|
||||
consumer.send(cookie_jar.cookies_for_url(&url, source)).unwrap();
|
||||
}
|
||||
consumer
|
||||
.send(cookie_jar.cookies_for_url(&url, source))
|
||||
.unwrap();
|
||||
},
|
||||
CoreResourceMsg::NetworkMediator(mediator_chan) => {
|
||||
self.resource_manager.swmanager_chan = Some(mediator_chan)
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::GetCookiesDataForUrl(url, consumer, source) => {
|
||||
let mut cookie_jar = http_state.cookie_jar.write().unwrap();
|
||||
let cookies = cookie_jar.cookies_data_for_url(&url, source).map(Serde).collect();
|
||||
let cookies = cookie_jar
|
||||
.cookies_data_for_url(&url, source)
|
||||
.map(Serde)
|
||||
.collect();
|
||||
consumer.send(cookies).unwrap();
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::GetHistoryState(history_state_id, consumer) => {
|
||||
let history_states = http_state.history_states.read().unwrap();
|
||||
consumer.send(history_states.get(&history_state_id).cloned()).unwrap();
|
||||
}
|
||||
consumer
|
||||
.send(history_states.get(&history_state_id).cloned())
|
||||
.unwrap();
|
||||
},
|
||||
CoreResourceMsg::SetHistoryState(history_state_id, history_state) => {
|
||||
let mut history_states = http_state.history_states.write().unwrap();
|
||||
history_states.insert(history_state_id, history_state);
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::RemoveHistoryStates(states_to_remove) => {
|
||||
let mut history_states = http_state.history_states.write().unwrap();
|
||||
for history_state in states_to_remove {
|
||||
history_states.remove(&history_state);
|
||||
}
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::Synchronize(sender) => {
|
||||
let _ = sender.send(());
|
||||
}
|
||||
},
|
||||
CoreResourceMsg::ToFileManager(msg) => self.resource_manager.filemanager.handle(msg),
|
||||
CoreResourceMsg::Exit(sender) => {
|
||||
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"),
|
||||
Ok(auth_cache) => {
|
||||
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() {
|
||||
|
@ -282,14 +309,15 @@ impl ResourceChannelManager {
|
|||
}
|
||||
let _ = sender.send(());
|
||||
return false;
|
||||
}
|
||||
},
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
|
||||
where T: for<'de> Deserialize<'de>
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
let path = config_dir.join(filename);
|
||||
let display = path.display();
|
||||
|
@ -304,10 +332,11 @@ pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
|
|||
|
||||
let mut string_buffer: String = String::new();
|
||||
match file.read_to_string(&mut string_buffer) {
|
||||
Err(why) => {
|
||||
panic!("couldn't read from {}: {}", display,
|
||||
Error::description(&why))
|
||||
},
|
||||
Err(why) => panic!(
|
||||
"couldn't read from {}: {}",
|
||||
display,
|
||||
Error::description(&why)
|
||||
),
|
||||
Ok(_) => println!("successfully read from {}", display),
|
||||
}
|
||||
|
||||
|
@ -318,7 +347,8 @@ pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
|
|||
}
|
||||
|
||||
pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
|
||||
where T: Serialize
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let json_encoded: String;
|
||||
match serde_json::to_string_pretty(&data) {
|
||||
|
@ -329,17 +359,16 @@ pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
|
|||
let display = path.display();
|
||||
|
||||
let mut file = match File::create(&path) {
|
||||
Err(why) => panic!("couldn't create {}: {}",
|
||||
display,
|
||||
Error::description(&why)),
|
||||
Err(why) => panic!("couldn't create {}: {}", display, Error::description(&why)),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
match file.write_all(json_encoded.as_bytes()) {
|
||||
Err(why) => {
|
||||
panic!("couldn't write to {}: {}", display,
|
||||
Error::description(&why))
|
||||
},
|
||||
Err(why) => panic!(
|
||||
"couldn't write to {}: {}",
|
||||
display,
|
||||
Error::description(&why)
|
||||
),
|
||||
Ok(_) => println!("successfully wrote to {}", display),
|
||||
}
|
||||
}
|
||||
|
@ -354,7 +383,7 @@ impl AuthCache {
|
|||
pub fn new() -> AuthCache {
|
||||
AuthCache {
|
||||
version: 1,
|
||||
entries: HashMap::new()
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -373,10 +402,12 @@ pub struct CoreResourceManager {
|
|||
}
|
||||
|
||||
impl CoreResourceManager {
|
||||
pub fn new(user_agent: Cow<'static, str>,
|
||||
devtools_channel: Option<Sender<DevtoolsControlMsg>>,
|
||||
_profiler_chan: ProfilerChan,
|
||||
embedder_proxy: EmbedderProxy) -> CoreResourceManager {
|
||||
pub fn new(
|
||||
user_agent: Cow<'static, str>,
|
||||
devtools_channel: Option<Sender<DevtoolsControlMsg>>,
|
||||
_profiler_chan: ProfilerChan,
|
||||
embedder_proxy: EmbedderProxy,
|
||||
) -> CoreResourceManager {
|
||||
CoreResourceManager {
|
||||
user_agent: user_agent,
|
||||
devtools_chan: devtools_channel,
|
||||
|
@ -385,55 +416,67 @@ impl CoreResourceManager {
|
|||
}
|
||||
}
|
||||
|
||||
fn set_cookie_for_url(&mut self, request: &ServoUrl,
|
||||
cookie: cookie_rs::Cookie<'static>,
|
||||
source: CookieSource,
|
||||
http_state: &Arc<HttpState>) {
|
||||
fn set_cookie_for_url(
|
||||
&mut self,
|
||||
request: &ServoUrl,
|
||||
cookie: cookie_rs::Cookie<'static>,
|
||||
source: CookieSource,
|
||||
http_state: &Arc<HttpState>,
|
||||
) {
|
||||
if let Some(cookie) = cookie::Cookie::new_wrapped(cookie, request, source) {
|
||||
let mut cookie_jar = http_state.cookie_jar.write().unwrap();
|
||||
cookie_jar.push(cookie, request, source)
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch(&self,
|
||||
req_init: RequestInit,
|
||||
res_init_: Option<ResponseInit>,
|
||||
mut sender: IpcSender<FetchResponseMsg>,
|
||||
http_state: &Arc<HttpState>,
|
||||
cancel_chan: Option<IpcReceiver<()>>) {
|
||||
fn fetch(
|
||||
&self,
|
||||
req_init: RequestInit,
|
||||
res_init_: Option<ResponseInit>,
|
||||
mut sender: IpcSender<FetchResponseMsg>,
|
||||
http_state: &Arc<HttpState>,
|
||||
cancel_chan: Option<IpcReceiver<()>>,
|
||||
) {
|
||||
let http_state = http_state.clone();
|
||||
let ua = self.user_agent.clone();
|
||||
let dc = self.devtools_chan.clone();
|
||||
let filemanager = self.filemanager.clone();
|
||||
|
||||
thread::Builder::new().name(format!("fetch thread for {}", req_init.url)).spawn(move || {
|
||||
let mut request = Request::from_init(req_init);
|
||||
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
|
||||
// todo load context / mimesniff in fetch
|
||||
// todo referrer policy?
|
||||
// todo service worker stuff
|
||||
let context = FetchContext {
|
||||
state: http_state,
|
||||
user_agent: ua,
|
||||
devtools_chan: dc,
|
||||
filemanager: filemanager,
|
||||
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(cancel_chan))),
|
||||
};
|
||||
thread::Builder::new()
|
||||
.name(format!("fetch thread for {}", req_init.url))
|
||||
.spawn(move || {
|
||||
let mut request = Request::from_init(req_init);
|
||||
// XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed)
|
||||
// todo load context / mimesniff in fetch
|
||||
// todo referrer policy?
|
||||
// todo service worker stuff
|
||||
let context = FetchContext {
|
||||
state: http_state,
|
||||
user_agent: ua,
|
||||
devtools_chan: dc,
|
||||
filemanager: filemanager,
|
||||
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(
|
||||
cancel_chan,
|
||||
))),
|
||||
};
|
||||
|
||||
match res_init_ {
|
||||
Some(res_init) => {
|
||||
let response = Response::from_init(res_init);
|
||||
http_redirect_fetch(&mut request,
|
||||
&mut CorsCache::new(),
|
||||
response,
|
||||
true,
|
||||
&mut sender,
|
||||
&mut None,
|
||||
&context);
|
||||
},
|
||||
None => fetch(&mut request, &mut sender, &context),
|
||||
};
|
||||
}).expect("Thread spawning failed");
|
||||
match res_init_ {
|
||||
Some(res_init) => {
|
||||
let response = Response::from_init(res_init);
|
||||
http_redirect_fetch(
|
||||
&mut request,
|
||||
&mut CorsCache::new(),
|
||||
response,
|
||||
true,
|
||||
&mut sender,
|
||||
&mut None,
|
||||
&context,
|
||||
);
|
||||
},
|
||||
None => fetch(&mut request, &mut sender, &context),
|
||||
};
|
||||
})
|
||||
.expect("Thread spawning failed");
|
||||
}
|
||||
|
||||
fn websocket_connect(
|
||||
|
@ -441,7 +484,7 @@ impl CoreResourceManager {
|
|||
request: RequestInit,
|
||||
event_sender: IpcSender<WebSocketNetworkEvent>,
|
||||
action_receiver: IpcReceiver<WebSocketDomAction>,
|
||||
http_state: &Arc<HttpState>
|
||||
http_state: &Arc<HttpState>,
|
||||
) {
|
||||
websocket_loader::init(request, event_sender, action_receiver, http_state.clone());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue