Chromium Code Reviews| 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_threadsafe.h" | |
| 10 #include "base/sequenced_task_runner.h" | |
| 11 #include "chromeos/dbus/dbus_method_call_status.h" | |
| 12 #include "components/keyed_service/core/keyed_service.h" | |
| 13 #include "ipc/ipc_channel_proxy.h" | |
| 14 #include "ipc/ipc_listener.h" | |
| 15 #include "ipc/ipc_message.h" | |
| 16 | |
| 17 class PrefRegistrySimple; | |
| 18 class PrefService; | |
| 19 | |
| 20 namespace arc { | |
| 21 | |
| 22 class ArcBridgeTest; | |
| 23 | |
| 24 // The Chrome-side service that handles ARC instances and ARC bridge creation. | |
| 25 // This service handles the lifetime of ARC instances and sets up the | |
| 26 // communication channel (the ARC bridge) used to send and receive messages. | |
| 27 class ArcBridgeService : public KeyedService, | |
| 28 public IPC::Listener { | |
| 29 public: | |
| 30 // The possible states of the bridge. In the normal flow, the state changes | |
| 31 // in the following sequence: | |
| 32 // | |
| 33 // STOPPED | |
| 34 // SetEnabled(true) + HandleStartup() -> SocketConnect() -> | |
| 35 // CONNECTING | |
| 36 // Connect() -> | |
| 37 // CONNECTED | |
| 38 // SocketConnectAfterSetsocketPermissions() -> | |
|
hidehiko
2015/11/05 18:08:50
nit: SocketConnecetAfterSet*S*ocketPermissions.
lhc(google)
2015/11/06 04:14:16
Done.
Luis Héctor Chávez
2015/11/09 16:41:04
Done.
| |
| 39 // STARTING | |
| 40 // StartInstance() -> OnInstanceReady() -> | |
| 41 // READY | |
| 42 // | |
| 43 // When Shutdown() is called, the state changes depending on the state it was | |
| 44 // at: | |
| 45 // | |
| 46 // CONNECTED/CONNECTING -> STOPPED | |
| 47 // STARTING/READY -> STOPPING -> StopInstance() -> STOPPED | |
| 48 enum State { | |
| 49 // ARC is not currently running. | |
| 50 STOPPED, | |
| 51 | |
| 52 // The request to connect has been sent. | |
| 53 CONNECTING, | |
| 54 | |
| 55 // The bridge has connected to the socket, but has not started the ARC | |
| 56 // instance. | |
| 57 CONNECTED, | |
| 58 | |
| 59 // The ARC bridge has been set up and ARC is starting up. | |
| 60 STARTING, | |
| 61 | |
| 62 // The ARC instance has been fully initialized and is now ready for user | |
| 63 // interaction. | |
| 64 READY, | |
| 65 | |
| 66 // The ARC instance has started shutting down. | |
| 67 STOPPING, | |
| 68 }; | |
| 69 | |
| 70 class Observer { | |
| 71 public: | |
| 72 // Called whenever the state of the bridge has changed. | |
| 73 virtual void OnStateChanged(State state) {} | |
| 74 | |
| 75 // Called whenever ARC's enabled status has changed for this session. | |
| 76 virtual void OnEnabledChanged(bool enabled) {} | |
| 77 | |
| 78 protected: | |
| 79 virtual ~Observer() {} | |
| 80 }; | |
| 81 | |
| 82 explicit ArcBridgeService( | |
| 83 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
| 84 virtual ~ArcBridgeService(); | |
| 85 | |
| 86 // Registers ARC preferences. | |
| 87 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 88 | |
| 89 // Reads from the preference store if ARC has been enabled. | |
| 90 static bool GetEnabledPref(PrefService* pref_service); | |
| 91 | |
| 92 // HandleStartup() should be called upon profile startup. This will only | |
| 93 // launch an instance if the instance service is available and it is enabled. | |
| 94 void HandleStartup(); | |
| 95 | |
| 96 // Shuts down the running instance, if any. This is safe to call even if | |
| 97 // there is no instance running. | |
| 98 void Shutdown(); | |
| 99 | |
| 100 // Adds or removes observers. | |
| 101 void AddObserver(Observer* observer); | |
| 102 void RemoveObserver(Observer* observer); | |
| 103 | |
| 104 // Gets the current state of the bridge service. | |
| 105 State GetState() const { return state_; } | |
| 106 | |
| 107 // Enables or disables the bridge for this session. | |
| 108 void SetEnabled(bool enabled); | |
| 109 | |
| 110 // Gets if the bridge is enabled for this session. | |
| 111 bool IsEnabled() const { return enabled_; } | |
| 112 | |
| 113 // Requests registration of an input device on the ARC instance. | |
| 114 // TODO(denniskempin): Make this interface more typesafe. | |
| 115 // |name| should be the displayable name of the emulated device (e.g. "Chrome | |
| 116 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") | |
| 117 // and |fd| a file descriptor that emulates the kernel events of the device. | |
| 118 bool RegisterInputDevice(const std::string& name, | |
| 119 const std::string& device_type, | |
| 120 base::ScopedFD fd); | |
| 121 | |
| 122 private: | |
| 123 friend class arc::ArcBridgeTest; | |
| 124 | |
| 125 // Binds to the socket specified by |socket_path|. | |
| 126 void SocketConnect(const base::FilePath& socket_path); | |
| 127 | |
| 128 // Binds to the socket specified by |socket_path| after creating its parent | |
| 129 // directory is present. | |
| 130 void SocketConnectAfterEnsureParentDirectory( | |
| 131 const base::FilePath& socket_path, bool directory_present); | |
| 132 | |
| 133 // Internal connection method. Separated to make testing easier. | |
| 134 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); | |
| 135 | |
| 136 // Finishes connecting after setting socket permissions. | |
| 137 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, | |
| 138 bool socket_permissions_success); | |
| 139 | |
| 140 // Called when the ARC instance has finished setting up and is ready for user | |
| 141 // interaction. | |
| 142 void OnInstanceReady(); | |
| 143 | |
| 144 // Changes the current state and notify all observers. | |
| 145 void SetState(State state); | |
| 146 | |
| 147 // IPC::Listener: | |
| 148 bool OnMessageReceived(const IPC::Message& message) override; | |
| 149 | |
| 150 // DBus callbacks. | |
| 151 void OnInstanceStarted(bool success); | |
| 152 void OnInstanceStopped(bool success); | |
| 153 | |
| 154 // Thread in which IPC messaging is performed. | |
| 155 base::Thread ipc_thread_; | |
| 156 | |
| 157 // Task runner on which incoming messages are handled. | |
| 158 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | |
| 159 | |
| 160 // Task runner on which file operations are performed. | |
| 161 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 162 | |
| 163 // The channel through which messages are sent. | |
| 164 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | |
|
hidehiko
2015/11/05 18:08:50
Design question.
ChannelProxy looks designed to be
lhc(google)
2015/11/06 04:14:16
Based on the number of comments about it, making t
| |
| 165 | |
| 166 // A Lock that guards the |ipc_channel_|. Most of the methods are expected | |
| 167 // to be run in the same thread (the one in which the ArcBridgeService | |
| 168 // instance was created), but all the ones that send IPC messages can be | |
| 169 // called from any method. This will prevent the |ipc_channel_| from being | |
| 170 // destroyed while a message is being sent. | |
| 171 base::Lock ipc_channel_lock_; | |
| 172 | |
| 173 // List of observers to notify of state changes. | |
| 174 scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_; | |
| 175 | |
| 176 // If the ARC instance service is available. | |
| 177 bool available_; | |
| 178 | |
| 179 // If ARC has been enabled by policy and user choice. | |
| 180 bool enabled_; | |
| 181 | |
| 182 // The current state of the bridge. | |
| 183 ArcBridgeService::State state_; | |
| 184 | |
| 185 // WeakPtrFactory to use callbacks. | |
| 186 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | |
| 189 }; | |
| 190 | |
| 191 } // namespace arc | |
| 192 | |
| 193 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | |
| OLD | NEW |