refactor(filemanager): uses embedderproxy directly

This commit is contained in:
OJ Kwon 2018-04-14 10:29:09 -07:00
parent 7cec47b3fa
commit 2fab94785b
No known key found for this signature in database
GPG key ID: 6C23A45602A44DA6
10 changed files with 54 additions and 84 deletions

View file

@ -15,6 +15,7 @@ doctest = false
base64 = "0.6"
brotli = "1.0.6"
cookie = "0.10"
compositing = {path = "../compositing"}
devtools_traits = {path = "../devtools_traits"}
embedder_traits = { path = "../embedder_traits" }
flate2 = "1"
@ -34,7 +35,6 @@ msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
openssl = "0.9"
profile_traits = {path = "../profile_traits"}
script_traits = {path = "../script_traits"}
serde = "1.0"
serde_json = "1.0"
servo_allocator = {path = "../allocator"}

View file

@ -2,14 +2,12 @@
* 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 compositing::compositor_thread::{EmbedderMsg, EmbedderProxy};
use ipc_channel::ipc::{self, IpcSender};
use mime_guess::guess_mime_type_opt;
use net_traits::blob_url_store::{BlobBuf, BlobURLStoreError};
use net_traits::filemanager_thread::{FileManagerResult, FileManagerThreadMsg, FileOrigin, FilterPattern};
use net_traits::filemanager_thread::{FileManagerThreadError, ReadFileProgress, RelativePos, SelectedFile};
use script_traits::FileManagerMsg;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use servo_config::opts;
use servo_config::prefs::PREFS;
use std::collections::HashMap;
use std::fs::File;
@ -19,7 +17,6 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::sync::atomic::{self, AtomicBool, AtomicUsize, Ordering};
use std::thread;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use url::Url;
use uuid::Uuid;
@ -62,14 +59,14 @@ enum FileImpl {
#[derive(Clone)]
pub struct FileManager {
constellation_chan: IpcSender<FileManagerMsg>,
embedder_proxy: EmbedderProxy,
store: Arc<FileManagerStore>,
}
impl FileManager {
pub fn new(constellation_chan: IpcSender<FileManagerMsg>) -> FileManager {
pub fn new(embedder_proxy: EmbedderProxy) -> FileManager {
FileManager {
constellation_chan: constellation_chan,
embedder_proxy: embedder_proxy,
store: Arc::new(FileManagerStore::new()),
}
}
@ -104,16 +101,16 @@ impl FileManager {
match msg {
FileManagerThreadMsg::SelectFile(filter, sender, origin, opt_test_path) => {
let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
let embedder = self.embedder_proxy.clone();
thread::Builder::new().name("select file".to_owned()).spawn(move || {
store.select_file(filter, sender, origin, opt_test_path, constellation_chan);
store.select_file(filter, sender, origin, opt_test_path, embedder);
}).expect("Thread spawning failed");
}
FileManagerThreadMsg::SelectFiles(filter, sender, origin, opt_test_paths) => {
let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
let embedder = self.embedder_proxy.clone();
thread::Builder::new().name("select files".to_owned()).spawn(move || {
store.select_files(filter, sender, origin, opt_test_paths, constellation_chan);
store.select_files(filter, sender, origin, opt_test_paths, embedder);
}).expect("Thread spawning failed");
}
FileManagerThreadMsg::ReadFile(sender, id, check_url_validity, origin) => {
@ -218,18 +215,21 @@ impl FileManagerStore {
}
}
fn get_selected_files(&self,
patterns: Vec<FilterPattern>,
multiple_files: bool,
constellation_chan: IpcSender<FileManagerMsg>) -> Option<Vec<String>> {
if opts::get().headless {
return None;
}
fn query_files_from_embedder(&self,
patterns: Vec<FilterPattern>,
multiple_files: bool,
embedder_proxy: EmbedderProxy) -> Option<Vec<String>> {
let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!");
let msg = FileManagerMsg::OpenFileSelectDialog(patterns, multiple_files, ipc_sender);
let msg = EmbedderMsg::GetSelectedFiles(patterns, multiple_files, ipc_sender);
constellation_chan.send(msg).map(|_| ipc_receiver.recv().unwrap()).unwrap_or_default()
embedder_proxy.send(msg);
match ipc_receiver.recv() {
Ok(result) => result,
Err(e) => {
warn!("Failed to receive files from embedder ({}).", e);
None
}
}
}
fn select_file(&self,
@ -237,14 +237,14 @@ impl FileManagerStore {
sender: IpcSender<FileManagerResult<SelectedFile>>,
origin: FileOrigin,
opt_test_path: Option<String>,
constellation_chan: IpcSender<FileManagerMsg>) {
embedder_proxy: EmbedderProxy) {
// Check if the select_files preference is enabled
// to ensure process-level security against compromised script;
// Then try applying opt_test_path directly for testing convenience
let opt_s = if select_files_pref_enabled() {
opt_test_path
} else {
self.get_selected_files(patterns, false, constellation_chan).map(|mut x| x.pop().unwrap())
self.query_files_from_embedder(patterns, false, embedder_proxy).and_then(|mut x| x.pop())
};
match opt_s {
@ -265,14 +265,14 @@ impl FileManagerStore {
sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>,
origin: FileOrigin,
opt_test_paths: Option<Vec<String>>,
constellation_chan: IpcSender<FileManagerMsg>) {
embedder_proxy: EmbedderProxy) {
// Check if the select_files preference is enabled
// to ensure process-level security against compromised script;
// Then try applying opt_test_paths directly for testing convenience
let opt_v = if select_files_pref_enabled() {
opt_test_paths
} else {
self.get_selected_files(patterns, true, constellation_chan)
self.query_files_from_embedder(patterns, true, embedder_proxy)
};
match opt_v {

View file

@ -6,6 +6,7 @@
extern crate base64;
extern crate brotli;
extern crate compositing;
extern crate cookie as cookie_rs;
extern crate devtools_traits;
extern crate embedder_traits;
@ -29,7 +30,6 @@ extern crate net_traits;
extern crate openssl;
#[macro_use]
extern crate profile_traits;
extern crate script_traits;
#[macro_use] extern crate serde;
extern crate serde_json;
extern crate servo_allocator;

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! A thread that takes a URL and streams back the binary data.
use compositing::compositor_thread::EmbedderProxy;
use connector::{create_http_connector, create_ssl_client};
use cookie;
use cookie_rs;
@ -28,7 +29,6 @@ use net_traits::storage_thread::StorageThreadMsg;
use profile_traits::mem::{Report, ReportsChan, ReportKind};
use profile_traits::mem::ProfilerChan as MemProfilerChan;
use profile_traits::time::ProfilerChan;
use script_traits::FileManagerMsg;
use serde::{Deserialize, Serialize};
use serde_json;
use servo_allocator;
@ -52,18 +52,19 @@ 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, IpcSender<IpcSender<FileManagerMsg>>) {
let (public_core, private_core, constellation_sender) = new_core_resource_thread(
-> (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());
let storage: IpcSender<StorageThreadMsg> = StorageThreadFactory::new(config_dir);
(ResourceThreads::new(public_core, storage.clone()),
ResourceThreads::new(private_core, storage),
constellation_sender)
ResourceThreads::new(private_core, storage))
}
@ -72,17 +73,16 @@ 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, IpcSender<IpcSender<FileManagerMsg>>) {
-> (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();
let (constellation_sender, constellation_receiver) = ipc::channel().unwrap();
thread::Builder::new().name("ResourceManager".to_owned()).spawn(move || {
let constellation_chan = constellation_receiver.recv().unwrap();
let resource_manager = CoreResourceManager::new(
user_agent, devtools_chan, time_profiler_chan, constellation_chan
user_agent, devtools_chan, time_profiler_chan, embedder_proxy
);
let mut channel_manager = ResourceChannelManager {
@ -101,7 +101,7 @@ pub fn new_core_resource_thread(user_agent: Cow<'static, str>,
|report_chan| report_chan);
}).expect("Thread spawning failed");
(public_setup_chan, private_setup_chan, constellation_sender)
(public_setup_chan, private_setup_chan)
}
struct ResourceChannelManager {
@ -377,12 +377,12 @@ impl CoreResourceManager {
pub fn new(user_agent: Cow<'static, str>,
devtools_channel: Option<Sender<DevtoolsControlMsg>>,
_profiler_chan: ProfilerChan,
constellation_chan: IpcSender<FileManagerMsg>) -> CoreResourceManager {
embedder_proxy: EmbedderProxy) -> CoreResourceManager {
CoreResourceManager {
user_agent: user_agent,
devtools_chan: devtools_channel,
swmanager_chan: None,
filemanager: FileManager::new(constellation_chan),
filemanager: FileManager::new(embedder_proxy),
}
}