| 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 <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "tools/android/forwarder2/socket.h" | 16 #include "tools/android/forwarder2/socket.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
| 20 } // namespace base | 20 } // namespace base |
| 21 | 21 |
| 22 namespace forwarder2 { | 22 namespace forwarder2 { |
| 23 | 23 |
| 24 class DeviceListener; | 24 class DeviceListener; |
| 25 | 25 |
| 26 // There is a single DeviceController per device_forwarder process, and it is in | 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 | 27 // charge of managing all active redirections on the device side (one |
| 28 // DeviceListener each). | 28 // DeviceListener each). |
| 29 class DeviceController { | 29 class DeviceController { |
| 30 public: | 30 public: |
| 31 static scoped_ptr<DeviceController> Create(const std::string& adb_unix_socket, | 31 static std::unique_ptr<DeviceController> Create( |
| 32 int exit_notifier_fd); | 32 const std::string& adb_unix_socket, |
| 33 int exit_notifier_fd); |
| 33 ~DeviceController(); | 34 ~DeviceController(); |
| 34 | 35 |
| 35 void Start(); | 36 void Start(); |
| 36 | 37 |
| 37 private: | 38 private: |
| 38 typedef base::hash_map< | 39 typedef base::hash_map< |
| 39 int /* port */, linked_ptr<DeviceListener> > ListenersMap; | 40 int /* port */, linked_ptr<DeviceListener> > ListenersMap; |
| 40 | 41 |
| 41 DeviceController(scoped_ptr<Socket> host_socket, int exit_notifier_fd); | 42 DeviceController(std::unique_ptr<Socket> host_socket, int exit_notifier_fd); |
| 42 | 43 |
| 43 void AcceptHostCommandSoon(); | 44 void AcceptHostCommandSoon(); |
| 44 void AcceptHostCommandInternal(); | 45 void AcceptHostCommandInternal(); |
| 45 | 46 |
| 46 // Note that this can end up being called after the DeviceController is | 47 // Note that this can end up being called after the DeviceController is |
| 47 // destroyed which is why a weak pointer is used. | 48 // destroyed which is why a weak pointer is used. |
| 48 static void DeleteListenerOnError( | 49 static void DeleteListenerOnError( |
| 49 const base::WeakPtr<DeviceController>& device_controller_ptr, | 50 const base::WeakPtr<DeviceController>& device_controller_ptr, |
| 50 scoped_ptr<DeviceListener> device_listener); | 51 std::unique_ptr<DeviceListener> device_listener); |
| 51 | 52 |
| 52 const scoped_ptr<Socket> host_socket_; | 53 const std::unique_ptr<Socket> host_socket_; |
| 53 // Used to notify the controller to exit. | 54 // Used to notify the controller to exit. |
| 54 const int exit_notifier_fd_; | 55 const int exit_notifier_fd_; |
| 55 // Lets ensure DeviceListener instances are deleted on the thread they were | 56 // Lets ensure DeviceListener instances are deleted on the thread they were |
| 56 // created on. | 57 // created on. |
| 57 const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_; | 58 const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_; |
| 58 ListenersMap listeners_; | 59 ListenersMap listeners_; |
| 59 | 60 |
| 60 //WeakPtrFactory's documentation says: | 61 //WeakPtrFactory's documentation says: |
| 61 // Member variables should appear before the WeakPtrFactory, to ensure | 62 // Member variables should appear before the WeakPtrFactory, to ensure |
| 62 // that any WeakPtrs to Controller are invalidated before its members | 63 // that any WeakPtrs to Controller are invalidated before its members |
| 63 // variable's destructors are executed, rendering them invalid. | 64 // variable's destructors are executed, rendering them invalid. |
| 64 base::WeakPtrFactory<DeviceController> weak_ptr_factory_; | 65 base::WeakPtrFactory<DeviceController> weak_ptr_factory_; |
| 65 | 66 |
| 66 DISALLOW_COPY_AND_ASSIGN(DeviceController); | 67 DISALLOW_COPY_AND_ASSIGN(DeviceController); |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 } // namespace forwarder | 70 } // namespace forwarder |
| 70 | 71 |
| 71 #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ | 72 #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_ |
| OLD | NEW |