| 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 #ifndef COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ | 5 #ifndef COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| 6 #define COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ | 6 #define COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_session.h" |
| 15 #include "components/arc/arc_session_observer.h" | 15 #include "components/arc/arc_stop_reason.h" |
| 16 | 16 |
| 17 namespace arc { | 17 namespace arc { |
| 18 | 18 |
| 19 class ArcSession; | |
| 20 | |
| 21 // Accept requests to start/stop ARC instance. Also supports automatic | 19 // Accept requests to start/stop ARC instance. Also supports automatic |
| 22 // restarting on unexpected ARC instance crash. | 20 // restarting on unexpected ARC instance crash. |
| 23 // TODO(hidehiko): Get rid of ArcBridgeService inheritance. | 21 class ArcSessionRunner : public ArcSession::Observer { |
| 24 class ArcSessionRunner : public ArcSessionObserver { | |
| 25 public: | 22 public: |
| 23 // Observer to notify events across multiple ARC session runs. |
| 24 class Observer { |
| 25 public: |
| 26 // Called when ARC instance is stopped. If |restarting| is true, another |
| 27 // ARC session is being restarted (practically after certain delay). |
| 28 // Note: this is called once per ARC session, including unexpected |
| 29 // CRASH on ARC container, and expected SHUTDOWN of ARC triggered by |
| 30 // RequestStop(), so may be called multiple times for one RequestStart(). |
| 31 virtual void OnSessionStopped(ArcStopReason reason, bool restarting) = 0; |
| 32 |
| 33 protected: |
| 34 virtual ~Observer() = default; |
| 35 }; |
| 36 |
| 26 // This is the factory interface to inject ArcSession instance | 37 // This is the factory interface to inject ArcSession instance |
| 27 // for testing purpose. | 38 // for testing purpose. |
| 28 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; | 39 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; |
| 29 | 40 |
| 30 explicit ArcSessionRunner(const ArcSessionFactory& factory); | 41 explicit ArcSessionRunner(const ArcSessionFactory& factory); |
| 31 ~ArcSessionRunner() override; | 42 ~ArcSessionRunner() override; |
| 32 | 43 |
| 33 // Add/Remove an observer. | 44 // Add/Remove an observer. |
| 34 void AddObserver(ArcSessionObserver* observer); | 45 void AddObserver(Observer* observer); |
| 35 void RemoveObserver(ArcSessionObserver* observer); | 46 void RemoveObserver(Observer* observer); |
| 36 | 47 |
| 37 // Starts the ARC service, then it will connect the Mojo channel. When the | 48 // Starts the ARC service, then it will connect the Mojo channel. When the |
| 38 // bridge becomes ready, registered Observer's OnSessionReady() is called. | 49 // bridge becomes ready, registered Observer's OnSessionReady() is called. |
| 39 void RequestStart(); | 50 void RequestStart(); |
| 40 | 51 |
| 41 // Stops the ARC service. | 52 // Stops the ARC service. |
| 42 void RequestStop(); | 53 void RequestStop(); |
| 43 | 54 |
| 44 // OnShutdown() should be called when the browser is shutting down. This can | 55 // 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 | 56 // only be called on the thread that this class was created on. We assume that |
| (...skipping 15 matching lines...) Expand all Loading... |
| 61 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); | 72 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); |
| 62 | 73 |
| 63 private: | 74 private: |
| 64 // The possible states. In the normal flow, the state changes in the | 75 // The possible states. In the normal flow, the state changes in the |
| 65 // following sequence: | 76 // following sequence: |
| 66 // | 77 // |
| 67 // STOPPED | 78 // STOPPED |
| 68 // RequestStart() -> | 79 // RequestStart() -> |
| 69 // STARTING | 80 // STARTING |
| 70 // OnSessionReady() -> | 81 // OnSessionReady() -> |
| 71 // READY | 82 // RUNNING |
| 72 // | 83 // |
| 73 // The ArcSession state machine can be thought of being substates of | 84 // The ArcSession state machine can be thought of being substates of |
| 74 // ArcBridgeService's STARTING state. | 85 // ArcBridgeService's STARTING state. |
| 75 // ArcBridgeService's state machine can be stopped at any phase. | 86 // ArcBridgeService's state machine can be stopped at any phase. |
| 76 // | 87 // |
| 77 // * | 88 // * |
| 78 // RequestStop() -> | 89 // RequestStop() -> |
| 79 // STOPPING | 90 // STOPPING |
| 80 // OnSessionStopped() -> | 91 // OnSessionStopped() -> |
| 81 // STOPPED | 92 // STOPPED |
| 82 enum class State { | 93 enum class State { |
| 83 // ARC instance is not currently running. | 94 // ARC instance is not currently running. |
| 84 STOPPED, | 95 STOPPED, |
| 85 | 96 |
| 86 // Request to start ARC instance is received. Starting an ARC instance. | 97 // Request to start ARC instance is received. Starting an ARC instance. |
| 87 STARTING, | 98 STARTING, |
| 88 | 99 |
| 89 // ARC instance has finished initializing, and is now ready for interaction | 100 // ARC instance has finished initializing, and is now ready for interaction |
| 90 // with other services. | 101 // with other services. |
| 91 RUNNING, | 102 RUNNING, |
| 92 | 103 |
| 93 // Request to stop ARC instance is recieved. Stopping the ARC instance. | 104 // Request to stop ARC instance is recieved. Stopping the ARC instance. |
| 94 STOPPING, | 105 STOPPING, |
| 95 }; | 106 }; |
| 96 | 107 |
| 97 // Starts to run an ARC instance. | 108 // Starts to run an ARC instance. |
| 98 void StartArcSession(); | 109 void StartArcSession(); |
| 99 | 110 |
| 100 // ArcSessionObserver: | 111 // ArcSession::Observer: |
| 101 void OnSessionReady() override; | 112 void OnSessionReady() override; |
| 102 void OnSessionStopped(StopReason reason) override; | 113 void OnSessionStopped(ArcStopReason reason) override; |
| 103 | 114 |
| 104 base::ThreadChecker thread_checker_; | 115 base::ThreadChecker thread_checker_; |
| 105 | 116 |
| 106 // Observers for the ARC instance state change events. | 117 // Observers for the ARC instance state change events. |
| 107 base::ObserverList<ArcSessionObserver> observer_list_; | 118 base::ObserverList<Observer> observer_list_; |
| 108 | 119 |
| 109 // Whether a client requests to run session or not. | 120 // Whether a client requests to run session or not. |
| 110 bool run_requested_ = false; | 121 bool run_requested_ = false; |
| 111 | 122 |
| 112 // Instead of immediately trying to restart the container, give it some time | 123 // 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. | 124 // to finish tearing down in case it is still in the process of stopping. |
| 114 base::TimeDelta restart_delay_; | 125 base::TimeDelta restart_delay_; |
| 115 base::OneShotTimer restart_timer_; | 126 base::OneShotTimer restart_timer_; |
| 116 | 127 |
| 117 // Factory to inject a fake ArcSession instance for testing. | 128 // Factory to inject a fake ArcSession instance for testing. |
| 118 ArcSessionFactory factory_; | 129 ArcSessionFactory factory_; |
| 119 | 130 |
| 120 // Current runner's state. | 131 // Current runner's state. |
| 121 State state_ = State::STOPPED; | 132 State state_ = State::STOPPED; |
| 122 | 133 |
| 123 // ArcSession object for currently running ARC instance. This should be | 134 // ArcSession object for currently running ARC instance. This should be |
| 124 // nullptr if the state is STOPPED, otherwise non-nullptr. | 135 // nullptr if the state is STOPPED, otherwise non-nullptr. |
| 125 std::unique_ptr<ArcSession> arc_session_; | 136 std::unique_ptr<ArcSession> arc_session_; |
| 126 | 137 |
| 127 // WeakPtrFactory to use callbacks. | 138 // WeakPtrFactory to use callbacks. |
| 128 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; | 139 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; |
| 129 | 140 |
| 130 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); | 141 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); |
| 131 }; | 142 }; |
| 132 | 143 |
| 133 } // namespace arc | 144 } // namespace arc |
| 134 | 145 |
| 135 #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ | 146 #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| OLD | NEW |