Auto merge of #20480 - kwonoj:refactor-file-manager, r=paulrouget

refactor(net): removes direct ui invocation from filemgr thread

<!-- Please describe your changes on the following line: -->
- relates to https://github.com/servo/servo/issues/20428.

This PR tries to refactor `net::filemanager_thread` implementation, removes direct ui-related code invocation but ask constellation. I believe overall organization might need to be refactored still as I took my own liberty to wire up dots and dots between components, which could be non-recommended practices.

Probably point of review / need to be updated are

1. Communication between components
Currently it's wired as like below:
```
+----------------+                    +---------------+              +---------+
|                |  constellationMsg  |               | embedderMsg  |         |
| filemgr_thread +------------------->+ constellation +------------->+ browser |
|                |                    |               |              |         |
+-----+----------+                    +---------------+              +----+----+
      ^                                                                   |
      +-------------------------------------------------------------------+
                                filelist: Vec(String)
```
- is this feasible approach?
- does organization of message / fn (where to put consteallation / embedder msg & fns) are legit?

2. Removal of `filemanger_thread::UIProvider`
- As filemanager_thread no longer need to aware actual ui, this PR removes `UIProvider` completely and let invoke tinyfiledialog directly in message listener. Maybe UIProvider itself still being needed?

3. Overall fn organization
- To reduce duplicated code it takes single msg with boolean flag to distinguish selecting multiple files, may feasible / or better to create explicit paths between two.

4. Invoking tfd in a separate thread
- This was mainly to align behavior to previous implentation, where tfd was invoked inside of filemanager_thread so does not block main. It may possibly just let block instead.

and of course, a lot of other codes need to follow better practices.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #20428 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
- Manually verified file picker `<input type=files>` can pick up files.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20480)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-04-29 03:57:46 -04:00 committed by GitHub
commit bf667677f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 160 additions and 147 deletions

View file

@ -10,14 +10,16 @@ use servo::compositing::windowing::{WebRenderDebugOption, WindowEvent};
use servo::ipc_channel::ipc::IpcSender;
use servo::msg::constellation_msg::{Key, TopLevelBrowsingContextId as BrowserId};
use servo::msg::constellation_msg::{KeyModifiers, KeyState, TraversalDirection};
use servo::net_traits::filemanager_thread::FilterPattern;
use servo::net_traits::pub_domains::is_reg_domain;
use servo::script_traits::TouchEventType;
use servo::servo_config::opts;
use servo::servo_config::prefs::PREFS;
use servo::servo_url::ServoUrl;
use servo::webrender_api::ScrollLocation;
use std::mem;
use std::rc::Rc;
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use std::thread;
use tinyfiledialogs;
@ -294,6 +296,12 @@ impl Browser {
},
EmbedderMsg::GetSelectedBluetoothDevice(devices, sender) => {
platform_get_selected_devices(devices, sender);
},
EmbedderMsg::SelectFiles(patterns, multiple_files, sender) => {
if opts::get().headless {
let _ = sender.send(None);
}
platform_get_selected_files(patterns, multiple_files, sender);
}
EmbedderMsg::ShowIME(_browser_id, _kind) => {
debug!("ShowIME received");
@ -340,6 +348,40 @@ fn platform_get_selected_devices(devices: Vec<String>, sender: IpcSender<Option<
let _ = sender.send(None);
}
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
fn platform_get_selected_files(patterns: Vec<FilterPattern>,
multiple_files: bool,
sender: IpcSender<Option<Vec<String>>>) {
let picker_name = if multiple_files { "Pick files" } else { "Pick a file" };
thread::Builder::new().name(picker_name.to_owned()).spawn(move || {
let mut filter = vec![];
for p in patterns {
let s = "*.".to_string() + &p.0;
filter.push(s)
}
let filter_ref = &(filter.iter().map(|s| s.as_str()).collect::<Vec<&str>>()[..]);
let filter_opt = if filter.len() > 0 { Some((filter_ref, "")) } else { None };
if multiple_files {
let files = tinyfiledialogs::open_file_dialog_multi(picker_name, "", filter_opt);
let _ = sender.send(files);
} else {
let file = tinyfiledialogs::open_file_dialog(picker_name, "", filter_opt);
let _ = sender.send(file.map(|x| vec![x]));
}
}).expect("Thread spawning failed");
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn platform_get_selected_files(_patterns: Vec<FilterPattern>,
_multiple_files: bool,
sender: IpcSender<Option<Vec<String>>>) {
warn!("File picker not implemented");
let _ = sender.send(None);
}
fn sanitize_url(request: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(&request).ok()