service_provider_impl.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <memory>
  13. #include <string>
  14. #include "base/base_export.h"
  15. #include "base/callback.h"
  16. namespace sys {
  17. class OutgoingDirectory;
  18. } // namespace sys
  19. namespace base {
  20. // Implementation of the legacy sys.ServiceProvider interface which delegates
  21. // requests to an underlying fuchsia.io.Directory of services.
  22. // TODO(https://crbug.com/1065707): Remove this when it is no longer required
  23. // by //fuchsia_web/runners/common/modular/agent_impl.h and
  24. // //fuchsia_web/runners/web/web_runner_smoke_test.cc.
  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(const ServiceProviderImpl&) = delete;
  34. ServiceProviderImpl& operator=(const ServiceProviderImpl&) = delete;
  35. ~ServiceProviderImpl() override;
  36. // Binds a |request| from a new client to be serviced by this ServiceProvider.
  37. void AddBinding(
  38. fidl::InterfaceRequest<::fuchsia::sys::ServiceProvider> request);
  39. // Sets a Closure to be invoked when the last client disconnects.
  40. void SetOnLastClientDisconnectedClosure(
  41. base::OnceClosure on_last_client_disconnected);
  42. // Returns true if one or more clients are connected.
  43. bool has_clients() const { return bindings_.size() != 0; }
  44. private:
  45. // fuchsia::sys::ServiceProvider implementation.
  46. void ConnectToService(std::string service_name,
  47. zx::channel client_handle) override;
  48. void OnBindingSetEmpty();
  49. const sys::ServiceDirectory directory_;
  50. fidl::BindingSet<::fuchsia::sys::ServiceProvider> bindings_;
  51. base::OnceClosure on_last_client_disconnected_;
  52. };
  53. } // namespace base
  54. #endif // BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_