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

Side by Side Diff: components/arc/arc_session_runner.cc

Issue 2586183002: Refactor ArcSessionRunner part 2. (Closed)
Patch Set: Created 4 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/arc_session_runner.h" 5 #include "components/arc/arc_session_runner.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/task_runner.h" 9 #include "base/task_runner.h"
10 #include "components/arc/arc_session.h" 10 #include "components/arc/arc_session.h"
11 11
12 namespace arc { 12 namespace arc {
13 13
14 namespace { 14 namespace {
15 15
16 constexpr base::TimeDelta kDefaultRestartDelay = 16 constexpr base::TimeDelta kDefaultRestartDelay =
17 base::TimeDelta::FromSeconds(5); 17 base::TimeDelta::FromSeconds(5);
18 18
19 } // namespace 19 } // namespace
20 20
21 ArcSessionRunner::ArcSessionRunner(scoped_refptr<base::TaskRunner> task_runner)
22 : ArcSessionRunner(base::Bind(&ArcSession::Create, this, task_runner)) {}
23
24 ArcSessionRunner::ArcSessionRunner(const ArcSessionFactory& factory) 21 ArcSessionRunner::ArcSessionRunner(const ArcSessionFactory& factory)
25 : restart_delay_(kDefaultRestartDelay), 22 : restart_delay_(kDefaultRestartDelay),
26 factory_(factory), 23 factory_(factory),
27 weak_ptr_factory_(this) {} 24 weak_ptr_factory_(this) {}
28 25
29 ArcSessionRunner::~ArcSessionRunner() { 26 ArcSessionRunner::~ArcSessionRunner() {
30 DCHECK(CalledOnValidThread()); 27 DCHECK(thread_checker_.CalledOnValidThread());
31 if (arc_session_) 28 if (arc_session_)
32 arc_session_->RemoveObserver(this); 29 arc_session_->RemoveObserver(this);
33 } 30 }
34 31
32 void ArcSessionRunner::AddObserver(ArcSessionObserver* observer) {
33 DCHECK(thread_checker_.CalledOnValidThread());
34 observer_list_.AddObserver(observer);
35 }
36
37 void ArcSessionRunner::RemoveObserver(ArcSessionObserver* observer) {
38 DCHECK(thread_checker_.CalledOnValidThread());
39 observer_list_.RemoveObserver(observer);
40 }
41
35 void ArcSessionRunner::RequestStart() { 42 void ArcSessionRunner::RequestStart() {
36 DCHECK(CalledOnValidThread()); 43 DCHECK(thread_checker_.CalledOnValidThread());
37 44
38 // Consecutive RequestStart() call. Do nothing. 45 // Consecutive RequestStart() call. Do nothing.
39 if (run_requested_) 46 if (run_requested_)
40 return; 47 return;
41 48
42 VLOG(1) << "Session started"; 49 VLOG(1) << "Session started";
43 run_requested_ = true; 50 run_requested_ = true;
44 // Here |run_requested_| transitions from false to true. So, |restart_timer_| 51 // Here |run_requested_| transitions from false to true. So, |restart_timer_|
45 // must be stopped (either not even started, or has been cancelled in 52 // must be stopped (either not even started, or has been cancelled in
46 // previous RequestStop() call). 53 // previous RequestStop() call).
47 DCHECK(!restart_timer_.IsRunning()); 54 DCHECK(!restart_timer_.IsRunning());
48 55
49 if (arc_session_) { 56 if (arc_session_) {
50 // In this case, RequestStop() was called, and before |arc_session_| had 57 // In this case, RequestStop() was called, and before |arc_session_| had
51 // finished stopping, RequestStart() was called. Do nothing in that case, 58 // finished stopping, RequestStart() was called. Do nothing in that case,
52 // since when |arc_session_| does actually stop, OnSessionStopped() will 59 // since when |arc_session_| does actually stop, OnSessionStopped() will
53 // be called, where it should automatically restart. 60 // be called, where it should automatically restart.
54 DCHECK_EQ(state(), State::STOPPING); 61 DCHECK_EQ(state_, State::STOPPING);
55 } else { 62 } else {
56 DCHECK_EQ(state(), State::STOPPED); 63 DCHECK_EQ(state_, State::STOPPED);
57 StartArcSession(); 64 StartArcSession();
58 } 65 }
59 } 66 }
60 67
61 void ArcSessionRunner::RequestStop() { 68 void ArcSessionRunner::RequestStop() {
62 DCHECK(CalledOnValidThread()); 69 DCHECK(thread_checker_.CalledOnValidThread());
63 70
64 // Consecutive RequestStop() call. Do nothing. 71 // Consecutive RequestStop() call. Do nothing.
65 if (!run_requested_) 72 if (!run_requested_)
66 return; 73 return;
67 74
68 VLOG(1) << "Session ended"; 75 VLOG(1) << "Session ended";
69 run_requested_ = false; 76 run_requested_ = false;
70 77
71 if (arc_session_) { 78 if (arc_session_) {
72 // The |state_| could be either STARTING, RUNNING or STOPPING. 79 // The |state_| could be either STARTING, RUNNING or STOPPING.
73 DCHECK_NE(state(), State::STOPPED); 80 DCHECK_NE(state_, State::STOPPED);
74 81
75 if (state() == State::STOPPING) { 82 if (state_ == State::STOPPING) {
76 // STOPPING is found in the senario of "RequestStart() -> RequestStop() 83 // STOPPING is found in the senario of "RequestStart() -> RequestStop()
77 // -> RequestStart() -> RequestStop()" case. 84 // -> RequestStart() -> RequestStop()" case.
78 // In the first RequestStop() call, |state_| is set to STOPPING, 85 // In the first RequestStop() call, |state_| is set to STOPPING,
79 // and in the second RequestStop() finds it (so this is the second call). 86 // and in the second RequestStop() finds it (so this is the second call).
80 // In that case, ArcSession::Stop() is already called, so do nothing. 87 // In that case, ArcSession::Stop() is already called, so do nothing.
81 return; 88 return;
82 } 89 }
83 SetState(State::STOPPING); 90 state_ = State::STOPPING;
84 arc_session_->Stop(); 91 arc_session_->Stop();
85 } else { 92 } else {
86 DCHECK_EQ(state(), State::STOPPED); 93 DCHECK_EQ(state_, State::STOPPED);
87 // In case restarting is in progress, cancel it. 94 // In case restarting is in progress, cancel it.
88 restart_timer_.Stop(); 95 restart_timer_.Stop();
89 } 96 }
90 } 97 }
91 98
92 void ArcSessionRunner::OnShutdown() { 99 void ArcSessionRunner::OnShutdown() {
93 DCHECK(CalledOnValidThread()); 100 DCHECK(thread_checker_.CalledOnValidThread());
94 101
95 VLOG(1) << "OnShutdown"; 102 VLOG(1) << "OnShutdown";
96 run_requested_ = false; 103 run_requested_ = false;
97 restart_timer_.Stop(); 104 restart_timer_.Stop();
98 if (arc_session_) 105 if (arc_session_)
99 arc_session_->OnShutdown(); 106 arc_session_->OnShutdown();
100 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously. 107 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously.
101 // In the observer method, |arc_session_| should be destroyed. 108 // In the observer method, |arc_session_| should be destroyed.
102 DCHECK(!arc_session_); 109 DCHECK(!arc_session_);
103 } 110 }
104 111
112 bool ArcSessionRunner::IsRunning() const {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 return state_ == State::RUNNING;
115 }
116
117 bool ArcSessionRunner::IsStopped() const {
118 DCHECK(thread_checker_.CalledOnValidThread());
119 return state_ == State::STOPPED;
120 }
121
105 void ArcSessionRunner::SetRestartDelayForTesting( 122 void ArcSessionRunner::SetRestartDelayForTesting(
106 const base::TimeDelta& restart_delay) { 123 const base::TimeDelta& restart_delay) {
107 DCHECK_EQ(state(), State::STOPPED); 124 DCHECK_EQ(state_, State::STOPPED);
108 DCHECK(!arc_session_); 125 DCHECK(!arc_session_);
109 DCHECK(!restart_timer_.IsRunning()); 126 DCHECK(!restart_timer_.IsRunning());
110 restart_delay_ = restart_delay; 127 restart_delay_ = restart_delay;
111 } 128 }
112 129
113 void ArcSessionRunner::StartArcSession() { 130 void ArcSessionRunner::StartArcSession() {
114 DCHECK(CalledOnValidThread()); 131 DCHECK(thread_checker_.CalledOnValidThread());
115 DCHECK_EQ(state(), State::STOPPED); 132 DCHECK_EQ(state_, State::STOPPED);
116 DCHECK(!arc_session_); 133 DCHECK(!arc_session_);
117 DCHECK(!restart_timer_.IsRunning()); 134 DCHECK(!restart_timer_.IsRunning());
118 135
119 VLOG(1) << "Starting ARC instance"; 136 VLOG(1) << "Starting ARC instance";
120 SetStopReason(StopReason::SHUTDOWN);
121 arc_session_ = factory_.Run(); 137 arc_session_ = factory_.Run();
122 arc_session_->AddObserver(this); 138 arc_session_->AddObserver(this);
123 SetState(State::STARTING); 139 state_ = State::STARTING;
124 arc_session_->Start(); 140 arc_session_->Start();
125 } 141 }
126 142
127 void ArcSessionRunner::OnSessionReady() { 143 void ArcSessionRunner::OnSessionReady() {
128 DCHECK(CalledOnValidThread()); 144 DCHECK(thread_checker_.CalledOnValidThread());
129 DCHECK_EQ(state(), State::STARTING); 145 DCHECK_EQ(state_, State::STARTING);
130 DCHECK(arc_session_); 146 DCHECK(arc_session_);
131 DCHECK(!restart_timer_.IsRunning()); 147 DCHECK(!restart_timer_.IsRunning());
132 148
133 VLOG(0) << "ARC ready"; 149 VLOG(0) << "ARC ready";
134 SetState(State::RUNNING); 150 state_ = State::RUNNING;
151
152 for (auto& observer : observer_list_)
153 observer.OnSessionReady();
135 } 154 }
136 155
137 void ArcSessionRunner::OnSessionStopped(StopReason stop_reason) { 156 void ArcSessionRunner::OnSessionStopped(StopReason stop_reason) {
138 DCHECK(CalledOnValidThread()); 157 DCHECK(thread_checker_.CalledOnValidThread());
139 DCHECK_NE(state(), State::STOPPED); 158 DCHECK_NE(state_, State::STOPPED);
140 DCHECK(arc_session_); 159 DCHECK(arc_session_);
141 DCHECK(!restart_timer_.IsRunning()); 160 DCHECK(!restart_timer_.IsRunning());
142 161
143 VLOG(0) << "ARC stopped: " << stop_reason; 162 VLOG(0) << "ARC stopped: " << stop_reason;
144 arc_session_->RemoveObserver(this); 163 arc_session_->RemoveObserver(this);
145 arc_session_.reset(); 164 arc_session_.reset();
146 165
147 // If RUNNING, ARC instance unexpectedly crashed so we need to restart it 166 // If RUNNING, ARC instance unexpectedly crashed so we need to restart it
148 // automatically. If STOPPING, it is the result of consecutive RequestStop() 167 // automatically. If STOPPING, it is the result of consecutive RequestStop()
149 // followed by RequestStart() invocation. 168 // followed by RequestStart() invocation.
150 // If STARTING, ARC instance has not been booted properly, so do not 169 // If STARTING, ARC instance has not been booted properly, so do not
151 // restart it automatically. 170 // restart it automatically.
152 if (run_requested_ && 171 if (run_requested_ &&
153 (state() == State::RUNNING || state() == State::STOPPING)) { 172 (state_ == State::RUNNING || state_ == State::STOPPING)) {
154 // There was a previous invocation and it crashed for some reason. Try 173 // There was a previous invocation and it crashed for some reason. Try
155 // starting ARC instance later again. 174 // starting ARC instance later again.
156 // Note that even |restart_delay_| is 0 (for testing), it needs to 175 // Note that even |restart_delay_| is 0 (for testing), it needs to
157 // PostTask, because observer callback may call RequestStart()/Stop(). 176 // PostTask, because observer callback may call RequestStart()/Stop().
158 VLOG(0) << "ARC restarting"; 177 VLOG(0) << "ARC restarting";
159 restart_timer_.Start(FROM_HERE, restart_delay_, 178 restart_timer_.Start(FROM_HERE, restart_delay_,
160 base::Bind(&ArcSessionRunner::StartArcSession, 179 base::Bind(&ArcSessionRunner::StartArcSession,
161 weak_ptr_factory_.GetWeakPtr())); 180 weak_ptr_factory_.GetWeakPtr()));
162 } 181 }
163 182
164 SetStopReason(stop_reason); 183 state_ = State::STOPPED;
165 SetState(State::STOPPED); 184 for (auto& observer : observer_list_)
185 observer.OnSessionStopped(stop_reason);
166 } 186 }
167 187
168 } // namespace arc 188 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698