Chromium Code Reviews| Index: remoting/host/it2me/it2me_confirmation_dialog_proxy.cc |
| diff --git a/remoting/host/it2me/it2me_confirmation_dialog_proxy.cc b/remoting/host/it2me/it2me_confirmation_dialog_proxy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5f44f0ce61d6c681dbf8c9895653f009974cfb43 |
| --- /dev/null |
| +++ b/remoting/host/it2me/it2me_confirmation_dialog_proxy.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2014 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. |
| + |
| +#include "remoting/host/it2me/it2me_confirmation_dialog_proxy.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/location.h" |
| + |
| +namespace remoting { |
| + |
| +class It2MeConfirmationDialogProxy::Core { |
| + public: |
| + Core(scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| + base::WeakPtr<It2MeConfirmationDialogProxy> parent, |
| + scoped_ptr<It2MeConfirmationDialog> dialog); |
| + ~Core(); |
| + |
| + // Shows the wrapped dialog. Must be called on the UI thread. |
| + void Show(); |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner() { |
| + return ui_task_runner_; |
| + } |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner() { |
| + return caller_task_runner_; |
| + } |
| + |
| + private: |
| + 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.
|
| + |
| + // Reports the dialog result on the caller's thread. |
| + void ReportResult(It2MeConfirmationDialog::Result result); |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| + scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| + base::WeakPtr<It2MeConfirmationDialogProxy> parent_; |
| + scoped_ptr<It2MeConfirmationDialog> dialog_; |
| + scoped_ptr<base::WeakPtrFactory<Core>> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Core); |
| +}; |
| + |
| +It2MeConfirmationDialogProxy::Core::Core( |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| + base::WeakPtr<It2MeConfirmationDialogProxy> parent, |
| + scoped_ptr<It2MeConfirmationDialog> dialog) |
| + : ui_task_runner_(ui_task_runner), |
| + caller_task_runner_(caller_task_runner), |
| + parent_(parent), |
| + dialog_(dialog.Pass()) { |
| +} |
| + |
| +It2MeConfirmationDialogProxy::Core::~Core() { |
| + DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| +} |
| + |
| +void It2MeConfirmationDialogProxy::Core::Show() { |
| + DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| + |
| + // We must create and init |weak_factory_| here because the constructor does |
| + // not run in the UI thread. |
| + DCHECK(!weak_factory_); |
| + weak_factory_.reset(new base::WeakPtrFactory<Core>(this)); |
| + dialog_->Show(base::Bind(&It2MeConfirmationDialogProxy::Core::ReportResult, |
| + 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.
|
| +} |
| + |
| +void It2MeConfirmationDialogProxy::Core::ReportResult( |
| + It2MeConfirmationDialog::Result result) { |
| + DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| + caller_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&It2MeConfirmationDialogProxy::ReportResult, parent_, result)); |
| +} |
| + |
| +// It2MeConfirmationDialogProxy =============================================== |
| + |
| +It2MeConfirmationDialogProxy::It2MeConfirmationDialogProxy( |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| + scoped_ptr<It2MeConfirmationDialog> dialog) |
| + : weak_factory_(this) { |
| + DCHECK(caller_task_runner->BelongsToCurrentThread()); |
| + core_.reset(new Core(ui_task_runner, caller_task_runner, |
| + weak_factory_.GetWeakPtr(), dialog.Pass())); |
| +} |
| + |
| +It2MeConfirmationDialogProxy::~It2MeConfirmationDialogProxy() { |
| + DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); |
| + core_->ui_task_runner()->DeleteSoon(FROM_HERE, core_.release()); |
| +} |
| + |
| +void It2MeConfirmationDialogProxy::Show( |
| + const It2MeConfirmationDialog::ResultCallback& callback) { |
| + DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); |
| + |
| + callback_ = callback; |
| + core_->ui_task_runner()->PostTask(FROM_HERE, |
| + base::Bind(&Core::Show, |
| + base::Unretained(core_.get()))); |
| +} |
| + |
| +void It2MeConfirmationDialogProxy::ReportResult( |
| + It2MeConfirmationDialog::Result result) { |
| + DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); |
| + base::ResetAndReturn(&callback_).Run(result); |
| +} |
| + |
| +} // namespace remoting |