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