OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/it2me/it2me_confirmation_dialog_proxy.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/location.h" | |
10 | |
11 namespace remoting { | |
12 | |
13 class It2MeConfirmationDialogProxy::Core { | |
14 public: | |
15 Core(scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
16 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
17 base::WeakPtr<It2MeConfirmationDialogProxy> parent, | |
18 scoped_ptr<It2MeConfirmationDialog> dialog); | |
19 ~Core(); | |
20 | |
21 // Shows the wrapped dialog. Must be called on the UI thread. | |
22 void Show(); | |
23 | |
24 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner() { | |
25 return ui_task_runner_; | |
26 } | |
27 | |
28 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner() { | |
29 return caller_task_runner_; | |
30 } | |
31 | |
32 private: | |
33 friend base::RefCountedThreadSafe<Core>; | |
kelvinp
2014/12/29 22:27:42
This is no longer needed as Core is no longer ref-
dcaiafa
2015/01/05 19:39:27
Done.
| |
34 | |
35 // Reports the dialog result on the caller's thread. | |
36 void ReportResult(It2MeConfirmationDialog::Result result); | |
37 | |
38 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
39 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
40 base::WeakPtr<It2MeConfirmationDialogProxy> parent_; | |
41 scoped_ptr<It2MeConfirmationDialog> dialog_; | |
42 scoped_ptr<base::WeakPtrFactory<Core>> weak_factory_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(Core); | |
45 }; | |
46 | |
47 It2MeConfirmationDialogProxy::Core::Core( | |
48 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
49 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
50 base::WeakPtr<It2MeConfirmationDialogProxy> parent, | |
51 scoped_ptr<It2MeConfirmationDialog> dialog) | |
52 : ui_task_runner_(ui_task_runner), | |
53 caller_task_runner_(caller_task_runner), | |
54 parent_(parent), | |
55 dialog_(dialog.Pass()) { | |
56 } | |
57 | |
58 It2MeConfirmationDialogProxy::Core::~Core() { | |
59 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
60 } | |
61 | |
62 void It2MeConfirmationDialogProxy::Core::Show() { | |
63 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
64 | |
65 // We must create and init |weak_factory_| here because the constructor does | |
66 // not run in the UI thread. | |
67 DCHECK(!weak_factory_); | |
68 weak_factory_.reset(new base::WeakPtrFactory<Core>(this)); | |
69 dialog_->Show(base::Bind(&It2MeConfirmationDialogProxy::Core::ReportResult, | |
70 weak_factory_->GetWeakPtr())); | |
Sergey Ulanov
2014/12/22 19:29:08
It should be safe to use base::Unretained() here.
dcaiafa
2015/01/05 19:39:27
Done.
| |
71 } | |
72 | |
73 void It2MeConfirmationDialogProxy::Core::ReportResult( | |
74 It2MeConfirmationDialog::Result result) { | |
75 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
76 caller_task_runner_->PostTask( | |
77 FROM_HERE, | |
78 base::Bind(&It2MeConfirmationDialogProxy::ReportResult, parent_, result)); | |
79 } | |
80 | |
81 // It2MeConfirmationDialogProxy =============================================== | |
82 | |
83 It2MeConfirmationDialogProxy::It2MeConfirmationDialogProxy( | |
84 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
85 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
86 scoped_ptr<It2MeConfirmationDialog> dialog) | |
87 : weak_factory_(this) { | |
88 DCHECK(caller_task_runner->BelongsToCurrentThread()); | |
89 core_.reset(new Core(ui_task_runner, caller_task_runner, | |
90 weak_factory_.GetWeakPtr(), dialog.Pass())); | |
91 } | |
92 | |
93 It2MeConfirmationDialogProxy::~It2MeConfirmationDialogProxy() { | |
94 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
95 core_->ui_task_runner()->DeleteSoon(FROM_HERE, core_.release()); | |
96 } | |
97 | |
98 void It2MeConfirmationDialogProxy::Show( | |
99 const It2MeConfirmationDialog::ResultCallback& callback) { | |
100 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
101 | |
102 callback_ = callback; | |
103 core_->ui_task_runner()->PostTask(FROM_HERE, | |
104 base::Bind(&Core::Show, | |
105 base::Unretained(core_.get()))); | |
106 } | |
107 | |
108 void It2MeConfirmationDialogProxy::ReportResult( | |
109 It2MeConfirmationDialog::Result result) { | |
110 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
111 base::ResetAndReturn(&callback_).Run(result); | |
112 } | |
113 | |
114 } // namespace remoting | |
OLD | NEW |