2022-07-04 11:49:15 +02:00
|
|
|
// Copyright (c) 2022 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/common/process_message_smr_impl.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/ptr_util.h"
|
|
|
|
#include "base/memory/shared_memory_mapping.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefSharedMemoryRegionImpl final : public CefSharedMemoryRegion {
|
|
|
|
public:
|
2024-01-20 23:48:57 +01:00
|
|
|
explicit CefSharedMemoryRegionImpl(
|
|
|
|
base::WritableSharedMemoryMapping&& mapping)
|
2022-07-04 11:49:15 +02:00
|
|
|
: mapping_(std::move(mapping)) {}
|
|
|
|
CefSharedMemoryRegionImpl(const CefSharedMemoryRegionImpl&) = delete;
|
|
|
|
CefSharedMemoryRegionImpl& operator=(const CefSharedMemoryRegionImpl&) =
|
|
|
|
delete;
|
|
|
|
|
|
|
|
// CefSharedMemoryRegion methods
|
|
|
|
bool IsValid() override { return mapping_.IsValid(); }
|
|
|
|
size_t Size() override { return IsValid() ? mapping_.size() : 0; }
|
2023-10-17 00:43:29 +02:00
|
|
|
void* Memory() override { return mapping_.memory(); }
|
2022-07-04 11:49:15 +02:00
|
|
|
|
|
|
|
private:
|
2023-10-17 00:43:29 +02:00
|
|
|
base::WritableSharedMemoryMapping mapping_;
|
2022-07-04 11:49:15 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(CefSharedMemoryRegionImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefProcessMessageSMRImpl::CefProcessMessageSMRImpl(
|
|
|
|
const CefString& name,
|
2023-10-17 00:43:29 +02:00
|
|
|
base::WritableSharedMemoryRegion&& region)
|
2022-07-04 11:49:15 +02:00
|
|
|
: name_(name), region_(std::move(region)) {
|
|
|
|
DCHECK(!name_.empty());
|
|
|
|
DCHECK(region_.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefProcessMessageSMRImpl::~CefProcessMessageSMRImpl() = default;
|
|
|
|
|
|
|
|
bool CefProcessMessageSMRImpl::IsValid() {
|
|
|
|
return region_.IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefProcessMessageSMRImpl::GetName() {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefSharedMemoryRegion>
|
|
|
|
CefProcessMessageSMRImpl::GetSharedMemoryRegion() {
|
|
|
|
return new CefSharedMemoryRegionImpl(region_.Map());
|
|
|
|
}
|
|
|
|
|
2023-10-17 00:43:29 +02:00
|
|
|
base::WritableSharedMemoryRegion CefProcessMessageSMRImpl::TakeRegion() {
|
2022-07-04 11:49:15 +02:00
|
|
|
return std::move(region_);
|
2022-09-26 21:30:45 +02:00
|
|
|
}
|
2022-07-04 11:49:15 +02:00
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefSharedProcessMessageBuilder>
|
|
|
|
CefSharedProcessMessageBuilder::Create(const CefString& name,
|
|
|
|
size_t byte_size) {
|
|
|
|
return new CefSharedProcessMessageBuilderImpl(name, byte_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefSharedProcessMessageBuilderImpl::CefSharedProcessMessageBuilderImpl(
|
|
|
|
const CefString& name,
|
|
|
|
size_t byte_size)
|
|
|
|
: name_(name),
|
2023-10-17 00:43:29 +02:00
|
|
|
region_(base::WritableSharedMemoryRegion::Create(byte_size)),
|
|
|
|
mapping_(region_.Map()) {}
|
2022-07-04 11:49:15 +02:00
|
|
|
|
|
|
|
bool CefSharedProcessMessageBuilderImpl::IsValid() {
|
2023-10-17 00:43:29 +02:00
|
|
|
return region_.IsValid() && mapping_.IsValid();
|
2022-07-04 11:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefSharedProcessMessageBuilderImpl::Size() {
|
2023-10-17 00:43:29 +02:00
|
|
|
return !IsValid() ? 0 : region_.GetSize();
|
2022-07-04 11:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* CefSharedProcessMessageBuilderImpl::Memory() {
|
2023-10-17 00:43:29 +02:00
|
|
|
return !IsValid() ? nullptr : mapping_.memory();
|
2022-07-04 11:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefProcessMessage> CefSharedProcessMessageBuilderImpl::Build() {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-10-17 00:43:29 +02:00
|
|
|
|
|
|
|
// Invalidate mappping
|
|
|
|
mapping_ = base::WritableSharedMemoryMapping();
|
|
|
|
return new CefProcessMessageSMRImpl(name_, std::move(region_));
|
2024-01-20 23:48:57 +01:00
|
|
|
}
|