diff --git a/components/constellation/constellation.rs b/components/constellation/constellation.rs index 93581d9f884..1a8d8e825e0 100644 --- a/components/constellation/constellation.rs +++ b/components/constellation/constellation.rs @@ -1012,6 +1012,9 @@ impl Constellation FromCompositorMsg::SetCursor(cursor) => { self.handle_set_cursor_msg(cursor) } + FromCompositorMsg::OpenFileSelectDialog(patterns, multiple_files, sender) => { + return; + } } } diff --git a/components/net/filemanager_thread.rs b/components/net/filemanager_thread.rs index 4a84cd29714..bc433895ba9 100644 --- a/components/net/filemanager_thread.rs +++ b/components/net/filemanager_thread.rs @@ -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; @@ -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; @@ -123,12 +125,14 @@ enum FileImpl { #[derive(Clone)] pub struct FileManager { + constellation_chan: Sender, store: Arc, } impl FileManager { - pub fn new() -> FileManager { + pub fn new(constellation_chan: Sender) -> FileManager { FileManager { + constellation_chan: constellation_chan, store: Arc::new(FileManagerStore::new()), } } @@ -159,22 +163,20 @@ impl FileManager { } /// Message handler - pub fn handle(&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) => { @@ -279,21 +281,33 @@ impl FileManagerStore { } } - fn select_file(&self, - patterns: Vec, - sender: IpcSender>, - origin: FileOrigin, - opt_test_path: Option, - ui: &UI) - where UI: UIProvider, - { + fn get_selected_files(&self, + patterns: Vec, + multiple_files: bool, + constellation_chan: Sender) -> Option> { + 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, + sender: IpcSender>, + origin: FileOrigin, + opt_test_path: Option, + constellation_chan: Sender) { // 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 { @@ -309,21 +323,19 @@ impl FileManagerStore { } } - fn select_files(&self, - patterns: Vec, - sender: IpcSender>>, - origin: FileOrigin, - opt_test_paths: Option>, - ui: &UI) - where UI: UIProvider, - { + fn select_files(&self, + patterns: Vec, + sender: IpcSender>>, + origin: FileOrigin, + opt_test_paths: Option>, + constellation_chan: Sender) { // 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 { diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index a7715e1fdf7..fce89fd6624 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -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() { @@ -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), } } diff --git a/components/script_traits/lib.rs b/components/script_traits/lib.rs index 272cfe3cedb..584798caf0e 100644 --- a/components/script_traits/lib.rs +++ b/components/script_traits/lib.rs @@ -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; @@ -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, bool, IpcSender>>), } /// Resources required by workerglobalscopes