| 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_bridge_service.h" |
| 15 #include "components/arc/arc_session_observer.h" | 15 #include "components/arc/arc_session_observer.h" |
| 16 | 16 |
| 17 template <typename T> | |
| 18 class scoped_refptr; | |
| 19 | |
| 20 namespace base { | |
| 21 class TaskRunner; | |
| 22 } // namespace base | |
| 23 | |
| 24 namespace arc { | 17 namespace arc { |
| 25 | 18 |
| 26 class ArcSession; | 19 class ArcSession; |
| 27 | 20 |
| 28 // Accept requests to start/stop ARC instance. Also supports automatic | 21 // Accept requests to start/stop ARC instance. Also supports automatic |
| 29 // restarting on unexpected ARC instance crash. | 22 // restarting on unexpected ARC instance crash. |
| 30 // TODO(hidehiko): Get rid of ArcBridgeService inheritance. | 23 // TODO(hidehiko): Get rid of ArcBridgeService inheritance. |
| 31 class ArcSessionRunner : public ArcBridgeService, public ArcSessionObserver { | 24 class ArcSessionRunner : public ArcSessionObserver { |
| 32 public: | 25 public: |
| 33 // This is the factory interface to inject ArcSession instance | 26 // This is the factory interface to inject ArcSession instance |
| 34 // for testing purpose. | 27 // for testing purpose. |
| 35 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; | 28 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; |
| 36 | 29 |
| 37 explicit ArcSessionRunner(scoped_refptr<base::TaskRunner> task_runner); | |
| 38 | |
| 39 // For testing. | |
| 40 // TODO(hidehiko): Migrate this and above constructors. | |
| 41 explicit ArcSessionRunner(const ArcSessionFactory& factory); | 30 explicit ArcSessionRunner(const ArcSessionFactory& factory); |
| 42 ~ArcSessionRunner() override; | 31 ~ArcSessionRunner() override; |
| 43 | 32 |
| 44 // ArcBridgeService overrides: | 33 // Add/Remove an observer. |
| 45 void RequestStart() override; | 34 void AddObserver(ArcSessionObserver* observer); |
| 46 void RequestStop() override; | 35 void RemoveObserver(ArcSessionObserver* observer); |
| 47 void OnShutdown() override; | 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; |
| 48 | 54 |
| 49 // Returns the current ArcSession instance for testing purpose. | 55 // Returns the current ArcSession instance for testing purpose. |
| 50 ArcSession* GetArcSessionForTesting() { return arc_session_.get(); } | 56 ArcSession* GetArcSessionForTesting() { return arc_session_.get(); } |
| 51 | 57 |
| 52 // Normally, automatic restarting happens after a short delay. When testing, | 58 // Normally, automatic restarting happens after a short delay. When testing, |
| 53 // however, we'd like it to happen immediately to avoid adding unnecessary | 59 // however, we'd like it to happen immediately to avoid adding unnecessary |
| 54 // delays. | 60 // delays. |
| 55 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); | 61 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); |
| 56 | 62 |
| 57 private: | 63 private: |
| 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 |
| 58 // Starts to run an ARC instance. | 97 // Starts to run an ARC instance. |
| 59 void StartArcSession(); | 98 void StartArcSession(); |
| 60 | 99 |
| 61 // ArcSessionObserver: | 100 // ArcSessionObserver: |
| 62 void OnSessionReady() override; | 101 void OnSessionReady() override; |
| 63 void OnSessionStopped(StopReason reason) override; | 102 void OnSessionStopped(StopReason reason) override; |
| 64 | 103 |
| 104 base::ThreadChecker thread_checker_; |
| 105 |
| 106 // Observers for the ARC instance state change events. |
| 107 base::ObserverList<ArcSessionObserver> observer_list_; |
| 108 |
| 65 // Whether a client requests to run session or not. | 109 // Whether a client requests to run session or not. |
| 66 bool run_requested_ = false; | 110 bool run_requested_ = false; |
| 67 | 111 |
| 68 // Instead of immediately trying to restart the container, give it some time | 112 // Instead of immediately trying to restart the container, give it some time |
| 69 // to finish tearing down in case it is still in the process of stopping. | 113 // to finish tearing down in case it is still in the process of stopping. |
| 70 base::TimeDelta restart_delay_; | 114 base::TimeDelta restart_delay_; |
| 71 base::OneShotTimer restart_timer_; | 115 base::OneShotTimer restart_timer_; |
| 72 | 116 |
| 73 // Factory to inject a fake ArcSession instance for testing. | 117 // Factory to inject a fake ArcSession instance for testing. |
| 74 ArcSessionFactory factory_; | 118 ArcSessionFactory factory_; |
| 75 | 119 |
| 120 // Current runner's state. |
| 121 State state_ = State::STOPPED; |
| 122 |
| 76 // ArcSession object for currently running ARC instance. This should be | 123 // ArcSession object for currently running ARC instance. This should be |
| 77 // nullptr if the state is STOPPED, otherwise non-nullptr. | 124 // nullptr if the state is STOPPED, otherwise non-nullptr. |
| 78 std::unique_ptr<ArcSession> arc_session_; | 125 std::unique_ptr<ArcSession> arc_session_; |
| 79 | 126 |
| 80 // WeakPtrFactory to use callbacks. | 127 // WeakPtrFactory to use callbacks. |
| 81 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; | 128 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; |
| 82 | 129 |
| 83 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); | 130 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); |
| 84 }; | 131 }; |
| 85 | 132 |
| 86 } // namespace arc | 133 } // namespace arc |
| 87 | 134 |
| 88 #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ | 135 #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| OLD | NEW |