forked from adamlaska/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialog_thread.h
More file actions
80 lines (71 loc) · 2.61 KB
/
dialog_thread.h
File metadata and controls
80 lines (71 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_UI_WIN_DIALOG_THREAD_H_
#define ELECTRON_SHELL_BROWSER_UI_WIN_DIALOG_THREAD_H_
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_thread.h"
namespace dialog_thread {
// Returns the dedicated single-threaded sequence that the dialog will be on.
using TaskRunner = scoped_refptr<base::SingleThreadTaskRunner>;
TaskRunner CreateDialogTaskRunner();
// Runs the |execute| in dialog thread and pass result to |done| in UI thread.
template <typename R>
void Run(base::OnceCallback<R()> execute, base::OnceCallback<void(R)> done) {
// dialogThread.postTask(() => {
// r = execute()
// uiThread.postTask(() => {
// done(r)
// }
// })
TaskRunner task_runner = CreateDialogTaskRunner();
task_runner->PostTask(
FROM_HERE,
base::BindOnce(
[](TaskRunner task_runner, base::OnceCallback<R()> execute,
base::OnceCallback<void(R)> done) {
R r = std::move(execute).Run();
base::PostTask(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(
[](TaskRunner task_runner, base::OnceCallback<void(R)> done,
R r) {
std::move(done).Run(std::move(r));
// Task runner will destroyed automatically after the
// scope ends.
},
std::move(task_runner), std::move(done), std::move(r)));
},
std::move(task_runner), std::move(execute), std::move(done)));
}
// Adaptor to handle the |execute| that returns bool.
template <typename R>
void Run(base::OnceCallback<bool(R*)> execute,
base::OnceCallback<void(bool, R)> done) {
// run(() => {
// result = execute(&value)
// return {result, value}
// }, ({result, value}) => {
// done(result, value)
// })
struct Result {
bool result;
R value;
};
Run(base::BindOnce(
[](base::OnceCallback<bool(R*)> execute) {
Result r;
r.result = std::move(execute).Run(&r.value);
return r;
},
std::move(execute)),
base::BindOnce(
[](base::OnceCallback<void(bool, R)> done, Result r) {
std::move(done).Run(r.result, std::move(r.value));
},
std::move(done)));
}
} // namespace dialog_thread
#endif // ELECTRON_SHELL_BROWSER_UI_WIN_DIALOG_THREAD_H_