service_provider_impl.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 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_SERVICE_PROVIDER_IMPL_H_
  5. #define BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_
  6. #include <fuchsia/io/cpp/fidl.h>
  7. #include <fuchsia/sys/cpp/fidl.h>
  8. #include <lib/fidl/cpp/binding_set.h>
  9. #include <lib/fidl/cpp/interface_handle.h>
  10. #include <lib/sys/cpp/component_context.h>
  11. #include <lib/zx/channel.h>
  12. #include <string>
  13. #include "base/base_export.h"
  14. #include "base/callback.h"
  15. #include "base/macros.h"
  16. namespace sys {
  17. class OutgoingDirectory;
  18. } // namespace sys
  19. namespace base {
  20. namespace fuchsia {
  21. // Implementation of the legacy sys.ServiceProvider interface which delegates
  22. // requests to an underlying fuchsia.io.Directory of services.
  23. // TODO(https://crbug.com/920920): Remove this when ServiceProvider is gone.
  24. class BASE_EXPORT ServiceProviderImpl : public ::fuchsia::sys::ServiceProvider {
  25. public:
  26. // Constructor that creates ServiceProvider for public services in the
  27. // specified OutgoingDirectory.
  28. static std::unique_ptr<ServiceProviderImpl> CreateForOutgoingDirectory(
  29. sys::OutgoingDirectory* outgoing_directory);
  30. explicit ServiceProviderImpl(
  31. fidl::InterfaceHandle<::fuchsia::io::Directory> service_directory);
  32. ~ServiceProviderImpl() override;
  33. // Binds a |request| from a new client to be serviced by this ServiceProvider.
  34. void AddBinding(
  35. fidl::InterfaceRequest<::fuchsia::sys::ServiceProvider> request);
  36. // Sets a Closure to be invoked when the last client disconnects.
  37. void SetOnLastClientDisconnectedClosure(
  38. base::OnceClosure on_last_client_disconnected);
  39. // Returns true if one or more clients are connected.
  40. bool has_clients() const { return bindings_.size() != 0; }
  41. private:
  42. // fuchsia::sys::ServiceProvider implementation.
  43. void ConnectToService(std::string service_name,
  44. zx::channel client_handle) override;
  45. void OnBindingSetEmpty();
  46. const sys::ServiceDirectory directory_;
  47. fidl::BindingSet<::fuchsia::sys::ServiceProvider> bindings_;
  48. base::OnceClosure on_last_client_disconnected_;
  49. DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
  50. };
  51. } // namespace fuchsia
  52. } // namespace base
  53. #endif // BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_