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 protected: |
| 76 virtual ~Observer() {} |
| 77 }; |
| 78 |
| 79 ArcBridgeService( |
| 80 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 81 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); |
| 82 ~ArcBridgeService() override; |
| 83 |
| 84 // Gets the global instance of the ARC Bridge Service. |
| 85 static ArcBridgeService* Get(); |
| 86 |
| 87 // DetectAvailability() should be called once D-Bus is available. It will |
| 88 // call CheckArcAvailability() on the session_manager. |
| 89 void DetectAvailability(); |
| 90 |
| 91 // HandleStartup() should be called upon profile startup. This will only |
| 92 // launch an instance if the instance service is available and it is enabled. |
| 93 void HandleStartup(); |
| 94 |
| 95 // Shutdown() should be called when the browser is shutting down. |
| 96 void Shutdown(); |
| 97 |
| 98 // Adds or removes observers. |
| 99 void AddObserver(Observer* observer); |
| 100 void RemoveObserver(Observer* observer); |
| 101 |
| 102 // Gets the current state of the bridge service. |
| 103 State state() const { return state_; } |
| 104 |
| 105 // Enables or disables the bridge for this session. |
| 106 void SetEnabled(bool enabled); |
| 107 |
| 108 // Gets if the bridge is enabled for this session. |
| 109 bool enabled() const { return enabled_; } |
| 110 |
| 111 // Gets if ARC is available in this system. |
| 112 bool available() const { return available_; } |
| 113 |
| 114 // Requests registration of an input device on the ARC instance. |
| 115 // TODO(denniskempin): Make this interface more typesafe. |
| 116 // |name| should be the displayable name of the emulated device (e.g. "Chrome |
| 117 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") |
| 118 // and |fd| a file descriptor that emulates the kernel events of the device. |
| 119 bool RegisterInputDevice(const std::string& name, |
| 120 const std::string& device_type, |
| 121 base::ScopedFD fd); |
| 122 |
| 123 private: |
| 124 friend class arc::ArcBridgeTest; |
| 125 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
| 126 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownWhenDisabled); |
| 127 |
| 128 // If all pre-requisites are true (ARC is available, it has been enabled, and |
| 129 // the session has started), and ARC is stopped, start ARC. If ARC is running |
| 130 // and the pre-requisites stop being true, stop ARC. |
| 131 void PrerequisitesChanged(); |
| 132 |
| 133 // Binds to the socket specified by |socket_path|. |
| 134 void SocketConnect(const base::FilePath& socket_path); |
| 135 |
| 136 // Binds to the socket specified by |socket_path| after creating its parent |
| 137 // directory is present. |
| 138 void SocketConnectAfterEnsureParentDirectory( |
| 139 const base::FilePath& socket_path, |
| 140 bool directory_present); |
| 141 |
| 142 // Internal connection method. Separated to make testing easier. |
| 143 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); |
| 144 |
| 145 // Finishes connecting after setting socket permissions. |
| 146 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, |
| 147 bool socket_permissions_success); |
| 148 |
| 149 // Stops the running instance. |
| 150 void StopInstance(); |
| 151 |
| 152 // Called when the ARC instance has finished setting up and is ready for user |
| 153 // interaction. |
| 154 void OnInstanceReady(); |
| 155 |
| 156 // Changes the current state and notifies all observers. |
| 157 void SetState(State state); |
| 158 |
| 159 // IPC::Listener: |
| 160 bool OnMessageReceived(const IPC::Message& message) override; |
| 161 |
| 162 // DBus callbacks. |
| 163 void OnArcAvailable(bool available); |
| 164 void OnInstanceStarted(bool success); |
| 165 void OnInstanceStopped(bool success); |
| 166 |
| 167 // Task runner on which incoming messages are handled. |
| 168 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; |
| 169 |
| 170 // Task runner on which ipc operations are performed. |
| 171 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
| 172 |
| 173 // Task runner on which file operations are performed. |
| 174 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 175 |
| 176 // The channel through which messages are sent. |
| 177 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 178 |
| 179 // List of observers to notify of state changes. |
| 180 base::ObserverList<Observer> observer_list_; |
| 181 |
| 182 // If the user's session has started. |
| 183 bool session_started_; |
| 184 |
| 185 // If the ARC instance service is available. |
| 186 bool available_; |
| 187 |
| 188 // If ARC has been enabled by policy and user choice. |
| 189 bool enabled_; |
| 190 |
| 191 // The current state of the bridge. |
| 192 ArcBridgeService::State state_; |
| 193 |
| 194 // WeakPtrFactory to use callbacks. |
| 195 base::WeakPtrFactory<ArcBridgeService> weak_factory_; |
| 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 198 }; |
| 199 |
| 200 } // namespace arc |
| 201 |
| 202 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |