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.
5 #ifndef TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
6 #define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
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/socket.h"
18 namespace forwarder2 {
20 // This class partners with DeviceController and has the same lifetime and
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.
30 class HostController {
32 // Callback used for self-deletion that lets the client perform some cleanup
33 // work before deleting the HostController instance.
34 typedef base::Callback<void (scoped_ptr<HostController>)> DeletionCallback;
36 // If |device_port| is zero then a dynamic port is allocated (and retrievable
37 // through device_port() below).
38 static scoped_ptr<HostController> Create(
43 const DeletionCallback& deletion_callback);
47 // Starts the internal controller thread.
50 int adb_port() const { return adb_port_; }
52 int device_port() const { return device_port_; }
55 HostController(int device_port,
59 const DeletionCallback& deletion_callback,
60 scoped_ptr<Socket> adb_control_socket,
61 scoped_ptr<PipeNotifier> delete_controller_notifier);
63 void ReadNextCommandSoon();
64 void ReadCommandOnInternalThread();
66 void StartForwarder(scoped_ptr<Socket> host_server_data_socket);
68 // Helper method that creates a socket and adds the appropriate event file
70 scoped_ptr<Socket> CreateSocket();
74 static void SelfDeleteOnDeletionTaskRunner(
75 const DeletionCallback& deletion_callback,
76 scoped_ptr<HostController> controller);
78 const int device_port_;
81 // Used to notify the controller when the process is killed.
82 const int global_exit_notifier_fd_;
83 // Used to let the client delete the instance in case an error happened.
84 const DeletionCallback deletion_callback_;
85 scoped_ptr<Socket> adb_control_socket_;
86 scoped_ptr<PipeNotifier> delete_controller_notifier_;
87 // Used to cancel the pending blocking IO operations when the host controller
88 // instance is deleted.
89 // Task runner used for deletion set at construction time (i.e. the object is
90 // deleted on the same thread it is created on).
91 const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_;
94 DISALLOW_COPY_AND_ASSIGN(HostController);
97 } // namespace forwarder2
99 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_