device_controller.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_DEVICE_CONTROLLER_H_
  5. #define TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "base/macros.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "tools/android/forwarder2/socket.h"
  13. namespace base {
  14. class SingleThreadTaskRunner;
  15. } // namespace base
  16. namespace forwarder2 {
  17. class DeviceListener;
  18. // There is a single DeviceController per device_forwarder process, and it is in
  19. // charge of managing all active redirections on the device side (one
  20. // DeviceListener each).
  21. class DeviceController {
  22. public:
  23. static std::unique_ptr<DeviceController> Create(
  24. const std::string& adb_unix_socket,
  25. int exit_notifier_fd);
  26. ~DeviceController();
  27. void Start();
  28. private:
  29. DeviceController(std::unique_ptr<Socket> host_socket, int exit_notifier_fd);
  30. void AcceptHostCommandSoon();
  31. void AcceptHostCommandInternal();
  32. // Note that this can end up being called after the DeviceController is
  33. // destroyed which is why a weak pointer is used.
  34. static void DeleteListenerOnError(
  35. const base::WeakPtr<DeviceController>& device_controller_ptr,
  36. std::unique_ptr<DeviceListener> device_listener);
  37. const std::unique_ptr<Socket> host_socket_;
  38. // Used to notify the controller to exit.
  39. const int exit_notifier_fd_;
  40. // Lets ensure DeviceListener instances are deleted on the thread they were
  41. // created on.
  42. const scoped_refptr<base::SingleThreadTaskRunner> construction_task_runner_;
  43. std::unordered_map<int /* port */, std::unique_ptr<DeviceListener>>
  44. listeners_;
  45. //WeakPtrFactory's documentation says:
  46. // Member variables should appear before the WeakPtrFactory, to ensure
  47. // that any WeakPtrs to Controller are invalidated before its members
  48. // variable's destructors are executed, rendering them invalid.
  49. base::WeakPtrFactory<DeviceController> weak_ptr_factory_{this};
  50. DISALLOW_COPY_AND_ASSIGN(DeviceController);
  51. };
  52. } // namespace forwarder
  53. #endif // TOOLS_ANDROID_FORWARDER2_DEVICE_CONTROLLER_H_