scoped_service_publisher.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2020 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_PUBLISHER_H_
  5. #define BASE_FUCHSIA_SCOPED_SERVICE_PUBLISHER_H_
  6. #include <lib/async/dispatcher.h>
  7. #include <lib/fidl/cpp/interface_request.h>
  8. #include <lib/sys/cpp/outgoing_directory.h>
  9. #include <lib/vfs/cpp/pseudo_dir.h>
  10. #include <lib/vfs/cpp/service.h>
  11. #include <lib/zx/channel.h>
  12. #include "base/base_export.h"
  13. #include "base/macros.h"
  14. namespace base {
  15. namespace fuchsia {
  16. template <typename Interface>
  17. class BASE_EXPORT ScopedServicePublisher {
  18. public:
  19. // Publishes a public service in the specified |outgoing_directory|.
  20. // |outgoing_directory| and |handler| must outlive the binding.
  21. ScopedServicePublisher(sys::OutgoingDirectory* outgoing_directory,
  22. fidl::InterfaceRequestHandler<Interface> handler)
  23. : ScopedServicePublisher(outgoing_directory->GetOrCreateDirectory("svc"),
  24. std::move(handler)) {}
  25. // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and
  26. // |handler| must outlive the binding.
  27. ScopedServicePublisher(vfs::PseudoDir* pseudo_dir,
  28. fidl::InterfaceRequestHandler<Interface> handler)
  29. : pseudo_dir_(pseudo_dir) {
  30. pseudo_dir_->AddEntry(Interface::Name_,
  31. std::make_unique<vfs::Service>(std::move(handler)));
  32. }
  33. ~ScopedServicePublisher() { pseudo_dir_->RemoveEntry(Interface::Name_); }
  34. private:
  35. vfs::PseudoDir* const pseudo_dir_ = nullptr;
  36. DISALLOW_COPY_AND_ASSIGN(ScopedServicePublisher);
  37. };
  38. } // namespace fuchsia
  39. } // namespace base
  40. #endif // BASE_FUCHSIA_SCOPED_SERVICE_PUBLISHER_H_