host_controller.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
  5. #define TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/macros.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/thread.h"
  13. #include "tools/android/forwarder2/forwarders_manager.h"
  14. #include "tools/android/forwarder2/pipe_notifier.h"
  15. #include "tools/android/forwarder2/self_deleter_helper.h"
  16. #include "tools/android/forwarder2/socket.h"
  17. namespace forwarder2 {
  18. // This class partners with DeviceController and has the same lifetime and
  19. // threading characteristics as DeviceListener. In a nutshell, this class
  20. // operates on its own thread and is destroyed on the thread it was constructed
  21. // on. The class' deletion can happen in two different ways:
  22. // - Its destructor was called by its owner (HostControllersManager).
  23. // - Its internal thread requested self-deletion after an error happened. In
  24. // this case the owner (HostControllersManager) is notified on the
  25. // construction thread through the provided ErrorCallback invoked with the
  26. // HostController instance. When this callback is invoked, it's up to the
  27. // owner to delete the instance.
  28. class HostController {
  29. public:
  30. // Callback used for self-deletion when an error happens so that the client
  31. // can perform some cleanup work before deleting the HostController instance.
  32. using ErrorCallback =
  33. base::OnceCallback<void(std::unique_ptr<HostController>)>;
  34. // If |device_port| is zero then a dynamic port is allocated (and retrievable
  35. // through device_port() below).
  36. static std::unique_ptr<HostController> Create(
  37. const std::string& device_serial,
  38. int device_port,
  39. int host_port,
  40. int adb_port,
  41. int exit_notifier_fd,
  42. ErrorCallback error_callback);
  43. ~HostController();
  44. // Starts the internal controller thread.
  45. void Start();
  46. int adb_port() const { return adb_port_; }
  47. int device_port() const { return device_port_; }
  48. private:
  49. HostController(const std::string& device_serial,
  50. int device_port,
  51. int host_port,
  52. int adb_port,
  53. ErrorCallback error_callback,
  54. std::unique_ptr<Socket> adb_control_socket,
  55. std::unique_ptr<PipeNotifier> delete_controller_notifier);
  56. void ReadNextCommandSoon();
  57. void ReadCommandOnInternalThread();
  58. bool StartForwarder(std::unique_ptr<Socket> host_server_data_socket);
  59. // Note that this gets also called when ~HostController() is invoked.
  60. void OnInternalThreadError();
  61. void UnmapPortOnDevice();
  62. SelfDeleterHelper<HostController> self_deleter_helper_;
  63. const std::string device_serial_;
  64. const int device_port_;
  65. const int host_port_;
  66. const int adb_port_;
  67. std::unique_ptr<Socket> adb_control_socket_;
  68. // Used to cancel the pending blocking IO operations when the host controller
  69. // instance is deleted.
  70. std::unique_ptr<PipeNotifier> delete_controller_notifier_;
  71. // Task runner used for deletion set at deletion time (i.e. the object is
  72. // deleted on the same thread it is created on).
  73. const scoped_refptr<base::SingleThreadTaskRunner> deletion_task_runner_;
  74. base::Thread thread_;
  75. ForwardersManager forwarders_manager_;
  76. DISALLOW_COPY_AND_ASSIGN(HostController);
  77. };
  78. } // namespace forwarder2
  79. #endif // TOOLS_ANDROID_FORWARDER2_HOST_CONTROLLER_H_