From fc68e0a6caa43b1f91194a5101644fa26dd7bd72 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 13 Oct 2016 13:45:36 +0200 Subject: [PATCH] Add a FileManager to FetchContext. --- components/net/fetch/methods.rs | 87 +++++++++++++++++----------- components/net/resource_thread.rs | 8 ++- tests/unit/net/fetch.rs | 5 +- tests/unit/net/filemanager_thread.rs | 4 +- 4 files changed, 66 insertions(+), 38 deletions(-) diff --git a/components/net/fetch/methods.rs b/components/net/fetch/methods.rs index e1e66b37f9d..717ddb784e1 100644 --- a/components/net/fetch/methods.rs +++ b/components/net/fetch/methods.rs @@ -6,6 +6,7 @@ use connector::create_http_connector; use data_loader::decode; use devtools_traits::DevtoolsControlMsg; use fetch::cors_cache::CORSCache; +use filemanager_thread::{FileManager, UIProvider}; use http_loader::{HttpState, set_default_accept_encoding, set_request_cookies}; use http_loader::{NetworkHttpRequestFactory, ReadResult, StreamedResponse, obtain_response, read_block}; use http_loader::{auth_from_cache, determine_request_referrer}; @@ -50,23 +51,28 @@ enum Data { Done, } -pub struct FetchContext { +pub struct FetchContext { pub state: HttpState, pub user_agent: Cow<'static, str>, pub devtools_chan: Option>, + pub filemanager: FileManager, } type DoneChannel = Option<(Sender, Receiver)>; /// [Fetch](https://fetch.spec.whatwg.org#concept-fetch) -pub fn fetch(request: Rc, target: &mut Target, context: FetchContext) -> Response { +pub fn fetch(request: Rc, + target: &mut Target, + context: FetchContext) + -> Response { fetch_with_cors_cache(request, &mut CORSCache::new(), target, context) } -pub fn fetch_with_cors_cache(request: Rc, - cache: &mut CORSCache, - target: &mut Target, - context: FetchContext) -> Response { +pub fn fetch_with_cors_cache(request: Rc, + cache: &mut CORSCache, + target: &mut Target, + context: FetchContext) + -> Response { // Step 1 if request.window.get() == Window::Client { // TODO: Set window to request's client object if client is a Window object @@ -131,9 +137,14 @@ pub fn fetch_with_cors_cache(request: Rc, } /// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch) -fn main_fetch(request: Rc, cache: &mut CORSCache, cors_flag: bool, - recursive_flag: bool, target: &mut Target, done_chan: &mut DoneChannel, - context: &FetchContext) -> Response { +fn main_fetch(request: Rc, + cache: &mut CORSCache, + cors_flag: bool, + recursive_flag: bool, + target: &mut Target, + done_chan: &mut DoneChannel, + context: &FetchContext) + -> Response { // TODO: Implement main fetch spec // Step 1 @@ -389,9 +400,12 @@ fn main_fetch(request: Rc, cache: &mut CORSCache, cors_flag: bool, } /// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch) -fn basic_fetch(request: Rc, cache: &mut CORSCache, - target: &mut Target, done_chan: &mut DoneChannel, - context: &FetchContext) -> Response { +fn basic_fetch(request: Rc, + cache: &mut CORSCache, + target: &mut Target, + done_chan: &mut DoneChannel, + context: &FetchContext) + -> Response { let url = request.current_url(); match url.scheme() { @@ -460,14 +474,15 @@ fn basic_fetch(request: Rc, cache: &mut CORSCache, } /// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch) -fn http_fetch(request: Rc, - cache: &mut CORSCache, - cors_flag: bool, - cors_preflight_flag: bool, - authentication_fetch_flag: bool, - target: &mut Target, - done_chan: &mut DoneChannel, - context: &FetchContext) -> Response { +fn http_fetch(request: Rc, + cache: &mut CORSCache, + cors_flag: bool, + cors_preflight_flag: bool, + authentication_fetch_flag: bool, + target: &mut Target, + done_chan: &mut DoneChannel, + context: &FetchContext) + -> Response { // This is a new async fetch, reset the channel we are waiting on *done_chan = None; // Step 1 @@ -631,13 +646,14 @@ fn http_fetch(request: Rc, } /// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch) -fn http_redirect_fetch(request: Rc, - cache: &mut CORSCache, - response: Rc, - cors_flag: bool, - target: &mut Target, - done_chan: &mut DoneChannel, - context: &FetchContext) -> Response { +fn http_redirect_fetch(request: Rc, + cache: &mut CORSCache, + response: Rc, + cors_flag: bool, + target: &mut Target, + done_chan: &mut DoneChannel, + context: &FetchContext) + -> Response { // Step 1 assert_eq!(response.return_internal.get(), true); @@ -711,11 +727,12 @@ fn http_redirect_fetch(request: Rc, } /// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch) -fn http_network_or_cache_fetch(request: Rc, - credentials_flag: bool, - authentication_fetch_flag: bool, - done_chan: &mut DoneChannel, - context: &FetchContext) -> Response { +fn http_network_or_cache_fetch(request: Rc, + credentials_flag: bool, + authentication_fetch_flag: bool, + done_chan: &mut DoneChannel, + context: &FetchContext) + -> Response { // TODO: Implement Window enum for Request let request_has_no_window = true; @@ -1108,8 +1125,10 @@ fn http_network_fetch(request: Rc, } /// [CORS preflight fetch](https://fetch.spec.whatwg.org#cors-preflight-fetch) -fn cors_preflight_fetch(request: Rc, cache: &mut CORSCache, - context: &FetchContext) -> Response { +fn cors_preflight_fetch(request: Rc, + cache: &mut CORSCache, + context: &FetchContext) + -> Response { // Step 1 let mut preflight = Request::new(request.current_url(), Some(request.origin.borrow().clone()), request.is_service_worker_global_scope, request.pipeline_id.get()); diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index 6f596684408..e6606b87b8c 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -592,6 +592,7 @@ impl CoreResourceManager { }; let ua = self.user_agent.clone(); let dc = self.devtools_chan.clone(); + let filemanager = self.filemanager.clone(); spawn_named(format!("fetch thread for {}", init.url), move || { let request = Request::from_init(init); // XXXManishearth: Check origin against pipeline id (also ensure that the mode is allowed) @@ -599,7 +600,12 @@ impl CoreResourceManager { // todo referrer policy? // todo service worker stuff let mut target = Some(Box::new(sender) as Box); - let context = FetchContext { state: http_state, user_agent: ua, devtools_chan: dc }; + let context = FetchContext { + state: http_state, + user_agent: ua, + devtools_chan: dc, + filemanager: filemanager, + }; fetch(Rc::new(request), &mut target, context); }) } diff --git a/tests/unit/net/fetch.rs b/tests/unit/net/fetch.rs index 7f368516656..10156af5486 100644 --- a/tests/unit/net/fetch.rs +++ b/tests/unit/net/fetch.rs @@ -5,6 +5,7 @@ use devtools_traits::DevtoolsControlMsg; use devtools_traits::HttpRequest as DevtoolsHttpRequest; use devtools_traits::HttpResponse as DevtoolsHttpResponse; +use filemanager_thread::{TestProvider, TEST_PROVIDER}; use http_loader::{expect_devtools_http_request, expect_devtools_http_response}; use hyper::LanguageTag; use hyper::header::{Accept, AccessControlAllowCredentials, AccessControlAllowHeaders, AccessControlAllowOrigin}; @@ -22,6 +23,7 @@ use hyper::uri::RequestUri; use msg::constellation_msg::{ReferrerPolicy, TEST_PIPELINE_ID}; use net::fetch::cors_cache::CORSCache; use net::fetch::methods::{FetchContext, fetch, fetch_with_cors_cache}; +use net::filemanager_thread::FileManager; use net::http_loader::HttpState; use net_traits::FetchTaskTarget; use net_traits::request::{Origin, RedirectMode, Referrer, Request, RequestMode}; @@ -46,11 +48,12 @@ struct FetchResponseCollector { sender: Sender, } -fn new_fetch_context(dc: Option>) -> FetchContext { +fn new_fetch_context(dc: Option>) -> FetchContext { FetchContext { state: HttpState::new(), user_agent: DEFAULT_USER_AGENT.into(), devtools_chan: dc, + filemanager: FileManager::new(TEST_PROVIDER), } } impl FetchTaskTarget for FetchResponseCollector { diff --git a/tests/unit/net/filemanager_thread.rs b/tests/unit/net/filemanager_thread.rs index 7266157481a..75facb0b6aa 100644 --- a/tests/unit/net/filemanager_thread.rs +++ b/tests/unit/net/filemanager_thread.rs @@ -10,9 +10,9 @@ use std::fs::File; use std::io::Read; use std::path::PathBuf; -const TEST_PROVIDER: &'static TestProvider = &TestProvider; +pub const TEST_PROVIDER: &'static TestProvider = &TestProvider; -struct TestProvider; +pub struct TestProvider; impl UIProvider for TestProvider { fn open_file_dialog(&self, _path: &str, _patterns: Vec) -> Option {