khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 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 | |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 5 | #ifndef COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| 6 | #define COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 7 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 8 | #include <memory> |
lhchavez | f1c05537 | 2015-12-07 22:51:18 | [diff] [blame] | 9 | |
hidehiko | 70586e6f | 2016-12-20 20:19:50 | [diff] [blame] | 10 | #include "base/callback.h" |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 11 | #include "base/macros.h" |
hidehiko | e18e9bc | 2016-12-20 18:28:25 | [diff] [blame] | 12 | #include "base/time/time.h" |
| 13 | #include "base/timer/timer.h" |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 14 | #include "components/arc/arc_bridge_service.h" |
hidehiko | 9bc9ba5 | 2016-12-14 09:01:18 | [diff] [blame] | 15 | #include "components/arc/arc_session_observer.h" |
hidehiko | 70586e6f | 2016-12-20 20:19:50 | [diff] [blame] | 16 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 17 | namespace arc { |
| 18 | |
hidehiko | 9bc9ba5 | 2016-12-14 09:01:18 | [diff] [blame] | 19 | class ArcSession; |
| 20 | |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 21 | // 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. |
hidehiko | 515fe2b | 2016-12-21 06:09:02 | [diff] [blame] | 24 | class ArcSessionRunner : public ArcSessionObserver { |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 25 | public: |
hidehiko | 88160c8 | 2016-10-18 09:43:47 | [diff] [blame] | 26 | // This is the factory interface to inject ArcSession instance |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame] | 27 | // for testing purpose. |
hidehiko | 88160c8 | 2016-10-18 09:43:47 | [diff] [blame] | 28 | using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame] | 29 | |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 30 | explicit ArcSessionRunner(const ArcSessionFactory& factory); |
| 31 | ~ArcSessionRunner() override; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 32 | |
hidehiko | 515fe2b | 2016-12-21 06:09:02 | [diff] [blame] | 33 | // 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; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 54 | |
hidehiko | 88160c8 | 2016-10-18 09:43:47 | [diff] [blame] | 55 | // Returns the current ArcSession instance for testing purpose. |
| 56 | ArcSession* GetArcSessionForTesting() { return arc_session_.get(); } |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame] | 57 | |
hidehiko | e18e9bc | 2016-12-20 18:28:25 | [diff] [blame] | 58 | // 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); |
lhchavez | e143a02 | 2016-05-04 03:15:59 | [diff] [blame] | 62 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 63 | private: |
hidehiko | 515fe2b | 2016-12-21 06:09:02 | [diff] [blame] | 64 | // 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 | |
hidehiko | e18e9bc | 2016-12-20 18:28:25 | [diff] [blame] | 97 | // Starts to run an ARC instance. |
| 98 | void StartArcSession(); |
| 99 | |
hidehiko | 9bc9ba5 | 2016-12-14 09:01:18 | [diff] [blame] | 100 | // ArcSessionObserver: |
| 101 | void OnSessionReady() override; |
| 102 | void OnSessionStopped(StopReason reason) override; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 103 | |
hidehiko | 515fe2b | 2016-12-21 06:09:02 | [diff] [blame] | 104 | base::ThreadChecker thread_checker_; |
| 105 | |
| 106 | // Observers for the ARC instance state change events. |
| 107 | base::ObserverList<ArcSessionObserver> observer_list_; |
| 108 | |
hidehiko | 70586e6f | 2016-12-20 20:19:50 | [diff] [blame] | 109 | // Whether a client requests to run session or not. |
| 110 | bool run_requested_ = false; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 111 | |
hidehiko | e18e9bc | 2016-12-20 18:28:25 | [diff] [blame] | 112 | // 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_; |
lhchavez | e143a02 | 2016-05-04 03:15:59 | [diff] [blame] | 116 | |
hidehiko | 88160c8 | 2016-10-18 09:43:47 | [diff] [blame] | 117 | // Factory to inject a fake ArcSession instance for testing. |
| 118 | ArcSessionFactory factory_; |
hidehiko | 321d423c | 2016-09-30 15:51:30 | [diff] [blame] | 119 | |
hidehiko | 515fe2b | 2016-12-21 06:09:02 | [diff] [blame] | 120 | // Current runner's state. |
| 121 | State state_ = State::STOPPED; |
| 122 | |
hidehiko | 70586e6f | 2016-12-20 20:19:50 | [diff] [blame] | 123 | // 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 | |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 127 | // WeakPtrFactory to use callbacks. |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 128 | base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 129 | |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); |
khmel | 46050cb | 2015-11-26 09:49:33 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace arc |
| 134 | |
hidehiko | 359b352 | 2016-12-21 04:49:35 | [diff] [blame] | 135 | #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |