OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "base/observer_list.h" |
| 10 #include "base/sequenced_task_runner.h" |
| 11 #include "ipc/ipc_channel_proxy.h" |
| 12 #include "ipc/ipc_listener.h" |
| 13 #include "ipc/ipc_message.h" |
| 14 |
| 15 namespace arc { |
| 16 |
| 17 class ArcBridgeTest; |
| 18 |
| 19 // The Chrome-side service that handles ARC instances and ARC bridge creation. |
| 20 // This service handles the lifetime of ARC instances and sets up the |
| 21 // communication channel (the ARC bridge) used to send and receive messages. |
| 22 class ArcBridgeService : public IPC::Listener { |
| 23 public: |
| 24 // The possible states of the bridge. In the normal flow, the state changes |
| 25 // in the following sequence: |
| 26 // |
| 27 // STOPPED |
| 28 // SetEnabled(true) + HandleStartup() -> SocketConnect() -> |
| 29 // CONNECTING |
| 30 // Connect() -> |
| 31 // CONNECTED |
| 32 // SocketConnectAfterSetSocketPermissions() -> |
| 33 // STARTING |
| 34 // StartInstance() -> OnInstanceReady() -> |
| 35 // READY |
| 36 // |
| 37 // When Shutdown() is called, the state changes depending on the state it was |
| 38 // at: |
| 39 // |
| 40 // CONNECTED/CONNECTING -> STOPPED |
| 41 // STARTING/READY -> STOPPING -> StopInstance() -> STOPPED |
| 42 enum class State { |
| 43 // ARC is not currently running. |
| 44 STOPPED, |
| 45 |
| 46 // The request to connect has been sent. |
| 47 CONNECTING, |
| 48 |
| 49 // The bridge has connected to the socket, but has not started the ARC |
| 50 // instance. |
| 51 CONNECTED, |
| 52 |
| 53 // The ARC bridge has been set up and ARC is starting up. |
| 54 STARTING, |
| 55 |
| 56 // The ARC instance has been fully initialized and is now ready for user |
| 57 // interaction. |
| 58 READY, |
| 59 |
| 60 // The ARC instance has started shutting down. |
| 61 STOPPING, |
| 62 }; |
| 63 |
| 64 class Observer { |
| 65 public: |
| 66 // Called whenever the state of the bridge has changed. |
| 67 virtual void OnStateChanged(State state) {} |
| 68 |
| 69 // Called whenever ARC's enabled status has changed for this session. |
| 70 virtual void OnEnabledChanged(bool enabled) {} |
| 71 |
| 72 // Called whenever ARC's availability has changed for this system. |
| 73 virtual void OnAvailableChanged(bool available) {} |
| 74 |
| 75 // Called whenever ARC sends information about available apps. |
| 76 virtual void OnAppsRefreshed(const std::vector<std::string>& name, |
| 77 const std::vector<std::string>& packages) {} |
| 78 |
| 79 // Called whenever ARC sends app icon data for specific scale factor. |
| 80 virtual void OnAppIcon(const std::string& package, |
| 81 const std::string& activity, |
| 82 int scale_factor, |
| 83 const std::vector<uint8_t>& icon_png_data) {} |
| 84 protected: |
| 85 virtual ~Observer() {} |
| 86 }; |
| 87 |
| 88 ArcBridgeService( |
| 89 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 90 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); |
| 91 ~ArcBridgeService() override; |
| 92 |
| 93 // Gets the global instance of the ARC Bridge Service. |
| 94 static ArcBridgeService* Get(); |
| 95 |
| 96 // DetectAvailability() should be called once D-Bus is available. It will |
| 97 // call CheckArcAvailability() on the session_manager. |
| 98 void DetectAvailability(); |
| 99 |
| 100 // HandleStartup() should be called upon profile startup. This will only |
| 101 // launch an instance if the instance service is available and it is enabled. |
| 102 void HandleStartup(); |
| 103 |
| 104 // Shutdown() should be called when the browser is shutting down. |
| 105 void Shutdown(); |
| 106 |
| 107 // Adds or removes observers. |
| 108 void AddObserver(Observer* observer); |
| 109 void RemoveObserver(Observer* observer); |
| 110 |
| 111 // Gets the current state of the bridge service. |
| 112 State state() const { return state_; } |
| 113 |
| 114 // Enables or disables the bridge for this session. |
| 115 void SetEnabled(bool enabled); |
| 116 |
| 117 // Gets if the bridge is enabled for this session. |
| 118 bool enabled() const { return enabled_; } |
| 119 |
| 120 // Gets if ARC is available in this system. |
| 121 bool available() const { return available_; } |
| 122 |
| 123 // Requests registration of an input device on the ARC instance. |
| 124 // TODO(denniskempin): Make this interface more typesafe. |
| 125 // |name| should be the displayable name of the emulated device (e.g. "Chrome |
| 126 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") |
| 127 // and |fd| a file descriptor that emulates the kernel events of the device. |
| 128 bool RegisterInputDevice(const std::string& name, |
| 129 const std::string& device_type, |
| 130 base::ScopedFD fd); |
| 131 |
| 132 // Requests to refresh an app list. |
| 133 bool RefreshApps(); |
| 134 |
| 135 // Requests to launch an app. |
| 136 bool LaunchApp(const std::string& package, const std::string& activity); |
| 137 |
| 138 // Request to load icon of specific density. |
| 139 bool RequestIcon(const std::string& package, |
| 140 const std::string& activity, |
| 141 int density); |
| 142 |
| 143 private: |
| 144 friend class arc::ArcBridgeTest; |
| 145 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
| 146 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownWhenDisabled); |
| 147 |
| 148 |
| 149 // If all pre-requisites are true (ARC is available, it has been enabled, and |
| 150 // the session has started), and ARC is stopped, start ARC. If ARC is running |
| 151 // and the pre-requisites stop being true, stop ARC. |
| 152 void PrerequisitesChanged(); |
| 153 |
| 154 // Binds to the socket specified by |socket_path|. |
| 155 void SocketConnect(const base::FilePath& socket_path); |
| 156 |
| 157 // Binds to the socket specified by |socket_path| after creating its parent |
| 158 // directory is present. |
| 159 void SocketConnectAfterEnsureParentDirectory( |
| 160 const base::FilePath& socket_path, |
| 161 bool directory_present); |
| 162 |
| 163 // Internal connection method. Separated to make testing easier. |
| 164 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); |
| 165 |
| 166 // Finishes connecting after setting socket permissions. |
| 167 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, |
| 168 bool socket_permissions_success); |
| 169 |
| 170 // Stops the running instance. |
| 171 void StopInstance(); |
| 172 |
| 173 // Called when the ARC instance has finished setting up and is ready for user |
| 174 // interaction. |
| 175 void OnInstanceReady(); |
| 176 |
| 177 // Called whenever ARC sends information about available apps. |
| 178 void OnAppsRefreshed(const std::vector<std::string>& name, |
| 179 const std::vector<std::string>& packages); |
| 180 |
| 181 // Called whenever ARC sends app icon data for specific scale factor. |
| 182 void OnAppIcon(const std::string& package, |
| 183 const std::string& activity, |
| 184 int density, |
| 185 const std::vector<uint8_t>& icon_png_data); |
| 186 |
| 187 // Changes the current state and notifies all observers. |
| 188 void SetState(State state); |
| 189 |
| 190 // IPC::Listener: |
| 191 bool OnMessageReceived(const IPC::Message& message) override; |
| 192 |
| 193 // DBus callbacks. |
| 194 void OnArcAvailable(bool available); |
| 195 void OnInstanceStarted(bool success); |
| 196 void OnInstanceStopped(bool success); |
| 197 |
| 198 // Task runner on which incoming messages are handled. |
| 199 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; |
| 200 |
| 201 // Task runner on which ipc operations are performed. |
| 202 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
| 203 |
| 204 // Task runner on which file operations are performed. |
| 205 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 206 |
| 207 // The channel through which messages are sent. |
| 208 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 209 |
| 210 // List of observers to notify of state changes. |
| 211 base::ObserverList<Observer> observer_list_; |
| 212 |
| 213 // If the user's session has started. |
| 214 bool session_started_; |
| 215 |
| 216 // If the ARC instance service is available. |
| 217 bool available_; |
| 218 |
| 219 // If ARC has been enabled by policy and user choice. |
| 220 bool enabled_; |
| 221 |
| 222 // The current state of the bridge. |
| 223 ArcBridgeService::State state_; |
| 224 |
| 225 // WeakPtrFactory to use callbacks. |
| 226 base::WeakPtrFactory<ArcBridgeService> weak_factory_; |
| 227 |
| 228 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 229 }; |
| 230 |
| 231 } // namespace arc |
| 232 |
| 233 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |