| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 5 #ifndef COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 6 #define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/files/scoped_file.h" |
| 8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 10 #include "base/sequenced_task_runner.h" | |
| 11 #include "components/arc/common/arc_message_types.h" | 12 #include "components/arc/common/arc_message_types.h" |
| 12 #include "ipc/ipc_channel_proxy.h" | |
| 13 #include "ipc/ipc_listener.h" | |
| 14 #include "ipc/ipc_message.h" | |
| 15 | 13 |
| 16 namespace base { | 14 namespace base { |
| 17 class CommandLine; | 15 class CommandLine; |
| 18 } | 16 class SequencedTaskRunner; |
| 17 class SingleThreadTaskRunner; |
| 18 } // namespace base |
| 19 | 19 |
| 20 namespace arc { | 20 namespace arc { |
| 21 | 21 |
| 22 // The Chrome-side service that handles ARC instances and ARC bridge creation. | 22 // The Chrome-side service that handles ARC instances and ARC bridge creation. |
| 23 // This service handles the lifetime of ARC instances and sets up the | 23 // This service handles the lifetime of ARC instances and sets up the |
| 24 // communication channel (the ARC bridge) used to send and receive messages. | 24 // communication channel (the ARC bridge) used to send and receive messages. |
| 25 class ArcBridgeService : public IPC::Listener { | 25 class ArcBridgeService { |
| 26 public: | 26 public: |
| 27 // The possible states of the bridge. In the normal flow, the state changes | 27 // The possible states of the bridge. In the normal flow, the state changes |
| 28 // in the following sequence: | 28 // in the following sequence: |
| 29 // | 29 // |
| 30 // STOPPED | 30 // STOPPED |
| 31 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> | 31 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> |
| 32 // CONNECTING | 32 // CONNECTING |
| 33 // Connect() -> | 33 // Connect() -> |
| 34 // CONNECTED | 34 // CONNECTED |
| 35 // SocketConnectAfterSetSocketPermissions() -> | 35 // SocketConnectAfterSetSocketPermissions() -> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // Called when the instance has reached a boot phase | 72 // Called when the instance has reached a boot phase |
| 73 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} | 73 virtual void OnInstanceBootPhase(InstanceBootPhase phase) {} |
| 74 | 74 |
| 75 // Called whenever ARC's availability has changed for this system. | 75 // Called whenever ARC's availability has changed for this system. |
| 76 virtual void OnAvailableChanged(bool available) {} | 76 virtual void OnAvailableChanged(bool available) {} |
| 77 | 77 |
| 78 protected: | 78 protected: |
| 79 virtual ~Observer() {} | 79 virtual ~Observer() {} |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 ArcBridgeService( | 82 virtual ~ArcBridgeService(); |
| 83 |
| 84 // Creates instance of |ArcBridgeService| for normal use. |
| 85 static scoped_ptr<ArcBridgeService> Create( |
| 83 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 86 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 84 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | 87 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); |
| 85 ~ArcBridgeService() override; | |
| 86 | 88 |
| 87 // Gets the global instance of the ARC Bridge Service. This can only be | 89 // Gets the global instance of the ARC Bridge Service. This can only be |
| 88 // called on the thread that this class was created on. | 90 // called on the thread that this class was created on. |
| 89 static ArcBridgeService* Get(); | 91 static ArcBridgeService* Get(); |
| 90 | 92 |
| 91 // Return true if ARC has been enabled through a commandline | 93 // Return true if ARC has been enabled through a commandline |
| 92 // switch. | 94 // switch. |
| 93 static bool GetEnabled(const base::CommandLine* command_line); | 95 static bool GetEnabled(const base::CommandLine* command_line); |
| 94 | 96 |
| 95 // DetectAvailability() should be called once D-Bus is available. It will | 97 // DetectAvailability() should be called once D-Bus is available. It will |
| 96 // call CheckArcAvailability() on the session_manager. This can only be | 98 // call CheckArcAvailability() on the session_manager. This can only be |
| 97 // called on the thread that this class was created on. | 99 // called on the thread that this class was created on. |
| 98 void DetectAvailability(); | 100 virtual void DetectAvailability() = 0; |
| 99 | 101 |
| 100 // HandleStartup() should be called upon profile startup. This will only | 102 // HandleStartup() should be called upon profile startup. This will only |
| 101 // launch an instance if the instance service is available and it is enabled. | 103 // launch an instance if the instance service is available and it is enabled. |
| 102 // This can only be called on the thread that this class was created on. | 104 // This can only be called on the thread that this class was created on. |
| 103 void HandleStartup(); | 105 virtual void HandleStartup() = 0; |
| 104 | 106 |
| 105 // Shutdown() should be called when the browser is shutting down. This can | 107 // Shutdown() should be called when the browser is shutting down. This can |
| 106 // only be called on the thread that this class was created on. | 108 // only be called on the thread that this class was created on. |
| 107 void Shutdown(); | 109 virtual void Shutdown() = 0; |
| 108 | 110 |
| 109 // Adds or removes observers. This can only be called on the thread that this | 111 // Adds or removes observers. This can only be called on the thread that this |
| 110 // class was created on. | 112 // class was created on. |
| 111 void AddObserver(Observer* observer); | 113 void AddObserver(Observer* observer); |
| 112 void RemoveObserver(Observer* observer); | 114 void RemoveObserver(Observer* observer); |
| 113 | 115 |
| 114 // Gets the current state of the bridge service. | 116 // Gets the current state of the bridge service. |
| 115 State state() const { return state_; } | 117 State state() const { return state_; } |
| 116 | 118 |
| 117 // Gets if ARC is available in this system. | 119 // Gets if ARC is available in this system. |
| 118 bool available() const { return available_; } | 120 bool available() const { return available_; } |
| 119 | 121 |
| 120 // Requests registration of an input device on the ARC instance. | 122 // Requests registration of an input device on the ARC instance. |
| 121 // TODO(denniskempin): Make this interface more typesafe. | 123 // TODO(denniskempin): Make this interface more typesafe. |
| 122 // |name| should be the displayable name of the emulated device (e.g. "Chrome | 124 // |name| should be the displayable name of the emulated device (e.g. "Chrome |
| 123 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") | 125 // OS Keyboard"), |device_type| the name of the device type (e.g. "keyboard") |
| 124 // and |fd| a file descriptor that emulates the kernel events of the device. | 126 // and |fd| a file descriptor that emulates the kernel events of the device. |
| 125 // This can only be called on the thread that this class was created on. | 127 // This can only be called on the thread that this class was created on. |
| 126 bool RegisterInputDevice(const std::string& name, | 128 virtual bool RegisterInputDevice(const std::string& name, |
| 127 const std::string& device_type, | 129 const std::string& device_type, |
| 128 base::ScopedFD fd); | 130 base::ScopedFD fd) = 0; |
| 129 | 131 |
| 130 private: | 132 protected: |
| 131 friend class ArcBridgeTest; | 133 ArcBridgeService(); |
| 132 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | |
| 133 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | |
| 134 | |
| 135 // If all pre-requisites are true (ARC is available, it has been enabled, and | |
| 136 // the session has started), and ARC is stopped, start ARC. If ARC is running | |
| 137 // and the pre-requisites stop being true, stop ARC. | |
| 138 void PrerequisitesChanged(); | |
| 139 | |
| 140 // Binds to the socket specified by |socket_path|. | |
| 141 void SocketConnect(const base::FilePath& socket_path); | |
| 142 | |
| 143 // Binds to the socket specified by |socket_path| after creating its parent | |
| 144 // directory is present. | |
| 145 void SocketConnectAfterEnsureParentDirectory( | |
| 146 const base::FilePath& socket_path, | |
| 147 bool directory_present); | |
| 148 | |
| 149 // Internal connection method. Separated to make testing easier. | |
| 150 bool Connect(const IPC::ChannelHandle& handle, IPC::Channel::Mode mode); | |
| 151 | |
| 152 // Finishes connecting after setting socket permissions. | |
| 153 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, | |
| 154 bool socket_permissions_success); | |
| 155 | |
| 156 // Stops the running instance. | |
| 157 void StopInstance(); | |
| 158 | |
| 159 // Called when the instance has reached a boot phase | |
| 160 void OnInstanceBootPhase(InstanceBootPhase phase); | |
| 161 | 134 |
| 162 // Changes the current state and notifies all observers. | 135 // Changes the current state and notifies all observers. |
| 163 void SetState(State state); | 136 void SetState(State state); |
| 164 | 137 |
| 165 // IPC::Listener: | 138 // Changes the current availability and notifies all observers. |
| 166 bool OnMessageReceived(const IPC::Message& message) override; | 139 void SetAvailable(bool availability); |
| 167 | 140 |
| 168 // DBus callbacks. | 141 const scoped_refptr<base::SequencedTaskRunner>& origin_task_runner() const { |
| 169 void OnArcAvailable(bool available); | 142 return origin_task_runner_; |
| 170 void OnInstanceStarted(bool success); | 143 } |
| 171 void OnInstanceStopped(bool success); | |
| 172 | 144 |
| 145 base::ObserverList<Observer>& observer_list() { return observer_list_; } |
| 146 |
| 147 private: |
| 173 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | 148 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; |
| 174 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
| 175 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | |
| 176 | |
| 177 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | |
| 178 | 149 |
| 179 base::ObserverList<Observer> observer_list_; | 150 base::ObserverList<Observer> observer_list_; |
| 180 | 151 |
| 181 // If the user's session has started. | |
| 182 bool session_started_; | |
| 183 | |
| 184 // If the ARC instance service is available. | 152 // If the ARC instance service is available. |
| 185 bool available_; | 153 bool available_; |
| 186 | 154 |
| 187 // The current state of the bridge. | 155 // The current state of the bridge. |
| 188 ArcBridgeService::State state_; | 156 ArcBridgeService::State state_; |
| 189 | 157 |
| 190 // WeakPtrFactory to use callbacks. | |
| 191 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
| 192 | |
| 193 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 158 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 194 }; | 159 }; |
| 195 | 160 |
| 196 } // namespace arc | 161 } // namespace arc |
| 197 | 162 |
| 198 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 163 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| OLD | NEW |