Chromium Code Reviews| Index: remoting/host/ack_or_timeout_reporter.cc |
| diff --git a/remoting/host/ack_or_timeout_reporter.cc b/remoting/host/ack_or_timeout_reporter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f35e8f504afc1ffcd7fb76629ab117241862aa66 |
| --- /dev/null |
| +++ b/remoting/host/ack_or_timeout_reporter.cc |
| @@ -0,0 +1,97 @@ |
| +// 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/ack_or_timeout_reporter.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/sequence_checker.h" |
| +#include "base/time/time.h" |
| + |
| +namespace remoting { |
| + |
| +class AckOrTimeoutReporter : public base::RefCounted<AckOrTimeoutReporter> { |
|
Wez
2014/12/09 00:51:58
Why does this need to be ref-counted? The only thi
Łukasz Anforowicz
2014/12/09 18:47:07
"The only thing that owns it is itself" is not tru
Łukasz Anforowicz
2014/12/10 05:11:09
I've added a unit test where ack_callback is synch
|
| + public: |
| + AckOrTimeoutReporter( |
| + scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, |
| + const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback); |
| + |
| + void Execute( |
| + const base::Callback<void(const base::Closure& ack)>& function_that_acks, |
| + const base::TimeDelta& timeout); |
| + |
| + private: |
| + friend class base::RefCounted<AckOrTimeoutReporter>; |
| + ~AckOrTimeoutReporter(); |
| + |
| + void OnAckOrTimeout(AckOrTimeout ack_or_timeout); |
| + |
| + scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| + base::Callback<void(AckOrTimeout)> ack_or_timeout_callback_; |
| + base::WeakPtrFactory<AckOrTimeoutReporter> weak_ptr_factory_; |
| + scoped_refptr<AckOrTimeoutReporter> self_; |
| + base::SequenceChecker sequence_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AckOrTimeoutReporter); |
| +}; |
| + |
| +AckOrTimeoutReporter::AckOrTimeoutReporter( |
| + scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, |
| + const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback) |
| + : sequenced_task_runner_(sequenced_task_runner), |
| + ack_or_timeout_callback_(ack_or_timeout_callback), |
| + weak_ptr_factory_(this), |
| + self_(this) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
|
Wez
2014/12/09 00:51:57
This check is a no-op; the sequence checker binds
Łukasz Anforowicz
2014/12/09 18:47:07
Good point. This was written this way before I wa
|
| +} |
| + |
| +AckOrTimeoutReporter::~AckOrTimeoutReporter() { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
|
Wez
2014/12/09 00:51:57
SequenceChecker will do this in its own dtor.
Łukasz Anforowicz
2014/12/09 18:47:06
I'll keep this (because based on another comment I
|
| +} |
| + |
| +void AckOrTimeoutReporter::Execute( |
| + const base::Callback<void(const base::Closure& ack)>& function_that_acks, |
| + const base::TimeDelta& timeout) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + base::Closure timeout_callback = |
| + base::Bind(&AckOrTimeoutReporter::OnAckOrTimeout, |
| + weak_ptr_factory_.GetWeakPtr(), AckOrTimeout::Timeout); |
| + base::Closure ack_callback = |
| + base::Bind(&AckOrTimeoutReporter::OnAckOrTimeout, |
| + weak_ptr_factory_.GetWeakPtr(), AckOrTimeout::Ack); |
| + |
| + sequenced_task_runner_->PostDelayedTask(FROM_HERE, timeout_callback, timeout); |
|
Wez
2014/12/09 00:51:58
Couldn't you use a OneShotTimer here rather than r
Łukasz Anforowicz
2014/12/09 18:47:06
I don't understand the proposal.
- Requirement #1
|
| + function_that_acks.Run(ack_callback); |
| +} |
| + |
| +void AckOrTimeoutReporter::OnAckOrTimeout(AckOrTimeout ack_or_timeout) { |
| + DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| + |
| + // Make sure |ack_callback| and |timeout_callback| created |
| + // in |Execute| method become no-ops that are safe to be invoked |
| + // after |this| is no longer alive. |
| + // This is done as the very first thing here, to make sure |
| + // |ack_callback| and |timeout_callback| won't be called |
| + // again no matter what happens next (i.e. as a consequence |
| + // of |ack_or_timeout_callback_|. |
|
Wez
2014/12/09 00:51:57
How could anything that that callback does possibl
Łukasz Anforowicz
2014/12/09 18:47:07
i.e. it could start a nested TaskRunner pump.
Wez
2014/12/15 20:34:24
That's specifically disallowed in Chromium product
|
| + weak_ptr_factory_.InvalidateWeakPtrs(); |
| + |
| + ack_or_timeout_callback_.Run(ack_or_timeout); |
| + self_ = nullptr; |
| +} |
| + |
| +void ReportAckOrTimeout( |
| + const base::Callback<void(const base::Closure&)>& function_that_acks, |
| + const base::TimeDelta& timeout, |
| + scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, |
| + const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback) { |
| + scoped_refptr<AckOrTimeoutReporter> reporter( |
| + new AckOrTimeoutReporter(sequenced_task_runner, ack_or_timeout_callback)); |
| + reporter->Execute(function_that_acks, timeout); |
| +} |
| + |
| +} // namespace remoting |