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

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

Issue 2574013003: Refactor ArcSessionRunner part 1. (Closed)
Patch Set: rebase to the split CL 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_bridge_service_impl.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 namespace { 13 namespace {
14 14
15 constexpr base::TimeDelta kDefaultRestartDelay = 15 constexpr base::TimeDelta kDefaultRestartDelay =
16 base::TimeDelta::FromSeconds(5); 16 base::TimeDelta::FromSeconds(5);
17 17
18 } // namespace 18 } // namespace
19 19
20 ArcBridgeServiceImpl::ArcBridgeServiceImpl( 20 ArcSessionRunner::ArcSessionRunner(scoped_refptr<base::TaskRunner> task_runner)
21 const scoped_refptr<base::TaskRunner>& blocking_task_runner) 21 : ArcSessionRunner(base::Bind(&ArcSession::Create, this, task_runner)) {}
22
23 ArcSessionRunner::ArcSessionRunner(const ArcSessionFactory& factory)
22 : restart_delay_(kDefaultRestartDelay), 24 : restart_delay_(kDefaultRestartDelay),
23 factory_(base::Bind(ArcSession::Create, this, blocking_task_runner)), 25 factory_(factory),
24 weak_ptr_factory_(this) {} 26 weak_ptr_factory_(this) {}
25 27
26 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { 28 ArcSessionRunner::~ArcSessionRunner() {
27 DCHECK(CalledOnValidThread()); 29 DCHECK(CalledOnValidThread());
28 if (arc_session_) 30 if (arc_session_)
29 arc_session_->RemoveObserver(this); 31 arc_session_->RemoveObserver(this);
30 } 32 }
31 33
32 void ArcBridgeServiceImpl::RequestStart() { 34 void ArcSessionRunner::RequestStart() {
33 DCHECK(CalledOnValidThread()); 35 DCHECK(CalledOnValidThread());
34 36
35 // Consecutive RequestStart() call. Do nothing. 37 // Consecutive RequestStart() call. Do nothing.
36 if (running_) 38 if (running_)
37 return; 39 return;
38 40
39 VLOG(1) << "Session started"; 41 VLOG(1) << "Session started";
40 running_ = true; 42 running_ = true;
41 // Here the |running_| becomes false to true. So, |restart_timer_| must be 43 // Here the |running_| becomes false to true. So, |restart_timer_| must be
42 // stopped (either not even started, or has been cancelled in previous 44 // stopped (either not even started, or has been cancelled in previous
43 // RequestStop() call). 45 // RequestStop() call).
44 DCHECK(!restart_timer_.IsRunning()); 46 DCHECK(!restart_timer_.IsRunning());
45 47
46 if (arc_session_) { 48 if (arc_session_) {
47 // This case, a client calls RequestStop() in advance, then soon 49 // This case, a client calls RequestStop() in advance, then soon
48 // RequestStart(). In such a case, |arc_session_| must be requested 50 // RequestStart(). In such a case, |arc_session_| must be requested
49 // to stop already. Then, do nothing now, and later in OnSessionStopped(), 51 // to stop already. Then, do nothing now, and later in OnSessionStopped(),
50 // restarting should be handled. 52 // restarting should be handled.
51 DCHECK_EQ(state(), State::STOPPING); 53 DCHECK_EQ(state(), State::STOPPING);
52 } else { 54 } else {
53 DCHECK_EQ(state(), State::STOPPED); 55 DCHECK_EQ(state(), State::STOPPED);
54 StartArcSession(); 56 StartArcSession();
55 } 57 }
56 } 58 }
57 59
58 void ArcBridgeServiceImpl::RequestStop() { 60 void ArcSessionRunner::RequestStop() {
59 DCHECK(CalledOnValidThread()); 61 DCHECK(CalledOnValidThread());
60 62
61 // Consecutive RequestStop() call. Do nothing. 63 // Consecutive RequestStop() call. Do nothing.
62 if (!running_) 64 if (!running_)
63 return; 65 return;
64 66
65 VLOG(1) << "Session ended"; 67 VLOG(1) << "Session ended";
66 running_ = false; 68 running_ = false;
67 69
68 if (arc_session_) { 70 if (arc_session_) {
69 // The |state_| could be either STARTING, RUNNING or STOPPING. 71 // The |state_| could be either STARTING, RUNNING or STOPPING.
70 DCHECK_NE(state(), State::STOPPED); 72 DCHECK_NE(state(), State::STOPPED);
71 73
72 // STOPPING is found in the senario of "RequestStart() -> RequestStop() 74 // STOPPING is found in the senario of "RequestStart() -> RequestStop()
73 // -> RequestStart() -> RequestStop()" case. 75 // -> RequestStart() -> RequestStop()" case.
74 // In the first RequestStop() call, |state_| is set to STOPPING, 76 // In the first RequestStop() call, |state_| is set to STOPPING,
75 // and in the second RequestStop() finds it (so this is the second call). 77 // and in the second RequestStop() finds it (so this is the second call).
76 // In that case, ArcSession::Stop() is already called, so do nothing. 78 // In that case, ArcSession::Stop() is already called, so do nothing.
77 if (state() != State::STOPPING) { 79 if (state() != State::STOPPING) {
78 SetState(State::STOPPING); 80 SetState(State::STOPPING);
79 arc_session_->Stop(); 81 arc_session_->Stop();
80 } 82 }
81 } else { 83 } else {
82 DCHECK_EQ(state(), State::STOPPED); 84 DCHECK_EQ(state(), State::STOPPED);
83 // In case restarting is in progress, cancel it. 85 // In case restarting is in progress, cancel it.
84 restart_timer_.Stop(); 86 restart_timer_.Stop();
85 } 87 }
86 } 88 }
87 89
88 void ArcBridgeServiceImpl::OnShutdown() { 90 void ArcSessionRunner::OnShutdown() {
89 DCHECK(CalledOnValidThread()); 91 DCHECK(CalledOnValidThread());
90 92
91 VLOG(1) << "OnShutdown"; 93 VLOG(1) << "OnShutdown";
92 running_ = false; 94 running_ = false;
93 restart_timer_.Stop(); 95 restart_timer_.Stop();
94 if (arc_session_) 96 if (arc_session_)
95 arc_session_->OnShutdown(); 97 arc_session_->OnShutdown();
96 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously. 98 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously.
97 // In the observer method, |arc_session_| should be destroyed. 99 // In the observer method, |arc_session_| should be destroyed.
98 DCHECK(!arc_session_); 100 DCHECK(!arc_session_);
99 } 101 }
100 102
101 void ArcBridgeServiceImpl::SetArcSessionFactoryForTesting( 103 void ArcSessionRunner::SetRestartDelayForTesting(
102 const ArcSessionFactory& factory) {
103 DCHECK(!factory.is_null());
104 DCHECK_EQ(state(), State::STOPPED);
105 DCHECK(!arc_session_);
106 DCHECK(!restart_timer_.IsRunning());
107 factory_ = factory;
108 }
109
110 void ArcBridgeServiceImpl::SetRestartDelayForTesting(
111 const base::TimeDelta& restart_delay) { 104 const base::TimeDelta& restart_delay) {
112 DCHECK_EQ(state(), State::STOPPED); 105 DCHECK_EQ(state(), State::STOPPED);
113 DCHECK(!arc_session_); 106 DCHECK(!arc_session_);
114 DCHECK(!restart_timer_.IsRunning()); 107 DCHECK(!restart_timer_.IsRunning());
115 restart_delay_ = restart_delay; 108 restart_delay_ = restart_delay;
116 } 109 }
117 110
118 void ArcBridgeServiceImpl::StartArcSession() { 111 void ArcSessionRunner::StartArcSession() {
119 DCHECK(CalledOnValidThread()); 112 DCHECK(CalledOnValidThread());
120 DCHECK_EQ(state(), State::STOPPED); 113 DCHECK_EQ(state(), State::STOPPED);
121 DCHECK(!arc_session_); 114 DCHECK(!arc_session_);
122 DCHECK(!restart_timer_.IsRunning()); 115 DCHECK(!restart_timer_.IsRunning());
123 116
124 VLOG(1) << "Starting ARC instance"; 117 VLOG(1) << "Starting ARC instance";
125 SetStopReason(StopReason::SHUTDOWN); 118 SetStopReason(StopReason::SHUTDOWN);
126 arc_session_ = factory_.Run(); 119 arc_session_ = factory_.Run();
127 arc_session_->AddObserver(this); 120 arc_session_->AddObserver(this);
128 SetState(State::STARTING); 121 SetState(State::STARTING);
129 arc_session_->Start(); 122 arc_session_->Start();
130 } 123 }
131 124
132 void ArcBridgeServiceImpl::OnSessionReady() { 125 void ArcSessionRunner::OnSessionReady() {
133 DCHECK(CalledOnValidThread()); 126 DCHECK(CalledOnValidThread());
134 DCHECK_EQ(state(), State::STARTING); 127 DCHECK_EQ(state(), State::STARTING);
135 DCHECK(arc_session_); 128 DCHECK(arc_session_);
136 DCHECK(!restart_timer_.IsRunning()); 129 DCHECK(!restart_timer_.IsRunning());
137 130
138 VLOG(0) << "ARC ready"; 131 VLOG(0) << "ARC ready";
139 SetState(State::RUNNING); 132 SetState(State::RUNNING);
140 } 133 }
141 134
142 void ArcBridgeServiceImpl::OnSessionStopped(StopReason stop_reason) { 135 void ArcSessionRunner::OnSessionStopped(StopReason stop_reason) {
143 DCHECK(CalledOnValidThread()); 136 DCHECK(CalledOnValidThread());
144 DCHECK_NE(state(), State::STOPPED); 137 DCHECK_NE(state(), State::STOPPED);
145 DCHECK(arc_session_); 138 DCHECK(arc_session_);
146 DCHECK(!restart_timer_.IsRunning()); 139 DCHECK(!restart_timer_.IsRunning());
147 140
148 VLOG(0) << "ARC stopped: " << stop_reason; 141 VLOG(0) << "ARC stopped: " << stop_reason;
149 arc_session_->RemoveObserver(this); 142 arc_session_->RemoveObserver(this);
150 arc_session_.reset(); 143 arc_session_.reset();
151 144
152 // If READY, ARC instance is unexpectedly crashed so we need to restart it 145 // If RUNNING, ARC instance is unexpectedly crashed so we need restart it
153 // automatically. If STOPPING, it is the result of consecutive RequestStop() 146 // automatically. If STOPPING, it is the result of consecutive RequestStop()
154 // followed by RequestStart() invocation. 147 // followed by RequestStart() invocation.
155 // If CONNECTING, ARC instance has not been booted properly, so do not 148 // If STARTING, ARC instance has not been booted properly, so do not restart
156 // restart it automatically. 149 // it automatically.
157 if (running_ && (state() == State::RUNNING || state() == State::STOPPING)) { 150 if (running_ && (state() == State::RUNNING || state() == State::STOPPING)) {
158 // There was a previous invocation and it crashed for some reason. Try 151 // There was a previous invocation and it crashed for some reason. Try
159 // starting ARC instance later again. 152 // starting ARC instance later again.
160 // Note that even |restart_delay_| is 0 (for testing), it needs to 153 // Note that even |restart_delay_| is 0 (for testing), it needs to
161 // PostTask, because observer callback may call RequestStart()/Stop(), 154 // PostTask, because observer callback may call RequestStart()/Stop().
162 // which can change restarting.
163 VLOG(0) << "ARC restarting"; 155 VLOG(0) << "ARC restarting";
164 restart_timer_.Start(FROM_HERE, restart_delay_, 156 restart_timer_.Start(FROM_HERE, restart_delay_,
165 base::Bind(&ArcBridgeServiceImpl::StartArcSession, 157 base::Bind(&ArcSessionRunner::StartArcSession,
166 weak_ptr_factory_.GetWeakPtr())); 158 weak_ptr_factory_.GetWeakPtr()));
167 } 159 }
168 160
169 SetStopReason(stop_reason); 161 SetStopReason(stop_reason);
170 SetState(State::STOPPED); 162 SetState(State::STOPPED);
171 } 163 }
172 164
173 } // namespace arc 165 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698