| 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_HOST_CONTROLLER_H_ | 5 #ifndef TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ |
| 6 #define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ | 6 #define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "tools/android/forwarder2/forwarders_manager.h" | 16 #include "tools/android/forwarder2/forwarders_manager.h" |
| 17 #include "tools/android/forwarder2/pipe_notifier.h" | 17 #include "tools/android/forwarder2/pipe_notifier.h" |
| 18 #include "tools/android/forwarder2/self_deleter_helper.h" | 18 #include "tools/android/forwarder2/self_deleter_helper.h" |
| 19 #include "tools/android/forwarder2/socket.h" | 19 #include "tools/android/forwarder2/socket.h" |
| 20 | 20 |
| 21 namespace forwarder2 { | 21 namespace forwarder2 { |
| 22 | 22 |
| 23 // This class partners with DeviceController and has the same lifetime and | 23 // This class partners with DeviceController and has the same lifetime and |
| 24 // threading characteristics as DeviceListener. In a nutshell, this class | 24 // threading characteristics as DeviceListener. In a nutshell, this class |
| 25 // operates on its own thread and is destroyed on the thread it was constructed | 25 // operates on its own thread and is destroyed on the thread it was constructed |
| 26 // on. The class' deletion can happen in two different ways: | 26 // on. The class' deletion can happen in two different ways: |
| 27 // - Its destructor was called by its owner (HostControllersManager). | 27 // - Its destructor was called by its owner (HostControllersManager). |
| 28 // - Its internal thread requested self-deletion after an error happened. In | 28 // - Its internal thread requested self-deletion after an error happened. In |
| 29 // this case the owner (HostControllersManager) is notified on the | 29 // this case the owner (HostControllersManager) is notified on the |
| 30 // construction thread through the provided ErrorCallback invoked with the | 30 // construction thread through the provided ErrorCallback invoked with the |
| 31 // HostController instance. When this callback is invoked, it's up to the | 31 // HostController instance. When this callback is invoked, it's up to the |
| 32 // owner to delete the instance. | 32 // owner to delete the instance. |
| 33 class HostController { | 33 class HostController { |
| 34 public: | 34 public: |
| 35 // Callback used for self-deletion when an error happens so that the client | 35 // Callback used for self-deletion when an error happens so that the client |
| 36 // can perform some cleanup work before deleting the HostController instance. | 36 // can perform some cleanup work before deleting the HostController instance. |
| 37 typedef base::Callback<void (scoped_ptr<HostController>)> ErrorCallback; | 37 typedef base::Callback<void(std::unique_ptr<HostController>)> ErrorCallback; |
| 38 | 38 |
| 39 // If |device_port| is zero then a dynamic port is allocated (and retrievable | 39 // If |device_port| is zero then a dynamic port is allocated (and retrievable |
| 40 // through device_port() below). | 40 // through device_port() below). |
| 41 static scoped_ptr<HostController> Create(int device_port, | 41 static std::unique_ptr<HostController> Create( |
| 42 int host_port, | 42 int device_port, |
| 43 int adb_port, | 43 int host_port, |
| 44 int exit_notifier_fd, | 44 int adb_port, |
| 45 const ErrorCallback& error_callback); | 45 int exit_notifier_fd, |
| 46 const ErrorCallback& error_callback); |
| 46 | 47 |
| 47 ~HostController(); | 48 ~HostController(); |
| 48 | 49 |
| 49 // Starts the internal controller thread. | 50 // Starts the internal controller thread. |
| 50 void Start(); | 51 void Start(); |
| 51 | 52 |
| 52 int adb_port() const { return adb_port_; } | 53 int adb_port() const { return adb_port_; } |
| 53 | 54 |
| 54 int device_port() const { return device_port_; } | 55 int device_port() const { return device_port_; } |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 HostController(int device_port, | 58 HostController(int device_port, |
| 58 int host_port, | 59 int host_port, |
| 59 int adb_port, | 60 int adb_port, |
| 60 int exit_notifier_fd, | 61 int exit_notifier_fd, |
| 61 const ErrorCallback& error_callback, | 62 const ErrorCallback& error_callback, |
| 62 scoped_ptr<Socket> adb_control_socket, | 63 std::unique_ptr<Socket> adb_control_socket, |
| 63 scoped_ptr<PipeNotifier> delete_controller_notifier); | 64 std::unique_ptr<PipeNotifier> delete_controller_notifier); |
| 64 | 65 |
| 65 void ReadNextCommandSoon(); | 66 void ReadNextCommandSoon(); |
| 66 void ReadCommandOnInternalThread(); | 67 void ReadCommandOnInternalThread(); |
| 67 | 68 |
| 68 void StartForwarder(scoped_ptr<Socket> host_server_data_socket); | 69 void StartForwarder(std::unique_ptr<Socket> host_server_data_socket); |
| 69 | 70 |
| 70 // Note that this gets also called when ~HostController() is invoked. | 71 // Note that this gets also called when ~HostController() is invoked. |
| 71 void OnInternalThreadError(); | 72 void OnInternalThreadError(); |
| 72 | 73 |
| 73 void UnmapPortOnDevice(); | 74 void UnmapPortOnDevice(); |
| 74 | 75 |
| 75 SelfDeleterHelper<HostController> self_deleter_helper_; | 76 SelfDeleterHelper<HostController> self_deleter_helper_; |
| 76 const int device_port_; | 77 const int device_port_; |
| 77 const int host_port_; | 78 const int host_port_; |
| 78 const int adb_port_; | 79 const int adb_port_; |
| 79 // Used to notify the controller when the process is killed. | 80 // Used to notify the controller when the process is killed. |
| 80 const int global_exit_notifier_fd_; | 81 const int global_exit_notifier_fd_; |
| 81 scoped_ptr<Socket> adb_control_socket_; | 82 std::unique_ptr<Socket> adb_control_socket_; |
| 82 // Used to cancel the pending blocking IO operations when the host controller | 83 // Used to cancel the pending blocking IO operations when the host controller |
| 83 // instance is deleted. | 84 // instance is deleted. |
| 84 scoped_ptr<PipeNotifier> delete_controller_notifier_; | 85 std::unique_ptr<PipeNotifier> delete_controller_notifier_; |
| 85 // Task runner used for deletion set at deletion time (i.e. the object is | 86 // Task runner used for deletion set at deletion time (i.e. the object is |
| 86 // deleted on the same thread it is created on). | 87 // deleted on the same thread it is created on). |
| 87 const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_; | 88 const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_; |
| 88 base::Thread thread_; | 89 base::Thread thread_; |
| 89 ForwardersManager forwarders_manager_; | 90 ForwardersManager forwarders_manager_; |
| 90 | 91 |
| 91 DISALLOW_COPY_AND_ASSIGN(HostController); | 92 DISALLOW_COPY_AND_ASSIGN(HostController); |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 } // namespace forwarder2 | 95 } // namespace forwarder2 |
| 95 | 96 |
| 96 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ | 97 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ |
| OLD | NEW |