|
16 | 16 | #include "base/strings/string_split.h"
|
17 | 17 | #include "base/strings/string_util.h"
|
18 | 18 | #include "base/strings/utf_string_conversions.h"
|
19 |
| -#include "base/threading/thread.h" |
20 |
| -#include "base/threading/thread_task_runner_handle.h" |
21 | 19 | #include "base/win/registry.h"
|
22 | 20 | #include "shell/browser/native_window_views.h"
|
| 21 | +#include "shell/browser/ui/win/dialog_thread.h" |
23 | 22 | #include "shell/browser/unresponsive_suppressor.h"
|
24 | 23 |
|
25 | 24 | namespace file_dialog {
|
@@ -64,65 +63,6 @@ void ConvertFilters(const Filters& filters,
|
64 | 63 | }
|
65 | 64 | }
|
66 | 65 |
|
67 |
| -struct RunState { |
68 |
| - base::Thread* dialog_thread; |
69 |
| - scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner; |
70 |
| -}; |
71 |
| - |
72 |
| -bool CreateDialogThread(RunState* run_state) { |
73 |
| - auto thread = |
74 |
| - std::make_unique<base::Thread>(ELECTRON_PRODUCT_NAME "FileDialogThread"); |
75 |
| - thread->init_com_with_mta(false); |
76 |
| - if (!thread->Start()) |
77 |
| - return false; |
78 |
| - |
79 |
| - run_state->dialog_thread = thread.release(); |
80 |
| - run_state->ui_task_runner = base::ThreadTaskRunnerHandle::Get(); |
81 |
| - return true; |
82 |
| -} |
83 |
| - |
84 |
| -void OnDialogOpened(electron::util::Promise promise, |
85 |
| - bool canceled, |
86 |
| - std::vector<base::FilePath> paths) { |
87 |
| - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); |
88 |
| - dict.Set("canceled", canceled); |
89 |
| - dict.Set("filePaths", paths); |
90 |
| - promise.Resolve(dict.GetHandle()); |
91 |
| -} |
92 |
| - |
93 |
| -void RunOpenDialogInNewThread(const RunState& run_state, |
94 |
| - const DialogSettings& settings, |
95 |
| - electron::util::Promise promise) { |
96 |
| - std::vector<base::FilePath> paths; |
97 |
| - bool result = ShowOpenDialogSync(settings, &paths); |
98 |
| - run_state.ui_task_runner->PostTask( |
99 |
| - FROM_HERE, |
100 |
| - base::BindOnce(&OnDialogOpened, std::move(promise), !result, paths)); |
101 |
| - run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread); |
102 |
| -} |
103 |
| - |
104 |
| -void OnSaveDialogDone(electron::util::Promise promise, |
105 |
| - bool canceled, |
106 |
| - const base::FilePath path) { |
107 |
| - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); |
108 |
| - dict.Set("canceled", canceled); |
109 |
| - dict.Set("filePath", path); |
110 |
| - promise.Resolve(dict.GetHandle()); |
111 |
| -} |
112 |
| - |
113 |
| -void RunSaveDialogInNewThread(const RunState& run_state, |
114 |
| - const DialogSettings& settings, |
115 |
| - electron::util::Promise promise) { |
116 |
| - base::FilePath path; |
117 |
| - bool result = ShowSaveDialogSync(settings, &path); |
118 |
| - run_state.ui_task_runner->PostTask( |
119 |
| - FROM_HERE, |
120 |
| - base::BindOnce(&OnSaveDialogDone, std::move(promise), !result, path)); |
121 |
| - run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread); |
122 |
| -} |
123 |
| - |
124 |
| -} // namespace |
125 |
| - |
126 | 66 | static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem,
|
127 | 67 | SIGDN type,
|
128 | 68 | LPWSTR lpstr,
|
@@ -217,6 +157,8 @@ static void ApplySettings(IFileDialog* dialog, const DialogSettings& settings) {
|
217 | 157 | }
|
218 | 158 | }
|
219 | 159 |
|
| 160 | +} // namespace |
| 161 | + |
220 | 162 | bool ShowOpenDialogSync(const DialogSettings& settings,
|
221 | 163 | std::vector<base::FilePath>* paths) {
|
222 | 164 | ATL::CComPtr<IFileOpenDialog> file_open_dialog;
|
@@ -273,17 +215,17 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
|
273 | 215 |
|
274 | 216 | void ShowOpenDialog(const DialogSettings& settings,
|
275 | 217 | electron::util::Promise promise) {
|
276 |
| - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); |
277 |
| - RunState run_state; |
278 |
| - if (!CreateDialogThread(&run_state)) { |
279 |
| - dict.Set("canceled", true); |
280 |
| - dict.Set("filePaths", std::vector<base::FilePath>()); |
281 |
| - promise.Resolve(dict.GetHandle()); |
282 |
| - } else { |
283 |
| - run_state.dialog_thread->task_runner()->PostTask( |
284 |
| - FROM_HERE, base::BindOnce(&RunOpenDialogInNewThread, run_state, |
285 |
| - settings, std::move(promise))); |
286 |
| - } |
| 218 | + auto done = [](electron::util::Promise promise, bool success, |
| 219 | + std::vector<base::FilePath> result) { |
| 220 | + v8::Locker locker(promise.isolate()); |
| 221 | + v8::HandleScope handle_scope(promise.isolate()); |
| 222 | + mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); |
| 223 | + dict.Set("canceled", !success); |
| 224 | + dict.Set("filePaths", result); |
| 225 | + promise.Resolve(dict); |
| 226 | + }; |
| 227 | + dialog_thread::Run(base::BindOnce(ShowOpenDialogSync, settings), |
| 228 | + base::BindOnce(done, std::move(promise))); |
287 | 229 | }
|
288 | 230 |
|
289 | 231 | bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
|
@@ -318,17 +260,17 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
|
318 | 260 |
|
319 | 261 | void ShowSaveDialog(const DialogSettings& settings,
|
320 | 262 | electron::util::Promise promise) {
|
321 |
| - RunState run_state; |
322 |
| - if (!CreateDialogThread(&run_state)) { |
| 263 | + auto done = [](electron::util::Promise promise, bool success, |
| 264 | + base::FilePath result) { |
| 265 | + v8::Locker locker(promise.isolate()); |
| 266 | + v8::HandleScope handle_scope(promise.isolate()); |
323 | 267 | mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate());
|
324 |
| - dict.Set("canceled", true); |
325 |
| - dict.Set("filePath", base::FilePath()); |
326 |
| - promise.Resolve(dict.GetHandle()); |
327 |
| - } else { |
328 |
| - run_state.dialog_thread->task_runner()->PostTask( |
329 |
| - FROM_HERE, base::BindOnce(&RunSaveDialogInNewThread, run_state, |
330 |
| - settings, std::move(promise))); |
331 |
| - } |
| 268 | + dict.Set("canceled", !success); |
| 269 | + dict.Set("filePath", result); |
| 270 | + promise.Resolve(dict); |
| 271 | + }; |
| 272 | + dialog_thread::Run(base::BindOnce(ShowSaveDialogSync, settings), |
| 273 | + base::BindOnce(done, std::move(promise))); |
332 | 274 | }
|
333 | 275 |
|
334 | 276 | } // namespace file_dialog
|
0 commit comments