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/macros.h" | 8 #include "base/macros.h" |
9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
11 #include "ipc/ipc_channel_proxy.h" | 11 #include "ipc/ipc_channel_proxy.h" |
12 #include "ipc/ipc_listener.h" | 12 #include "ipc/ipc_listener.h" |
13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
14 | 14 |
15 namespace arc { | 15 namespace arc { |
16 | 16 |
17 // The Chrome-side service that handles ARC instances and ARC bridge creation. | 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 | 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. | 19 // communication channel (the ARC bridge) used to send and receive messages. |
20 class ArcBridgeService : public IPC::Listener { | 20 class ArcBridgeService { |
21 public: | 21 public: |
22 // The possible states of the bridge. In the normal flow, the state changes | 22 // The possible states of the bridge. In the normal flow, the state changes |
23 // in the following sequence: | 23 // in the following sequence: |
24 // | 24 // |
25 // STOPPED | 25 // STOPPED |
26 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> | 26 // SetAvailable(true) + HandleStartup() -> SocketConnect() -> |
27 // CONNECTING | 27 // CONNECTING |
28 // Connect() -> | 28 // Connect() -> |
29 // CONNECTED | 29 // CONNECTED |
30 // SocketConnectAfterSetSocketPermissions() -> | 30 // SocketConnectAfterSetSocketPermissions() -> |
(...skipping 29 matching lines...) Expand all Loading... | |
60 }; | 60 }; |
61 | 61 |
62 class Observer { | 62 class Observer { |
63 public: | 63 public: |
64 // Called whenever the state of the bridge has changed. | 64 // Called whenever the state of the bridge has changed. |
65 virtual void OnStateChanged(State state) {} | 65 virtual void OnStateChanged(State state) {} |
66 | 66 |
67 // Called whenever ARC's availability has changed for this system. | 67 // Called whenever ARC's availability has changed for this system. |
68 virtual void OnAvailableChanged(bool available) {} | 68 virtual void OnAvailableChanged(bool available) {} |
69 | 69 |
70 // Called whenever ARC sends information about available apps. | |
71 virtual void OnAppsRefreshed(const std::vector<std::string>& name, | |
72 const std::vector<std::string>& packages, | |
73 const std::vector<std::string>& activities) {} | |
74 | |
75 // Called whenever ARC sends app icon data for specific scale factor. | |
76 virtual void OnAppIcon(const std::string& package, | |
77 const std::string& activity, | |
78 int scale_factor, | |
79 const std::vector<uint8_t>& icon_png_data) {} | |
80 | |
70 protected: | 81 protected: |
71 virtual ~Observer() {} | 82 virtual ~Observer() {} |
72 }; | 83 }; |
73 | 84 |
74 ArcBridgeService( | 85 ArcBridgeService(); |
75 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | 86 virtual ~ArcBridgeService(); |
76 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
77 ~ArcBridgeService() override; | |
78 | 87 |
79 // Gets the global instance of the ARC Bridge Service. This can only be | 88 // Gets the global instance of the ARC Bridge Service. This can only be |
80 // called on the thread that this class was created on. | 89 // called on the thread that this class was created on. |
81 static ArcBridgeService* Get(); | 90 static ArcBridgeService* Get(); |
82 | 91 |
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 | 92 // Adds or removes observers. This can only be called on the thread that this |
98 // class was created on. | 93 // class was created on. |
99 void AddObserver(Observer* observer); | 94 void AddObserver(Observer* observer); |
100 void RemoveObserver(Observer* observer); | 95 void RemoveObserver(Observer* observer); |
101 | 96 |
102 // Gets the current state of the bridge service. | 97 // Gets the current state of the bridge service. |
103 State state() const { return state_; } | 98 State state() const { return state_; } |
104 | 99 |
105 // Gets if ARC is available in this system. | 100 // Gets if ARC is available in this system. |
106 bool available() const { return available_; } | 101 bool available() const { return available_; } |
107 | 102 |
103 // HandleStartup() should be called upon profile startup. This will only | |
104 // launch an instance if the instance service is available and it is enabled. | |
105 // This can only be called on the thread that this class was created on. | |
106 virtual void HandleStartup() = 0; | |
107 | |
108 // Shutdown() should be called when the browser is shutting down. This can | |
109 // only be called on the thread that this class was created on. | |
110 virtual void Shutdown() = 0; | |
111 | |
112 | |
108 // Requests registration of an input device on the ARC instance. | 113 // Requests registration of an input device on the ARC instance. |
109 // TODO(denniskempin): Make this interface more typesafe. | 114 // TODO(denniskempin): Make this interface more typesafe. |
110 // |name| should be the displayable name of the emulated device (e.g. "Chrome | 115 // |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") | 116 // 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. | 117 // 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. | 118 // This can only be called on the thread that this class was created on. |
119 virtual bool RegisterInputDevice(const std::string& name, | |
120 const std::string& device_type, | |
121 base::ScopedFD fd) = 0; | |
122 | |
123 // Requests to refresh an app list. | |
124 virtual bool RefreshApps() = 0; | |
125 | |
126 // Requests to launch an app. | |
127 virtual bool LaunchApp(const std::string& package, | |
128 const std::string& activity) = 0; | |
129 | |
130 // Request to load icon of specific scale_factor. | |
131 virtual bool RequestIcon(const std::string& package, | |
132 const std::string& activity, | |
133 int scale_factor) = 0; | |
134 | |
135 protected: | |
136 // Changes the current state and notifies all observers. | |
137 void SetState(State state); | |
138 | |
139 // Changes the current availability and notifies all observers. | |
140 void SetAvailable(bool availability); | |
141 | |
142 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | |
143 | |
144 base::ObserverList<Observer> observer_list_; | |
145 | |
146 // If the ARC instance service is available. | |
147 bool available_; | |
148 | |
149 // The current state of the bridge. | |
150 ArcBridgeService::State state_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | |
153 }; | |
154 | |
155 class ArcBridgeServiceImpl : public ArcBridgeService, public IPC::Listener { | |
xiyuan
2015/11/18 03:42:16
Move this into its own file, arc_bridge_service_im
khmel1
2015/11/18 06:10:05
Done.
| |
156 public: | |
157 | |
158 ArcBridgeServiceImpl( | |
159 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, | |
160 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner); | |
161 ~ArcBridgeServiceImpl() override; | |
162 | |
163 // DetectAvailability() should be called once D-Bus is available. It will | |
164 // call CheckArcAvailability() on the session_manager. This can only be | |
165 // called on the thread that this class was created on. | |
166 void DetectAvailability(); | |
167 | |
168 void HandleStartup() override; | |
169 | |
170 void Shutdown() override; | |
171 | |
114 bool RegisterInputDevice(const std::string& name, | 172 bool RegisterInputDevice(const std::string& name, |
115 const std::string& device_type, | 173 const std::string& device_type, |
116 base::ScopedFD fd); | 174 base::ScopedFD fd) override; |
175 // Requests to refresh an app list. | |
176 bool RefreshApps() override; | |
177 | |
178 // Requests to launch an app. | |
179 bool LaunchApp(const std::string& package, | |
180 const std::string& activity) override; | |
181 | |
182 // Request to load icon of specific scale_factor. | |
183 bool RequestIcon(const std::string& package, | |
184 const std::string& activity, | |
185 int scale_factor) override; | |
117 | 186 |
118 private: | 187 private: |
119 friend class ArcBridgeTest; | 188 friend class ArcBridgeTest; |
120 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); | 189 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic); |
121 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); | 190 FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup); |
122 | 191 |
123 // If all pre-requisites are true (ARC is available, it has been enabled, and | 192 // 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 | 193 // the session has started), and ARC is stopped, start ARC. If ARC is running |
125 // and the pre-requisites stop being true, stop ARC. | 194 // and the pre-requisites stop being true, stop ARC. |
126 void PrerequisitesChanged(); | 195 void PrerequisitesChanged(); |
(...skipping 14 matching lines...) Expand all Loading... | |
141 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, | 210 void SocketConnectAfterSetSocketPermissions(const base::FilePath& socket_path, |
142 bool socket_permissions_success); | 211 bool socket_permissions_success); |
143 | 212 |
144 // Stops the running instance. | 213 // Stops the running instance. |
145 void StopInstance(); | 214 void StopInstance(); |
146 | 215 |
147 // Called when the ARC instance has finished setting up and is ready for user | 216 // Called when the ARC instance has finished setting up and is ready for user |
148 // interaction. | 217 // interaction. |
149 void OnInstanceReady(); | 218 void OnInstanceReady(); |
150 | 219 |
151 // Changes the current state and notifies all observers. | 220 // Called whenever ARC sends information about available apps. |
152 void SetState(State state); | 221 void OnAppsRefreshed(const std::vector<std::string>& name, |
222 const std::vector<std::string>& packages, | |
223 const std::vector<std::string>& activities); | |
224 | |
225 // Called whenever ARC sends app icon data for specific scale factor. | |
226 void OnAppIcon(const std::string& package, | |
227 const std::string& activity, | |
228 int scale_factor, | |
229 const std::vector<uint8_t>& icon_png_data); | |
153 | 230 |
154 // IPC::Listener: | 231 // IPC::Listener: |
155 bool OnMessageReceived(const IPC::Message& message) override; | 232 bool OnMessageReceived(const IPC::Message& message) override; |
156 | 233 |
157 // DBus callbacks. | 234 // DBus callbacks. |
158 void OnArcAvailable(bool available); | 235 void OnArcAvailable(bool available); |
159 void OnInstanceStarted(bool success); | 236 void OnInstanceStarted(bool success); |
160 void OnInstanceStopped(bool success); | 237 void OnInstanceStopped(bool success); |
161 | 238 |
162 scoped_refptr<base::SequencedTaskRunner> origin_task_runner_; | |
163 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | 239 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
164 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 240 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
165 | 241 |
166 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | 242 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
167 | 243 |
168 base::ObserverList<Observer> observer_list_; | |
169 | |
170 // If the user's session has started. | 244 // If the user's session has started. |
171 bool session_started_; | 245 bool session_started_; |
172 | 246 |
173 // If the ARC instance service is available. | 247 // WeakPtrFactory to use callbacks. |
174 bool available_; | 248 base::WeakPtrFactory<ArcBridgeServiceImpl> weak_factory_; |
175 | 249 |
176 // The current state of the bridge. | 250 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceImpl); |
177 ArcBridgeService::State state_; | |
178 | |
179 // WeakPtrFactory to use callbacks. | |
180 base::WeakPtrFactory<ArcBridgeService> weak_factory_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(ArcBridgeService); | |
183 }; | 251 }; |
184 | 252 |
185 } // namespace arc | 253 } // namespace arc |
186 | 254 |
187 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ | 255 #endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_ |
OLD | NEW |