2012-04-03 03:34:16 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_COMMON_VALUE_BASE_H_
|
|
|
|
#define CEF_LIBCEF_COMMON_VALUE_BASE_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include "include/cef_base.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/ref_counted.h"
|
2020-07-08 19:23:29 +02:00
|
|
|
#include "base/notreached.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/synchronization/lock.h"
|
2020-03-30 22:13:42 +02:00
|
|
|
#include "base/thread_annotations.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/threading/platform_thread.h"
|
|
|
|
|
|
|
|
// Controller implementation base class.
|
|
|
|
class CefValueController
|
|
|
|
: public base::RefCountedThreadSafe<CefValueController> {
|
|
|
|
public:
|
|
|
|
// Implemented by a class controlled using the access controller.
|
|
|
|
class Object {
|
|
|
|
public:
|
|
|
|
virtual ~Object() {}
|
|
|
|
|
|
|
|
// Called when the value has been removed.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void OnControlRemoved() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Encapsulates context locking and verification logic.
|
|
|
|
class AutoLock {
|
|
|
|
public:
|
|
|
|
explicit AutoLock(CefValueController* impl)
|
2017-05-17 11:29:28 +02:00
|
|
|
: impl_(impl), verified_(impl && impl->VerifyThread()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(impl);
|
|
|
|
if (verified_)
|
|
|
|
impl_->lock();
|
|
|
|
}
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
AutoLock(const AutoLock&) = delete;
|
|
|
|
AutoLock& operator=(const AutoLock&) = delete;
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
~AutoLock() {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (verified_)
|
|
|
|
impl_->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool verified() { return verified_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
scoped_refptr<CefValueController> impl_;
|
|
|
|
bool verified_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefValueController();
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefValueController(const CefValueController&) = delete;
|
|
|
|
CefValueController& operator=(const CefValueController&) = delete;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Returns true if this controller is thread safe.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual bool thread_safe() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Returns true if the current thread is allowed to access this controller.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual bool on_correct_thread() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Lock the controller.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void lock() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Unlock the controller.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void unlock() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Returns true if the controller is locked on the current thread.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual bool locked() = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-17 16:19:30 +02:00
|
|
|
// Assert that the lock has been acquired.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void AssertLockAcquired() = 0;
|
2015-04-17 16:19:30 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Verify that the current thread is correct for accessing the controller.
|
|
|
|
inline bool VerifyThread() {
|
|
|
|
if (!thread_safe() && !on_correct_thread()) {
|
|
|
|
// This object should only be accessed from the thread that created it.
|
|
|
|
NOTREACHED() << "object accessed from incorrect thread.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The controller must already be locked before calling the below methods.
|
|
|
|
|
|
|
|
// Set the owner for this controller.
|
|
|
|
void SetOwner(void* value, Object* object);
|
|
|
|
|
|
|
|
// Add a reference value and associated object.
|
|
|
|
void AddReference(void* value, Object* object);
|
|
|
|
|
|
|
|
// Remove the value. If |notify_object| is true the removed object will be
|
|
|
|
// notified. If |value| is the owner all reference objects will be removed.
|
|
|
|
// If |value| has dependencies those objects will also be removed.
|
|
|
|
void Remove(void* value, bool notify_object);
|
|
|
|
|
|
|
|
// Returns the object for the specified value.
|
|
|
|
Object* Get(void* value);
|
|
|
|
|
|
|
|
// Add a dependency between |parent| and |child|.
|
|
|
|
void AddDependency(void* parent, void* child);
|
|
|
|
|
|
|
|
// Recursively removes any dependent values.
|
|
|
|
void RemoveDependencies(void* value);
|
|
|
|
|
|
|
|
// Takes ownership of all references and dependencies currently controlled by
|
|
|
|
// |other|. The |other| controller must already be locked.
|
|
|
|
void TakeFrom(CefValueController* other);
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
// Replace all instances of |old_value| with |new_value|. Used in cases where
|
|
|
|
// move semantics may move the contents of an object without retaining the
|
|
|
|
// object pointer itself.
|
|
|
|
void Swap(void* old_value, void* new_value);
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
protected:
|
|
|
|
friend class base::RefCountedThreadSafe<CefValueController>;
|
|
|
|
|
|
|
|
virtual ~CefValueController();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
private:
|
|
|
|
// Owner object.
|
|
|
|
void* owner_value_;
|
|
|
|
Object* owner_object_;
|
|
|
|
|
|
|
|
// Map of reference objects.
|
2021-12-06 21:40:25 +01:00
|
|
|
using ReferenceMap = std::map<void*, Object*>;
|
2012-04-03 03:34:16 +02:00
|
|
|
ReferenceMap reference_map_;
|
|
|
|
|
|
|
|
// Map of dependency objects.
|
2021-12-06 21:40:25 +01:00
|
|
|
using DependencySet = std::set<void*>;
|
|
|
|
using DependencyMap = std::map<void*, DependencySet>;
|
2012-04-03 03:34:16 +02:00
|
|
|
DependencyMap dependency_map_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Thread-safe access control implementation.
|
|
|
|
class CefValueControllerThreadSafe : public CefValueController {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit CefValueControllerThreadSafe() : locked_thread_id_(0) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefValueControllerThreadSafe(const CefValueControllerThreadSafe&) = delete;
|
|
|
|
CefValueControllerThreadSafe& operator=(const CefValueControllerThreadSafe&) =
|
|
|
|
delete;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefValueController methods.
|
2014-11-12 20:25:15 +01:00
|
|
|
bool thread_safe() override { return true; }
|
|
|
|
bool on_correct_thread() override { return true; }
|
2020-03-30 22:13:42 +02:00
|
|
|
void lock() override NO_THREAD_SAFETY_ANALYSIS {
|
2012-04-03 03:34:16 +02:00
|
|
|
lock_.Acquire();
|
|
|
|
locked_thread_id_ = base::PlatformThread::CurrentId();
|
|
|
|
}
|
2020-03-30 22:13:42 +02:00
|
|
|
void unlock() override NO_THREAD_SAFETY_ANALYSIS {
|
2012-04-03 03:34:16 +02:00
|
|
|
locked_thread_id_ = 0;
|
|
|
|
lock_.Release();
|
|
|
|
}
|
2014-11-12 20:25:15 +01:00
|
|
|
bool locked() override {
|
2012-04-03 03:34:16 +02:00
|
|
|
return (locked_thread_id_ == base::PlatformThread::CurrentId());
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
void AssertLockAcquired() override { lock_.AssertAcquired(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
base::Lock lock_;
|
|
|
|
base::PlatformThreadId locked_thread_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Non-thread-safe access control implementation.
|
|
|
|
class CefValueControllerNonThreadSafe : public CefValueController {
|
|
|
|
public:
|
|
|
|
explicit CefValueControllerNonThreadSafe()
|
2017-05-17 11:29:28 +02:00
|
|
|
: thread_id_(base::PlatformThread::CurrentId()) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefValueControllerNonThreadSafe(const CefValueControllerNonThreadSafe&) =
|
|
|
|
delete;
|
|
|
|
CefValueControllerNonThreadSafe& operator=(
|
|
|
|
const CefValueControllerNonThreadSafe&) = delete;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefValueController methods.
|
2014-11-12 20:25:15 +01:00
|
|
|
bool thread_safe() override { return false; }
|
|
|
|
bool on_correct_thread() override {
|
2012-04-03 03:34:16 +02:00
|
|
|
return (thread_id_ == base::PlatformThread::CurrentId());
|
|
|
|
}
|
2014-11-12 20:25:15 +01:00
|
|
|
void lock() override {}
|
|
|
|
void unlock() override {}
|
|
|
|
bool locked() override { return on_correct_thread(); }
|
2017-05-17 11:29:28 +02:00
|
|
|
void AssertLockAcquired() override { DCHECK(locked()); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
base::PlatformThreadId thread_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper macros for verifying context.
|
|
|
|
|
|
|
|
#define CEF_VALUE_VERIFY_RETURN_VOID_EX(object, modify) \
|
2017-05-17 11:29:28 +02:00
|
|
|
if (!VerifyAttached()) \
|
|
|
|
return; \
|
|
|
|
AutoLock auto_lock(object, modify); \
|
|
|
|
if (!auto_lock.verified()) \
|
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#define CEF_VALUE_VERIFY_RETURN_VOID(modify) \
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_VALUE_VERIFY_RETURN_VOID_EX(this, modify)
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#define CEF_VALUE_VERIFY_RETURN_EX(object, modify, error_val) \
|
2017-05-17 11:29:28 +02:00
|
|
|
if (!VerifyAttached()) \
|
|
|
|
return error_val; \
|
|
|
|
AutoLock auto_lock(object, modify); \
|
|
|
|
if (!auto_lock.verified()) \
|
|
|
|
return error_val;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#define CEF_VALUE_VERIFY_RETURN(modify, error_val) \
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_VALUE_VERIFY_RETURN_EX(this, modify, error_val)
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Template class for implementing CEF wrappers of other types.
|
2017-05-17 11:29:28 +02:00
|
|
|
template <class CefType, class ValueType>
|
2012-04-03 03:34:16 +02:00
|
|
|
class CefValueBase : public CefType, public CefValueController::Object {
|
|
|
|
public:
|
|
|
|
// Specifies how the value will be used.
|
|
|
|
enum ValueMode {
|
|
|
|
// A reference to a value managed by an existing controller. These values
|
|
|
|
// can be safely detached but ownership should not be transferred (make a
|
|
|
|
// copy of the value instead).
|
|
|
|
kReference,
|
|
|
|
|
|
|
|
// The value has its own controller and will be deleted on destruction.
|
|
|
|
// These values can only be detached to another controller otherwise any
|
|
|
|
// references will not be properly managed.
|
|
|
|
kOwnerWillDelete,
|
|
|
|
|
|
|
|
// The value has its own controller and will not be deleted on destruction.
|
|
|
|
// This should only be used for global values or scope-limited values that
|
|
|
|
// will be explicitly detached.
|
|
|
|
kOwnerNoDelete,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new object.
|
|
|
|
// If |read_only| is true mutable access will not be allowed.
|
|
|
|
// If |parent_value| is non-NULL and the value mode is kReference a dependency
|
|
|
|
// will be added.
|
|
|
|
CefValueBase(ValueType* value,
|
|
|
|
void* parent_value,
|
|
|
|
ValueMode value_mode,
|
|
|
|
bool read_only,
|
|
|
|
CefValueController* controller)
|
2017-05-17 11:29:28 +02:00
|
|
|
: value_(value),
|
|
|
|
value_mode_(value_mode),
|
|
|
|
read_only_(read_only),
|
|
|
|
controller_(controller) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(value_);
|
|
|
|
|
|
|
|
// Specifying a parent value for a non-reference doesn't make sense.
|
|
|
|
DCHECK(!(!reference() && parent_value));
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (!reference() && !controller_.get()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// For owned values default to using a new multi-threaded controller.
|
|
|
|
controller_ = new CefValueControllerThreadSafe();
|
2012-04-19 22:31:46 +02:00
|
|
|
SetOwnsController();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A controller is required.
|
2014-09-27 01:48:19 +02:00
|
|
|
DCHECK(controller_.get());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (reference()) {
|
|
|
|
// Register the reference with the controller.
|
|
|
|
controller_->AddReference(value_, this);
|
|
|
|
|
|
|
|
// Add a dependency on the parent value.
|
|
|
|
if (parent_value)
|
|
|
|
controller_->AddDependency(parent_value, value_);
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 20:25:15 +01:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefValueBase(const CefValueBase&) = delete;
|
|
|
|
CefValueBase& operator=(const CefValueBase&) = delete;
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
~CefValueBase() override {
|
2014-09-27 01:48:19 +02:00
|
|
|
if (controller_.get() && value_)
|
2012-04-03 03:34:16 +02:00
|
|
|
Delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
// True if the underlying value is referenced instead of owned.
|
|
|
|
inline bool reference() const { return (value_mode_ == kReference); }
|
|
|
|
|
|
|
|
// True if the underlying value will be deleted.
|
|
|
|
inline bool will_delete() const { return (value_mode_ == kOwnerWillDelete); }
|
|
|
|
|
|
|
|
// True if access to the underlying value is read-only.
|
|
|
|
inline bool read_only() const { return read_only_; }
|
|
|
|
|
|
|
|
// True if the underlying value has been detached.
|
2014-09-27 01:48:19 +02:00
|
|
|
inline bool detached() { return !controller_.get(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Returns the controller.
|
2014-09-27 01:48:19 +02:00
|
|
|
inline CefValueController* controller() { return controller_.get(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Deletes the underlying value.
|
|
|
|
void Delete() {
|
|
|
|
CEF_VALUE_VERIFY_RETURN_VOID(false);
|
|
|
|
|
|
|
|
// Remove the object from the controller. If this is the owner object any
|
|
|
|
// references will be detached.
|
|
|
|
controller()->Remove(value_, false);
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
if (will_delete()) {
|
|
|
|
// Remove any dependencies.
|
|
|
|
controller()->RemoveDependencies(value_);
|
|
|
|
|
|
|
|
// Delete the value.
|
|
|
|
DeleteValue(value_);
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
controller_ = nullptr;
|
|
|
|
value_ = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detaches the underlying value and returns a pointer to it. If this is an
|
|
|
|
// owner and a |new_controller| value is specified any existing references
|
|
|
|
// will be passed to the new controller.
|
2021-05-14 18:58:55 +02:00
|
|
|
ValueType* Detach(CefValueController* new_controller) WARN_UNUSED_RESULT {
|
2020-01-15 14:36:24 +01:00
|
|
|
CEF_VALUE_VERIFY_RETURN(false, nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (new_controller && !reference()) {
|
|
|
|
// Pass any existing references and dependencies to the new controller.
|
|
|
|
// They will be removed from this controller.
|
|
|
|
new_controller->TakeFrom(controller());
|
|
|
|
}
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Remove the object from the controller. If this is the owner object any
|
|
|
|
// references will be detached.
|
|
|
|
controller()->Remove(value_, false);
|
2020-01-15 14:36:24 +01:00
|
|
|
controller_ = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Return the old value.
|
|
|
|
ValueType* old_val = value_;
|
2020-01-15 14:36:24 +01:00
|
|
|
value_ = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
return old_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the value is attached.
|
|
|
|
inline bool VerifyAttached() {
|
|
|
|
if (detached()) {
|
|
|
|
// This object should not be accessed after being detached.
|
|
|
|
NOTREACHED() << "object accessed after being detached.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// CefValueController::Object methods.
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnControlRemoved() override {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(controller()->locked());
|
|
|
|
|
|
|
|
// Only references should be removed in this manner.
|
|
|
|
DCHECK(reference());
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
controller_ = nullptr;
|
|
|
|
value_ = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override to customize value deletion.
|
|
|
|
virtual void DeleteValue(ValueType* value) { delete value; }
|
|
|
|
|
|
|
|
// Returns a mutable reference to the value.
|
|
|
|
inline ValueType* mutable_value() {
|
|
|
|
DCHECK(value_);
|
|
|
|
DCHECK(!read_only_);
|
|
|
|
DCHECK(controller()->locked());
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
// Returns a const reference to the value.
|
|
|
|
inline const ValueType& const_value() {
|
|
|
|
DCHECK(value_);
|
|
|
|
DCHECK(controller()->locked());
|
|
|
|
return *value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the value can be accessed.
|
|
|
|
inline bool VerifyAccess(bool modify) {
|
|
|
|
// The controller must already be locked.
|
|
|
|
DCHECK(controller()->locked());
|
|
|
|
|
|
|
|
if (read_only() && modify) {
|
|
|
|
// This object cannot be modified.
|
|
|
|
NOTREACHED() << "mutation attempted on read-only object.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
// Used to indicate that this object owns the controller.
|
|
|
|
inline void SetOwnsController() {
|
2014-09-27 01:48:19 +02:00
|
|
|
CefValueController::AutoLock lock_scope(controller_.get());
|
2012-04-19 22:31:46 +02:00
|
|
|
if (lock_scope.verified())
|
|
|
|
controller_->SetOwner(value_, this);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Encapsulates value locking and verification logic.
|
|
|
|
class AutoLock {
|
|
|
|
public:
|
|
|
|
explicit AutoLock(CefValueBase* impl, bool modify)
|
2017-05-17 11:29:28 +02:00
|
|
|
: auto_lock_(impl->controller()) {
|
2014-09-27 01:48:19 +02:00
|
|
|
verified_ = (auto_lock_.verified() && impl->VerifyAccess(modify));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
AutoLock(const AutoLock&) = delete;
|
|
|
|
AutoLock& operator=(const AutoLock&) = delete;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
inline bool verified() { return verified_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefValueController::AutoLock auto_lock_;
|
|
|
|
bool verified_;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueType* value_;
|
|
|
|
ValueMode value_mode_;
|
|
|
|
bool read_only_;
|
|
|
|
scoped_refptr<CefValueController> controller_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefValueBase);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_COMMON_VALUE_BASE_H_
|