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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/arc/arc_bridge_service_impl.cc
diff --git a/components/arc/arc_bridge_service_impl.cc b/components/arc/arc_bridge_service_impl.cc
deleted file mode 100644
index 524b13ab069c594f7bf9af0e0e21b941b8317156..0000000000000000000000000000000000000000
--- a/components/arc/arc_bridge_service_impl.cc
+++ /dev/null
@@ -1,173 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "components/arc/arc_bridge_service_impl.h"
-
-#include "base/logging.h"
-#include "base/memory/ref_counted.h"
-#include "base/task_runner.h"
-#include "components/arc/arc_session.h"
-
-namespace arc {
-namespace {
-
-constexpr base::TimeDelta kDefaultRestartDelay =
- base::TimeDelta::FromSeconds(5);
-
-} // namespace
-
-ArcBridgeServiceImpl::ArcBridgeServiceImpl(
- const scoped_refptr<base::TaskRunner>& blocking_task_runner)
- : restart_delay_(kDefaultRestartDelay),
- factory_(base::Bind(ArcSession::Create, this, blocking_task_runner)),
- weak_ptr_factory_(this) {}
-
-ArcBridgeServiceImpl::~ArcBridgeServiceImpl() {
- DCHECK(CalledOnValidThread());
- if (arc_session_)
- arc_session_->RemoveObserver(this);
-}
-
-void ArcBridgeServiceImpl::RequestStart() {
- DCHECK(CalledOnValidThread());
-
- // Consecutive RequestStart() call. Do nothing.
- if (running_)
- return;
-
- VLOG(1) << "Session started";
- running_ = true;
- // Here the |running_| becomes false to true. So, |restart_timer_| must be
- // stopped (either not even started, or has been cancelled in previous
- // RequestStop() call).
- DCHECK(!restart_timer_.IsRunning());
-
- if (arc_session_) {
- // This case, a client calls RequestStop() in advance, then soon
- // RequestStart(). In such a case, |arc_session_| must be requested
- // to stop already. Then, do nothing now, and later in OnSessionStopped(),
- // restarting should be handled.
- DCHECK_EQ(state(), State::STOPPING);
- } else {
- DCHECK_EQ(state(), State::STOPPED);
- StartArcSession();
- }
-}
-
-void ArcBridgeServiceImpl::RequestStop() {
- DCHECK(CalledOnValidThread());
-
- // Consecutive RequestStop() call. Do nothing.
- if (!running_)
- return;
-
- VLOG(1) << "Session ended";
- running_ = false;
-
- if (arc_session_) {
- // The |state_| could be either STARTING, RUNNING or STOPPING.
- DCHECK_NE(state(), State::STOPPED);
-
- // STOPPING is found in the senario of "RequestStart() -> RequestStop()
- // -> RequestStart() -> RequestStop()" case.
- // In the first RequestStop() call, |state_| is set to STOPPING,
- // and in the second RequestStop() finds it (so this is the second call).
- // In that case, ArcSession::Stop() is already called, so do nothing.
- if (state() != State::STOPPING) {
- SetState(State::STOPPING);
- arc_session_->Stop();
- }
- } else {
- DCHECK_EQ(state(), State::STOPPED);
- // In case restarting is in progress, cancel it.
- restart_timer_.Stop();
- }
-}
-
-void ArcBridgeServiceImpl::OnShutdown() {
- DCHECK(CalledOnValidThread());
-
- VLOG(1) << "OnShutdown";
- running_ = false;
- restart_timer_.Stop();
- if (arc_session_)
- arc_session_->OnShutdown();
- // ArcSession::OnShutdown() invokes OnSessionStopped() synchronously.
- // In the observer method, |arc_session_| should be destroyed.
- DCHECK(!arc_session_);
-}
-
-void ArcBridgeServiceImpl::SetArcSessionFactoryForTesting(
- const ArcSessionFactory& factory) {
- DCHECK(!factory.is_null());
- DCHECK_EQ(state(), State::STOPPED);
- DCHECK(!arc_session_);
- DCHECK(!restart_timer_.IsRunning());
- factory_ = factory;
-}
-
-void ArcBridgeServiceImpl::SetRestartDelayForTesting(
- const base::TimeDelta& restart_delay) {
- DCHECK_EQ(state(), State::STOPPED);
- DCHECK(!arc_session_);
- DCHECK(!restart_timer_.IsRunning());
- restart_delay_ = restart_delay;
-}
-
-void ArcBridgeServiceImpl::StartArcSession() {
- DCHECK(CalledOnValidThread());
- DCHECK_EQ(state(), State::STOPPED);
- DCHECK(!arc_session_);
- DCHECK(!restart_timer_.IsRunning());
-
- VLOG(1) << "Starting ARC instance";
- SetStopReason(StopReason::SHUTDOWN);
- arc_session_ = factory_.Run();
- arc_session_->AddObserver(this);
- SetState(State::STARTING);
- arc_session_->Start();
-}
-
-void ArcBridgeServiceImpl::OnSessionReady() {
- DCHECK(CalledOnValidThread());
- DCHECK_EQ(state(), State::STARTING);
- DCHECK(arc_session_);
- DCHECK(!restart_timer_.IsRunning());
-
- VLOG(0) << "ARC ready";
- SetState(State::RUNNING);
-}
-
-void ArcBridgeServiceImpl::OnSessionStopped(StopReason stop_reason) {
- DCHECK(CalledOnValidThread());
- DCHECK_NE(state(), State::STOPPED);
- DCHECK(arc_session_);
- DCHECK(!restart_timer_.IsRunning());
-
- VLOG(0) << "ARC stopped: " << stop_reason;
- arc_session_->RemoveObserver(this);
- arc_session_.reset();
-
- // If READY, ARC instance is unexpectedly crashed so we need to restart it
- // automatically. If STOPPING, it is the result of consecutive RequestStop()
- // followed by RequestStart() invocation.
- // If CONNECTING, ARC instance has not been booted properly, so do not
- // restart it automatically.
- if (running_ && (state() == State::RUNNING || state() == State::STOPPING)) {
- // There was a previous invocation and it crashed for some reason. Try
- // starting ARC instance later again.
- // Note that even |restart_delay_| is 0 (for testing), it needs to
- // PostTask, because observer callback may call RequestStart()/Stop(),
- // which can change restarting.
- VLOG(0) << "ARC restarting";
- restart_timer_.Start(FROM_HERE, restart_delay_,
- base::Bind(&ArcBridgeServiceImpl::StartArcSession,
- weak_ptr_factory_.GetWeakPtr()));
- }
-
- SetStopReason(stop_reason);
- SetState(State::STOPPED);
-}
-
-} // namespace arc

Powered by Google App Engine
This is Rietveld 408576698