cef/libcef/common/process_message_impl.cc
Marshall Greenblatt f3c513bafd Allow a CefProcessMessage to remain valid after receipt (see issue #3126)
A reference to a received CefProcessMessage object and/or associated argument
list can now be kept outside of the OnProcessMessageReceived callback. The
argument list is no longer explicitly owned by the CefProcessMessage object
and can be individually assigned to other CefValue types as needed (e.g. by
passing to SetList, etc). Depending on client usage this could reduce the
potential for unnecessary copies of the list contents.

Received messages can also be sent back using SendProcessMessage (after which
the CefProcessMessage would become invalid as discussed in issue #3123). This
is not new behavior but we have now added explicit unit test coverage for it.
This also no longer requires a copy of the argument list contents.

Note that a received argument list is initially read-only for logical
consistency. Assignment to another CefValue object could potentially remove
the read-only status because it is not an intrinsic property of the underlying
Chromium data type. This is fine because, at that point, ownership has been
transfered to the new CefValue object and the original logical context (as
part of the CefProcessMessage) no longer applies.
2021-05-20 14:46:19 -04:00

69 lines
2.0 KiB
C++

// Copyright (c) 2012 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_impl.h"
#include <memory>
#include "libcef/common/values_impl.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
// static
CefRefPtr<CefProcessMessage> CefProcessMessage::Create(const CefString& name) {
return new CefProcessMessageImpl(name, CefListValue::Create());
}
CefProcessMessageImpl::CefProcessMessageImpl(const CefString& name,
CefRefPtr<CefListValue> arguments)
: name_(name), arguments_(arguments) {
DCHECK(!name_.empty());
DCHECK(arguments_ && arguments_->IsValid());
}
CefProcessMessageImpl::CefProcessMessageImpl(const CefString& name,
base::ListValue arguments,
bool read_only)
: name_(name) {
DCHECK(!name_.empty());
auto new_obj = std::make_unique<base::ListValue>();
*new_obj = std::move(arguments);
arguments_ =
new CefListValueImpl(new_obj.release(), /*will_delete=*/true, read_only);
}
CefProcessMessageImpl::~CefProcessMessageImpl() = default;
base::ListValue CefProcessMessageImpl::TakeArgumentList() {
DCHECK(IsValid());
CefListValueImpl* value_impl =
static_cast<CefListValueImpl*>(arguments_.get());
auto value = base::WrapUnique(value_impl->CopyOrDetachValue(nullptr));
return std::move(*value);
}
bool CefProcessMessageImpl::IsValid() {
return arguments_->IsValid();
}
bool CefProcessMessageImpl::IsReadOnly() {
return arguments_->IsReadOnly();
}
CefRefPtr<CefProcessMessage> CefProcessMessageImpl::Copy() {
if (!IsValid())
return nullptr;
return new CefProcessMessageImpl(name_, arguments_->Copy());
}
CefString CefProcessMessageImpl::GetName() {
return name_;
}
CefRefPtr<CefListValue> CefProcessMessageImpl::GetArgumentList() {
return arguments_;
}