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

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

Powered by Google App Engine
This is Rietveld 408576698