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_H_ | 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "components/arc/common/arc_bridge.mojom.h" | 16 #include "components/arc/common/arc_bridge.mojom.h" |
17 #include "components/arc/instance_holder.h" | 17 #include "components/arc/instance_holder.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class CommandLine; | 20 class CommandLine; |
21 } // namespace base | 21 } // namespace base |
22 | 22 |
23 namespace arc { | 23 namespace arc { |
24 | 24 |
25 class ArcBridgeTest; | 25 class ArcBridgeTest; |
26 | 26 |
27 // The Chrome-side service that handles ARC instances and ARC bridge creation. | 27 // The Chrome-side service that handles ARC instances and ARC bridge creation. |
28 // This service handles the lifetime of ARC instances and sets up the | 28 // This service handles the lifetime of ARC instances and sets up the |
29 // communication channel (the ARC bridge) used to send and receive messages. | 29 // communication channel (the ARC bridge) used to send and receive messages. |
30 class ArcBridgeService : public mojom::ArcBridgeHost { | 30 class ArcBridgeService { |
31 public: | 31 public: |
32 // Describes the reason the bridge is stopped. | 32 // Describes the reason the bridge is stopped. |
33 enum class StopReason { | 33 enum class StopReason { |
34 // ARC instance has been gracefully shut down. | 34 // ARC instance has been gracefully shut down. |
35 SHUTDOWN, | 35 SHUTDOWN, |
36 | 36 |
37 // Errors occurred during the ARC instance boot. This includes any failures | 37 // Errors occurred during the ARC instance boot. This includes any failures |
38 // before the instance is actually attempted to be started, and also | 38 // before the instance is actually attempted to be started, and also |
39 // failures on bootstrapping IPC channels with Android. | 39 // failures on bootstrapping IPC channels with Android. |
40 GENERIC_BOOT_FAILURE, | 40 GENERIC_BOOT_FAILURE, |
41 | 41 |
42 // ARC instance has crashed. | 42 // ARC instance has crashed. |
43 CRASH, | 43 CRASH, |
44 }; | 44 }; |
45 | 45 |
46 // Notifies life cycle events of ArcBridgeService. | 46 // Notifies life cycle events of ArcBridgeService. |
47 class Observer { | 47 class Observer { |
48 public: | 48 public: |
49 // Called whenever the state of the bridge has changed. | 49 // Called whenever the state of the bridge has changed. |
50 virtual void OnBridgeReady() {} | 50 virtual void OnBridgeReady() {} |
51 virtual void OnBridgeStopped(StopReason reason) {} | 51 virtual void OnBridgeStopped(StopReason reason) {} |
52 | 52 |
53 // Called whenever ARC's availability has changed for this system. | 53 // Called whenever ARC's availability has changed for this system. |
54 virtual void OnAvailableChanged(bool available) {} | 54 virtual void OnAvailableChanged(bool available) {} |
55 | 55 |
56 protected: | 56 protected: |
57 virtual ~Observer() {} | 57 virtual ~Observer() {} |
58 }; | 58 }; |
59 | 59 |
60 ~ArcBridgeService() override; | 60 virtual ~ArcBridgeService(); |
61 | 61 |
62 // Gets the global instance of the ARC Bridge Service. This can only be | 62 // Gets the global instance of the ARC Bridge Service. This can only be |
63 // called on the thread that this class was created on. | 63 // called on the thread that this class was created on. |
64 static ArcBridgeService* Get(); | 64 static ArcBridgeService* Get(); |
65 | 65 |
66 // Return true if ARC has been enabled through a commandline | 66 // Return true if ARC has been enabled through a commandline |
67 // switch. | 67 // switch. |
68 static bool GetEnabled(const base::CommandLine* command_line); | 68 static bool GetEnabled(const base::CommandLine* command_line); |
69 | 69 |
70 // SetDetectedAvailability() should be called once CheckArcAvailability() on | 70 // SetDetectedAvailability() should be called once CheckArcAvailability() on |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 InstanceHolder<mojom::PowerInstance>* power() { return &power_; } | 114 InstanceHolder<mojom::PowerInstance>* power() { return &power_; } |
115 InstanceHolder<mojom::ProcessInstance>* process() { return &process_; } | 115 InstanceHolder<mojom::ProcessInstance>* process() { return &process_; } |
116 InstanceHolder<mojom::StorageManagerInstance>* storage_manager() { | 116 InstanceHolder<mojom::StorageManagerInstance>* storage_manager() { |
117 return &storage_manager_; | 117 return &storage_manager_; |
118 } | 118 } |
119 InstanceHolder<mojom::VideoInstance>* video() { return &video_; } | 119 InstanceHolder<mojom::VideoInstance>* video() { return &video_; } |
120 InstanceHolder<mojom::WindowManagerInstance>* window_manager() { | 120 InstanceHolder<mojom::WindowManagerInstance>* window_manager() { |
121 return &window_manager_; | 121 return &window_manager_; |
122 } | 122 } |
123 | 123 |
124 // ArcHost: | |
125 void OnAppInstanceReady(mojom::AppInstancePtr app_ptr) override; | |
126 void OnAudioInstanceReady(mojom::AudioInstancePtr audio_ptr) override; | |
127 void OnAuthInstanceReady(mojom::AuthInstancePtr auth_ptr) override; | |
128 void OnBluetoothInstanceReady( | |
129 mojom::BluetoothInstancePtr bluetooth_ptr) override; | |
130 void OnClipboardInstanceReady( | |
131 mojom::ClipboardInstancePtr clipboard_ptr) override; | |
132 void OnCrashCollectorInstanceReady( | |
133 mojom::CrashCollectorInstancePtr crash_collector_ptr) override; | |
134 void OnFileSystemInstanceReady( | |
135 mojom::FileSystemInstancePtr file_system_ptr) override; | |
136 void OnImeInstanceReady(mojom::ImeInstancePtr ime_ptr) override; | |
137 void OnIntentHelperInstanceReady( | |
138 mojom::IntentHelperInstancePtr intent_helper_ptr) override; | |
139 void OnMetricsInstanceReady(mojom::MetricsInstancePtr metrics_ptr) override; | |
140 void OnNetInstanceReady(mojom::NetInstancePtr net_ptr) override; | |
141 void OnNotificationsInstanceReady( | |
142 mojom::NotificationsInstancePtr notifications_ptr) override; | |
143 void OnObbMounterInstanceReady( | |
144 mojom::ObbMounterInstancePtr obb_mounter_ptr) override; | |
145 void OnPolicyInstanceReady(mojom::PolicyInstancePtr policy_ptr) override; | |
146 void OnPowerInstanceReady(mojom::PowerInstancePtr power_ptr) override; | |
147 void OnProcessInstanceReady(mojom::ProcessInstancePtr process_ptr) override; | |
148 void OnStorageManagerInstanceReady( | |
149 mojom::StorageManagerInstancePtr storage_manager_ptr) override; | |
150 void OnVideoInstanceReady(mojom::VideoInstancePtr video_ptr) override; | |
151 void OnWindowManagerInstanceReady( | |
152 mojom::WindowManagerInstancePtr window_manager_ptr) override; | |
153 | |
154 // Gets if ARC is available in this system. | 124 // Gets if ARC is available in this system. |
155 bool available() const { return available_; } | 125 bool available() const { return available_; } |
156 | 126 |
157 // Gets if ARC is currently running. | 127 // Gets if ARC is currently running. |
158 bool ready() const { return state() == State::READY; } | 128 bool ready() const { return state() == State::READY; } |
159 | 129 |
160 // Gets if ARC is currently stopped. This is not exactly !ready() since there | 130 // Gets if ARC is currently stopped. This is not exactly !ready() since there |
161 // are transient states between ready() and stopped(). | 131 // are transient states between ready() and stopped(). |
162 bool stopped() const { return state() == State::STOPPED; } | 132 bool stopped() const { return state() == State::STOPPED; } |
163 | 133 |
(...skipping 28 matching lines...) Expand all Loading... |
192 // The ARC instance has finished initializing and is now ready for user | 162 // The ARC instance has finished initializing and is now ready for user |
193 // interaction. | 163 // interaction. |
194 READY, | 164 READY, |
195 | 165 |
196 // The ARC instance has started shutting down. | 166 // The ARC instance has started shutting down. |
197 STOPPING, | 167 STOPPING, |
198 }; | 168 }; |
199 | 169 |
200 ArcBridgeService(); | 170 ArcBridgeService(); |
201 | 171 |
| 172 // Instance holders. |
| 173 InstanceHolder<mojom::AppInstance> app_; |
| 174 InstanceHolder<mojom::AudioInstance> audio_; |
| 175 InstanceHolder<mojom::AuthInstance> auth_; |
| 176 InstanceHolder<mojom::BluetoothInstance> bluetooth_; |
| 177 InstanceHolder<mojom::ClipboardInstance> clipboard_; |
| 178 InstanceHolder<mojom::CrashCollectorInstance> crash_collector_; |
| 179 InstanceHolder<mojom::FileSystemInstance> file_system_; |
| 180 InstanceHolder<mojom::ImeInstance> ime_; |
| 181 InstanceHolder<mojom::IntentHelperInstance> intent_helper_; |
| 182 InstanceHolder<mojom::MetricsInstance> metrics_; |
| 183 InstanceHolder<mojom::NetInstance> net_; |
| 184 InstanceHolder<mojom::NotificationsInstance> notifications_; |
| 185 InstanceHolder<mojom::ObbMounterInstance> obb_mounter_; |
| 186 InstanceHolder<mojom::PolicyInstance> policy_; |
| 187 InstanceHolder<mojom::PowerInstance> power_; |
| 188 InstanceHolder<mojom::ProcessInstance> process_; |
| 189 InstanceHolder<mojom::StorageManagerInstance> storage_manager_; |
| 190 InstanceHolder<mojom::VideoInstance> video_; |
| 191 InstanceHolder<mojom::WindowManagerInstance> window_manager_; |
| 192 |
202 // Gets the current state of the bridge service. | 193 // Gets the current state of the bridge service. |
203 State state() const { return state_; } | 194 State state() const { return state_; } |
204 | 195 |
205 // Changes the current state and notifies all observers. | 196 // Changes the current state and notifies all observers. |
206 void SetState(State state); | 197 void SetState(State state); |
207 | 198 |
208 // Changes the current availability and notifies all observers. | 199 // Changes the current availability and notifies all observers. |
209 void SetAvailable(bool availability); | 200 void SetAvailable(bool availability); |
210 | 201 |
211 // Sets the reason the bridge is stopped. This function must be always called | 202 // Sets the reason the bridge is stopped. This function must be always called |
212 // before SetState(State::STOPPED) to report a correct reason with | 203 // before SetState(State::STOPPED) to report a correct reason with |
213 // Observer::OnBridgeStopped(). | 204 // Observer::OnBridgeStopped(). |
214 void SetStopReason(StopReason stop_reason); | 205 void SetStopReason(StopReason stop_reason); |
215 | 206 |
216 base::ObserverList<Observer>& observer_list() { return observer_list_; } | 207 base::ObserverList<Observer>& observer_list() { return observer_list_; } |
217 | 208 |
218 bool CalledOnValidThread(); | 209 bool CalledOnValidThread(); |
219 | 210 |
220 // Closes all Mojo channels. | |
221 void CloseAllChannels(); | |
222 | |
223 private: | 211 private: |
224 friend class ArcBridgeTest; | 212 friend class ArcBridgeTest; |
225 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | 213 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
226 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); | 214 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); |
227 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | 215 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); |
228 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Restart); | 216 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Restart); |
229 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, OnBridgeStopped); | 217 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, OnBridgeStopped); |
230 | 218 |
231 // Instance holders. | |
232 InstanceHolder<mojom::AppInstance> app_; | |
233 InstanceHolder<mojom::AudioInstance> audio_; | |
234 InstanceHolder<mojom::AuthInstance> auth_; | |
235 InstanceHolder<mojom::BluetoothInstance> bluetooth_; | |
236 InstanceHolder<mojom::ClipboardInstance> clipboard_; | |
237 InstanceHolder<mojom::CrashCollectorInstance> crash_collector_; | |
238 InstanceHolder<mojom::FileSystemInstance> file_system_; | |
239 InstanceHolder<mojom::ImeInstance> ime_; | |
240 InstanceHolder<mojom::IntentHelperInstance> intent_helper_; | |
241 InstanceHolder<mojom::MetricsInstance> metrics_; | |
242 InstanceHolder<mojom::NetInstance> net_; | |
243 InstanceHolder<mojom::NotificationsInstance> notifications_; | |
244 InstanceHolder<mojom::ObbMounterInstance> obb_mounter_; | |
245 InstanceHolder<mojom::PolicyInstance> policy_; | |
246 InstanceHolder<mojom::PowerInstance> power_; | |
247 InstanceHolder<mojom::ProcessInstance> process_; | |
248 InstanceHolder<mojom::StorageManagerInstance> storage_manager_; | |
249 InstanceHolder<mojom::VideoInstance> video_; | |
250 InstanceHolder<mojom::WindowManagerInstance> window_manager_; | |
251 | |
252 base::ObserverList<Observer> observer_list_; | 219 base::ObserverList<Observer> observer_list_; |
253 | 220 |
254 base::ThreadChecker thread_checker_; | 221 base::ThreadChecker thread_checker_; |
255 | 222 |
256 // If the ARC instance service is available. | 223 // If the ARC instance service is available. |
257 bool available_; | 224 bool available_; |
258 | 225 |
259 // The current state of the bridge. | 226 // The current state of the bridge. |
260 ArcBridgeService::State state_; | 227 ArcBridgeService::State state_; |
261 | 228 |
262 // The reason the bridge is stopped. | 229 // The reason the bridge is stopped. |
263 StopReason stop_reason_; | 230 StopReason stop_reason_; |
264 | 231 |
265 // WeakPtrFactory to use callbacks. | 232 // WeakPtrFactory to use callbacks. |
266 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | 233 base::WeakPtrFactory<ArcBridgeService> weak_factory_; |
267 | 234 |
268 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 235 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
269 }; | 236 }; |
270 | 237 |
271 } // namespace arc | 238 } // namespace arc |
272 | 239 |
273 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 240 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |