Chromium Code Reviews| 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_BRIDGE_SERVICE_IMPL_H_ | 5 #ifndef COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_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> | 17 template <typename T> |
| 18 class scoped_refptr; | 18 class scoped_refptr; |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class TaskRunner; | 21 class TaskRunner; |
| 22 } // namespace base | 22 } // namespace base |
| 23 | 23 |
| 24 namespace arc { | 24 namespace arc { |
| 25 | 25 |
| 26 class ArcSession; | 26 class ArcSession; |
| 27 | 27 |
| 28 // Real IPC based ArcBridgeService that is used in production. | 28 // Accept requests to start/stop ARC instance. Also supports automatic |
| 29 class ArcBridgeServiceImpl : public ArcBridgeService, | 29 // restarting on unexpected ARC instance crash. |
| 30 public ArcSessionObserver { | 30 // TODO(hidehiko): Get rid of ArcBridgeService inheritance. |
| 31 class ArcSessionRunner : public ArcBridgeService, public ArcSessionObserver { | |
| 31 public: | 32 public: |
| 32 // This is the factory interface to inject ArcSession instance | 33 // This is the factory interface to inject ArcSession instance |
| 33 // for testing purpose. | 34 // for testing purpose. |
| 34 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; | 35 using ArcSessionFactory = base::Callback<std::unique_ptr<ArcSession>()>; |
| 35 | 36 |
| 36 explicit ArcBridgeServiceImpl( | 37 explicit ArcSessionRunner(scoped_refptr<base::TaskRunner> task_runner); |
| 37 const scoped_refptr<base::TaskRunner>& blocking_task_runner); | 38 |
| 38 ~ArcBridgeServiceImpl() override; | 39 // For testing. |
| 40 // TODO(hidehiko): Migrate this and above constructors. | |
| 41 explicit ArcSessionRunner(const ArcSessionFactory& factory); | |
| 42 ~ArcSessionRunner() override; | |
| 39 | 43 |
| 40 // ArcBridgeService overrides: | 44 // ArcBridgeService overrides: |
| 41 void RequestStart() override; | 45 void RequestStart() override; |
| 42 void RequestStop() override; | 46 void RequestStop() override; |
| 43 void OnShutdown() override; | 47 void OnShutdown() override; |
| 44 | 48 |
| 45 // Inject a factory to create ArcSession instance for testing purpose. | |
| 46 // |factory| must not be null. | |
| 47 void SetArcSessionFactoryForTesting(const ArcSessionFactory& factory); | |
| 48 | |
| 49 // Returns the current ArcSession instance for testing purpose. | 49 // Returns the current ArcSession instance for testing purpose. |
| 50 ArcSession* GetArcSessionForTesting() { return arc_session_.get(); } | 50 ArcSession* GetArcSessionForTesting() { return arc_session_.get(); } |
| 51 | 51 |
| 52 // Normally, automatic restarting happens after a short delay. When testing, | 52 // Normally, automatic restarting happens after a short delay. When testing, |
| 53 // however, we'd like it to happen immediately to avoid adding unnecessary | 53 // however, we'd like to happen immediately to avoid adding unnecessary |
|
Luis Héctor Chávez
2016/12/16 23:31:00
why did you remove this? In this context, "it" ==
hidehiko
2016/12/19 08:27:03
Oh, I misinterprested the sentence. Reverted. Than
| |
| 54 // delays. | 54 // delays. |
| 55 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); | 55 void SetRestartDelayForTesting(const base::TimeDelta& restart_delay); |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 // Starts to run an ARC instance. | 58 // Starts to run an ARC instance. |
| 59 void StartArcSession(); | 59 void StartArcSession(); |
| 60 | 60 |
| 61 // Stops the running instance. | |
| 62 void StopInstance(); | |
| 63 | |
| 64 // ArcSessionObserver: | 61 // ArcSessionObserver: |
| 65 void OnSessionReady() override; | 62 void OnSessionReady() override; |
| 66 void OnSessionStopped(StopReason reason) override; | 63 void OnSessionStopped(StopReason reason) override; |
| 67 | 64 |
| 68 // Whether a client requests to run session or not. | 65 // Whether a client requests to run session or not. |
| 69 bool running_ = false; | 66 bool running_ = false; |
| 70 | 67 |
| 71 // Instead of immediately trying to restart the container, give it some time | 68 // Instead of immediately trying to restart the container, give it some time |
| 72 // to finish tearing down in case it is still in the process of stopping. | 69 // to finish tearing down in case it is still in the process of stopping. |
| 73 base::TimeDelta restart_delay_; | 70 base::TimeDelta restart_delay_; |
| 74 base::OneShotTimer restart_timer_; | 71 base::OneShotTimer restart_timer_; |
| 75 | 72 |
| 76 // Factory to inject a fake ArcSession instance for testing. | 73 // Factory to inject a fake ArcSession instance for testing. |
| 77 ArcSessionFactory factory_; | 74 ArcSessionFactory factory_; |
| 78 | 75 |
| 79 // ArcSession object for currently running ARC instance. This should be | 76 // ArcSession object for currently running ARC instance. This should be |
| 80 // nullptr if the state is STOPPED, otherwise non-nullptr. | 77 // nullptr if the state is STOPPED, otherwise non-nullptr. |
| 81 std::unique_ptr<ArcSession> arc_session_; | 78 std::unique_ptr<ArcSession> arc_session_; |
| 82 | 79 |
| 83 // WeakPtrFactory to use callbacks. | 80 // WeakPtrFactory to use callbacks. |
| 84 base::WeakPtrFactory<ArcBridgeServiceImpl> weak_ptr_factory_; | 81 base::WeakPtrFactory<ArcSessionRunner> weak_ptr_factory_; |
| 85 | 82 |
| 86 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceImpl); | 83 DISALLOW_COPY_AND_ASSIGN(ArcSessionRunner); |
| 87 }; | 84 }; |
| 88 | 85 |
| 89 } // namespace arc | 86 } // namespace arc |
| 90 | 87 |
| 91 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_IMPL_H_ | 88 #endif // COMPONENTS_ARC_ARC_SESSION_RUNNER_H_ |
| OLD | NEW |