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/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"
21 namespace forwarder2
{
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
{
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
;
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
,
45 const ErrorCallback
& error_callback
);
49 // Starts the internal controller thread.
52 int adb_port() const { return adb_port_
; }
54 int device_port() const { return device_port_
; }
57 HostController(int device_port
,
61 const ErrorCallback
& error_callback
,
62 scoped_ptr
<Socket
> adb_control_socket
,
63 scoped_ptr
<PipeNotifier
> delete_controller_notifier
);
65 void ReadNextCommandSoon();
66 void ReadCommandOnInternalThread();
68 void StartForwarder(scoped_ptr
<Socket
> host_server_data_socket
);
70 // Note that this gets also called when ~HostController() is invoked.
71 void OnInternalThreadError();
73 void UnmapPortOnDevice();
75 SelfDeleterHelper
<HostController
> self_deleter_helper_
;
76 const int device_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_
;
89 ForwardersManager forwarders_manager_
;
91 DISALLOW_COPY_AND_ASSIGN(HostController
);
94 } // namespace forwarder2
96 #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_