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/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/arc/common/arc_bridge.mojom.h" | 17 #include "components/arc/common/arc_bridge.mojom.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 ArcBridgeBootstrap; | 25 class ArcBridgeBootstrap; |
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 { | 30 class ArcBridgeService : public ArcBridgeHost { |
31 public: | 31 public: |
32 // The possible states of the bridge. In the normal flow, the state changes | 32 // The possible states of the bridge. In the normal flow, the state changes |
33 // in the following sequence: | 33 // in the following sequence: |
34 // | 34 // |
35 // STOPPED | 35 // STOPPED |
36 // PrerequisitesChanged() -> | 36 // PrerequisitesChanged() -> |
37 // CONNECTING | 37 // CONNECTING |
38 // OnConnectionEstablished() -> | 38 // OnConnectionEstablished() -> |
39 // CONNECTED | |
40 // OnInstanceBootPhase(INSTANCE_BOOT_PHASE_BRIDGE_READY) -> | |
41 // READY | 39 // READY |
42 // | 40 // |
43 // The ArcBridgeBootstrap state machine can be thought of being substates of | 41 // The ArcBridgeBootstrap state machine can be thought of being substates of |
44 // ArcBridgeService's CONNECTING state. | 42 // ArcBridgeService's CONNECTING state. |
45 // | 43 // |
46 // * | 44 // * |
47 // StopInstance() -> | 45 // StopInstance() -> |
48 // STOPPING | 46 // STOPPING |
49 // OnStopped() -> | 47 // OnStopped() -> |
50 // STOPPED | 48 // STOPPED |
(...skipping 14 matching lines...) Expand all Loading... |
65 // The ARC instance has started shutting down. | 63 // The ARC instance has started shutting down. |
66 STOPPING, | 64 STOPPING, |
67 }; | 65 }; |
68 | 66 |
69 // Notifies life cycle events of ArcBridgeService. | 67 // Notifies life cycle events of ArcBridgeService. |
70 class Observer { | 68 class Observer { |
71 public: | 69 public: |
72 // Called whenever the state of the bridge has changed. | 70 // Called whenever the state of the bridge has changed. |
73 virtual void OnStateChanged(State state) {} | 71 virtual void OnStateChanged(State state) {} |
74 | 72 |
75 // Called when the instance has reached a boot phase | |
76 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} | |
77 | |
78 // Called whenever ARC's availability has changed for this system. | 73 // Called whenever ARC's availability has changed for this system. |
79 virtual void OnAvailableChanged(bool available) {} | 74 virtual void OnAvailableChanged(bool available) {} |
80 | 75 |
| 76 // Called whenever the ARC app list is ready. |
| 77 virtual void OnAppInstanceReady() {} |
| 78 |
| 79 // Called whenever the ARC input is ready. |
| 80 virtual void OnInputInstanceReady() {} |
| 81 |
| 82 // Called whenever the ARC notification is ready. |
| 83 virtual void OnNotificationsInstanceReady() {} |
| 84 |
| 85 // Called whenever the ARC power is ready. |
| 86 virtual void OnPowerInstanceReady() {} |
| 87 |
| 88 // Called whenever the ARC process is ready. |
| 89 virtual void OnProcessInstanceReady() {} |
| 90 |
| 91 // Called whenever the ARC settings is ready. |
| 92 virtual void OnSettingsInstanceReady() {} |
| 93 |
81 protected: | 94 protected: |
82 virtual ~Observer() {} | 95 virtual ~Observer() {} |
83 }; | 96 }; |
84 | 97 |
85 class NotificationObserver { | 98 ~ArcBridgeService() override; |
86 public: | |
87 // Called whenever a notification has been posted on Android side. This | |
88 // event is used for both creation and update. | |
89 virtual void OnNotificationPostedFromAndroid( | |
90 const ArcNotificationData& data) {} | |
91 // Called whenever a notification has been removed on Android side. | |
92 virtual void OnNotificationRemovedFromAndroid(const std::string& key) {} | |
93 | |
94 protected: | |
95 virtual ~NotificationObserver() {} | |
96 }; | |
97 | |
98 // Notifies ARC apps related events. | |
99 class AppObserver { | |
100 public: | |
101 // Called whenever ARC sends information about available apps. | |
102 virtual void OnAppListRefreshed(const std::vector<AppInfo>& apps) {} | |
103 | |
104 // Called whenever ARC sends app icon data for specific scale factor. | |
105 virtual void OnAppIcon(const std::string& package, | |
106 const std::string& activity, | |
107 ScaleFactor scale_factor, | |
108 const std::vector<uint8_t>& icon_png_data) {} | |
109 | |
110 protected: | |
111 virtual ~AppObserver() {} | |
112 }; | |
113 | |
114 // Notifies ARC process related events. | |
115 class ProcessObserver { | |
116 public: | |
117 // Called when the latest process list has been received after | |
118 // ArcBridgeService::RequestProcessList() was called. | |
119 // | |
120 // NB: Due to the nature of Linux PID, we can not avoid the race condition | |
121 // that the process info is already outdated when the message is received. | |
122 // Do not use the process list obtained here for security-sensitive purpose. | |
123 // Good news is that Android processes are designed to be ready to be killed | |
124 // at any time, so killing a wrong process is not a disaster. | |
125 virtual void OnUpdateProcessList( | |
126 const std::vector<RunningAppProcessInfo>& processes) {} | |
127 | |
128 protected: | |
129 virtual ~ProcessObserver() {} | |
130 }; | |
131 | |
132 virtual ~ArcBridgeService(); | |
133 | 99 |
134 // Gets the global instance of the ARC Bridge Service. This can only be | 100 // Gets the global instance of the ARC Bridge Service. This can only be |
135 // called on the thread that this class was created on. | 101 // called on the thread that this class was created on. |
136 static ArcBridgeService* Get(); | 102 static ArcBridgeService* Get(); |
137 | 103 |
138 // Return true if ARC has been enabled through a commandline | 104 // Return true if ARC has been enabled through a commandline |
139 // switch. | 105 // switch. |
140 static bool GetEnabled(const base::CommandLine* command_line); | 106 static bool GetEnabled(const base::CommandLine* command_line); |
141 | 107 |
142 // DetectAvailability() should be called once D-Bus is available. It will | 108 // DetectAvailability() should be called once D-Bus is available. It will |
143 // call CheckArcAvailability() on the session_manager. This can only be | 109 // call CheckArcAvailability() on the session_manager. This can only be |
144 // called on the thread that this class was created on. | 110 // called on the thread that this class was created on. |
145 virtual void DetectAvailability() = 0; | 111 virtual void DetectAvailability() = 0; |
146 | 112 |
147 // HandleStartup() should be called upon profile startup. This will only | 113 // HandleStartup() should be called upon profile startup. This will only |
148 // launch an instance if the instance service is available and it is enabled. | 114 // launch an instance if the instance service is available and it is enabled. |
149 // This can only be called on the thread that this class was created on. | 115 // This can only be called on the thread that this class was created on. |
150 virtual void HandleStartup() = 0; | 116 virtual void HandleStartup() = 0; |
151 | 117 |
152 // Shutdown() should be called when the browser is shutting down. This can | 118 // Shutdown() should be called when the browser is shutting down. This can |
153 // only be called on the thread that this class was created on. | 119 // only be called on the thread that this class was created on. |
154 virtual void Shutdown() = 0; | 120 virtual void Shutdown() = 0; |
155 | 121 |
156 // Adds or removes observers. This can only be called on the thread that this | 122 // Adds or removes observers. This can only be called on the thread that this |
157 // class was created on. | 123 // class was created on. |
158 void AddObserver(Observer* observer); | 124 void AddObserver(Observer* observer); |
159 void RemoveObserver(Observer* observer); | 125 void RemoveObserver(Observer* observer); |
160 void AddNotificationObserver(NotificationObserver* observer); | |
161 void RemoveNotificationObserver(NotificationObserver* observer); | |
162 | 126 |
163 // Adds or removes ARC app observers. This can only be called on the thread | 127 // Gets the Mojo interface for all the instance services. This will return |
164 // that this class was created on. | 128 // nullptr if that particular service is not ready yet. Use an Observer if |
165 void AddAppObserver(AppObserver* observer); | 129 // you want to be notified when this is ready. This can only be called on the |
166 void RemoveAppObserver(AppObserver* observer); | 130 // thread that this class was created on. |
| 131 AppInstance* app_instance() { return app_ptr_.get(); } |
| 132 InputInstance* input_instance() { return input_ptr_.get(); } |
| 133 NotificationsInstance* notifications_instance() { |
| 134 return notifications_ptr_.get(); |
| 135 } |
| 136 PowerInstance* power_instance() { return power_ptr_.get(); } |
| 137 ProcessInstance* process_instance() { return process_ptr_.get(); } |
| 138 SettingsInstance* settings_instance() { return settings_ptr_.get(); } |
167 | 139 |
168 // Adds or removes ARC process observers. This can only be called on the | 140 // ArcHost: |
169 // thread that this class was created on. | 141 void OnAppInstanceReady(AppInstancePtr app_ptr) override; |
170 void AddProcessObserver(ProcessObserver* observer); | 142 void OnInputInstanceReady(InputInstancePtr input_ptr) override; |
171 void RemoveProcessObserver(ProcessObserver* observer); | 143 void OnNotificationsInstanceReady( |
| 144 NotificationsInstancePtr notifications_ptr) override; |
| 145 void OnPowerInstanceReady(PowerInstancePtr power_ptr) override; |
| 146 void OnProcessInstanceReady(ProcessInstancePtr process_ptr) override; |
| 147 void OnSettingsInstanceReady(SettingsInstancePtr process_ptr) override; |
172 | 148 |
173 // Gets the current state of the bridge service. | 149 // Gets the current state of the bridge service. |
174 State state() const { return state_; } | 150 State state() const { return state_; } |
175 | 151 |
176 // Gets if ARC is available in this system. | 152 // Gets if ARC is available in this system. |
177 bool available() const { return available_; } | 153 bool available() const { return available_; } |
178 | 154 |
179 // Requests registration of an input device on the ARC instance. | |
180 // TODO(denniskempin): Make this interface more typesafe. | |
181 // |name| should be the displayable name of the emulated device (e.g. "Chrome | |
182 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") | |
183 // and |fd| a file descriptor that emulates the kernel events of the device. | |
184 // This can only be called on the thread that this class was created on. | |
185 virtual bool RegisterInputDevice(const std::string& name, | |
186 const std::string& device_type, | |
187 base::ScopedFD fd) = 0; | |
188 | |
189 // Sends a notification event to Android side. | |
190 virtual bool SendNotificationEventToAndroid(const std::string& key, | |
191 ArcNotificationEvent event) = 0; | |
192 | |
193 // Requests to refresh an app list. | |
194 virtual bool RefreshAppList() = 0; | |
195 | |
196 // Requests to launch an app. | |
197 virtual bool LaunchApp(const std::string& package, | |
198 const std::string& activity) = 0; | |
199 | |
200 // Requests to load an icon of specific scale_factor. | |
201 virtual bool RequestAppIcon(const std::string& package, | |
202 const std::string& activity, | |
203 ScaleFactor scale_factor) = 0; | |
204 | |
205 // Requests a list of processes from the ARC instance. | |
206 // When the result comes back, Observer::OnUpdateProcessList() is called. | |
207 virtual bool RequestProcessList() = 0; | |
208 | |
209 // Send an Android broadcast message to the Android package and class | |
210 // specified. The 'extras' DictionaryValue will be converted to an Android | |
211 // Bundle accessible by the broadcast receiver. | |
212 // | |
213 // Note: Broadcasts can only be sent to whitelisted packages. Packages can be | |
214 // added to the whitelist in ArcBridgeService.java in the Android source. | |
215 virtual bool SendBroadcast(const std::string& action, | |
216 const std::string& package, | |
217 const std::string& clazz, | |
218 const base::DictionaryValue& extras) = 0; | |
219 | |
220 protected: | 155 protected: |
221 ArcBridgeService(); | 156 ArcBridgeService(); |
222 | 157 |
223 // Changes the current state and notifies all observers. | 158 // Changes the current state and notifies all observers. |
224 void SetState(State state); | 159 void SetState(State state); |
225 | 160 |
226 // Changes the current availability and notifies all observers. | 161 // Changes the current availability and notifies all observers. |
227 void SetAvailable(bool availability); | 162 void SetAvailable(bool availability); |
228 | 163 |
229 base::ObserverList<Observer>& observer_list() { return observer_list_; } | 164 base::ObserverList<Observer>& observer_list() { return observer_list_; } |
230 | 165 |
231 base::ObserverList<NotificationObserver>& notification_observer_list() { | |
232 return notification_observer_list_; | |
233 } | |
234 | |
235 base::ObserverList<AppObserver>& app_observer_list() { | |
236 return app_observer_list_; | |
237 } | |
238 | |
239 base::ObserverList<ProcessObserver>& process_observer_list() { | |
240 return process_observer_list_; | |
241 } | |
242 | |
243 bool CalledOnValidThread(); | 166 bool CalledOnValidThread(); |
244 | 167 |
245 private: | 168 private: |
246 friend class ArcBridgeTest; | 169 friend class ArcBridgeTest; |
247 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | 170 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
248 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); | 171 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); |
249 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | 172 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); |
250 | 173 |
| 174 // Mojo interfaces. |
| 175 AppInstancePtr app_ptr_; |
| 176 InputInstancePtr input_ptr_; |
| 177 NotificationsInstancePtr notifications_ptr_; |
| 178 PowerInstancePtr power_ptr_; |
| 179 ProcessInstancePtr process_ptr_; |
| 180 SettingsInstancePtr settings_ptr_; |
| 181 |
251 base::ObserverList<Observer> observer_list_; | 182 base::ObserverList<Observer> observer_list_; |
252 base::ObserverList<NotificationObserver> notification_observer_list_; | |
253 base::ObserverList<AppObserver> app_observer_list_; | |
254 base::ObserverList<ProcessObserver> process_observer_list_; | |
255 | 183 |
256 base::ThreadChecker thread_checker_; | 184 base::ThreadChecker thread_checker_; |
257 | 185 |
258 // If the ARC instance service is available. | 186 // If the ARC instance service is available. |
259 bool available_; | 187 bool available_; |
260 | 188 |
261 // The current state of the bridge. | 189 // The current state of the bridge. |
262 ArcBridgeService::State state_; | 190 ArcBridgeService::State state_; |
263 | 191 |
264 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 192 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
265 }; | 193 }; |
266 | 194 |
267 } // namespace arc | 195 } // namespace arc |
268 | 196 |
269 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 197 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |