Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: remoting/host/ack_or_timeout_reporter.cc

Issue 719983002: Reporting of policy errors via host-offline-reason: part 3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hor-nohoststatussender
Patch Set: Rebasing... Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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"
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"
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.
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 = base::Bind(
64 &AckOrTimeoutReporter::OnAckOrTimeout,
65 weak_ptr_factory_.GetWeakPtr(),
66 AckOrTimeout::Timeout);
67 base::Closure ack_callback = base::Bind(
68 &AckOrTimeoutReporter::OnAckOrTimeout,
69 weak_ptr_factory_.GetWeakPtr(),
70 AckOrTimeout::Ack);
71
72 sequenced_task_runner_->PostDelayedTask(
73 FROM_HERE, timeout_callback, timeout);
74 function_that_acks.Run(ack_callback);
75 }
76
77 void AckOrTimeoutReporter::OnAckOrTimeout(AckOrTimeout ack_or_timeout) {
78 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
79
80 // Make sure |ack_callback| and |timeout_callback| created
81 // in |Execute| method become no-ops that are safe to be invoked
82 // after |this| is no longer alive.
83 // This is done as the very first thing here, to make sure
84 // |ack_callback| and |timeout_callback| won't be called
85 // again no matter what happens next (i.e. as a consequence
86 // of |ack_or_timeout_callback_|.
87 weak_ptr_factory_.InvalidateWeakPtrs();
88
89 // Call our external callback.
90 ack_or_timeout_callback_.Run(ack_or_timeout);
91
92 // Release outselves from memory - we won't be called anymore.
Lambros 2014/12/03 03:20:25 typo: ourselves But the grammar is a bit dubious h
Łukasz Anforowicz 2014/12/03 17:54:21 Done.
93 self_ = nullptr;
94 }
95
96 void ReportAckOrTimeout(
97 const base::Callback<void(const base::Closure&)> function_that_acks,
98 const base::TimeDelta& timeout,
99 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner,
100 const base::Callback<void(AckOrTimeout)> ack_or_timeout_callback) {
101 scoped_refptr<AckOrTimeoutReporter> reporter(
102 new AckOrTimeoutReporter(sequenced_task_runner,
103 ack_or_timeout_callback));
104 reporter->Execute(function_that_acks, timeout);
105 }
106
107 } // namespace remoting
108
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698