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