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