refactor(filemanager): send constellationmsg to select file

This commit is contained in:
OJ Kwon 2018-03-30 10:49:24 -07:00
parent 1c465bcd66
commit b396a2f6ca
No known key found for this signature in database
GPG key ID: 6C23A45602A44DA6
4 changed files with 47 additions and 29 deletions

View file

@ -1012,6 +1012,9 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
FromCompositorMsg::SetCursor(cursor) => { FromCompositorMsg::SetCursor(cursor) => {
self.handle_set_cursor_msg(cursor) self.handle_set_cursor_msg(cursor)
} }
FromCompositorMsg::OpenFileSelectDialog(patterns, multiple_files, sender) => {
return;
}
} }
} }

View file

@ -2,11 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::{self, IpcSender};
use mime_guess::guess_mime_type_opt; use mime_guess::guess_mime_type_opt;
use net_traits::blob_url_store::{BlobBuf, BlobURLStoreError}; use net_traits::blob_url_store::{BlobBuf, BlobURLStoreError};
use net_traits::filemanager_thread::{FileManagerResult, FileManagerThreadMsg, FileOrigin, FilterPattern}; use net_traits::filemanager_thread::{FileManagerResult, FileManagerThreadMsg, FileOrigin, FilterPattern};
use net_traits::filemanager_thread::{FileManagerThreadError, ReadFileProgress, RelativePos, SelectedFile}; use net_traits::filemanager_thread::{FileManagerThreadError, ReadFileProgress, RelativePos, SelectedFile};
use script_traits::ConstellationMsg;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))] #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use servo_config::opts; use servo_config::opts;
use servo_config::prefs::PREFS; use servo_config::prefs::PREFS;
@ -17,6 +18,7 @@ use std::ops::Index;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::sync::atomic::{self, AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{self, AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::Sender;
use std::thread; use std::thread;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))] #[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use tinyfiledialogs; use tinyfiledialogs;
@ -123,12 +125,14 @@ enum FileImpl {
#[derive(Clone)] #[derive(Clone)]
pub struct FileManager { pub struct FileManager {
constellation_chan: Sender<ConstellationMsg>,
store: Arc<FileManagerStore>, store: Arc<FileManagerStore>,
} }
impl FileManager { impl FileManager {
pub fn new() -> FileManager { pub fn new(constellation_chan: Sender<ConstellationMsg>) -> FileManager {
FileManager { FileManager {
constellation_chan: constellation_chan,
store: Arc::new(FileManagerStore::new()), store: Arc::new(FileManagerStore::new()),
} }
} }
@ -159,22 +163,20 @@ impl FileManager {
} }
/// Message handler /// Message handler
pub fn handle<UI>(&self, pub fn handle(&self, msg: FileManagerThreadMsg) {
msg: FileManagerThreadMsg,
ui: &'static UI)
where UI: UIProvider + 'static,
{
match msg { match msg {
FileManagerThreadMsg::SelectFile(filter, sender, origin, opt_test_path) => { FileManagerThreadMsg::SelectFile(filter, sender, origin, opt_test_path) => {
let store = self.store.clone(); let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
thread::Builder::new().name("select file".to_owned()).spawn(move || { thread::Builder::new().name("select file".to_owned()).spawn(move || {
store.select_file(filter, sender, origin, opt_test_path, ui); store.select_file(filter, sender, origin, opt_test_path, constellation_chan);
}).expect("Thread spawning failed"); }).expect("Thread spawning failed");
} }
FileManagerThreadMsg::SelectFiles(filter, sender, origin, opt_test_paths) => { FileManagerThreadMsg::SelectFiles(filter, sender, origin, opt_test_paths) => {
let store = self.store.clone(); let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
thread::Builder::new().name("select files".to_owned()).spawn(move || { thread::Builder::new().name("select files".to_owned()).spawn(move || {
store.select_files(filter, sender, origin, opt_test_paths, ui); store.select_files(filter, sender, origin, opt_test_paths, constellation_chan);
}).expect("Thread spawning failed"); }).expect("Thread spawning failed");
} }
FileManagerThreadMsg::ReadFile(sender, id, check_url_validity, origin) => { FileManagerThreadMsg::ReadFile(sender, id, check_url_validity, origin) => {
@ -279,21 +281,33 @@ impl FileManagerStore {
} }
} }
fn select_file<UI>(&self, fn get_selected_files(&self,
patterns: Vec<FilterPattern>, patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<SelectedFile>>, multiple_files: bool,
origin: FileOrigin, constellation_chan: Sender<ConstellationMsg>) -> Option<Vec<String>> {
opt_test_path: Option<String>, if opts::get().headless {
ui: &UI) return None;
where UI: UIProvider, }
{
let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!");
let msg = ConstellationMsg::OpenFileSelectDialog(patterns, multiple_files, ipc_sender);
constellation_chan.send(msg).map(|_| ipc_receiver.recv().unwrap()).unwrap_or_default()
}
fn select_file(&self,
patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<SelectedFile>>,
origin: FileOrigin,
opt_test_path: Option<String>,
constellation_chan: Sender<ConstellationMsg>) {
// Check if the select_files preference is enabled // Check if the select_files preference is enabled
// to ensure process-level security against compromised script; // to ensure process-level security against compromised script;
// Then try applying opt_test_path directly for testing convenience // Then try applying opt_test_path directly for testing convenience
let opt_s = if select_files_pref_enabled() { let opt_s = if select_files_pref_enabled() {
opt_test_path opt_test_path
} else { } else {
ui.open_file_dialog("", patterns) self.get_selected_files(patterns, false, constellation_chan).map(|mut x| x.pop().unwrap())
}; };
match opt_s { match opt_s {
@ -309,21 +323,19 @@ impl FileManagerStore {
} }
} }
fn select_files<UI>(&self, fn select_files(&self,
patterns: Vec<FilterPattern>, patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>, sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>,
origin: FileOrigin, origin: FileOrigin,
opt_test_paths: Option<Vec<String>>, opt_test_paths: Option<Vec<String>>,
ui: &UI) constellation_chan: Sender<ConstellationMsg>) {
where UI: UIProvider,
{
// Check if the select_files preference is enabled // Check if the select_files preference is enabled
// to ensure process-level security against compromised script; // to ensure process-level security against compromised script;
// Then try applying opt_test_paths directly for testing convenience // Then try applying opt_test_paths directly for testing convenience
let opt_v = if select_files_pref_enabled() { let opt_v = if select_files_pref_enabled() {
opt_test_paths opt_test_paths
} else { } else {
ui.open_file_dialog_multi("", patterns) self.get_selected_files(patterns, true, constellation_chan)
}; };
match opt_v { match opt_v {

View file

@ -267,7 +267,7 @@ impl ResourceChannelManager {
CoreResourceMsg::Synchronize(sender) => { CoreResourceMsg::Synchronize(sender) => {
let _ = sender.send(()); let _ = sender.send(());
} }
CoreResourceMsg::ToFileManager(msg) => self.resource_manager.filemanager.handle(msg, TFD_PROVIDER), CoreResourceMsg::ToFileManager(msg) => self.resource_manager.filemanager.handle(msg),
CoreResourceMsg::Exit(sender) => { CoreResourceMsg::Exit(sender) => {
if let Some(ref config_dir) = self.config_dir { if let Some(ref config_dir) = self.config_dir {
match http_state.auth_cache.read() { match http_state.auth_cache.read() {
@ -384,7 +384,7 @@ impl CoreResourceManager {
user_agent: user_agent, user_agent: user_agent,
devtools_chan: devtools_channel, devtools_chan: devtools_channel,
swmanager_chan: None, swmanager_chan: None,
filemanager: FileManager::new(), filemanager: FileManager::new(constellation_chan),
} }
} }

View file

@ -51,6 +51,7 @@ use libc::c_void;
use msg::constellation_msg::{BrowsingContextId, HistoryStateId, Key, KeyModifiers, KeyState, PipelineId}; use msg::constellation_msg::{BrowsingContextId, HistoryStateId, Key, KeyModifiers, KeyState, PipelineId};
use msg::constellation_msg::{PipelineNamespaceId, TraversalDirection, TopLevelBrowsingContextId}; use msg::constellation_msg::{PipelineNamespaceId, TraversalDirection, TopLevelBrowsingContextId};
use net_traits::{FetchResponseMsg, ReferrerPolicy, ResourceThreads}; use net_traits::{FetchResponseMsg, ReferrerPolicy, ResourceThreads};
use net_traits::filemanager_thread::FilterPattern;
use net_traits::image::base::Image; use net_traits::image::base::Image;
use net_traits::image::base::PixelFormat; use net_traits::image::base::PixelFormat;
use net_traits::image_cache::ImageCache; use net_traits::image_cache::ImageCache;
@ -705,6 +706,8 @@ pub enum ConstellationMsg {
ForwardEvent(PipelineId, CompositorEvent), ForwardEvent(PipelineId, CompositorEvent),
/// Requesting a change to the onscreen cursor. /// Requesting a change to the onscreen cursor.
SetCursor(CursorKind), SetCursor(CursorKind),
/// Requesting to open file select dialog
OpenFileSelectDialog(Vec<FilterPattern>, bool, IpcSender<Option<Vec<String>>>),
} }
/// Resources required by workerglobalscopes /// Resources required by workerglobalscopes