| OLD | NEW |
| 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 if (state() != State::STOPPING) | 106 DCHECK_NE(state_, State::STOPPED); |
| 100 SetState(State::STOPPING); | 107 state_ = State::STOPPING; |
| 101 arc_session_->OnShutdown(); | 108 arc_session_->OnShutdown(); |
| 102 } | 109 } |
| 103 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously. | 110 // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously. |
| 104 // In the observer method, |arc_session_| should be destroyed. | 111 // In the observer method, |arc_session_| should be destroyed. |
| 105 DCHECK(!arc_session_); | 112 DCHECK(!arc_session_); |
| 106 } | 113 } |
| 107 | 114 |
| 115 bool ArcSessionRunner::IsRunning() const { |
| 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 return state_ == State::RUNNING; |
| 118 } |
| 119 |
| 120 bool ArcSessionRunner::IsStopped() const { |
| 121 DCHECK(thread_checker_.CalledOnValidThread()); |
| 122 return state_ == State::STOPPED; |
| 123 } |
| 124 |
| 108 void ArcSessionRunner::SetRestartDelayForTesting( | 125 void ArcSessionRunner::SetRestartDelayForTesting( |
| 109 const base::TimeDelta& restart_delay) { | 126 const base::TimeDelta& restart_delay) { |
| 110 DCHECK_EQ(state(), State::STOPPED); | 127 DCHECK_EQ(state_, State::STOPPED); |
| 111 DCHECK(!arc_session_); | 128 DCHECK(!arc_session_); |
| 112 DCHECK(!restart_timer_.IsRunning()); | 129 DCHECK(!restart_timer_.IsRunning()); |
| 113 restart_delay_ = restart_delay; | 130 restart_delay_ = restart_delay; |
| 114 } | 131 } |
| 115 | 132 |
| 116 void ArcSessionRunner::StartArcSession() { | 133 void ArcSessionRunner::StartArcSession() { |
| 117 DCHECK(CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
| 118 DCHECK_EQ(state(), State::STOPPED); | 135 DCHECK_EQ(state_, State::STOPPED); |
| 119 DCHECK(!arc_session_); | 136 DCHECK(!arc_session_); |
| 120 DCHECK(!restart_timer_.IsRunning()); | 137 DCHECK(!restart_timer_.IsRunning()); |
| 121 | 138 |
| 122 VLOG(1) << "Starting ARC instance"; | 139 VLOG(1) << "Starting ARC instance"; |
| 123 SetStopReason(StopReason::SHUTDOWN); | |
| 124 arc_session_ = factory_.Run(); | 140 arc_session_ = factory_.Run(); |
| 125 arc_session_->AddObserver(this); | 141 arc_session_->AddObserver(this); |
| 126 SetState(State::STARTING); | 142 state_ = State::STARTING; |
| 127 arc_session_->Start(); | 143 arc_session_->Start(); |
| 128 } | 144 } |
| 129 | 145 |
| 130 void ArcSessionRunner::OnSessionReady() { | 146 void ArcSessionRunner::OnSessionReady() { |
| 131 DCHECK(CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 DCHECK_EQ(state(), State::STARTING); | 148 DCHECK_EQ(state_, State::STARTING); |
| 133 DCHECK(arc_session_); | 149 DCHECK(arc_session_); |
| 134 DCHECK(!restart_timer_.IsRunning()); | 150 DCHECK(!restart_timer_.IsRunning()); |
| 135 | 151 |
| 136 VLOG(0) << "ARC ready"; | 152 VLOG(0) << "ARC ready"; |
| 137 SetState(State::RUNNING); | 153 state_ = State::RUNNING; |
| 154 |
| 155 for (auto& observer : observer_list_) |
| 156 observer.OnSessionReady(); |
| 138 } | 157 } |
| 139 | 158 |
| 140 void ArcSessionRunner::OnSessionStopped(StopReason stop_reason) { | 159 void ArcSessionRunner::OnSessionStopped(StopReason stop_reason) { |
| 141 DCHECK(CalledOnValidThread()); | 160 DCHECK(thread_checker_.CalledOnValidThread()); |
| 142 DCHECK_NE(state(), State::STOPPED); | 161 DCHECK_NE(state_, State::STOPPED); |
| 143 DCHECK(arc_session_); | 162 DCHECK(arc_session_); |
| 144 DCHECK(!restart_timer_.IsRunning()); | 163 DCHECK(!restart_timer_.IsRunning()); |
| 145 | 164 |
| 146 VLOG(0) << "ARC stopped: " << stop_reason; | 165 VLOG(0) << "ARC stopped: " << stop_reason; |
| 147 arc_session_->RemoveObserver(this); | 166 arc_session_->RemoveObserver(this); |
| 148 arc_session_.reset(); | 167 arc_session_.reset(); |
| 149 | 168 |
| 150 // If RUNNING, ARC instance unexpectedly crashed so we need to restart it | 169 // If RUNNING, ARC instance unexpectedly crashed so we need to restart it |
| 151 // automatically. | 170 // automatically. |
| 152 // If STOPPING, at least once RequestStop() is called. If |session_started_| | 171 // If STOPPING, at least once RequestStop() is called. If |session_started_| |
| 153 // is true, RequestStart() is following so schedule to restart ARC session. | 172 // is true, RequestStart() is following so schedule to restart ARC session. |
| 154 // Otherwise, do nothing. | 173 // Otherwise, do nothing. |
| 155 // If STARTING, ARC instance has not been booted properly, so do not | 174 // If STARTING, ARC instance has not been booted properly, so do not |
| 156 // restart it automatically. | 175 // restart it automatically. |
| 157 if (state() == State::RUNNING || | 176 if (state_ == State::RUNNING || |
| 158 (state() == State::STOPPING && run_requested_)) { | 177 (state_ == State::STOPPING && run_requested_)) { |
| 159 // This check is for READY case. In RUNNING case |run_requested_| should | 178 // This check is for RUNNING case. In RUNNING case |run_requested_| should |
| 160 // be always true, because if once RequestStop() is called, the state() | 179 // be always true, because if once RequestStop() is called, the state_ |
| 161 // will be set to STOPPING. | 180 // will be set to STOPPING. |
| 162 DCHECK(run_requested_); | 181 DCHECK(run_requested_); |
| 163 | 182 |
| 164 // There was a previous invocation and it crashed for some reason. Try | 183 // There was a previous invocation and it crashed for some reason. Try |
| 165 // starting ARC instance later again. | 184 // starting ARC instance later again. |
| 166 // Note that even |restart_delay_| is 0 (for testing), it needs to | 185 // Note that even |restart_delay_| is 0 (for testing), it needs to |
| 167 // PostTask, because observer callback may call RequestStart()/Stop(). | 186 // PostTask, because observer callback may call RequestStart()/Stop(). |
| 168 VLOG(0) << "ARC restarting"; | 187 VLOG(0) << "ARC restarting"; |
| 169 restart_timer_.Start(FROM_HERE, restart_delay_, | 188 restart_timer_.Start(FROM_HERE, restart_delay_, |
| 170 base::Bind(&ArcSessionRunner::StartArcSession, | 189 base::Bind(&ArcSessionRunner::StartArcSession, |
| 171 weak_ptr_factory_.GetWeakPtr())); | 190 weak_ptr_factory_.GetWeakPtr())); |
| 172 } | 191 } |
| 173 | 192 |
| 174 // TODO(hidehiko): Consider to let observers know whether there is scheduled | 193 // TODO(hidehiko): Consider to let observers know whether there is scheduled |
| 175 // restarting event, or not. | 194 // restarting event, or not. |
| 176 SetStopReason(stop_reason); | 195 state_ = State::STOPPED; |
| 177 SetState(State::STOPPED); | 196 for (auto& observer : observer_list_) |
| 197 observer.OnSessionStopped(stop_reason); |
| 178 } | 198 } |
| 179 | 199 |
| 180 } // namespace arc | 200 } // namespace arc |
| OLD | NEW |