Skip to content

Commit

Permalink
refactor(filemanager): send constellationmsg to select file
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 27, 2018
1 parent 1c465bc commit b396a2f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
3 changes: 3 additions & 0 deletions components/constellation/constellation.rs
Expand Up @@ -1012,6 +1012,9 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
FromCompositorMsg::SetCursor(cursor) => {
self.handle_set_cursor_msg(cursor)
}
FromCompositorMsg::OpenFileSelectDialog(patterns, multiple_files, sender) => {
return;
}
}
}

Expand Down
66 changes: 39 additions & 27 deletions components/net/filemanager_thread.rs
Expand Up @@ -2,11 +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 ipc_channel::ipc::IpcSender;
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::ConstellationMsg;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use servo_config::opts;
use servo_config::prefs::PREFS;
Expand All @@ -17,6 +18,7 @@ use std::ops::Index;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::sync::atomic::{self, AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::Sender;
use std::thread;
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use tinyfiledialogs;
Expand Down Expand Up @@ -123,12 +125,14 @@ enum FileImpl {

#[derive(Clone)]
pub struct FileManager {
constellation_chan: Sender<ConstellationMsg>,
store: Arc<FileManagerStore>,
}

impl FileManager {
pub fn new() -> FileManager {
pub fn new(constellation_chan: Sender<ConstellationMsg>) -> FileManager {
FileManager {
constellation_chan: constellation_chan,
store: Arc::new(FileManagerStore::new()),
}
}
Expand Down Expand Up @@ -159,22 +163,20 @@ impl FileManager {
}

/// Message handler
pub fn handle<UI>(&self,
msg: FileManagerThreadMsg,
ui: &'static UI)
where UI: UIProvider + 'static,
{
pub fn handle(&self, msg: FileManagerThreadMsg) {
match msg {
FileManagerThreadMsg::SelectFile(filter, sender, origin, opt_test_path) => {
let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
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");
}
FileManagerThreadMsg::SelectFiles(filter, sender, origin, opt_test_paths) => {
let store = self.store.clone();
let constellation_chan = self.constellation_chan.clone();
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");
}
FileManagerThreadMsg::ReadFile(sender, id, check_url_validity, origin) => {
Expand Down Expand Up @@ -279,21 +281,33 @@ impl FileManagerStore {
}
}

fn select_file<UI>(&self,
patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<SelectedFile>>,
origin: FileOrigin,
opt_test_path: Option<String>,
ui: &UI)
where UI: UIProvider,
{
fn get_selected_files(&self,
patterns: Vec<FilterPattern>,
multiple_files: bool,
constellation_chan: Sender<ConstellationMsg>) -> Option<Vec<String>> {
if opts::get().headless {
return None;
}

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
// 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 {
ui.open_file_dialog("", patterns)
self.get_selected_files(patterns, false, constellation_chan).map(|mut x| x.pop().unwrap())
};

match opt_s {
Expand All @@ -309,21 +323,19 @@ impl FileManagerStore {
}
}

fn select_files<UI>(&self,
patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>,
origin: FileOrigin,
opt_test_paths: Option<Vec<String>>,
ui: &UI)
where UI: UIProvider,
{
fn select_files(&self,
patterns: Vec<FilterPattern>,
sender: IpcSender<FileManagerResult<Vec<SelectedFile>>>,
origin: FileOrigin,
opt_test_paths: Option<Vec<String>>,
constellation_chan: Sender<ConstellationMsg>) {
// 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 {
ui.open_file_dialog_multi("", patterns)
self.get_selected_files(patterns, true, constellation_chan)
};

match opt_v {
Expand Down
4 changes: 2 additions & 2 deletions components/net/resource_thread.rs
Expand Up @@ -267,7 +267,7 @@ impl ResourceChannelManager {
CoreResourceMsg::Synchronize(sender) => {
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) => {
if let Some(ref config_dir) = self.config_dir {
match http_state.auth_cache.read() {
Expand Down Expand Up @@ -384,7 +384,7 @@ impl CoreResourceManager {
user_agent: user_agent,
devtools_chan: devtools_channel,
swmanager_chan: None,
filemanager: FileManager::new(),
filemanager: FileManager::new(constellation_chan),
}
}

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

/// Resources required by workerglobalscopes
Expand Down

0 comments on commit b396a2f

Please sign in to comment.