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