1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_COMMON_VALUES_IMPL_H_ 6 #define CEF_LIBCEF_COMMON_VALUES_IMPL_H_ 7 #pragma once 8 9 #include <vector> 10 11 #include "include/cef_values.h" 12 #include "libcef/common/value_base.h" 13 14 #include "base/threading/platform_thread.h" 15 #include "base/values.h" 16 17 // CefValue implementation 18 class CefValueImpl : public CefValue { 19 public: 20 // Get or create a reference to a complex value or copy a simple value. 21 static CefRefPtr<CefValue> GetOrCreateRefOrCopy( 22 base::Value* value, 23 void* parent_value, 24 bool read_only, 25 CefValueController* controller); 26 27 CefValueImpl(); 28 29 // Take ownership of |value|. Do not pass in a value owned by something else 30 // (use GetOrCreateRefOrCopy instead). 31 explicit CefValueImpl(base::Value* value); 32 33 // Keep a reference to |value|. 34 explicit CefValueImpl(CefRefPtr<CefBinaryValue> value); 35 explicit CefValueImpl(CefRefPtr<CefDictionaryValue> value); 36 explicit CefValueImpl(CefRefPtr<CefListValue> value); 37 38 CefValueImpl(const CefValueImpl&) = delete; 39 CefValueImpl& operator=(const CefValueImpl&) = delete; 40 41 ~CefValueImpl() override; 42 43 // Take ownership of |value|. Do not pass in a value owned by something else 44 // (use GetOrCreateRefOrCopy or Set*() instead). 45 void SetValue(base::Value* value); 46 47 // Copy a simple value or transfer ownership of a complex value. If ownership 48 // of the value is tranferred then this object's internal reference to the 49 // value will be updated and remain valid. base::Value now uses move semantics 50 // so we need to perform the copy and swap in two steps. 51 base::Value* CopyOrDetachValue(CefValueController* new_controller); 52 void SwapValue(base::Value* new_value, 53 void* new_parent_value, 54 CefValueController* new_controller); 55 56 // Returns a reference to the underlying data. Access must be protected by 57 // calling AcquireLock/ReleaseLock. 58 base::Value* GetValueUnsafe() const; 59 60 // CefValue methods. 61 bool IsValid() override; 62 bool IsOwned() override; 63 bool IsReadOnly() override; 64 bool IsSame(CefRefPtr<CefValue> that) override; 65 bool IsEqual(CefRefPtr<CefValue> that) override; 66 CefRefPtr<CefValue> Copy() override; 67 CefValueType GetType() override; 68 bool GetBool() override; 69 int GetInt() override; 70 double GetDouble() override; 71 CefString GetString() override; 72 CefRefPtr<CefBinaryValue> GetBinary() override; 73 CefRefPtr<CefDictionaryValue> GetDictionary() override; 74 CefRefPtr<CefListValue> GetList() override; 75 bool SetNull() override; 76 bool SetBool(bool value) override; 77 bool SetInt(int value) override; 78 bool SetDouble(double value) override; 79 bool SetString(const CefString& value) override; 80 bool SetBinary(CefRefPtr<CefBinaryValue> value) override; 81 bool SetDictionary(CefRefPtr<CefDictionaryValue> value) override; 82 bool SetList(CefRefPtr<CefListValue> value) override; 83 84 // Ensures exclusive access to the underlying data for the life of this scoped 85 // object. 86 class ScopedLockedValue { 87 public: ScopedLockedValue(CefRefPtr<CefValueImpl> impl)88 explicit ScopedLockedValue(CefRefPtr<CefValueImpl> impl) : impl_(impl) { 89 impl_->AcquireLock(); 90 } 91 92 ScopedLockedValue(const ScopedLockedValue&) = delete; 93 ScopedLockedValue& operator=(const ScopedLockedValue&) = delete; 94 ~ScopedLockedValue()95 ~ScopedLockedValue() { impl_->ReleaseLock(); } 96 value()97 base::Value* value() const { return impl_->GetValueUnsafe(); } 98 99 private: 100 CefRefPtr<CefValueImpl> impl_; 101 }; 102 103 private: 104 void SetValueInternal(base::Value* value); 105 106 // Returns the controller for the current value, if any. 107 CefValueController* GetValueController() const; 108 109 // Explicitly lock/unlock this object and the underlying data. 110 void AcquireLock(); 111 void ReleaseLock(); 112 113 // Access to all members must be protected by |lock_|. 114 base::Lock lock_; 115 116 // Simple values only. 117 std::unique_ptr<base::Value> value_; 118 119 // Complex values. 120 CefRefPtr<CefBinaryValue> binary_value_; 121 CefRefPtr<CefDictionaryValue> dictionary_value_; 122 CefRefPtr<CefListValue> list_value_; 123 124 IMPLEMENT_REFCOUNTING(CefValueImpl); 125 }; 126 127 // CefBinaryValue implementation 128 class CefBinaryValueImpl : public CefValueBase<CefBinaryValue, base::Value> { 129 public: 130 // Get or create a reference value. 131 static CefRefPtr<CefBinaryValue> GetOrCreateRef( 132 base::Value* value, 133 void* parent_value, 134 CefValueController* controller); 135 136 // Reference an existing value (set |will_delete| to false) or take ownership 137 // of an existing value (set |will_delete| to true). When referencing an 138 // existing value you must explicitly call Detach(nullptr) when |value| is no 139 // longer valid. Use GetOrCreateRef instead of this constructor if |value| is 140 // owned by some other object and you do not plan to explicitly call 141 // Detach(nullptr). 142 CefBinaryValueImpl(base::Value* value, bool will_delete); 143 144 // The data will always be copied. 145 CefBinaryValueImpl(char* data, size_t data_size); 146 147 CefBinaryValueImpl(const CefBinaryValueImpl&) = delete; 148 CefBinaryValueImpl& operator=(const CefBinaryValueImpl&) = delete; 149 150 // Return a copy of the value. 151 base::Value* CopyValue() WARN_UNUSED_RESULT; 152 153 // If this value is a reference then return a copy. Otherwise, detach and 154 // transfer ownership of the value. 155 base::Value* CopyOrDetachValue(CefValueController* new_controller) 156 WARN_UNUSED_RESULT; 157 158 bool IsSameValue(const base::Value* that); 159 bool IsEqualValue(const base::Value* that); 160 161 // Returns the underlying value. Access must be protected by calling 162 // lock/unlock on the controller. 163 base::Value* GetValueUnsafe(); 164 165 // CefBinaryValue methods. 166 bool IsValid() override; 167 bool IsOwned() override; 168 bool IsSame(CefRefPtr<CefBinaryValue> that) override; 169 bool IsEqual(CefRefPtr<CefBinaryValue> that) override; 170 CefRefPtr<CefBinaryValue> Copy() override; 171 size_t GetSize() override; 172 size_t GetData(void* buffer, size_t buffer_size, size_t data_offset) override; 173 174 private: 175 // See the CefValueBase constructor for usage. Binary values are always 176 // read-only. 177 CefBinaryValueImpl(base::Value* value, 178 void* parent_value, 179 ValueMode value_mode, 180 CefValueController* controller); 181 }; 182 183 // CefDictionaryValue implementation 184 class CefDictionaryValueImpl 185 : public CefValueBase<CefDictionaryValue, base::DictionaryValue> { 186 public: 187 // Get or create a reference value. 188 static CefRefPtr<CefDictionaryValue> GetOrCreateRef( 189 base::DictionaryValue* value, 190 void* parent_value, 191 bool read_only, 192 CefValueController* controller); 193 194 // Reference an existing value (set |will_delete| to false) or take ownership 195 // of an existing value (set |will_delete| to true). When referencing an 196 // existing value you must explicitly call Detach(nullptr) when |value| is no 197 // longer valid. Use GetOrCreateRef instead of this constructor if |value| is 198 // owned by some other object and you do not plan to explicitly call 199 // Detach(nullptr). 200 CefDictionaryValueImpl(base::DictionaryValue* value, 201 bool will_delete, 202 bool read_only); 203 204 CefDictionaryValueImpl(const CefDictionaryValueImpl&) = delete; 205 CefDictionaryValueImpl& operator=(const CefDictionaryValueImpl&) = delete; 206 207 // Return a copy of the value. 208 base::DictionaryValue* CopyValue() WARN_UNUSED_RESULT; 209 210 // If this value is a reference then return a copy. Otherwise, detach and 211 // transfer ownership of the value. 212 base::DictionaryValue* CopyOrDetachValue(CefValueController* new_controller) 213 WARN_UNUSED_RESULT; 214 215 bool IsSameValue(const base::DictionaryValue* that); 216 bool IsEqualValue(const base::DictionaryValue* that); 217 218 // Returns the underlying value. Access must be protected by calling 219 // lock/unlock on the controller. 220 base::DictionaryValue* GetValueUnsafe(); 221 222 // CefDictionaryValue methods. 223 bool IsValid() override; 224 bool IsOwned() override; 225 bool IsReadOnly() override; 226 bool IsSame(CefRefPtr<CefDictionaryValue> that) override; 227 bool IsEqual(CefRefPtr<CefDictionaryValue> that) override; 228 CefRefPtr<CefDictionaryValue> Copy(bool exclude_empty_children) override; 229 size_t GetSize() override; 230 bool Clear() override; 231 bool HasKey(const CefString& key) override; 232 bool GetKeys(KeyList& keys) override; 233 bool Remove(const CefString& key) override; 234 CefValueType GetType(const CefString& key) override; 235 CefRefPtr<CefValue> GetValue(const CefString& key) override; 236 bool GetBool(const CefString& key) override; 237 int GetInt(const CefString& key) override; 238 double GetDouble(const CefString& key) override; 239 CefString GetString(const CefString& key) override; 240 CefRefPtr<CefBinaryValue> GetBinary(const CefString& key) override; 241 CefRefPtr<CefDictionaryValue> GetDictionary(const CefString& key) override; 242 CefRefPtr<CefListValue> GetList(const CefString& key) override; 243 bool SetValue(const CefString& key, CefRefPtr<CefValue> value) override; 244 bool SetNull(const CefString& key) override; 245 bool SetBool(const CefString& key, bool value) override; 246 bool SetInt(const CefString& key, int value) override; 247 bool SetDouble(const CefString& key, double value) override; 248 bool SetString(const CefString& key, const CefString& value) override; 249 bool SetBinary(const CefString& key, 250 CefRefPtr<CefBinaryValue> value) override; 251 bool SetDictionary(const CefString& key, 252 CefRefPtr<CefDictionaryValue> value) override; 253 bool SetList(const CefString& key, CefRefPtr<CefListValue> value) override; 254 255 private: 256 // See the CefValueBase constructor for usage. 257 CefDictionaryValueImpl(base::DictionaryValue* value, 258 void* parent_value, 259 ValueMode value_mode, 260 bool read_only, 261 CefValueController* controller); 262 263 bool RemoveInternal(const CefString& key); 264 base::Value* SetInternal(const CefString& key, base::Value* value); 265 }; 266 267 // CefListValue implementation 268 class CefListValueImpl : public CefValueBase<CefListValue, base::ListValue> { 269 public: 270 // Get or create a reference value. 271 static CefRefPtr<CefListValue> GetOrCreateRef(base::ListValue* value, 272 void* parent_value, 273 bool read_only, 274 CefValueController* controller); 275 276 // Reference an existing value (set |will_delete| to false) or take ownership 277 // of an existing value (set |will_delete| to true). When referencing an 278 // existing value you must explicitly call Detach(nullptr) when |value| is no 279 // longer valid. Use GetOrCreateRef instead of this constructor if |value| is 280 // owned by some other object and you do not plan to explicitly call 281 // Detach(nullptr). 282 CefListValueImpl(base::ListValue* value, bool will_delete, bool read_only); 283 284 CefListValueImpl(const CefListValueImpl&) = delete; 285 CefListValueImpl& operator=(const CefListValueImpl&) = delete; 286 287 // Return a copy of the value. 288 base::ListValue* CopyValue() WARN_UNUSED_RESULT; 289 290 // If this value is a reference then return a copy. Otherwise, detach and 291 // transfer ownership of the value. 292 base::ListValue* CopyOrDetachValue(CefValueController* new_controller) 293 WARN_UNUSED_RESULT; 294 295 bool IsSameValue(const base::ListValue* that); 296 bool IsEqualValue(const base::ListValue* that); 297 298 // Returns the underlying value. Access must be protected by calling 299 // lock/unlock on the controller. 300 base::ListValue* GetValueUnsafe(); 301 302 // CefListValue methods. 303 bool IsValid() override; 304 bool IsOwned() override; 305 bool IsReadOnly() override; 306 bool IsSame(CefRefPtr<CefListValue> that) override; 307 bool IsEqual(CefRefPtr<CefListValue> that) override; 308 CefRefPtr<CefListValue> Copy() override; 309 bool SetSize(size_t size) override; 310 size_t GetSize() override; 311 bool Clear() override; 312 bool Remove(size_t index) override; 313 CefValueType GetType(size_t index) override; 314 CefRefPtr<CefValue> GetValue(size_t index) override; 315 bool GetBool(size_t index) override; 316 int GetInt(size_t index) override; 317 double GetDouble(size_t index) override; 318 CefString GetString(size_t index) override; 319 CefRefPtr<CefBinaryValue> GetBinary(size_t index) override; 320 CefRefPtr<CefDictionaryValue> GetDictionary(size_t index) override; 321 CefRefPtr<CefListValue> GetList(size_t index) override; 322 bool SetValue(size_t index, CefRefPtr<CefValue> value) override; 323 bool SetNull(size_t index) override; 324 bool SetBool(size_t index, bool value) override; 325 bool SetInt(size_t index, int value) override; 326 bool SetDouble(size_t index, double value) override; 327 bool SetString(size_t index, const CefString& value) override; 328 bool SetBinary(size_t index, CefRefPtr<CefBinaryValue> value) override; 329 bool SetDictionary(size_t index, 330 CefRefPtr<CefDictionaryValue> value) override; 331 bool SetList(size_t index, CefRefPtr<CefListValue> value) override; 332 333 private: 334 // See the CefValueBase constructor for usage. 335 CefListValueImpl(base::ListValue* value, 336 void* parent_value, 337 ValueMode value_mode, 338 bool read_only, 339 CefValueController* controller); 340 341 bool RemoveInternal(size_t index); 342 base::Value* SetInternal(size_t index, base::Value* value); 343 }; 344 345 #endif // CEF_LIBCEF_COMMON_VALUES_IMPL_H_ 346