read_only_shared_memory_region.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018 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. #include "base/memory/read_only_shared_memory_region.h"
  5. #include <utility>
  6. #include "build/build_config.h"
  7. namespace base {
  8. ReadOnlySharedMemoryRegion::CreateFunction*
  9. ReadOnlySharedMemoryRegion::create_hook_ = nullptr;
  10. // static
  11. MappedReadOnlyRegion ReadOnlySharedMemoryRegion::Create(
  12. size_t size,
  13. SharedMemoryMapper* mapper) {
  14. if (create_hook_)
  15. return create_hook_(size, mapper);
  16. subtle::PlatformSharedMemoryRegion handle =
  17. subtle::PlatformSharedMemoryRegion::CreateWritable(size);
  18. if (!handle.IsValid())
  19. return {};
  20. auto result = handle.MapAt(0, handle.GetSize(), mapper);
  21. if (!result.has_value())
  22. return {};
  23. WritableSharedMemoryMapping mapping(result.value(), size, handle.GetGUID(),
  24. mapper);
  25. #if BUILDFLAG(IS_MAC)
  26. handle.ConvertToReadOnly(mapping.memory());
  27. #else
  28. handle.ConvertToReadOnly();
  29. #endif // BUILDFLAG(IS_MAC)
  30. ReadOnlySharedMemoryRegion region(std::move(handle));
  31. if (!region.IsValid() || !mapping.IsValid())
  32. return {};
  33. return {std::move(region), std::move(mapping)};
  34. }
  35. // static
  36. ReadOnlySharedMemoryRegion ReadOnlySharedMemoryRegion::Deserialize(
  37. subtle::PlatformSharedMemoryRegion handle) {
  38. return ReadOnlySharedMemoryRegion(std::move(handle));
  39. }
  40. // static
  41. subtle::PlatformSharedMemoryRegion
  42. ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
  43. ReadOnlySharedMemoryRegion region) {
  44. return std::move(region.handle_);
  45. }
  46. ReadOnlySharedMemoryRegion::ReadOnlySharedMemoryRegion() = default;
  47. ReadOnlySharedMemoryRegion::ReadOnlySharedMemoryRegion(
  48. ReadOnlySharedMemoryRegion&& region) = default;
  49. ReadOnlySharedMemoryRegion& ReadOnlySharedMemoryRegion::operator=(
  50. ReadOnlySharedMemoryRegion&& region) = default;
  51. ReadOnlySharedMemoryRegion::~ReadOnlySharedMemoryRegion() = default;
  52. ReadOnlySharedMemoryRegion ReadOnlySharedMemoryRegion::Duplicate() const {
  53. return ReadOnlySharedMemoryRegion(handle_.Duplicate());
  54. }
  55. ReadOnlySharedMemoryMapping ReadOnlySharedMemoryRegion::Map(
  56. SharedMemoryMapper* mapper) const {
  57. return MapAt(0, handle_.GetSize(), mapper);
  58. }
  59. ReadOnlySharedMemoryMapping ReadOnlySharedMemoryRegion::MapAt(
  60. uint64_t offset,
  61. size_t size,
  62. SharedMemoryMapper* mapper) const {
  63. if (!IsValid())
  64. return {};
  65. auto result = handle_.MapAt(offset, size, mapper);
  66. if (!result.has_value())
  67. return {};
  68. return ReadOnlySharedMemoryMapping(result.value(), size, handle_.GetGUID(),
  69. mapper);
  70. }
  71. bool ReadOnlySharedMemoryRegion::IsValid() const {
  72. return handle_.IsValid();
  73. }
  74. ReadOnlySharedMemoryRegion::ReadOnlySharedMemoryRegion(
  75. subtle::PlatformSharedMemoryRegion handle)
  76. : handle_(std::move(handle)) {
  77. if (handle_.IsValid()) {
  78. CHECK_EQ(handle_.GetMode(),
  79. subtle::PlatformSharedMemoryRegion::Mode::kReadOnly);
  80. }
  81. }
  82. } // namespace base