blob: 8eeeabd14dc3f2d85d442feb662bdb9f92a9a5b1 [file] [log] [blame]
khmel46050cb2015-11-26 09:49:331// 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
hidehiko359b3522016-12-21 04:49:355#ifndef COMPONENTS_ARC_ARC_SESSION_RUNNER_H_
6#define COMPONENTS_ARC_ARC_SESSION_RUNNER_H_
khmel46050cb2015-11-26 09:49:337
dchenga0ee5fb82016-04-26 02:46:558#include <memory>
lhchavezf1c055372015-12-07 22:51:189
hidehiko70586e6f2016-12-20 20:19:5010#include "base/callback.h"
khmel46050cb2015-11-26 09:49:3311#include "base/macros.h"
hidehikoe18e9bc2016-12-20 18:28:2512#include "base/time/time.h"
13#include "base/timer/timer.h"
khmel46050cb2015-11-26 09:49:3314#include "components/arc/arc_bridge_service.h"
hidehiko9bc9ba52016-12-14 09:01:1815#include "components/arc/arc_session_observer.h"
hidehiko70586e6f2016-12-20 20:19:5016
khmel46050cb2015-11-26 09:49:3317namespace arc {
18
hidehiko9bc9ba52016-12-14 09:01:1819class ArcSession;
20
hidehiko359b3522016-12-21 04:49:3521// Accept requests to start/stop ARC instance. Also supports automatic
22// restarting on unexpected ARC instance crash.
23// TODO(hidehiko): Get rid of ArcBridgeService inheritance.
hidehiko515fe2b2016-12-21 06:09:0224class ArcSessionRunner : public ArcSessionObserver {
khmel46050cb2015-11-26 09:49:3325 public:
hidehiko88160c82016-10-18 09:43:4726 // This is the factory interface to inject ArcSession instance
hidehiko321d423c2016-09-30 15:51:3027 // for testing purpose.
hidehiko88160c82016-10-18 09:43:4728 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>;
hidehiko321d423c2016-09-30 15:51:3029
hidehiko359b3522016-12-21 04:49:3530 explicit ArcSessionRunner(const ArcSessionFactory& factory);
31 ~ArcSessionRunner() override;
khmel46050cb2015-11-26 09:49:3332
hidehiko515fe2b2016-12-21 06:09:0233 // Add/Remove an observer.
34 void AddObserver(ArcSessionObserver* observer);
35 void RemoveObserver(ArcSessionObserver* observer);
36
37 // Starts the ARC service, then it will connect the Mojo channel. When the
38 // bridge becomes ready, registered Observer's OnSessionReady() is called.
39 void RequestStart();
40
41 // Stops the ARC service.
42 void RequestStop();
43
44 // OnShutdown() should be called when the browser is shutting down. This can
45 // only be called on the thread that this class was created on. We assume that
46 // when this function is called, MessageLoop is no longer exists.
47 void OnShutdown();
48
49 // Returns whether currently ARC instance is running or stopped respectively.
50 // Note that, both can return false at same time when, e.g., starting
51 // or stopping ARC instance.
52 bool IsRunning() const;
53 bool IsStopped() const;
khmel46050cb2015-11-26 09:49:3354
hidehiko88160c82016-10-18 09:43:4755 // Returns the current ArcSession instance for testing purpose.
56 ArcSession* GetArcSessionForTesting() { return arc_session_.get(); }
hidehiko321d423c2016-09-30 15:51:3057
hidehikoe18e9bc2016-12-20 18:28:2558 // Normally, automatic restarting happens after a short delay. When testing,
59 // however, we'd like it to happen immediately to avoid adding unnecessary
60 // delays.
61 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay);
lhchaveze143a022016-05-04 03:15:5962
khmel46050cb2015-11-26 09:49:3363 private:
hidehiko515fe2b2016-12-21 06:09:0264 // The possible states. In the normal flow, the state changes in the
65 // following sequence:
66 //
67 // STOPPED
68 // RequestStart() ->
69 // STARTING
70 // OnSessionReady() ->
71 // READY
72 //
73 // The ArcSession state machine can be thought of being substates of
74 // ArcBridgeService's STARTING state.
75 // ArcBridgeService's state machine can be stopped at any phase.
76 //
77 // *
78 // RequestStop() ->
79 // STOPPING
80 // OnSessionStopped() ->
81 // STOPPED
82 enum class State {
83 // ARC instance is not currently running.
84 STOPPED,
85
86 // Request to start ARC instance is received. Starting an ARC instance.
87 STARTING,
88
89 // ARC instance has finished initializing, and is now ready for interaction
90 // with other services.
91 RUNNING,
92
93 // Request to stop ARC instance is recieved. Stopping the ARC instance.
94 STOPPING,
95 };
96
hidehikoe18e9bc2016-12-20 18:28:2597 // Starts to run an ARC instance.
98 void StartArcSession();
99
hidehiko9bc9ba52016-12-14 09:01:18100 // ArcSessionObserver:
101 void OnSessionReady() override;
102 void OnSessionStopped(StopReason reason) override;
khmel46050cb2015-11-26 09:49:33103
hidehiko515fe2b2016-12-21 06:09:02104 base::ThreadChecker thread_checker_;
105
106 // Observers for the ARC instance state change events.
107 base::ObserverList<ArcSessionObserver> observer_list_;
108
hidehiko70586e6f2016-12-20 20:19:50109 // Whether a client requests to run session or not.
110 bool run_requested_ = false;
khmel46050cb2015-11-26 09:49:33111
hidehikoe18e9bc2016-12-20 18:28:25112 // Instead of immediately trying to restart the container, give it some time
113 // to finish tearing down in case it is still in the process of stopping.
114 base::TimeDelta restart_delay_;
115 base::OneShotTimer restart_timer_;
lhchaveze143a022016-05-04 03:15:59116
hidehiko88160c82016-10-18 09:43:47117 // Factory to inject a fake ArcSession instance for testing.
118 ArcSessionFactory factory_;
hidehiko321d423c2016-09-30 15:51:30119
hidehiko515fe2b2016-12-21 06:09:02120 // Current runner's state.
121 State state_ = State::STOPPED;
122
hidehiko70586e6f2016-12-20 20:19:50123 // ArcSession object for currently running ARC instance. This should be
124 // nullptr if the state is STOPPED, otherwise non-nullptr.
125 std::unique_ptr<ArcSession> arc_session_;
126
khmel46050cb2015-11-26 09:49:33127 // WeakPtrFactory to use callbacks.
hidehiko359b3522016-12-21 04:49:35128 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_;
khmel46050cb2015-11-26 09:49:33129
hidehiko359b3522016-12-21 04:49:35130 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner);
khmel46050cb2015-11-26 09:49:33131};
132
133} // namespace arc
134
hidehiko359b3522016-12-21 04:49:35135#endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_