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