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