Chromium Code Reviews| 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 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 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 { |
| 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 list is ready. | |
| 88 virtual void OnProcessListInstanceReady() {} | |
| 89 | |
| 80 protected: | 90 protected: |
| 81 virtual ~Observer() {} | 91 virtual ~Observer() {} |
| 82 }; | 92 }; |
| 83 | 93 |
| 84 class NotificationObserver { | |
| 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(); | 94 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); | |
| 166 | |
| 167 // Adds or removes ARC process observers. This can only be called on the | |
| 168 // thread that this class was created on. | 126 // thread that this class was created on. |
| 169 void AddProcessObserver(ProcessObserver* observer); | 127 AppInstance* app_instance() { return app_ptr_.get(); } |
| 170 void RemoveProcessObserver(ProcessObserver* observer); | 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 ProcessListInstance* process_list_instance() { | |
| 134 return process_list_ptr_.get(); | |
| 135 } | |
| 171 | 136 |
| 172 // Gets the current state of the bridge service. | 137 // Gets the current state of the bridge service. |
| 173 State state() const { return state_; } | 138 State state() const { return state_; } |
| 174 | 139 |
| 175 // Gets if ARC is available in this system. | 140 // Gets if ARC is available in this system. |
| 176 bool available() const { return available_; } | 141 bool available() const { return available_; } |
| 177 | 142 |
| 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: | 143 protected: |
| 209 ArcBridgeService(); | 144 ArcBridgeService(); |
| 210 | 145 |
| 211 // Changes the current state and notifies all observers. | 146 // Changes the current state and notifies all observers. |
| 212 void SetState(State state); | 147 void SetState(State state); |
| 213 | 148 |
| 214 // Changes the current availability and notifies all observers. | 149 // Changes the current availability and notifies all observers. |
| 215 void SetAvailable(bool availability); | 150 void SetAvailable(bool availability); |
| 216 | 151 |
| 217 base::ObserverList<Observer>& observer_list() { return observer_list_; } | 152 base::ObserverList<Observer>& observer_list() { return observer_list_; } |
| 218 | 153 |
| 219 base::ObserverList<NotificationObserver>& notification_observer_list() { | 154 bool CalledOnValidThread(); |
| 220 return notification_observer_list_; | |
| 221 } | |
| 222 | 155 |
| 223 base::ObserverList<AppObserver>& app_observer_list() { | 156 // Mojo interfaces. |
|
hidehiko
2015/12/16 18:34:03
Should these be private (in impl), so getters shou
Luis Héctor Chávez
2015/12/16 19:09:49
Actually I made ArcBridgeService implement ArcBrid
| |
| 224 return app_observer_list_; | 157 AppInstancePtr app_ptr_; |
| 225 } | 158 InputInstancePtr input_ptr_; |
| 226 | 159 NotificationsInstancePtr notifications_ptr_; |
| 227 base::ObserverList<ProcessObserver>& process_observer_list() { | 160 PowerInstancePtr power_ptr_; |
| 228 return process_observer_list_; | 161 ProcessListInstancePtr process_list_ptr_; |
| 229 } | |
| 230 | |
| 231 bool CalledOnValidThread(); | |
| 232 | 162 |
| 233 private: | 163 private: |
| 234 friend class ArcBridgeTest; | 164 friend class ArcBridgeTest; |
| 235 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | 165 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
| 236 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); | 166 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites); |
| 237 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | 167 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); |
| 238 | 168 |
| 239 base::ObserverList<Observer> observer_list_; | 169 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 | 170 |
| 244 base::ThreadChecker thread_checker_; | 171 base::ThreadChecker thread_checker_; |
| 245 | 172 |
| 246 // If the ARC instance service is available. | 173 // If the ARC instance service is available. |
| 247 bool available_; | 174 bool available_; |
| 248 | 175 |
| 249 // The current state of the bridge. | 176 // The current state of the bridge. |
| 250 ArcBridgeService::State state_; | 177 ArcBridgeService::State state_; |
| 251 | 178 |
| 252 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 179 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 253 }; | 180 }; |
| 254 | 181 |
| 255 } // namespace arc | 182 } // namespace arc |
| 256 | 183 |
| 257 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 184 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| OLD | NEW |