| 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" |
| 9 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 10 #include "base/sequenced_task_runner.h" | 11 #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 | 12 |
| 15 namespace arc { | 13 namespace arc { |
| 16 | 14 |
| 17 // The Chrome-side service that handles ARC instances and ARC bridge creation. | 15 // 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 | 16 // This service handles the lifetime of ARC instances and sets up the |
| 19 // communication channel (the ARC bridge) used to send and receive messages. | 17 // communication channel (the ARC bridge) used to send and receive messages. |
| 20 class ArcBridgeService : public IPC::Listener { | 18 class ArcBridgeService { |
| 21 public: | 19 public: |
| 22 // The possible states of the bridge. In the normal flow, the state changes | 20 // The possible states of the bridge. In the normal flow, the state changes |
| 23 // in the following sequence: | 21 // in the following sequence: |
| 24 // | 22 // |
| 25 // STOPPED | 23 // STOPPED |
| 26 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> | 24 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> |
| 27 // CONNECTING | 25 // CONNECTING |
| 28 // Connect() -> | 26 // Connect() -> |
| 29 // CONNECTED | 27 // CONNECTED |
| 30 // SocketConnectAfterSetSocketPermissions() -> | 28 // SocketConnectAfterSetSocketPermissions() -> |
| (...skipping 29 matching lines...) Expand all Loading... |
| 60 }; | 58 }; |
| 61 | 59 |
| 62 class Observer { | 60 class Observer { |
| 63 public: | 61 public: |
| 64 // Called whenever the state of the bridge has changed. | 62 // Called whenever the state of the bridge has changed. |
| 65 virtual void OnStateChanged(State state) {} | 63 virtual void OnStateChanged(State state) {} |
| 66 | 64 |
| 67 // Called whenever ARC's availability has changed for this system. | 65 // Called whenever ARC's availability has changed for this system. |
| 68 virtual void OnAvailableChanged(bool available) {} | 66 virtual void OnAvailableChanged(bool available) {} |
| 69 | 67 |
| 68 // Called whenever ARC sends information about available apps. |
| 69 virtual void OnAppsRefreshed(const std::vector<std::string>& name, |
| 70 const std::vector<std::string>& packages, |
| 71 const std::vector<std::string>& activities) {} |
| 72 |
| 73 // Called whenever ARC sends app icon data for specific scale factor. |
| 74 virtual void OnAppIcon(const std::string& package, |
| 75 const std::string& activity, |
| 76 int scale_factor, |
| 77 const std::vector<uint8_t>& icon_png_data) {} |
| 78 |
| 70 protected: | 79 protected: |
| 71 virtual ~Observer() {} | 80 virtual ~Observer() {} |
| 72 }; | 81 }; |
| 73 | 82 |
| 74 ArcBridgeService( | 83 ArcBridgeService(); |
| 75 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 84 virtual ~ArcBridgeService(); |
| 76 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
| 77 ~ArcBridgeService() override; | |
| 78 | 85 |
| 79 // Gets the global instance of the ARC Bridge Service. This can only be | 86 // Gets the global instance of the ARC Bridge Service. This can only be |
| 80 // called on the thread that this class was created on. | 87 // called on the thread that this class was created on. |
| 81 static ArcBridgeService* Get(); | 88 static ArcBridgeService* Get(); |
| 82 | 89 |
| 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 | 90 // Adds or removes observers. This can only be called on the thread that this |
| 98 // class was created on. | 91 // class was created on. |
| 99 void AddObserver(Observer* observer); | 92 void AddObserver(Observer* observer); |
| 100 void RemoveObserver(Observer* observer); | 93 void RemoveObserver(Observer* observer); |
| 101 | 94 |
| 102 // Gets the current state of the bridge service. | 95 // Gets the current state of the bridge service. |
| 103 State state() const { return state_; } | 96 State state() const { return state_; } |
| 104 | 97 |
| 105 // Gets if ARC is available in this system. | 98 // Gets if ARC is available in this system. |
| 106 bool available() const { return available_; } | 99 bool available() const { return available_; } |
| 107 | 100 |
| 101 // HandleStartup() should be called upon profile startup. This will only |
| 102 // launch an instance if the instance service is available and it is enabled. |
| 103 // This can only be called on the thread that this class was created on. |
| 104 virtual void HandleStartup() = 0; |
| 105 |
| 106 // Shutdown() should be called when the browser is shutting down. This can |
| 107 // only be called on the thread that this class was created on. |
| 108 virtual void Shutdown() = 0; |
| 109 |
| 110 |
| 108 // Requests registration of an input device on the ARC instance. | 111 // Requests registration of an input device on the ARC instance. |
| 109 // TODO(denniskempin): Make this interface more typesafe. | 112 // TODO(denniskempin): Make this interface more typesafe. |
| 110 // |name| should be the displayable name of the emulated device (e.g. "Chrome | 113 // |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") | 114 // 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. | 115 // 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. | 116 // This can only be called on the thread that this class was created on. |
| 114 bool RegisterInputDevice(const std::string& name, | 117 virtual bool RegisterInputDevice(const std::string& name, |
| 115 const std::string& device_type, | 118 const std::string& device_type, |
| 116 base::ScopedFD fd); | 119 base::ScopedFD fd) = 0; |
| 117 | 120 |
| 118 private: | 121 // Requests to refresh an app list. |
| 119 friend class ArcBridgeTest; | 122 virtual bool RefreshApps() = 0; |
| 120 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | |
| 121 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | |
| 122 | 123 |
| 123 // If all pre-requisites are true (ARC is available, it has been enabled, and | 124 // Requests to launch an app. |
| 124 // the session has started), and ARC is stopped, start ARC. If ARC is running | 125 virtual bool LaunchApp(const std::string& package, |
| 125 // and the pre-requisites stop being true, stop ARC. | 126 const std::string& activity) = 0; |
| 126 void PrerequisitesChanged(); | |
| 127 | 127 |
| 128 // Binds to the socket specified by |socket_path|. | 128 // Request to load icon of specific scale_factor. |
| 129 void SocketConnect(const base::FilePath& socket_path); | 129 virtual bool RequestIcon(const std::string& package, |
| 130 const std::string& activity, |
| 131 int scale_factor) = 0; |
| 130 | 132 |
| 131 // Binds to the socket specified by |socket_path| after creating its parent | 133 protected: |
| 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. | 134 // Changes the current state and notifies all observers. |
| 152 void SetState(State state); | 135 void SetState(State state); |
| 153 | 136 |
| 154 // IPC::Listener: | 137 // Changes the current availability and notifies all observers. |
| 155 bool OnMessageReceived(const IPC::Message& message) override; | 138 void SetAvailable(bool availability); |
| 156 | |
| 157 // DBus callbacks. | |
| 158 void OnArcAvailable(bool available); | |
| 159 void OnInstanceStarted(bool success); | |
| 160 void OnInstanceStopped(bool success); | |
| 161 | 139 |
| 162 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | 140 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 | 141 |
| 168 base::ObserverList<Observer> observer_list_; | 142 base::ObserverList<Observer> observer_list_; |
| 169 | 143 |
| 170 // If the user's session has started. | |
| 171 bool session_started_; | |
| 172 | |
| 173 // If the ARC instance service is available. | 144 // If the ARC instance service is available. |
| 174 bool available_; | 145 bool available_; |
| 175 | 146 |
| 176 // The current state of the bridge. | 147 // The current state of the bridge. |
| 177 ArcBridgeService::State state_; | 148 ArcBridgeService::State state_; |
| 178 | 149 |
| 179 // WeakPtrFactory to use callbacks. | |
| 180 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | 150 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); |
| 183 }; | 151 }; |
| 184 | 152 |
| 185 } // namespace arc | 153 } // namespace arc |
| 186 | 154 |
| 187 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 155 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
| OLD | NEW |