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 : public ArcBridgeHost { | 30 class ArcBridgeService { |
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) -> |
39 // READY | 41 // READY |
40 // | 42 // |
41 // The ArcBridgeBootstrap state machine can be thought of being substates of | 43 // The ArcBridgeBootstrap state machine can be thought of being substates of |
42 // ArcBridgeService's CONNECTING state. | 44 // ArcBridgeService's CONNECTING state. |
43 // | 45 // |
44 // * | 46 // * |
45 // StopInstance() -> | 47 // StopInstance() -> |
46 // STOPPING | 48 // STOPPING |
47 // OnStopped() -> | 49 // OnStopped() -> |
48 // STOPPED | 50 // STOPPED |
(...skipping 14 matching lines...) Expand all Loading... |
63 // The ARC instance has started shutting down. | 65 // The ARC instance has started shutting down. |
64 STOPPING, | 66 STOPPING, |
65 }; | 67 }; |
66 | 68 |
67 // Notifies life cycle events of ArcBridgeService. | 69 // Notifies life cycle events of ArcBridgeService. |
68 class Observer { | 70 class Observer { |
69 public: | 71 public: |
70 // Called whenever the state of the bridge has changed. | 72 // Called whenever the state of the bridge has changed. |
71 virtual void OnStateChanged(State state) {} | 73 virtual void OnStateChanged(State state) {} |
72 | 74 |
| 75 // Called when the instance has reached a boot phase |
| 76 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} |
| 77 |
73 // Called whenever ARC's availability has changed for this system. | 78 // Called whenever ARC's availability has changed for this system. |
74 virtual void OnAvailableChanged(bool available) {} | 79 virtual void OnAvailableChanged(bool available) {} |
75 | 80 |
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 | |
94 protected: | 81 protected: |
95 virtual ~Observer() {} | 82 virtual ~Observer() {} |
96 }; | 83 }; |
97 | 84 |
98 ~ArcBridgeService() override; | 85 class NotificationObserver { |
| 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(); |
99 | 133 |
100 // Gets the global instance of the ARC Bridge Service. This can only be | 134 // Gets the global instance of the ARC Bridge Service. This can only be |
101 // called on the thread that this class was created on. | 135 // called on the thread that this class was created on. |
102 static ArcBridgeService* Get(); | 136 static ArcBridgeService* Get(); |
103 | 137 |
104 // Return true if ARC has been enabled through a commandline | 138 // Return true if ARC has been enabled through a commandline |
105 // switch. | 139 // switch. |
106 static bool GetEnabled(const base::CommandLine* command_line); | 140 static bool GetEnabled(const base::CommandLine* command_line); |
107 | 141 |
108 // DetectAvailability() should be called once D-Bus is available. It will | 142 // DetectAvailability() should be called once D-Bus is available. It will |
109 // call CheckArcAvailability() on the session_manager. This can only be | 143 // call CheckArcAvailability() on the session_manager. This can only be |
110 // called on the thread that this class was created on. | 144 // called on the thread that this class was created on. |
111 virtual void DetectAvailability() = 0; | 145 virtual void DetectAvailability() = 0; |
112 | 146 |
113 // HandleStartup() should be called upon profile startup. This will only | 147 // HandleStartup() should be called upon profile startup. This will only |
114 // launch an instance if the instance service is available and it is enabled. | 148 // launch an instance if the instance service is available and it is enabled. |
115 // This can only be called on the thread that this class was created on. | 149 // This can only be called on the thread that this class was created on. |
116 virtual void HandleStartup() = 0; | 150 virtual void HandleStartup() = 0; |
117 | 151 |
118 // Shutdown() should be called when the browser is shutting down. This can | 152 // Shutdown() should be called when the browser is shutting down. This can |
119 // only be called on the thread that this class was created on. | 153 // only be called on the thread that this class was created on. |
120 virtual void Shutdown() = 0; | 154 virtual void Shutdown() = 0; |
121 | 155 |
122 // Adds or removes observers. This can only be called on the thread that this | 156 // Adds or removes observers. This can only be called on the thread that this |
123 // class was created on. | 157 // class was created on. |
124 void AddObserver(Observer* observer); | 158 void AddObserver(Observer* observer); |
125 void RemoveObserver(Observer* observer); | 159 void RemoveObserver(Observer* observer); |
| 160 void AddNotificationObserver(NotificationObserver* observer); |
| 161 void RemoveNotificationObserver(NotificationObserver* observer); |
126 | 162 |
127 // Gets the Mojo interface for all the instance services. This will return | 163 // Adds or removes ARC app observers. This can only be called on the thread |
128 // nullptr if that particular service is not ready yet. Use an Observer if | 164 // that this class was created on. |
129 // you want to be notified when this is ready. This can only be called on the | 165 void AddAppObserver(AppObserver* observer); |
| 166 void RemoveAppObserver(AppObserver* observer); |
| 167 |
| 168 // Adds or removes ARC process observers. This can only be called on the |
130 // thread that this class was created on. | 169 // thread that this class was created on. |
131 AppInstance* app_instance() { return app_ptr_.get(); } | 170 void AddProcessObserver(ProcessObserver* observer); |
132 InputInstance* input_instance() { return input_ptr_.get(); } | 171 void RemoveProcessObserver(ProcessObserver* observer); |
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(); } | |
139 | |
140 // ArcHost: | |
141 void OnAppInstanceReady(AppInstancePtr app_ptr) override; | |
142 void OnInputInstanceReady(InputInstancePtr input_ptr) override; | |
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; | |
148 | 172 |
149 // Gets the current state of the bridge service. | 173 // Gets the current state of the bridge service. |
150 State state() const { return state_; } | 174 State state() const { return state_; } |
151 | 175 |
152 // Gets if ARC is available in this system. | 176 // Gets if ARC is available in this system. |
153 bool available() const { return available_; } | 177 bool available() const { return available_; } |
154 | 178 |
| 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 |
155 protected: | 220 protected: |
156 ArcBridgeService(); | 221 ArcBridgeService(); |
157 | 222 |
158 // Changes the current state and notifies all observers. | 223 // Changes the current state and notifies all observers. |
159 void SetState(State state); | 224 void SetState(State state); |
160 | 225 |
161 // Changes the current availability and notifies all observers. | 226 // Changes the current availability and notifies all observers. |
162 void SetAvailable(bool availability); | 227 void SetAvailable(bool availability); |
163 | 228 |
164 base::ObserverList<Observer>& observer_list() { return observer_list_; } | 229 base::ObserverList<Observer>& observer_list() { return observer_list_; } |
165 | 230 |
| 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 |
166 bool CalledOnValidThread(); | 243 bool CalledOnValidThread(); |
167 | 244 |
168 private: | 245 private: |
169 friend class ArcBridgeTest; | 246 friend class ArcBridgeTest; |
170 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | 247 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
171 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); | 248 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); |
172 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | 249 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); |
173 | 250 |
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 | |
182 base::ObserverList<Observer> observer_list_; | 251 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_; |
183 | 255 |
184 base::ThreadChecker thread_checker_; | 256 base::ThreadChecker thread_checker_; |
185 | 257 |
186 // If the ARC instance service is available. | 258 // If the ARC instance service is available. |
187 bool available_; | 259 bool available_; |
188 | 260 |
189 // The current state of the bridge. | 261 // The current state of the bridge. |
190 ArcBridgeService::State state_; | 262 ArcBridgeService::State state_; |
191 | 263 |
192 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 264 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
193 }; | 265 }; |
194 | 266 |
195 } // namespace arc | 267 } // namespace arc |
196 | 268 |
197 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 269 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |