scoped_service_binding.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018 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 BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
  5. #define BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
  6. #include <lib/fidl/cpp/binding.h>
  7. #include <lib/fidl/cpp/binding_set.h>
  8. #include <lib/fidl/cpp/interface_request.h>
  9. #include <lib/zx/channel.h>
  10. #include "base/base_export.h"
  11. #include "base/callback.h"
  12. #include "base/fuchsia/scoped_service_publisher.h"
  13. #include "base/optional.h"
  14. namespace sys {
  15. class OutgoingDirectory;
  16. } // namespace sys
  17. namespace vfs {
  18. class PseudoDir;
  19. } // namespace vfs
  20. namespace base {
  21. namespace fuchsia {
  22. template <typename Interface>
  23. class BASE_EXPORT ScopedServiceBinding {
  24. public:
  25. // Published a public service in the specified |outgoing_directory|.
  26. // |outgoing_directory| and |impl| must outlive the binding.
  27. ScopedServiceBinding(sys::OutgoingDirectory* outgoing_directory,
  28. Interface* impl)
  29. : publisher_(outgoing_directory, bindings_.GetHandler(impl)) {}
  30. // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl|
  31. // must outlive the binding.
  32. ScopedServiceBinding(vfs::PseudoDir* pseudo_dir, Interface* impl)
  33. : publisher_(pseudo_dir, bindings_.GetHandler(impl)) {}
  34. ~ScopedServiceBinding() = default;
  35. // |on_last_client_callback| will be called every time the number of connected
  36. // clients drops to 0.
  37. void SetOnLastClientCallback(base::RepeatingClosure on_last_client_callback) {
  38. bindings_.set_empty_set_handler(
  39. [callback = std::move(on_last_client_callback)] { callback.Run(); });
  40. }
  41. bool has_clients() const { return bindings_.size() != 0; }
  42. private:
  43. fidl::BindingSet<Interface> bindings_;
  44. ScopedServicePublisher<Interface> publisher_;
  45. DISALLOW_COPY_AND_ASSIGN(ScopedServiceBinding);
  46. };
  47. // Scoped service binding which allows only a single client to be connected
  48. // at any time. By default a new connection will disconnect an existing client.
  49. enum class ScopedServiceBindingPolicy {
  50. kPreferNew,
  51. kPreferExisting,
  52. kConnectOnce
  53. };
  54. template <typename Interface,
  55. ScopedServiceBindingPolicy Policy =
  56. ScopedServiceBindingPolicy::kPreferNew>
  57. class BASE_EXPORT ScopedSingleClientServiceBinding {
  58. public:
  59. // |outgoing_directory| and |impl| must outlive the binding.
  60. ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory,
  61. Interface* impl)
  62. : binding_(impl) {
  63. publisher_.emplace(
  64. outgoing_directory,
  65. fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient));
  66. binding_.set_error_handler(fit::bind_member(
  67. this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
  68. }
  69. ~ScopedSingleClientServiceBinding() = default;
  70. typename Interface::EventSender_& events() { return binding_.events(); }
  71. // |on_last_client_callback| will be called the first time a client
  72. // disconnects. It is still possible for a client to connect after that point
  73. // if Policy is kPreferNew of kPreferExisting.
  74. void SetOnLastClientCallback(base::OnceClosure on_last_client_callback) {
  75. on_last_client_callback_ = std::move(on_last_client_callback);
  76. }
  77. bool has_clients() const { return binding_.is_bound(); }
  78. private:
  79. void BindClient(fidl::InterfaceRequest<Interface> request) {
  80. if (Policy == ScopedServiceBindingPolicy::kPreferExisting &&
  81. binding_.is_bound()) {
  82. return;
  83. }
  84. binding_.Bind(std::move(request));
  85. if (Policy == ScopedServiceBindingPolicy::kConnectOnce) {
  86. publisher_.reset();
  87. }
  88. }
  89. void OnBindingEmpty(zx_status_t status) {
  90. if (on_last_client_callback_) {
  91. std::move(on_last_client_callback_).Run();
  92. }
  93. }
  94. fidl::Binding<Interface> binding_;
  95. base::Optional<ScopedServicePublisher<Interface>> publisher_;
  96. base::OnceClosure on_last_client_callback_;
  97. DISALLOW_COPY_AND_ASSIGN(ScopedSingleClientServiceBinding);
  98. };
  99. } // namespace fuchsia
  100. } // namespace base
  101. #endif // BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_