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.
This commit is contained in:
Marshall Greenblatt
2021-05-20 12:26:36 -04:00
parent ff8f4a7217
commit f3c513bafd
8 changed files with 32 additions and 40 deletions

View File

@ -15,22 +15,20 @@ class ListValue;
// CefProcessMessage implementation.
class CefProcessMessageImpl : public CefProcessMessage {
public:
// Constructor for an owned list of arguments.
// Constructor for referencing existing |arguments|.
CefProcessMessageImpl(const CefString& name,
CefRefPtr<CefListValue> arguments);
// Constructor for an unowned list of arguments.
// Call Detach() when |arguments| is no longer valid.
// Constructor for creating a new CefListValue that takes ownership of
// |arguments|.
CefProcessMessageImpl(const CefString& name,
base::ListValue* arguments,
base::ListValue arguments,
bool read_only);
~CefProcessMessageImpl() override;
// Stop referencing the underlying argument list (which we never owned).
void Detach();
// Transfer ownership of the underlying argument list to the caller.
// Transfer ownership of the underlying argument list to the caller, or create
// a copy if the argument list is already owned by something else.
// TODO: Pass by reference instead of ownership if/when Mojo adds support
// for that.
base::ListValue TakeArgumentList() WARN_UNUSED_RESULT;
@ -45,7 +43,6 @@ class CefProcessMessageImpl : public CefProcessMessage {
private:
const CefString name_;
CefRefPtr<CefListValue> arguments_;
const bool should_detach_ = false;
IMPLEMENT_REFCOUNTING(CefProcessMessageImpl);
DISALLOW_COPY_AND_ASSIGN(CefProcessMessageImpl);