- Add CefValue for wrapping various value types in a single object (issue #1607).

- Add IsSame() and IsEqual() methods for comparing CefValue* types.
- Improve CefValue* documentation.
This commit is contained in:
Marshall Greenblatt
2015-04-15 15:45:30 +02:00
parent 740ad72f90
commit 0369063810
23 changed files with 3281 additions and 185 deletions

View File

@@ -7,6 +7,362 @@
#include <algorithm>
#include <vector>
// CefValueImpl implementation.
// static
CefRefPtr<CefValue> CefValue::Create() {
return new CefValueImpl(base::Value::CreateNullValue());
}
// static
CefRefPtr<CefValue> CefValueImpl::GetOrCreateRefOrCopy(
base::Value* value,
void* parent_value,
bool read_only,
CefValueController* controller) {
DCHECK(value);
if (value->IsType(base::Value::TYPE_BINARY)) {
base::BinaryValue* binary_value = static_cast<base::BinaryValue*>(value);
return new CefValueImpl(CefBinaryValueImpl::GetOrCreateRef(
binary_value, parent_value, controller));
}
if (value->IsType(base::Value::TYPE_DICTIONARY)) {
base::DictionaryValue* dict_value =
static_cast<base::DictionaryValue*>(value);
return new CefValueImpl(CefDictionaryValueImpl::GetOrCreateRef(
dict_value, parent_value, read_only, controller));
}
if (value->IsType(base::Value::TYPE_LIST)) {
base::ListValue* list_value = static_cast<base::ListValue*>(value);
return new CefValueImpl(CefListValueImpl::GetOrCreateRef(
list_value, parent_value, read_only, controller));
}
return new CefValueImpl(value->DeepCopy());
}
CefValueImpl::CefValueImpl() {
}
CefValueImpl::CefValueImpl(base::Value* value) {
SetValue(value);
}
CefValueImpl::CefValueImpl(CefRefPtr<CefBinaryValue> value)
: binary_value_(value) {
}
CefValueImpl::CefValueImpl(CefRefPtr<CefDictionaryValue> value)
: dictionary_value_(value) {
}
CefValueImpl::CefValueImpl(CefRefPtr<CefListValue> value)
: list_value_(value) {
}
CefValueImpl::~CefValueImpl() {
}
void CefValueImpl::SetValue(base::Value* value) {
base::AutoLock lock_scope(lock_);
SetValueInternal(value);
}
base::Value* CefValueImpl::CopyOrTransferValue(
void* new_parent_value,
bool new_read_only,
CefValueController* new_controller) {
base::AutoLock lock_scope(lock_);
if (binary_value_) {
base::BinaryValue* value =
static_cast<CefBinaryValueImpl*>(binary_value_.get())->
CopyOrDetachValue(new_controller);
binary_value_ = CefBinaryValueImpl::GetOrCreateRef(
value, new_parent_value, new_controller);
return value;
}
if (dictionary_value_) {
base::DictionaryValue* value =
static_cast<CefDictionaryValueImpl*>(dictionary_value_.get())->
CopyOrDetachValue(new_controller);
dictionary_value_ = CefDictionaryValueImpl::GetOrCreateRef(
value, new_parent_value, new_read_only, new_controller);
return value;
}
if (list_value_) {
base::ListValue* value =
static_cast<CefListValueImpl*>(list_value_.get())->
CopyOrDetachValue(new_controller);
list_value_ = CefListValueImpl::GetOrCreateRef(
value, new_parent_value, new_read_only, new_controller);
return value;
}
return value_->DeepCopy();
}
bool CefValueImpl::IsValid() {
base::AutoLock lock_scope(lock_);
if (binary_value_)
return binary_value_->IsValid();
if (dictionary_value_)
return dictionary_value_->IsValid();
if (list_value_)
return list_value_->IsValid();
return (value_ != NULL);
}
bool CefValueImpl::IsOwned() {
base::AutoLock lock_scope(lock_);
if (binary_value_)
return binary_value_->IsOwned();
if (dictionary_value_)
return dictionary_value_->IsOwned();
if (list_value_)
return list_value_->IsOwned();
return false;
}
bool CefValueImpl::IsReadOnly() {
base::AutoLock lock_scope(lock_);
if (binary_value_)
return true;
if (dictionary_value_)
return dictionary_value_->IsReadOnly();
if (list_value_)
return list_value_->IsReadOnly();
return false;
}
bool CefValueImpl::IsSame(CefRefPtr<CefValue> that) {
if (that.get() == this)
return true;
if (!that.get() || that->GetType() != GetType())
return false;
CefValueImpl* impl = static_cast<CefValueImpl*>(that.get());
base::AutoLock lock_scope(lock_);
base::AutoLock lock_scope2(impl->lock_);
if (binary_value_)
return binary_value_->IsSame(impl->binary_value_);
if (dictionary_value_)
return dictionary_value_->IsSame(impl->dictionary_value_);
if (list_value_)
return list_value_->IsSame(impl->list_value_);
// Simple types are never the same.
return false;
}
bool CefValueImpl::IsEqual(CefRefPtr<CefValue> that) {
if (that.get() == this)
return true;
if (!that.get() || that->GetType() != GetType())
return false;
CefValueImpl* impl = static_cast<CefValueImpl*>(that.get());
base::AutoLock lock_scope(lock_);
base::AutoLock lock_scope2(impl->lock_);
if (binary_value_)
return binary_value_->IsEqual(impl->binary_value_);
if (dictionary_value_)
return dictionary_value_->IsEqual(impl->dictionary_value_);
if (list_value_)
return list_value_->IsEqual(impl->list_value_);
if (!value_) // Invalid types are equal.
return true;
return value_->Equals(impl->value_.get());
}
CefRefPtr<CefValue> CefValueImpl::Copy() {
base::AutoLock lock_scope(lock_);
if (binary_value_)
return new CefValueImpl(binary_value_->Copy());
if (dictionary_value_)
return new CefValueImpl(dictionary_value_->Copy(false));
if (list_value_)
return new CefValueImpl(list_value_->Copy());
if (value_)
return new CefValueImpl(value_->DeepCopy());
return new CefValueImpl();
}
CefValueType CefValueImpl::GetType() {
base::AutoLock lock_scope(lock_);
if (binary_value_)
return VTYPE_BINARY;
if (dictionary_value_)
return VTYPE_DICTIONARY;
if (list_value_)
return VTYPE_LIST;
if (value_) {
switch (value_->GetType()) {
case base::Value::TYPE_NULL:
return VTYPE_NULL;
case base::Value::TYPE_BOOLEAN:
return VTYPE_BOOL;
case base::Value::TYPE_INTEGER:
return VTYPE_INT;
case base::Value::TYPE_DOUBLE:
return VTYPE_DOUBLE;
case base::Value::TYPE_STRING:
return VTYPE_STRING;
default:
NOTREACHED();
break;
}
}
return VTYPE_INVALID;
}
bool CefValueImpl::GetBool() {
base::AutoLock lock_scope(lock_);
bool ret_value = false;
if (value_)
value_->GetAsBoolean(&ret_value);
return ret_value;
}
int CefValueImpl::GetInt() {
base::AutoLock lock_scope(lock_);
int ret_value = 0;
if (value_)
value_->GetAsInteger(&ret_value);
return ret_value;
}
double CefValueImpl::GetDouble() {
base::AutoLock lock_scope(lock_);
double ret_value = 0;
if (value_)
value_->GetAsDouble(&ret_value);
return ret_value;
}
CefString CefValueImpl::GetString() {
base::AutoLock lock_scope(lock_);
std::string ret_value;
if (value_)
value_->GetAsString(&ret_value);
return ret_value;
}
CefRefPtr<CefBinaryValue> CefValueImpl::GetBinary() {
base::AutoLock lock_scope(lock_);
return binary_value_;
}
CefRefPtr<CefDictionaryValue> CefValueImpl::GetDictionary() {
base::AutoLock lock_scope(lock_);
return dictionary_value_;
}
CefRefPtr<CefListValue> CefValueImpl::GetList() {
base::AutoLock lock_scope(lock_);
return list_value_;
}
bool CefValueImpl::SetNull() {
SetValue(base::Value::CreateNullValue());
return true;
}
bool CefValueImpl::SetBool(bool value) {
SetValue(new base::FundamentalValue(value));
return true;
}
bool CefValueImpl::SetInt(int value) {
SetValue(new base::FundamentalValue(value));
return true;
}
bool CefValueImpl::SetDouble(double value) {
SetValue(new base::FundamentalValue(value));
return true;
}
bool CefValueImpl::SetString(const CefString& value) {
SetValue(new base::StringValue(value.ToString()));
return true;
}
bool CefValueImpl::SetBinary(CefRefPtr<CefBinaryValue> value) {
base::AutoLock lock_scope(lock_);
SetValueInternal(NULL);
binary_value_ = value;
return true;
}
bool CefValueImpl::SetDictionary(CefRefPtr<CefDictionaryValue> value) {
base::AutoLock lock_scope(lock_);
SetValueInternal(NULL);
dictionary_value_ = value;
return true;
}
bool CefValueImpl::SetList(CefRefPtr<CefListValue> value) {
base::AutoLock lock_scope(lock_);
SetValueInternal(NULL);
list_value_ = value;
return true;
}
void CefValueImpl::SetValueInternal(base::Value* value) {
lock_.AssertAcquired();
value_.reset(NULL);
binary_value_ = NULL;
dictionary_value_ = NULL;
list_value_ = NULL;
if (value) {
switch (value->GetType()) {
case base::Value::TYPE_BINARY:
binary_value_ = new CefBinaryValueImpl(
static_cast<base::BinaryValue*>(value), true);
return;
case base::Value::TYPE_DICTIONARY:
dictionary_value_ = new CefDictionaryValueImpl(
static_cast<base::DictionaryValue*>(value), true, false);
return;
case base::Value::TYPE_LIST:
list_value_ = new CefListValueImpl(
static_cast<base::ListValue*>(value), true, false);
return;
default:
value_.reset(value);
}
}
}
// CefBinaryValueImpl implementation.
@@ -39,11 +395,19 @@ CefRefPtr<CefBinaryValue> CefBinaryValueImpl::GetOrCreateRef(
}
CefBinaryValueImpl::CefBinaryValueImpl(base::BinaryValue* value,
bool will_delete,
bool read_only)
bool will_delete)
: CefValueBase<CefBinaryValue, base::BinaryValue>(
value, NULL, will_delete ? kOwnerWillDelete : kOwnerNoDelete,
read_only, NULL) {
true, NULL) {
}
CefBinaryValueImpl::CefBinaryValueImpl(char* data,
size_t data_size,
bool copy)
: CefValueBase<CefBinaryValue, base::BinaryValue>(
copy ? base::BinaryValue::CreateWithCopiedBuffer(data, data_size) :
new base::BinaryValue(scoped_ptr<char[]>(data), data_size),
NULL, kOwnerWillDelete, true, NULL) {
}
base::BinaryValue* CefBinaryValueImpl::CopyValue() {
@@ -67,6 +431,16 @@ base::BinaryValue* CefBinaryValueImpl::CopyOrDetachValue(
return new_value;
}
bool CefBinaryValueImpl::IsSameValue(const base::BinaryValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return (&const_value() == that);
}
bool CefBinaryValueImpl::IsEqualValue(const base::BinaryValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return const_value().Equals(that);
}
bool CefBinaryValueImpl::IsValid() {
return !detached();
}
@@ -75,6 +449,28 @@ bool CefBinaryValueImpl::IsOwned() {
return !will_delete();
}
bool CefBinaryValueImpl::IsSame(CefRefPtr<CefBinaryValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefBinaryValueImpl*>(that.get())->
IsSameValue(&const_value());
}
bool CefBinaryValueImpl::IsEqual(CefRefPtr<CefBinaryValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefBinaryValueImpl*>(that.get())->
IsEqualValue(&const_value());
}
CefRefPtr<CefBinaryValue> CefBinaryValueImpl::Copy() {
CEF_VALUE_VERIFY_RETURN(false, NULL);
return new CefBinaryValueImpl(const_value().DeepCopy(), NULL,
@@ -115,22 +511,12 @@ CefBinaryValueImpl::CefBinaryValueImpl(base::BinaryValue* value,
value, parent_value, value_mode, true, controller) {
}
CefBinaryValueImpl::CefBinaryValueImpl(char* data,
size_t data_size,
bool copy)
: CefValueBase<CefBinaryValue, base::BinaryValue>(
copy ? base::BinaryValue::CreateWithCopiedBuffer(data, data_size) :
new base::BinaryValue(scoped_ptr<char[]>(data), data_size),
NULL, kOwnerWillDelete, true, NULL) {
}
// CefDictionaryValueImpl implementation.
// static
CefRefPtr<CefDictionaryValue> CefDictionaryValue::Create() {
return new CefDictionaryValueImpl(new base::DictionaryValue(),
NULL, CefDictionaryValueImpl::kOwnerWillDelete, false, NULL);
return new CefDictionaryValueImpl(new base::DictionaryValue(), true, false);
}
// static
@@ -176,6 +562,16 @@ base::DictionaryValue* CefDictionaryValueImpl::CopyOrDetachValue(
return new_value;
}
bool CefDictionaryValueImpl::IsSameValue(const base::DictionaryValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return (&const_value() == that);
}
bool CefDictionaryValueImpl::IsEqualValue(const base::DictionaryValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return const_value().Equals(that);
}
bool CefDictionaryValueImpl::IsValid() {
return !detached();
}
@@ -188,6 +584,28 @@ bool CefDictionaryValueImpl::IsReadOnly() {
return read_only();
}
bool CefDictionaryValueImpl::IsSame(CefRefPtr<CefDictionaryValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefDictionaryValueImpl*>(that.get())->
IsSameValue(&const_value());
}
bool CefDictionaryValueImpl::IsEqual(CefRefPtr<CefDictionaryValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefDictionaryValueImpl*>(that.get())->
IsEqualValue(&const_value());
}
CefRefPtr<CefDictionaryValue> CefDictionaryValueImpl::Copy(
bool exclude_empty_children) {
CEF_VALUE_VERIFY_RETURN(false, NULL);
@@ -268,6 +686,21 @@ CefValueType CefDictionaryValueImpl::GetType(const CefString& key) {
return VTYPE_INVALID;
}
CefRefPtr<CefValue> CefDictionaryValueImpl::GetValue(const CefString& key) {
CEF_VALUE_VERIFY_RETURN(false, NULL);
const base::Value* out_value = NULL;
if (const_value().GetWithoutPathExpansion(key, &out_value)) {
return CefValueImpl::GetOrCreateRefOrCopy(
const_cast<base::Value*>(out_value),
const_cast<base::DictionaryValue*>(&const_value()),
read_only(),
controller());
}
return NULL;
}
bool CefDictionaryValueImpl::GetBool(const CefString& key) {
CEF_VALUE_VERIFY_RETURN(false, false);
@@ -373,83 +806,81 @@ CefRefPtr<CefListValue> CefDictionaryValueImpl::GetList(const CefString& key) {
return NULL;
}
bool CefDictionaryValueImpl::SetValue(const CefString& key,
CefRefPtr<CefValue> value) {
CEF_VALUE_VERIFY_RETURN(true, false);
CefValueImpl* impl = static_cast<CefValueImpl*>(value.get());
DCHECK(impl);
base::Value* new_value =
impl->CopyOrTransferValue(mutable_value(), false, controller());
SetInternal(key, new_value);
return true;
}
bool CefDictionaryValueImpl::SetNull(const CefString& key) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key, base::Value::CreateNullValue());
SetInternal(key, base::Value::CreateNullValue());
return true;
}
bool CefDictionaryValueImpl::SetBool(const CefString& key, bool value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key,
new base::FundamentalValue(value));
SetInternal(key, new base::FundamentalValue(value));
return true;
}
bool CefDictionaryValueImpl::SetInt(const CefString& key, int value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key,
new base::FundamentalValue(value));
SetInternal(key, new base::FundamentalValue(value));
return true;
}
bool CefDictionaryValueImpl::SetDouble(const CefString& key, double value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key,
new base::FundamentalValue(value));
SetInternal(key, new base::FundamentalValue(value));
return true;
}
bool CefDictionaryValueImpl::SetString(const CefString& key,
const CefString& value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key,
new base::StringValue(value.ToString()));
SetInternal(key, new base::StringValue(value.ToString()));
return true;
}
bool CefDictionaryValueImpl::SetBinary(const CefString& key,
CefRefPtr<CefBinaryValue> value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
CefBinaryValueImpl* impl = static_cast<CefBinaryValueImpl*>(value.get());
DCHECK(impl);
mutable_value()->SetWithoutPathExpansion(key,
impl->CopyOrDetachValue(controller()));
SetInternal(key, impl->CopyOrDetachValue(controller()));
return true;
}
bool CefDictionaryValueImpl::SetDictionary(
const CefString& key, CefRefPtr<CefDictionaryValue> value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
CefDictionaryValueImpl* impl =
static_cast<CefDictionaryValueImpl*>(value.get());
DCHECK(impl);
mutable_value()->SetWithoutPathExpansion(key,
impl->CopyOrDetachValue(controller()));
SetInternal(key, impl->CopyOrDetachValue(controller()));
return true;
}
bool CefDictionaryValueImpl::SetList(const CefString& key,
CefRefPtr<CefListValue> value) {
CEF_VALUE_VERIFY_RETURN(true, false);
RemoveInternal(key);
CefListValueImpl* impl = static_cast<CefListValueImpl*>(value.get());
DCHECK(impl);
mutable_value()->SetWithoutPathExpansion(key,
impl->CopyOrDetachValue(controller()));
SetInternal(key, impl->CopyOrDetachValue(controller()));
return true;
}
@@ -470,6 +901,13 @@ bool CefDictionaryValueImpl::RemoveInternal(const CefString& key) {
return true;
}
void CefDictionaryValueImpl::SetInternal(const CefString& key,
base::Value* value) {
DCHECK(value);
RemoveInternal(key);
mutable_value()->SetWithoutPathExpansion(key, value);
}
CefDictionaryValueImpl::CefDictionaryValueImpl(
base::DictionaryValue* value,
void* parent_value,
@@ -485,8 +923,7 @@ CefDictionaryValueImpl::CefDictionaryValueImpl(
// static
CefRefPtr<CefListValue> CefListValue::Create() {
return new CefListValueImpl(new base::ListValue(),
NULL, CefListValueImpl::kOwnerWillDelete, false, NULL);
return new CefListValueImpl(new base::ListValue(), true, false);
}
// static
@@ -532,6 +969,16 @@ base::ListValue* CefListValueImpl::CopyOrDetachValue(
return new_value;
}
bool CefListValueImpl::IsSameValue(const base::ListValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return (&const_value() == that);
}
bool CefListValueImpl::IsEqualValue(const base::ListValue* that) {
CEF_VALUE_VERIFY_RETURN(false, false);
return const_value().Equals(that);
}
bool CefListValueImpl::IsValid() {
return !detached();
}
@@ -544,6 +991,28 @@ bool CefListValueImpl::IsReadOnly() {
return read_only();
}
bool CefListValueImpl::IsSame(CefRefPtr<CefListValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefListValueImpl*>(that.get())->
IsSameValue(&const_value());
}
bool CefListValueImpl::IsEqual(CefRefPtr<CefListValue> that) {
if (!that.get())
return false;
if (that.get() == this)
return true;
CEF_VALUE_VERIFY_RETURN(false, false);
return static_cast<CefListValueImpl*>(that.get())->
IsEqualValue(&const_value());
}
CefRefPtr<CefListValue> CefListValueImpl::Copy() {
CEF_VALUE_VERIFY_RETURN(false, NULL);
@@ -614,6 +1083,21 @@ CefValueType CefListValueImpl::GetType(int index) {
return VTYPE_INVALID;
}
CefRefPtr<CefValue> CefListValueImpl::GetValue(int index) {
CEF_VALUE_VERIFY_RETURN(false, NULL);
const base::Value* out_value = NULL;
if (const_value().Get(index, &out_value)) {
return CefValueImpl::GetOrCreateRefOrCopy(
const_cast<base::Value*>(out_value),
const_cast<base::ListValue*>(&const_value()),
read_only(),
controller());
}
return NULL;
}
bool CefListValueImpl::GetBool(int index) {
CEF_VALUE_VERIFY_RETURN(false, false);
@@ -717,53 +1201,45 @@ CefRefPtr<CefListValue> CefListValueImpl::GetList(int index) {
return NULL;
}
bool CefListValueImpl::SetValue(int index, CefRefPtr<CefValue> value) {
CEF_VALUE_VERIFY_RETURN(true, false);
CefValueImpl* impl = static_cast<CefValueImpl*>(value.get());
DCHECK(impl);
base::Value* new_value =
impl->CopyOrTransferValue(mutable_value(), false, controller());
SetInternal(index, new_value);
return true;
}
bool CefListValueImpl::SetNull(int index) {
CEF_VALUE_VERIFY_RETURN(true, false);
base::Value* new_value = base::Value::CreateNullValue();
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, base::Value::CreateNullValue());
return true;
}
bool CefListValueImpl::SetBool(int index, bool value) {
CEF_VALUE_VERIFY_RETURN(true, false);
base::Value* new_value = new base::FundamentalValue(value);
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, new base::FundamentalValue(value));
return true;
}
bool CefListValueImpl::SetInt(int index, int value) {
CEF_VALUE_VERIFY_RETURN(true, false);
base::Value* new_value = new base::FundamentalValue(value);
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, new base::FundamentalValue(value));
return true;
}
bool CefListValueImpl::SetDouble(int index, double value) {
CEF_VALUE_VERIFY_RETURN(true, false);
base::Value* new_value = new base::FundamentalValue(value);
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, new base::FundamentalValue(value));
return true;
}
bool CefListValueImpl::SetString(int index, const CefString& value) {
CEF_VALUE_VERIFY_RETURN(true, false);
base::Value* new_value = new base::StringValue(value.ToString());
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, new base::StringValue(value.ToString()));
return true;
}
@@ -773,11 +1249,7 @@ bool CefListValueImpl::SetBinary(int index, CefRefPtr<CefBinaryValue> value) {
CefBinaryValueImpl* impl = static_cast<CefBinaryValueImpl*>(value.get());
DCHECK(impl);
base::Value* new_value = impl->CopyOrDetachValue(controller());
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, impl->CopyOrDetachValue(controller()));
return true;
}
@@ -789,11 +1261,7 @@ bool CefListValueImpl::SetDictionary(int index,
static_cast<CefDictionaryValueImpl*>(value.get());
DCHECK(impl);
base::Value* new_value = impl->CopyOrDetachValue(controller());
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, impl->CopyOrDetachValue(controller()));
return true;
}
@@ -803,11 +1271,7 @@ bool CefListValueImpl::SetList(int index, CefRefPtr<CefListValue> value) {
CefListValueImpl* impl = static_cast<CefListValueImpl*>(value.get());
DCHECK(impl);
base::Value* new_value = impl->CopyOrDetachValue(controller());
if (RemoveInternal(index))
mutable_value()->Insert(index, new_value);
else
mutable_value()->Set(index, new_value);
SetInternal(index, impl->CopyOrDetachValue(controller()));
return true;
}
@@ -828,6 +1292,14 @@ bool CefListValueImpl::RemoveInternal(int index) {
return true;
}
void CefListValueImpl::SetInternal(int index, base::Value* value) {
DCHECK(value);
if (RemoveInternal(index))
mutable_value()->Insert(index, value);
else
mutable_value()->Set(index, value);
}
CefListValueImpl::CefListValueImpl(
base::ListValue* value,
void* parent_value,

View File

@@ -15,6 +15,83 @@
#include "base/threading/platform_thread.h"
// CefValue implementation
class CefValueImpl : public CefValue {
public:
// Get or create a reference to a complex value or copy a simple value.
static CefRefPtr<CefValue> GetOrCreateRefOrCopy(
base::Value* value,
void* parent_value,
bool read_only,
CefValueController* controller);
CefValueImpl();
// Take ownership of |value|. Do not pass in a value owned by something else
// (use GetOrCreateRefOrCopy instead).
explicit CefValueImpl(base::Value* value);
// Keep a reference to |value|.
explicit CefValueImpl(CefRefPtr<CefBinaryValue> value);
explicit CefValueImpl(CefRefPtr<CefDictionaryValue> value);
explicit CefValueImpl(CefRefPtr<CefListValue> value);
~CefValueImpl() override;
// Take ownership of |value|. Do not pass in a value owned by something else
// (use GetOrCreateRefOrCopy or Set*() instead).
void SetValue(base::Value* value);
// Copy a simple value or transfer ownership of a complex value. If ownership
// of the value is tranferred then this object's internal reference to the
// value will be updated and remain valid.
base::Value* CopyOrTransferValue(void* new_parent_value,
bool new_read_only,
CefValueController* new_controller);
// CefValue methods.
bool IsValid() override;
bool IsOwned() override;
bool IsReadOnly() override;
bool IsSame(CefRefPtr<CefValue> that) override;
bool IsEqual(CefRefPtr<CefValue> that) override;
CefRefPtr<CefValue> Copy() override;
CefValueType GetType() override;
bool GetBool() override;
int GetInt() override;
double GetDouble() override;
CefString GetString() override;
CefRefPtr<CefBinaryValue> GetBinary() override;
CefRefPtr<CefDictionaryValue> GetDictionary() override;
CefRefPtr<CefListValue> GetList() override;
bool SetNull() override;
bool SetBool(bool value) override;
bool SetInt(int value) override;
bool SetDouble(double value) override;
bool SetString(const CefString& value) override;
bool SetBinary(CefRefPtr<CefBinaryValue> value) override;
bool SetDictionary(CefRefPtr<CefDictionaryValue> value) override;
bool SetList(CefRefPtr<CefListValue> value) override;
private:
void SetValueInternal(base::Value* value);
// Access to all members must be protected by |lock_|.
base::Lock lock_;
// Simple values only.
scoped_ptr<base::Value> value_;
// Complex values.
CefRefPtr<CefBinaryValue> binary_value_;
CefRefPtr<CefDictionaryValue> dictionary_value_;
CefRefPtr<CefListValue> list_value_;
IMPLEMENT_REFCOUNTING(CefValueImpl);
DISALLOW_COPY_AND_ASSIGN(CefValueImpl);
};
// CefBinaryValue implementation
class CefBinaryValueImpl
: public CefValueBase<CefBinaryValue, base::BinaryValue> {
@@ -25,21 +102,36 @@ class CefBinaryValueImpl
void* parent_value,
CefValueController* controller);
// Simple constructor for referencing existing value objects.
// Reference an existing value (set |will_delete| to false) or take ownership
// of an existing value (set |will_delete| to true). When referencing an
// existing value you must explicitly call Detach(NULL) when |value| is no
// longer valid. Use GetOrCreateRef instead of this constructor if |value| is
// owned by some other object and you do not plan to explicitly call
// Detach(NULL).
CefBinaryValueImpl(base::BinaryValue* value,
bool will_delete,
bool read_only);
bool will_delete);
// If |copy| is false this object will take ownership of the specified |data|
// buffer instead of copying it.
CefBinaryValueImpl(char* data,
size_t data_size,
bool copy);
// Return a copy of the value.
base::BinaryValue* CopyValue();
// If a reference return a copy of the value otherwise detach the value to the
// specified |new_controller|.
// If this value is a reference then return a copy. Otherwise, detach and
// transfer ownership of the value.
base::BinaryValue* CopyOrDetachValue(CefValueController* new_controller);
bool IsSameValue(const base::BinaryValue* that);
bool IsEqualValue(const base::BinaryValue* that);
// CefBinaryValue methods.
bool IsValid() override;
bool IsOwned() override;
bool IsSame(CefRefPtr<CefBinaryValue> that) override;
bool IsEqual(CefRefPtr<CefBinaryValue> that) override;
CefRefPtr<CefBinaryValue> Copy() override;
size_t GetSize() override;
size_t GetData(void* buffer,
@@ -53,14 +145,6 @@ class CefBinaryValueImpl
void* parent_value,
ValueMode value_mode,
CefValueController* controller);
// If |copy| is false this object will take ownership of the specified |data|
// buffer instead of copying it.
CefBinaryValueImpl(char* data,
size_t data_size,
bool copy);
// For the Create() method.
friend class CefBinaryValue;
DISALLOW_COPY_AND_ASSIGN(CefBinaryValueImpl);
};
@@ -77,7 +161,12 @@ class CefDictionaryValueImpl
bool read_only,
CefValueController* controller);
// Simple constructor for referencing existing value objects.
// Reference an existing value (set |will_delete| to false) or take ownership
// of an existing value (set |will_delete| to true). When referencing an
// existing value you must explicitly call Detach(NULL) when |value| is no
// longer valid. Use GetOrCreateRef instead of this constructor if |value| is
// owned by some other object and you do not plan to explicitly call
// Detach(NULL).
CefDictionaryValueImpl(base::DictionaryValue* value,
bool will_delete,
bool read_only);
@@ -85,14 +174,19 @@ class CefDictionaryValueImpl
// Return a copy of the value.
base::DictionaryValue* CopyValue();
// If a reference return a copy of the value otherwise detach the value to the
// specified |new_controller|.
// If this value is a reference then return a copy. Otherwise, detach and
// transfer ownership of the value.
base::DictionaryValue* CopyOrDetachValue(CefValueController* new_controller);
bool IsSameValue(const base::DictionaryValue* that);
bool IsEqualValue(const base::DictionaryValue* that);
// CefDictionaryValue methods.
bool IsValid() override;
bool IsOwned() override;
bool IsReadOnly() override;
bool IsSame(CefRefPtr<CefDictionaryValue> that) override;
bool IsEqual(CefRefPtr<CefDictionaryValue> that) override;
CefRefPtr<CefDictionaryValue> Copy(
bool exclude_empty_children) override;
size_t GetSize() override;
@@ -101,6 +195,7 @@ class CefDictionaryValueImpl
bool GetKeys(KeyList& keys) override;
bool Remove(const CefString& key) override;
CefValueType GetType(const CefString& key) override;
CefRefPtr<CefValue> GetValue(const CefString& key) override;
bool GetBool(const CefString& key) override;
int GetInt(const CefString& key) override;
double GetDouble(const CefString& key) override;
@@ -109,6 +204,7 @@ class CefDictionaryValueImpl
CefRefPtr<CefDictionaryValue> GetDictionary(
const CefString& key) override;
CefRefPtr<CefListValue> GetList(const CefString& key) override;
bool SetValue(const CefString& key, CefRefPtr<CefValue> value) override;
bool SetNull(const CefString& key) override;
bool SetBool(const CefString& key, bool value) override;
bool SetInt(const CefString& key, int value) override;
@@ -131,9 +227,7 @@ class CefDictionaryValueImpl
CefValueController* controller);
bool RemoveInternal(const CefString& key);
// For the Create() method.
friend class CefDictionaryValue;
void SetInternal(const CefString& key, base::Value* value);
DISALLOW_COPY_AND_ASSIGN(CefDictionaryValueImpl);
};
@@ -150,7 +244,12 @@ class CefListValueImpl
bool read_only,
CefValueController* controller);
// Simple constructor for referencing existing value objects.
// Reference an existing value (set |will_delete| to false) or take ownership
// of an existing value (set |will_delete| to true). When referencing an
// existing value you must explicitly call Detach(NULL) when |value| is no
// longer valid. Use GetOrCreateRef instead of this constructor if |value| is
// owned by some other object and you do not plan to explicitly call
// Detach(NULL).
CefListValueImpl(base::ListValue* value,
bool will_delete,
bool read_only);
@@ -158,20 +257,26 @@ class CefListValueImpl
// Return a copy of the value.
base::ListValue* CopyValue();
// If a reference return a copy of the value otherwise detach the value to the
// specified |new_controller|.
// If this value is a reference then return a copy. Otherwise, detach and
// transfer ownership of the value.
base::ListValue* CopyOrDetachValue(CefValueController* new_controller);
bool IsSameValue(const base::ListValue* that);
bool IsEqualValue(const base::ListValue* that);
/// CefListValue methods.
bool IsValid() override;
bool IsOwned() override;
bool IsReadOnly() override;
bool IsSame(CefRefPtr<CefListValue> that) override;
bool IsEqual(CefRefPtr<CefListValue> that) override;
CefRefPtr<CefListValue> Copy() override;
bool SetSize(size_t size) override;
size_t GetSize() override;
bool Clear() override;
bool Remove(int index) override;
CefValueType GetType(int index) override;
CefRefPtr<CefValue> GetValue(int index) override;
bool GetBool(int index) override;
int GetInt(int index) override;
double GetDouble(int index) override;
@@ -179,6 +284,7 @@ class CefListValueImpl
CefRefPtr<CefBinaryValue> GetBinary(int index) override;
CefRefPtr<CefDictionaryValue> GetDictionary(int index) override;
CefRefPtr<CefListValue> GetList(int index) override;
bool SetValue(int index, CefRefPtr<CefValue> value) override;
bool SetNull(int index) override;
bool SetBool(int index, bool value) override;
bool SetInt(int index, int value) override;
@@ -198,9 +304,7 @@ class CefListValueImpl
CefValueController* controller);
bool RemoveInternal(int index);
// For the Create() method.
friend class CefListValue;
void SetInternal(int index, base::Value* value);
DISALLOW_COPY_AND_ASSIGN(CefListValueImpl);
};