CefDictionaryValue.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class representing a dictionary value. Can be used on any process and thread.
  10. /// </summary>
  11. public sealed unsafe partial class CefDictionaryValue : ICefDictionaryValue
  12. {
  13. /// <summary>
  14. /// Creates a new object that is not owned by any other object.
  15. /// </summary>
  16. public static CefDictionaryValue Create()
  17. {
  18. return CefDictionaryValue.FromNative(
  19. cef_dictionary_value_t.create()
  20. );
  21. }
  22. /// <summary>
  23. /// Returns true if this object is valid. This object may become invalid if
  24. /// the underlying data is owned by another object (e.g. list or dictionary)
  25. /// and that other object is then modified or destroyed. Do not call any other
  26. /// methods if this method returns false.
  27. /// </summary>
  28. public bool IsValid
  29. {
  30. get { return cef_dictionary_value_t.is_valid(_self) != 0; }
  31. }
  32. /// <summary>
  33. /// Returns true if this object is currently owned by another object.
  34. /// </summary>
  35. public bool IsOwned
  36. {
  37. get { return cef_dictionary_value_t.is_owned(_self) != 0; }
  38. }
  39. /// <summary>
  40. /// Returns true if the values of this object are read-only. Some APIs may
  41. /// expose read-only objects.
  42. /// </summary>
  43. public bool IsReadOnly
  44. {
  45. get { return cef_dictionary_value_t.is_read_only(_self) != 0; }
  46. }
  47. /// <summary>
  48. /// Returns true if this object and |that| object have the same underlying
  49. /// data. If true modifications to this object will also affect |that| object
  50. /// and vice-versa.
  51. /// </summary>
  52. public bool IsSame(ICefDictionaryValue that)
  53. {
  54. return cef_dictionary_value_t.is_same(_self, ((CefDictionaryValue)that).ToNative()) != 0;
  55. }
  56. /// <summary>
  57. /// Returns true if this object and |that| object have an equivalent underlying
  58. /// value but are not necessarily the same object.
  59. /// </summary>
  60. public bool IsEqual(ICefDictionaryValue that)
  61. {
  62. return cef_dictionary_value_t.is_equal(_self, ((CefDictionaryValue)that).ToNative()) != 0;
  63. }
  64. /// <summary>
  65. /// Returns a writable copy of this object. If |exclude_empty_children| is true
  66. /// any empty dictionaries or lists will be excluded from the copy.
  67. /// </summary>
  68. public ICefDictionaryValue Copy(bool excludeEmptyChildren)
  69. {
  70. return CefDictionaryValue.FromNative(
  71. cef_dictionary_value_t.copy(_self, excludeEmptyChildren ? 1 : 0)
  72. );
  73. }
  74. /// <summary>
  75. /// Returns the number of values.
  76. /// </summary>
  77. public int Count
  78. {
  79. get { return (int)cef_dictionary_value_t.get_size(_self); }
  80. }
  81. /// <summary>
  82. /// Removes all values. Returns true on success.
  83. /// </summary>
  84. public bool Clear()
  85. {
  86. return cef_dictionary_value_t.clear(_self) != 0;
  87. }
  88. /// <summary>
  89. /// Returns true if the current dictionary has a value for the given key.
  90. /// </summary>
  91. public bool HasKey(string key)
  92. {
  93. fixed (char* key_str = key)
  94. {
  95. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  96. return cef_dictionary_value_t.has_key(_self, &n_key) != 0;
  97. }
  98. }
  99. /// <summary>
  100. /// Reads all keys for this dictionary into the specified vector.
  101. /// </summary>
  102. public string[] GetKeys()
  103. {
  104. var list = libcef.string_list_alloc();
  105. var success = cef_dictionary_value_t.get_keys(_self, list) != 0;
  106. if (!success) throw new InvalidOperationException(); // TODO: use ExceptionBuilder
  107. var result = cef_string_list.ToArray(list);
  108. libcef.string_list_free(list);
  109. return result;
  110. }
  111. /// <summary>
  112. /// Removes the value at the specified key. Returns true is the value was
  113. /// removed successfully.
  114. /// </summary>
  115. public bool Remove(string key)
  116. {
  117. fixed (char* key_str = key)
  118. {
  119. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  120. return cef_dictionary_value_t.remove(_self, &n_key) != 0;
  121. }
  122. }
  123. /// <summary>
  124. /// Returns the value type for the specified key.
  125. /// </summary>
  126. public CefValueType GetValueType(string key)
  127. {
  128. fixed (char* key_str = key)
  129. {
  130. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  131. return cef_dictionary_value_t.get_type(_self, &n_key);
  132. }
  133. }
  134. /// <summary>
  135. /// Returns the value at the specified key. For simple types the returned
  136. /// value will copy existing data and modifications to the value will not
  137. /// modify this object. For complex types (binary, dictionary and list) the
  138. /// returned value will reference existing data and modifications to the value
  139. /// will modify this object.
  140. /// </summary>
  141. public CefValue GetValue(string key)
  142. {
  143. fixed (char* key_str = key)
  144. {
  145. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  146. var n_result = cef_dictionary_value_t.get_value(_self, &n_key);
  147. return CefValue.FromNativeOrNull(n_result);
  148. }
  149. }
  150. /// <summary>
  151. /// Returns the value at the specified key as type bool.
  152. /// </summary>
  153. public bool GetBool(string key)
  154. {
  155. fixed (char* key_str = key)
  156. {
  157. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  158. return cef_dictionary_value_t.get_bool(_self, &n_key) != 0;
  159. }
  160. }
  161. /// <summary>
  162. /// Returns the value at the specified key as type int.
  163. /// </summary>
  164. public int GetInt(string key)
  165. {
  166. fixed (char* key_str = key)
  167. {
  168. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  169. return cef_dictionary_value_t.get_int(_self, &n_key);
  170. }
  171. }
  172. /// <summary>
  173. /// Returns the value at the specified key as type double.
  174. /// </summary>
  175. public double GetDouble(string key)
  176. {
  177. fixed (char* key_str = key)
  178. {
  179. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  180. return cef_dictionary_value_t.get_double(_self, &n_key);
  181. }
  182. }
  183. /// <summary>
  184. /// Returns the value at the specified key as type string.
  185. /// </summary>
  186. public string GetString(string key)
  187. {
  188. fixed (char* key_str = key)
  189. {
  190. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  191. var n_result = cef_dictionary_value_t.get_string(_self, &n_key);
  192. return cef_string_userfree.ToString(n_result);
  193. }
  194. }
  195. /// <summary>
  196. /// Returns the value at the specified key as type binary. The returned
  197. /// value will reference existing data.
  198. /// </summary>
  199. public ICefBinaryValue GetBinary(string key)
  200. {
  201. fixed (char* key_str = key)
  202. {
  203. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  204. var n_result = cef_dictionary_value_t.get_binary(_self, &n_key);
  205. return CefBinaryValue.FromNative(n_result);
  206. }
  207. }
  208. /// <summary>
  209. /// Returns the value at the specified key as type dictionary. The returned
  210. /// value will reference existing data and modifications to the value will
  211. /// modify this object.
  212. /// </summary>
  213. public ICefDictionaryValue GetDictionary(string key)
  214. {
  215. fixed (char* key_str = key)
  216. {
  217. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  218. var n_result = cef_dictionary_value_t.get_dictionary(_self, &n_key);
  219. return CefDictionaryValue.FromNative(n_result);
  220. }
  221. }
  222. /// <summary>
  223. /// Returns the value at the specified key as type list. The returned value
  224. /// will reference existing data and modifications to the value will modify
  225. /// this object.
  226. /// </summary>
  227. public ICefListValue GetList(string key)
  228. {
  229. fixed (char* key_str = key)
  230. {
  231. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  232. var n_result = cef_dictionary_value_t.get_list(_self, &n_key);
  233. return CefListValue.FromNative(n_result);
  234. }
  235. }
  236. /// <summary>
  237. /// Sets the value at the specified key. Returns true if the value was set
  238. /// successfully. If |value| represents simple data then the underlying data
  239. /// will be copied and modifications to |value| will not modify this object. If
  240. /// |value| represents complex data (binary, dictionary or list) then the
  241. /// underlying data will be referenced and modifications to |value| will modify
  242. /// this object.
  243. /// </summary>
  244. public bool SetValue(string key, CefValue value)
  245. {
  246. fixed (char* key_str = key)
  247. {
  248. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  249. var n_value = value.ToNative();
  250. return cef_dictionary_value_t.set_value(_self, &n_key, n_value) != 0;
  251. }
  252. }
  253. /// <summary>
  254. /// Sets the value at the specified key as type null. Returns true if the
  255. /// value was set successfully.
  256. /// </summary>
  257. public bool SetNull(string key)
  258. {
  259. fixed (char* key_str = key)
  260. {
  261. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  262. return cef_dictionary_value_t.set_null(_self, &n_key) != 0;
  263. }
  264. }
  265. /// <summary>
  266. /// Sets the value at the specified key as type bool. Returns true if the
  267. /// value was set successfully.
  268. /// </summary>
  269. public bool SetBool(string key, bool value)
  270. {
  271. fixed (char* key_str = key)
  272. {
  273. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  274. return cef_dictionary_value_t.set_bool(_self, &n_key, value ? 1 : 0) != 0;
  275. }
  276. }
  277. /// <summary>
  278. /// Sets the value at the specified key as type int. Returns true if the
  279. /// value was set successfully.
  280. /// </summary>
  281. public bool SetInt(string key, int value)
  282. {
  283. fixed (char* key_str = key)
  284. {
  285. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  286. return cef_dictionary_value_t.set_int(_self, &n_key, value) != 0;
  287. }
  288. }
  289. /// <summary>
  290. /// Sets the value at the specified key as type double. Returns true if the
  291. /// value was set successfully.
  292. /// </summary>
  293. public bool SetDouble(string key, double value)
  294. {
  295. fixed (char* key_str = key)
  296. {
  297. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  298. return cef_dictionary_value_t.set_double(_self, &n_key, value) != 0;
  299. }
  300. }
  301. /// <summary>
  302. /// Sets the value at the specified key as type string. Returns true if the
  303. /// value was set successfully.
  304. /// </summary>
  305. public bool SetString(string key, string value)
  306. {
  307. fixed (char* key_str = key)
  308. fixed (char* value_str = value)
  309. {
  310. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  311. var n_value = new cef_string_t(value_str, value != null ? value.Length : 0);
  312. return cef_dictionary_value_t.set_string(_self, &n_key, &n_value) != 0;
  313. }
  314. }
  315. /// <summary>
  316. /// Sets the value at the specified key as type binary. Returns true if the
  317. /// value was set successfully. If |value| is currently owned by another object
  318. /// then the value will be copied and the |value| reference will not change.
  319. /// Otherwise, ownership will be transferred to this object and the |value|
  320. /// reference will be invalidated.
  321. /// </summary>
  322. public bool SetBinary(string key, ICefBinaryValue value)
  323. {
  324. //FIXME: what means reference will be invalidated ?
  325. if (value == null) throw new ArgumentNullException("value");
  326. fixed (char* key_str = key)
  327. {
  328. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  329. return cef_dictionary_value_t.set_binary(_self, &n_key, ((CefBinaryValue)value).ToNative()) != 0;
  330. }
  331. }
  332. /// <summary>
  333. /// Sets the value at the specified key as type dict. Returns true if the
  334. /// value was set successfully. If |value| is currently owned by another object
  335. /// then the value will be copied and the |value| reference will not change.
  336. /// Otherwise, ownership will be transferred to this object and the |value|
  337. /// reference will be invalidated.
  338. /// </summary>
  339. public bool SetDictionary(string key, ICefDictionaryValue value)
  340. {
  341. if (value == null) throw new ArgumentNullException("value");
  342. fixed (char* key_str = key)
  343. {
  344. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  345. return cef_dictionary_value_t.set_dictionary(_self, &n_key, ((CefDictionaryValue)value).ToNative()) != 0;
  346. }
  347. }
  348. /// <summary>
  349. /// Sets the value at the specified key as type list. Returns true if the
  350. /// value was set successfully. If |value| is currently owned by another object
  351. /// then the value will be copied and the |value| reference will not change.
  352. /// Otherwise, ownership will be transferred to this object and the |value|
  353. /// reference will be invalidated.
  354. /// </summary>
  355. public bool SetList(string key, ICefListValue value)
  356. {
  357. if (value == null) throw new ArgumentNullException("value");
  358. fixed (char* key_str = key)
  359. {
  360. var n_key = new cef_string_t(key_str, key != null ? key.Length : 0);
  361. return cef_dictionary_value_t.set_list(_self, &n_key, ((CefListValue)value).ToNative()) != 0;
  362. }
  363. }
  364. }
  365. }