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_BOOTSTRAP_H_ | 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_BOOTSTRAP_H_ |
6 #define COMPONENTS_ARC_ARC_BRIDGE_BOOTSTRAP_H_ | 6 #define COMPONENTS_ARC_ARC_BRIDGE_BOOTSTRAP_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/observer_list.h" | |
11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
13 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_bridge_service.h" |
14 #include "components/arc/common/arc_bridge.mojom.h" | 15 #include "components/arc/common/arc_bridge.mojom.h" |
15 | 16 |
16 namespace arc { | 17 namespace arc { |
17 | 18 |
18 // Starts the ARC instance and bootstraps the bridge connection. | 19 // Starts the ARC instance and bootstraps the bridge connection. |
19 // Clients should implement the Delegate to be notified upon communications | 20 // Clients should implement the Delegate to be notified upon communications |
20 // being available. | 21 // being available. |
21 // The instance can be safely removed 1) before Start() is called, or 2) after | 22 // The instance can be safely removed 1) before Start() is called, or 2) after |
22 // OnStopped() is called. | 23 // OnStopped() is called. |
23 // The number of instances must be at most one. Otherwise, ARC instances will | 24 // The number of instances must be at most one. Otherwise, ARC instances will |
24 // conflict. | 25 // conflict. |
25 // TODO(hidehiko): This class manages more than "bootstrap" procedure now. | 26 // TODO(hidehiko): This class manages more than "bootstrap" procedure now. |
26 // Rename this to ArcSession. | 27 // Rename this to ArcSession. |
27 class ArcBridgeBootstrap { | 28 class ArcBridgeBootstrap { |
28 public: | 29 public: |
29 // TODO(hidehiko): Switch to Observer style, which fits more for this design. | 30 class Observer { |
30 class Delegate { | |
31 public: | 31 public: |
Luis Héctor Chávez
2016/10/03 18:05:32
hmmm looks like we (I?) missed adding a virtual de
hidehiko
2016/10/04 05:34:45
Good catch. Done.
| |
32 // Called when the connection with ARC instance has been established. | 32 // Called when the connection with ARC instance has been established. |
33 // TODO(hidehiko): Moving ArcBridgeHost to the ArcBridgeBootstrapImpl | 33 virtual void OnReady() = 0; |
34 // so that this can be replaced by OnReady() simply. | |
35 virtual void OnConnectionEstablished( | |
36 mojom::ArcBridgeInstancePtr instance_ptr) = 0; | |
37 | 34 |
38 // Called when ARC instance is stopped. This is called exactly once | 35 // Called when ARC instance is stopped. This is called exactly once |
39 // per instance which is Start()ed. | 36 // per instance which is Start()ed. |
40 virtual void OnStopped(ArcBridgeService::StopReason reason) = 0; | 37 virtual void OnStopped(ArcBridgeService::StopReason reason) = 0; |
41 }; | 38 }; |
Luis Héctor Chávez
2016/10/03 18:05:32
nit: DISALLOW_COPY_AND_ASSIGN(Observer); ?
hidehiko
2016/10/04 05:34:45
Done.
| |
42 | 39 |
43 // Creates a default instance of ArcBridgeBootstrap. | 40 // Creates a default instance of ArcBridgeBootstrap. |
44 static std::unique_ptr<ArcBridgeBootstrap> Create(); | 41 static std::unique_ptr<ArcBridgeBootstrap> Create(); |
45 virtual ~ArcBridgeBootstrap() = default; | 42 virtual ~ArcBridgeBootstrap(); |
46 | |
47 // This must be called before calling Start() or Stop(). |delegate| is owned | |
48 // by the caller and must outlive this instance. | |
49 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | |
50 | 43 |
51 // Starts and bootstraps a connection with the instance. The Delegate's | 44 // Starts and bootstraps a connection with the instance. The Delegate's |
52 // OnConnectionEstablished() will be called if the bootstrapping is | 45 // OnConnectionEstablished() will be called if the bootstrapping is |
53 // successful, or OnStopped() if it is not. | 46 // successful, or OnStopped() if it is not. |
54 // Start() should not be called twice or more. | 47 // Start() should not be called twice or more. |
55 virtual void Start() = 0; | 48 virtual void Start() = 0; |
56 | 49 |
57 // Requests to stop the currently-running instance. | 50 // Requests to stop the currently-running instance. |
58 // The completion is notified via OnStopped() of the Delegate. | 51 // The completion is notified via OnStopped() of the Delegate. |
59 virtual void Stop() = 0; | 52 virtual void Stop() = 0; |
60 | 53 |
54 void AddObserver(Observer* observer); | |
55 void RemoveObserver(Observer* observer); | |
56 | |
61 protected: | 57 protected: |
62 ArcBridgeBootstrap() = default; | 58 ArcBridgeBootstrap(); |
63 | 59 |
64 // Owned by the caller. | 60 base::ObserverList<Observer> observer_list_; |
65 Delegate* delegate_ = nullptr; | |
66 | 61 |
67 private: | 62 private: |
68 DISALLOW_COPY_AND_ASSIGN(ArcBridgeBootstrap); | 63 DISALLOW_COPY_AND_ASSIGN(ArcBridgeBootstrap); |
69 }; | 64 }; |
70 | 65 |
71 } // namespace arc | 66 } // namespace arc |
72 | 67 |
73 #endif // COMPONENTS_ARC_ARC_BRIDGE_BOOTSTRAP_H_ | 68 #endif // COMPONENTS_ARC_ARC_BRIDGE_BOOTSTRAP_H_ |
OLD | NEW |