service_provider_impl.h 2.3 KB

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