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 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_ANDROID_FORWARDER2_DEVICE_LISTENER_H_ 6 #define TOOLS_ANDROID_FORWARDER2_DEVICE_LISTENER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "base/compiler_specific.h" 11 #include "base/logging.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/threading/thread.h" 15 #include "tools/android/forwarder2/pipe_notifier.h" 16 #include "tools/android/forwarder2/socket.h" 17 18 namespace base { 19 class SingleThreadTaskRunner; 20 } // namespace base 21 22 namespace forwarder2 { 23 24 class Forwarder; 25 26 // A DeviceListener instance is used in the device_forwarder program to bind to 27 // a specific device-side |port| and wait for client connections. When a 28 // connection happens, it informs the corresponding HostController instance 29 // running on the host, through |host_socket|. Then the class expects a call to 30 // its SetAdbDataSocket() method (performed by the device controller) once the 31 // host opened a new connection to the device. When this happens, a new internal 32 // Forwarder instance is started. 33 // Note that instances of this class are owned by the device controller which 34 // creates and destroys them on the same thread. In case an internal error 35 // happens on the DeviceListener's internal thread, the DeviceListener 36 // can also self-delete by executing the user-provided callback on the thread 37 // the DeviceListener was created on. 38 // Note that the DeviceListener's destructor joins its internal thread (i.e. 39 // waits for its completion) which means that the internal thread is guaranteed 40 // not to be running anymore once the object is deleted. 41 class DeviceListener { 42 public: 43 // Callback that is used for self-deletion as a way to let the device 44 // controller perform some additional cleanup work (e.g. removing the device 45 // listener instance from its internal map before deleting it). 46 typedef base::Callback<void (int /* listener port */)> DeleteCallback; 47 48 static scoped_ptr<DeviceListener> Create( 49 scoped_ptr<Socket> host_socket, 50 int port, 51 const DeleteCallback& delete_callback); 52 53 ~DeviceListener(); 54 55 void Start(); 56 57 void SetAdbDataSocket(scoped_ptr<Socket> adb_data_socket); 58 59 int listener_port() const { return listener_port_; } 60 61 private: 62 DeviceListener(scoped_ptr<PipeNotifier> pipe_notifier, 63 scoped_ptr<Socket> listener_socket, 64 scoped_ptr<Socket> host_socket, 65 int port, 66 const DeleteCallback& delete_callback); 67 68 // Pushes an AcceptClientOnInternalThread() task to the internal thread's 69 // message queue in order to wait for a new client soon. 70 void AcceptNextClientSoon(); 71 72 void AcceptClientOnInternalThread(); 73 74 void OnAdbDataSocketReceivedOnInternalThread( 75 scoped_ptr<Socket> adb_data_socket); 76 77 void SelfDelete(); 78 79 // Note that this can be called after the DeviceListener instance gets deleted 80 // which is why this method is static. 81 static void SelfDeleteOnDeletionTaskRunner( 82 const DeleteCallback& delete_callback, 83 int listener_port); 84 85 // Used for the listener thread to be notified on destruction. We have one 86 // notifier per Listener thread since each Listener thread may be requested to 87 // exit for different reasons independently from each other and independent 88 // from the main program, ex. when the host requests to forward/listen the 89 // same port again. Both the |host_socket_| and |listener_socket_| 90 // must share the same receiver file descriptor from |exit_notifier_| and it 91 // is set in the constructor. 92 const scoped_ptr<PipeNotifier> exit_notifier_; 93 // The local device listener socket for accepting connections from the local 94 // port (listener_port_). 95 const scoped_ptr<Socket> listener_socket_; 96 // The listener socket for sending control commands. 97 const scoped_ptr<Socket> host_socket_; 98 scoped_ptr<Socket> device_data_socket_; 99 // This is the adb connection to transport the actual data, used for creating 100 // the forwarder. Ownership transferred to the Forwarder. 101 scoped_ptr<Socket> adb_data_socket_; 102 const int listener_port_; 103 const DeleteCallback delete_callback_; 104 // Task runner used for deletion set at construction time (i.e. the object is 105 // deleted on the same thread it is created on). 106 scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_; 107 base::Thread thread_; 108 109 DISALLOW_COPY_AND_ASSIGN(DeviceListener); 110 }; 111 112 } // namespace forwarder 113 114 #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_LISTENER_H_ 115