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 "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 enum State { |
| 31 // An attempt to start the ARC instance was done, but it resulted in an |
| 32 // error. |
| 33 ERROR, |
| 34 |
| 35 // ARC is enabled in this machine, but has not started. |
| 36 STOPPED, |
| 37 |
| 38 // The bridge has connected to the socket, but has not started the ARC |
| 39 // instance. |
| 40 CONNECTED, |
| 41 |
| 42 // The ARC bridge has been set up and ARC is starting up. |
| 43 STARTING, |
| 44 |
| 45 // The ARC instance has been fully initialized and is now ready for user |
| 46 // interaction. |
| 47 READY |
| 48 }; |
| 49 |
| 50 class Observer { |
| 51 public: |
| 52 // Called whenever the state of the bridge has changed. |
| 53 virtual void OnStateChanged(State state) {} |
| 54 |
| 55 // Called whenever ARC's enabled status has changed for this session. |
| 56 virtual void OnEnabledChanged(bool enabled) {} |
| 57 |
| 58 protected: |
| 59 virtual ~Observer() {} |
| 60 }; |
| 61 |
| 62 explicit ArcBridgeService( |
| 63 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); |
| 64 virtual ~ArcBridgeService(); |
| 65 |
| 66 // Registers ARC preferences. |
| 67 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 68 |
| 69 // Reads from the preference store if ARC has been enabled. |
| 70 static bool GetEnabledPref(PrefService* pref_service); |
| 71 |
| 72 // HandleStartup() should be called upon profile startup. This will only |
| 73 // launch an instance if the instance service is available and it is enabled. |
| 74 void HandleStartup(); |
| 75 |
| 76 // Shuts down the running instance, if any. This is safe to call even if |
| 77 // there is no instance running. |
| 78 void Shutdown(); |
| 79 |
| 80 // Adds or removes observers. |
| 81 void AddObserver(Observer* observer); |
| 82 void RemoveObserver(Observer* observer); |
| 83 |
| 84 // Gets the current state of the bridge service. |
| 85 State GetState() const { return state_; } |
| 86 |
| 87 // Enables or disables the bridge for this session. |
| 88 void SetEnabled(bool enabled); |
| 89 |
| 90 // Gets if the bridge is enabled for this session. |
| 91 bool IsEnabled() const { return enabled_; } |
| 92 |
| 93 // Requests registration of an input device on the ARC instance. |
| 94 // TODO(denniskempin): Make this interface more typesafe. |
| 95 // |name| should be the displayable name of the emulated device (e.g. "Chrome |
| 96 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") |
| 97 // and |fd| a file descriptor that emulates the kernel events of the device. |
| 98 bool RegisterInputDevice(const std::string& name, |
| 99 const std::string& device_type, |
| 100 base::ScopedFD fd); |
| 101 |
| 102 private: |
| 103 friend class arc::ArcBridgeTest; |
| 104 |
| 105 // Binds to the socket specified by |socket_path|. |
| 106 void SocketConnect(const base::FilePath& socket_path, |
| 107 bool directory_creation_success); |
| 108 |
| 109 // Internal connection method. Separated to make testing easier. |
| 110 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); |
| 111 |
| 112 // Finishes connecting after setting socket permissions. |
| 113 void FinishConnectAfterSetSocketPermissions(const base::FilePath& socket_path, |
| 114 bool socket_permissions_success); |
| 115 |
| 116 // Called when the ARC instance has finished setting up and is ready for user |
| 117 // interaction. |
| 118 void OnInstanceReady(); |
| 119 |
| 120 // Changes the current state and notify all observers. |
| 121 void NotifyStateChanged(State state); |
| 122 |
| 123 // Finishes shutdown after deleting the socket file. |
| 124 void FinishShutdownAfterSocketDeleted(bool socket_deleted); |
| 125 |
| 126 // IPC::Listener: |
| 127 bool OnMessageReceived(const IPC::Message& message) override; |
| 128 |
| 129 // DBus callbacks. |
| 130 void OnInstanceStarted(chromeos::DBusMethodCallStatus status); |
| 131 void OnInstanceStopped(chromeos::DBusMethodCallStatus status); |
| 132 |
| 133 // Thread in which IPC messaging is performed. |
| 134 base::Thread ipc_thread_; |
| 135 |
| 136 // Task runner on which incoming messages are handled. |
| 137 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; |
| 138 |
| 139 // Task runner on which file operations are performed. |
| 140 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 141 |
| 142 // The channel through which messages are sent. |
| 143 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 144 |
| 145 // List of observers to notify of state changes. |
| 146 // Makes sure list is empty on destruction. |
| 147 base::ObserverList<Observer, true> observer_list_; |
| 148 |
| 149 // If the ARC instance service is available. |
| 150 bool available_; |
| 151 |
| 152 // If ARC has been enabled by policy and user choice. |
| 153 bool enabled_; |
| 154 |
| 155 // The current state of the bridge. |
| 156 ArcBridgeService::State state_; |
| 157 |
| 158 // WeakPtrFactory to use callbacks. |
| 159 base::WeakPtrFactory<ArcBridgeService> weak_factory_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 162 }; |
| 163 |
| 164 } // namespace arc |
| 165 |
| 166 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |