broker.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2016 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 MOJO_CORE_BROKER_H_
  5. #define MOJO_CORE_BROKER_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "base/memory/writable_shared_memory_region.h"
  8. #include "base/synchronization/lock.h"
  9. #include "mojo/public/cpp/platform/platform_channel_endpoint.h"
  10. #include "mojo/public/cpp/platform/platform_handle.h"
  11. namespace mojo {
  12. namespace core {
  13. // The Broker is a channel to the broker process, which allows synchronous IPCs
  14. // to fulfill shared memory allocation requests on some platforms.
  15. class Broker {
  16. public:
  17. // Note: If |wait_for_channel_handle| is |true|, this constructor blocks the
  18. // calling thread until it reads first message from |handle|, which must
  19. // contain another PlatformHandle for a NodeChannel.
  20. //
  21. // Otherwise, no initialization message is expected and this will not wait for
  22. // one.
  23. Broker(PlatformHandle handle, bool wait_for_channel_handle);
  24. Broker(const Broker&) = delete;
  25. Broker& operator=(const Broker&) = delete;
  26. ~Broker();
  27. // Returns the platform handle that should be used to establish a NodeChannel
  28. // to the process which is inviting us to join its network. This is the first
  29. // handle read off the Broker channel upon construction.
  30. PlatformChannelEndpoint GetInviterEndpoint();
  31. // Request a shared buffer from the broker process. Blocks the current thread.
  32. base::WritableSharedMemoryRegion GetWritableSharedMemoryRegion(
  33. size_t num_bytes);
  34. private:
  35. // Handle to the broker process, used for synchronous IPCs.
  36. PlatformHandle sync_channel_;
  37. // Channel endpoint connected to the inviter process. Received in the
  38. // first message over |sync_channel_|.
  39. PlatformChannelEndpoint inviter_endpoint_;
  40. // Lock to only allow one sync message at a time. This avoids having to deal
  41. // with message ordering since we can only have one request at a time
  42. // in-flight.
  43. base::Lock lock_;
  44. };
  45. } // namespace core
  46. } // namespace mojo
  47. #endif // MOJO_CORE_BROKER_H_