| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "tools/android/forwarder2/pipe_notifier.h" | 15 #include "tools/android/forwarder2/pipe_notifier.h" |
| 16 #include "tools/android/forwarder2/socket.h" | 16 #include "tools/android/forwarder2/socket.h" |
| 17 | 17 |
| 18 namespace forwarder2 { | 18 namespace forwarder2 { |
| 19 | 19 |
| 20 class HostProxy; | 20 // This class partners with DeviceController and has the same lifetime and |
| 21 | 21 // threading characteristics as DeviceListener. In a nutshell, this class |
| 22 // operates on its own thread and is destroyed on the thread it was constructed |
| 23 // on. The class' deletion can happen in two different ways: |
| 24 // - Its destructor was called by its owner (HostControllersManager). |
| 25 // - Its internal thread requested self-deletion after an error happened. In |
| 26 // this case the owner (HostControllersManager) is notified on the |
| 27 // construction thread through the provided DeletionCallback invoked with the |
| 28 // HostController instance. When this callback is invoked, it's up to the |
| 29 // owner to delete the instance. |
| 22 class HostController { | 30 class HostController { |
| 23 public: | 31 public: |
| 24 // Callback used for self-deletion that lets the client perform some cleanup | 32 // Callback used for self-deletion that lets the client perform some cleanup |
| 25 // work before deleting the HostController instance. | 33 // work before deleting the HostController instance. |
| 26 typedef base::Callback<void (HostController*)> DeleteCallback; | 34 typedef base::Callback<void (scoped_ptr<HostController>)> DeletionCallback; |
| 27 | 35 |
| 28 // If |device_port| is zero, it dynamically allocates a port. | 36 // If |device_port| is zero then a dynamic port is allocated (and retrievable |
| 29 HostController(int device_port, | 37 // through device_port() below). |
| 30 const std::string& forward_to_host, | 38 static scoped_ptr<HostController> Create( |
| 31 int forward_to_host_port, | 39 int device_port, |
| 32 int adb_port, | 40 int host_port, |
| 33 int exit_notifier_fd, | 41 int adb_port, |
| 34 const DeleteCallback& delete_callback); | 42 int exit_notifier_fd, |
| 43 const DeletionCallback& deletion_callback); |
| 35 | 44 |
| 36 ~HostController(); | 45 ~HostController(); |
| 37 | 46 |
| 38 // Connects to the device forwarder app and sets the |device_port_| to the | |
| 39 // dynamically allocated port (when the provided |device_port| is zero). | |
| 40 // Returns true on success. Clients must call Connect() before calling | |
| 41 // Start(). | |
| 42 bool Connect(); | |
| 43 | |
| 44 // Starts the internal controller thread. | 47 // Starts the internal controller thread. |
| 45 void Start(); | 48 void Start(); |
| 46 | 49 |
| 47 int adb_port() const { return adb_port_; } | 50 int adb_port() const { return adb_port_; } |
| 48 | 51 |
| 49 // Gets the current device allocated port. Must be called after Connect(). | |
| 50 int device_port() const { return device_port_; } | 52 int device_port() const { return device_port_; } |
| 51 | 53 |
| 52 private: | 54 private: |
| 53 void ThreadHandler(); | 55 HostController(int device_port, |
| 56 int host_port, |
| 57 int adb_port, |
| 58 int exit_notifier_fd, |
| 59 const DeletionCallback& deletion_callback, |
| 60 scoped_ptr<Socket> adb_control_socket, |
| 61 scoped_ptr<PipeNotifier> delete_controller_notifier); |
| 54 | 62 |
| 55 void StartForwarder(scoped_ptr<Socket> host_server_data_socket_ptr); | 63 void ReadNextCommandSoon(); |
| 64 void ReadCommandOnInternalThread(); |
| 65 |
| 66 void StartForwarder(scoped_ptr<Socket> host_server_data_socket); |
| 56 | 67 |
| 57 // Helper method that creates a socket and adds the appropriate event file | 68 // Helper method that creates a socket and adds the appropriate event file |
| 58 // descriptors. | 69 // descriptors. |
| 59 scoped_ptr<Socket> CreateSocket(); | 70 scoped_ptr<Socket> CreateSocket(); |
| 60 | 71 |
| 61 void SelfDelete(); | 72 void SelfDelete(); |
| 62 | 73 |
| 63 Socket adb_control_socket_; | 74 static void SelfDeleteOnDeletionTaskRunner( |
| 64 int device_port_; | 75 const DeletionCallback& deletion_callback, |
| 65 const std::string forward_to_host_; | 76 scoped_ptr<HostController> controller); |
| 66 const int forward_to_host_port_; | 77 |
| 78 const int device_port_; |
| 79 const int host_port_; |
| 67 const int adb_port_; | 80 const int adb_port_; |
| 68 // Used to notify the controller when the process is killed. | 81 // Used to notify the controller when the process is killed. |
| 69 const int global_exit_notifier_fd_; | 82 const int global_exit_notifier_fd_; |
| 70 // Used to let the client delete the instance in case an error happened. | 83 // Used to let the client delete the instance in case an error happened. |
| 71 const DeleteCallback delete_callback_; | 84 const DeletionCallback deletion_callback_; |
| 85 scoped_ptr<Socket> adb_control_socket_; |
| 86 scoped_ptr<PipeNotifier> delete_controller_notifier_; |
| 72 // Used to cancel the pending blocking IO operations when the host controller | 87 // Used to cancel the pending blocking IO operations when the host controller |
| 73 // instance is deleted. | 88 // instance is deleted. |
| 74 PipeNotifier delete_controller_notifier_; | 89 // Task runner used for deletion set at construction time (i.e. the object is |
| 75 bool ready_; | 90 // deleted on the same thread it is created on). |
| 91 const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_; |
| 76 base::Thread thread_; | 92 base::Thread thread_; |
| 77 | 93 |
| 78 DISALLOW_COPY_AND_ASSIGN(HostController); | 94 DISALLOW_COPY_AND_ASSIGN(HostController); |
| 79 }; | 95 }; |
| 80 | 96 |
| 81 } // namespace forwarder2 | 97 } // namespace forwarder2 |
| 82 | 98 |
| 83 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ | 99 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_ |
| OLD | NEW |