Key.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_indexeddb_key_h__
  6. #define mozilla_dom_indexeddb_key_h__
  7. #include "js/RootingAPI.h"
  8. #include "nsString.h"
  9. class mozIStorageStatement;
  10. class mozIStorageValueArray;
  11. namespace IPC {
  12. template <typename> struct ParamTraits;
  13. } // namespace IPC
  14. namespace mozilla {
  15. namespace dom {
  16. namespace indexedDB {
  17. class Key
  18. {
  19. friend struct IPC::ParamTraits<Key>;
  20. nsCString mBuffer;
  21. public:
  22. enum {
  23. eTerminator = 0,
  24. eFloat = 0x10,
  25. eDate = 0x20,
  26. eString = 0x30,
  27. eBinary = 0x40,
  28. eArray = 0x50,
  29. eMaxType = eArray
  30. };
  31. static const uint8_t kMaxArrayCollapse = uint8_t(3);
  32. static const uint8_t kMaxRecursionDepth = uint8_t(64);
  33. Key()
  34. {
  35. Unset();
  36. }
  37. explicit
  38. Key(const nsACString& aBuffer)
  39. : mBuffer(aBuffer)
  40. { }
  41. Key&
  42. operator=(const nsAString& aString)
  43. {
  44. SetFromString(aString);
  45. return *this;
  46. }
  47. Key&
  48. operator=(int64_t aInt)
  49. {
  50. SetFromInteger(aInt);
  51. return *this;
  52. }
  53. bool
  54. operator==(const Key& aOther) const
  55. {
  56. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  57. return mBuffer.Equals(aOther.mBuffer);
  58. }
  59. bool
  60. operator!=(const Key& aOther) const
  61. {
  62. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  63. return !mBuffer.Equals(aOther.mBuffer);
  64. }
  65. bool
  66. operator<(const Key& aOther) const
  67. {
  68. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  69. return Compare(mBuffer, aOther.mBuffer) < 0;
  70. }
  71. bool
  72. operator>(const Key& aOther) const
  73. {
  74. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  75. return Compare(mBuffer, aOther.mBuffer) > 0;
  76. }
  77. bool
  78. operator<=(const Key& aOther) const
  79. {
  80. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  81. return Compare(mBuffer, aOther.mBuffer) <= 0;
  82. }
  83. bool
  84. operator>=(const Key& aOther) const
  85. {
  86. Assert(!mBuffer.IsVoid() && !aOther.mBuffer.IsVoid());
  87. return Compare(mBuffer, aOther.mBuffer) >= 0;
  88. }
  89. void
  90. Unset()
  91. {
  92. mBuffer.SetIsVoid(true);
  93. }
  94. bool
  95. IsUnset() const
  96. {
  97. return mBuffer.IsVoid();
  98. }
  99. bool
  100. IsFloat() const
  101. {
  102. return !IsUnset() && *BufferStart() == eFloat;
  103. }
  104. bool
  105. IsDate() const
  106. {
  107. return !IsUnset() && *BufferStart() == eDate;
  108. }
  109. bool
  110. IsString() const
  111. {
  112. return !IsUnset() && *BufferStart() == eString;
  113. }
  114. bool
  115. IsBinary() const
  116. {
  117. return !IsUnset() && *BufferStart() == eBinary;
  118. }
  119. bool
  120. IsArray() const
  121. {
  122. return !IsUnset() && *BufferStart() >= eArray;
  123. }
  124. double
  125. ToFloat() const
  126. {
  127. Assert(IsFloat());
  128. const unsigned char* pos = BufferStart();
  129. double res = DecodeNumber(pos, BufferEnd());
  130. Assert(pos >= BufferEnd());
  131. return res;
  132. }
  133. double
  134. ToDateMsec() const
  135. {
  136. Assert(IsDate());
  137. const unsigned char* pos = BufferStart();
  138. double res = DecodeNumber(pos, BufferEnd());
  139. Assert(pos >= BufferEnd());
  140. return res;
  141. }
  142. void
  143. ToString(nsString& aString) const
  144. {
  145. Assert(IsString());
  146. const unsigned char* pos = BufferStart();
  147. DecodeString(pos, BufferEnd(), aString);
  148. Assert(pos >= BufferEnd());
  149. }
  150. void
  151. SetFromString(const nsAString& aString)
  152. {
  153. mBuffer.Truncate();
  154. EncodeString(aString, 0);
  155. TrimBuffer();
  156. }
  157. void
  158. SetFromInteger(int64_t aInt)
  159. {
  160. mBuffer.Truncate();
  161. EncodeNumber(double(aInt), eFloat);
  162. TrimBuffer();
  163. }
  164. nsresult
  165. SetFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal, bool aCallGetters);
  166. nsresult
  167. ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) const;
  168. nsresult
  169. ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aVal) const;
  170. nsresult
  171. AppendItem(JSContext* aCx,
  172. bool aFirstOfArray,
  173. JS::Handle<JS::Value> aVal,
  174. bool aCallGetters);
  175. nsresult
  176. ToLocaleBasedKey(Key& aTarget, const nsCString& aLocale) const;
  177. void
  178. FinishArray()
  179. {
  180. TrimBuffer();
  181. }
  182. const nsCString&
  183. GetBuffer() const
  184. {
  185. return mBuffer;
  186. }
  187. nsresult
  188. BindToStatement(mozIStorageStatement* aStatement,
  189. const nsACString& aParamName) const;
  190. nsresult
  191. SetFromStatement(mozIStorageStatement* aStatement, uint32_t aIndex);
  192. nsresult
  193. SetFromValueArray(mozIStorageValueArray* aValues, uint32_t aIndex);
  194. static int16_t
  195. CompareKeys(const Key& aFirst, const Key& aSecond)
  196. {
  197. int32_t result = Compare(aFirst.mBuffer, aSecond.mBuffer);
  198. if (result < 0) {
  199. return -1;
  200. }
  201. if (result > 0) {
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. private:
  207. const unsigned char*
  208. BufferStart() const
  209. {
  210. return reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
  211. }
  212. const unsigned char*
  213. BufferEnd() const
  214. {
  215. return reinterpret_cast<const unsigned char*>(mBuffer.EndReading());
  216. }
  217. // Encoding helper. Trims trailing zeros off of mBuffer as a post-processing
  218. // step.
  219. void
  220. TrimBuffer()
  221. {
  222. const char* end = mBuffer.EndReading() - 1;
  223. while (!*end) {
  224. --end;
  225. }
  226. mBuffer.Truncate(end + 1 - mBuffer.BeginReading());
  227. }
  228. // Encoding functions. These append the encoded value to the end of mBuffer
  229. nsresult
  230. EncodeJSVal(JSContext* aCx,
  231. JS::Handle<JS::Value> aVal,
  232. uint8_t aTypeOffset,
  233. bool aCallGetters);
  234. void
  235. EncodeString(const nsAString& aString, uint8_t aTypeOffset);
  236. template <typename T>
  237. void
  238. EncodeString(const T* aStart, const T* aEnd, uint8_t aTypeOffset);
  239. template <typename T>
  240. void
  241. EncodeAsString(const T* aStart, const T* aEnd, uint8_t aType);
  242. nsresult
  243. EncodeLocaleString(const nsDependentString& aString, uint8_t aTypeOffset,
  244. const nsCString& aLocale);
  245. void
  246. EncodeNumber(double aFloat, uint8_t aType);
  247. void
  248. EncodeBinary(JSObject* aObject, bool aIsViewObject, uint8_t aTypeOffset);
  249. // Decoding functions. aPos points into mBuffer and is adjusted to point
  250. // past the consumed value.
  251. static nsresult
  252. DecodeJSVal(const unsigned char*& aPos,
  253. const unsigned char* aEnd,
  254. JSContext* aCx,
  255. JS::MutableHandle<JS::Value> aVal);
  256. static void
  257. DecodeString(const unsigned char*& aPos,
  258. const unsigned char* aEnd,
  259. nsString& aString);
  260. static double
  261. DecodeNumber(const unsigned char*& aPos, const unsigned char* aEnd);
  262. static JSObject*
  263. DecodeBinary(const unsigned char*& aPos,
  264. const unsigned char* aEnd,
  265. JSContext* aCx);
  266. nsresult
  267. EncodeJSValInternal(JSContext* aCx,
  268. JS::Handle<JS::Value> aVal,
  269. uint8_t aTypeOffset,
  270. uint16_t aRecursionDepth,
  271. bool aCallGetters);
  272. static nsresult
  273. DecodeJSValInternal(const unsigned char*& aPos,
  274. const unsigned char* aEnd,
  275. JSContext* aCx,
  276. uint8_t aTypeOffset,
  277. JS::MutableHandle<JS::Value> aVal,
  278. uint16_t aRecursionDepth);
  279. template <typename T>
  280. nsresult
  281. SetFromSource(T* aSource, uint32_t aIndex);
  282. void
  283. Assert(bool aCondition) const
  284. #ifdef DEBUG
  285. ;
  286. #else
  287. { }
  288. #endif
  289. };
  290. } // namespace indexedDB
  291. } // namespace dom
  292. } // namespace mozilla
  293. #endif // mozilla_dom_indexeddb_key_h__