OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ |
6 #define TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ |
7 | 7 |
8 #include <hash_map> | |
9 #include <string> | 8 #include <string> |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/id_map.h" | 11 #include "base/containers/hash_tables.h" |
13 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
14 #include "tools/android/forwarder2/socket.h" | 16 #include "tools/android/forwarder2/socket.h" |
15 | 17 |
| 18 namespace base { |
| 19 class SingleThreadTaskRunner; |
| 20 } // namespace base |
| 21 |
16 namespace forwarder2 { | 22 namespace forwarder2 { |
17 | 23 |
18 class DeviceListener; | 24 class DeviceListener; |
19 | 25 |
| 26 // There is a single DeviceController per device_forwarder process, and it is in |
| 27 // charge of managing all active redirections on the device side (one |
| 28 // DeviceListener each). |
20 class DeviceController { | 29 class DeviceController { |
21 public: | 30 public: |
22 explicit DeviceController(int exit_notifier_fd); | 31 static scoped_ptr<DeviceController> Create(const std::string& adb_unix_socket, |
| 32 int exit_notifier_fd); |
23 ~DeviceController(); | 33 ~DeviceController(); |
24 | 34 |
25 bool Init(const std::string& adb_unix_socket); | |
26 | |
27 void Start(); | 35 void Start(); |
28 | 36 |
29 private: | 37 private: |
30 void KillAllListeners(); | 38 typedef base::hash_map< |
31 void CleanUpDeadListeners(); | 39 int /* port */, linked_ptr<DeviceListener> > ListenersMap; |
32 | 40 |
33 // Map from Port to DeviceListener objects (owns the pointer). | 41 DeviceController(scoped_ptr<Socket> host_socket, int exit_notifier_fd); |
34 typedef IDMap<DeviceListener, IDMapOwnPointer> ListenersMap; | |
35 | 42 |
36 ListenersMap listeners_; | 43 void AcceptHostCommandSoon(); |
37 Socket kickstart_adb_socket_; | 44 void AcceptHostCommandInternal(); |
38 | 45 |
| 46 // Note that this can end up being called after the DeviceController is |
| 47 // destroyed which is why a weak pointer is used. |
| 48 static void DeleteListener( |
| 49 const base::WeakPtr<DeviceController>& device_controller_ptr, |
| 50 int listener_port); |
| 51 |
| 52 const scoped_ptr<Socket> host_socket_; |
39 // Used to notify the controller to exit. | 53 // Used to notify the controller to exit. |
40 const int exit_notifier_fd_; | 54 const int exit_notifier_fd_; |
| 55 // Lets ensure DeviceListener instances are deleted on the thread they were |
| 56 // created on. |
| 57 const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_; |
| 58 base::WeakPtrFactory<DeviceController> weak_ptr_factory_; |
| 59 ListenersMap listeners_; |
41 | 60 |
42 DISALLOW_COPY_AND_ASSIGN(DeviceController); | 61 DISALLOW_COPY_AND_ASSIGN(DeviceController); |
43 }; | 62 }; |
44 | 63 |
45 } // namespace forwarder | 64 } // namespace forwarder |
46 | 65 |
47 #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ | 66 #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ |
OLD | NEW |