• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/memory/weak_ptr.h"
15 #include "base/threading/thread.h"
16 #include "tools/android/forwarder2/forwarders_manager.h"
17 #include "tools/android/forwarder2/pipe_notifier.h"
18 #include "tools/android/forwarder2/self_deleter_helper.h"
19 #include "tools/android/forwarder2/socket.h"
20 
21 namespace forwarder2 {
22 
23 // This class partners with DeviceController and has the same lifetime and
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
26 // on. The class' deletion can happen in two different ways:
27 // - Its destructor was called by its owner (HostControllersManager).
28 // - Its internal thread requested self-deletion after an error happened. In
29 //   this case the owner (HostControllersManager) is notified on the
30 //   construction thread through the provided ErrorCallback invoked with the
31 //   HostController instance. When this callback is invoked, it's up to the
32 //   owner to delete the instance.
33 class HostController {
34  public:
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.
37   typedef base::Callback<void (scoped_ptr<HostController>)> ErrorCallback;
38 
39   // If |device_port| is zero then a dynamic port is allocated (and retrievable
40   // through device_port() below).
41   static scoped_ptr<HostController> Create(int device_port,
42                                            int host_port,
43                                            int adb_port,
44                                            int exit_notifier_fd,
45                                            const ErrorCallback& error_callback);
46 
47   ~HostController();
48 
49   // Starts the internal controller thread.
50   void Start();
51 
adb_port()52   int adb_port() const { return adb_port_; }
53 
device_port()54   int device_port() const { return device_port_; }
55 
56  private:
57   HostController(int device_port,
58                  int host_port,
59                  int adb_port,
60                  int exit_notifier_fd,
61                  const ErrorCallback& error_callback,
62                  scoped_ptr<Socket> adb_control_socket,
63                  scoped_ptr<PipeNotifier> delete_controller_notifier);
64 
65   void ReadNextCommandSoon();
66   void ReadCommandOnInternalThread();
67 
68   void StartForwarder(scoped_ptr<Socket> host_server_data_socket);
69 
70   // Note that this gets also called when ~HostController() is invoked.
71   void OnInternalThreadError();
72 
73   void UnmapPortOnDevice();
74 
75   SelfDeleterHelper<HostController> self_deleter_helper_;
76   const int device_port_;
77   const int host_port_;
78   const int adb_port_;
79   // Used to notify the controller when the process is killed.
80   const int global_exit_notifier_fd_;
81   scoped_ptr<Socket> adb_control_socket_;
82   // Used to cancel the pending blocking IO operations when the host controller
83   // instance is deleted.
84   scoped_ptr<PipeNotifier> delete_controller_notifier_;
85   // 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   const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_;
88   base::Thread thread_;
89   ForwardersManager forwarders_manager_;
90 
91   DISALLOW_COPY_AND_ASSIGN(HostController);
92 };
93 
94 }  // namespace forwarder2
95 
96 #endif  // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
97