channel_linux.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 MOJO_CORE_CHANNEL_LINUX_H_
  5. #define MOJO_CORE_CHANNEL_LINUX_H_
  6. #include <atomic>
  7. #include <memory>
  8. #include "build/build_config.h"
  9. #include "mojo/core/channel_posix.h"
  10. namespace mojo {
  11. namespace core {
  12. class DataAvailableNotifier;
  13. // ChannelLinux is a specialization of ChannelPosix which has support for shared
  14. // memory via Mojo channel upgrades. By default on Linux, CrOS, and Android
  15. // every channel will be of type ChannelLinux which can be upgraded at runtime
  16. // to take advantage of shared memory when all required kernel features are
  17. // present.
  18. class MOJO_SYSTEM_IMPL_EXPORT ChannelLinux : public ChannelPosix {
  19. public:
  20. ChannelLinux(Delegate* delegate,
  21. ConnectionParams connection_params,
  22. HandlePolicy handle_policy,
  23. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
  24. ChannelLinux(const ChannelLinux&) = delete;
  25. ChannelLinux& operator=(const ChannelLinux&) = delete;
  26. // KernelSupportsUpgradeRequirements will return true if the kernel supports
  27. // the features necessary to use an upgrade channel. How the channel will be
  28. // upgraded is an implementation detail and this just tells the caller that
  29. // calling Channel::UpgradeChannel() will have some effect.
  30. static bool KernelSupportsUpgradeRequirements();
  31. // Will return true if at least one feature that is available via upgrade is
  32. // enabled.
  33. static bool UpgradesEnabled();
  34. // SetSharedMemParams will control whether shared memory is used for this
  35. // channel.
  36. static void SetSharedMemParameters(bool enabled,
  37. uint32_t num_pages,
  38. bool use_zero_on_wake);
  39. // ChannelPosix impl:
  40. void Write(MessagePtr message) override;
  41. void OfferSharedMemUpgrade();
  42. bool OnControlMessage(Message::MessageType message_type,
  43. const void* payload,
  44. size_t payload_size,
  45. std::vector<PlatformHandle> handles) override;
  46. void OnWriteError(Error error) override;
  47. void StartOnIOThread() override;
  48. void ShutDownOnIOThread() override;
  49. private:
  50. ~ChannelLinux() override;
  51. class SharedBuffer;
  52. void OfferSharedMemUpgradeInternal();
  53. void SharedMemReadReady();
  54. // We only offer once, we use an atomic flag to guarantee no races to offer.
  55. std::atomic_flag offered_{false};
  56. // This flag keeps track of whether or not we've established a shared memory
  57. // channel with the remote. If false we always fall back to the PosixChannel
  58. // (socket).
  59. std::atomic_bool shared_mem_writer_{false};
  60. std::unique_ptr<DataAvailableNotifier> write_notifier_;
  61. std::unique_ptr<SharedBuffer> write_buffer_;
  62. std::unique_ptr<DataAvailableNotifier> read_notifier_;
  63. std::unique_ptr<SharedBuffer> read_buffer_;
  64. uint32_t num_pages_ = 0;
  65. std::atomic_bool reject_writes_{false};
  66. // This is a temporary buffer we use to remove messages from the shared buffer
  67. // for validation and dispatching.
  68. std::vector<uint8_t> read_buf_;
  69. };
  70. } // namespace core
  71. } // namespace mojo
  72. #endif // MOJO_CORE_CHANNEL_LINUX_H_