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/ack_or_timeout_reporter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/sequence_checker.h" | |
12 #include "base/time/time.h" | |
13 | |
14 namespace remoting { | |
15 | |
16 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
| |
17 public: | |
18 AckOrTimeoutReporter( | |
19 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, | |
20 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback); | |
21 | |
22 void Execute( | |
23 const base::Callback<void(const base::Closure& ack)>& function_that_acks, | |
24 const base::TimeDelta& timeout); | |
25 | |
26 private: | |
27 friend class base::RefCounted<AckOrTimeoutReporter>; | |
28 ~AckOrTimeoutReporter(); | |
29 | |
30 void OnAckOrTimeout(AckOrTimeout ack_or_timeout); | |
31 | |
32 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | |
33 base::Callback<void(AckOrTimeout)> ack_or_timeout_callback_; | |
34 base::WeakPtrFactory<AckOrTimeoutReporter> weak_ptr_factory_; | |
35 scoped_refptr<AckOrTimeoutReporter> self_; | |
36 base::SequenceChecker sequence_checker_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(AckOrTimeoutReporter); | |
39 }; | |
40 | |
41 AckOrTimeoutReporter::AckOrTimeoutReporter( | |
42 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, | |
43 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback) | |
44 : sequenced_task_runner_(sequenced_task_runner), | |
45 ack_or_timeout_callback_(ack_or_timeout_callback), | |
46 weak_ptr_factory_(this), | |
47 self_(this) { | |
48 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
| |
49 } | |
50 | |
51 AckOrTimeoutReporter::~AckOrTimeoutReporter() { | |
52 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
| |
53 } | |
54 | |
55 void AckOrTimeoutReporter::Execute( | |
56 const base::Callback<void(const base::Closure& ack)>& function_that_acks, | |
57 const base::TimeDelta& timeout) { | |
58 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
59 | |
60 base::Closure timeout_callback = | |
61 base::Bind(&AckOrTimeoutReporter::OnAckOrTimeout, | |
62 weak_ptr_factory_.GetWeakPtr(), AckOrTimeout::Timeout); | |
63 base::Closure ack_callback = | |
64 base::Bind(&AckOrTimeoutReporter::OnAckOrTimeout, | |
65 weak_ptr_factory_.GetWeakPtr(), AckOrTimeout::Ack); | |
66 | |
67 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
| |
68 function_that_acks.Run(ack_callback); | |
69 } | |
70 | |
71 void AckOrTimeoutReporter::OnAckOrTimeout(AckOrTimeout ack_or_timeout) { | |
72 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
73 | |
74 // Make sure |ack_callback| and |timeout_callback| created | |
75 // in |Execute| method become no-ops that are safe to be invoked | |
76 // after |this| is no longer alive. | |
77 // This is done as the very first thing here, to make sure | |
78 // |ack_callback| and |timeout_callback| won't be called | |
79 // again no matter what happens next (i.e. as a consequence | |
80 // 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
| |
81 weak_ptr_factory_.InvalidateWeakPtrs(); | |
82 | |
83 ack_or_timeout_callback_.Run(ack_or_timeout); | |
84 self_ = nullptr; | |
85 } | |
86 | |
87 void ReportAckOrTimeout( | |
88 const base::Callback<void(const base::Closure&)>& function_that_acks, | |
89 const base::TimeDelta& timeout, | |
90 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner, | |
91 const base::Callback<void(AckOrTimeout)>& ack_or_timeout_callback) { | |
92 scoped_refptr<AckOrTimeoutReporter> reporter( | |
93 new AckOrTimeoutReporter(sequenced_task_runner, ack_or_timeout_callback)); | |
94 reporter->Execute(function_that_acks, timeout); | |
95 } | |
96 | |
97 } // namespace remoting | |
OLD | NEW |