IDBRequest.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_idbrequest_h__
  6. #define mozilla_dom_idbrequest_h__
  7. #include "js/RootingAPI.h"
  8. #include "mozilla/Attributes.h"
  9. #include "mozilla/EventForwards.h"
  10. #include "mozilla/dom/IDBRequestBinding.h"
  11. #include "mozilla/dom/IDBWrapperCache.h"
  12. #include "nsAutoPtr.h"
  13. #include "nsCycleCollectionParticipant.h"
  14. #define PRIVATE_IDBREQUEST_IID \
  15. {0xe68901e5, 0x1d50, 0x4ee9, {0xaf, 0x49, 0x90, 0x99, 0x4a, 0xff, 0xc8, 0x39}}
  16. class nsPIDOMWindowInner;
  17. struct PRThread;
  18. namespace mozilla {
  19. class ErrorResult;
  20. namespace dom {
  21. class DOMError;
  22. class IDBCursor;
  23. class IDBDatabase;
  24. class IDBFactory;
  25. class IDBIndex;
  26. class IDBObjectStore;
  27. class IDBTransaction;
  28. template <typename> struct Nullable;
  29. class OwningIDBObjectStoreOrIDBIndexOrIDBCursor;
  30. class IDBRequest
  31. : public IDBWrapperCache
  32. {
  33. protected:
  34. // mSourceAsObjectStore and mSourceAsIndex are exclusive and one must always
  35. // be set. mSourceAsCursor is sometimes set also.
  36. RefPtr<IDBObjectStore> mSourceAsObjectStore;
  37. RefPtr<IDBIndex> mSourceAsIndex;
  38. RefPtr<IDBCursor> mSourceAsCursor;
  39. RefPtr<IDBTransaction> mTransaction;
  40. #ifdef DEBUG
  41. PRThread* mOwningThread;
  42. #endif
  43. JS::Heap<JS::Value> mResultVal;
  44. RefPtr<DOMError> mError;
  45. nsString mFilename;
  46. uint64_t mLoggingSerialNumber;
  47. nsresult mErrorCode;
  48. uint32_t mLineNo;
  49. uint32_t mColumn;
  50. bool mHaveResultOrErrorCode;
  51. public:
  52. class ResultCallback;
  53. static already_AddRefed<IDBRequest>
  54. Create(JSContext* aCx, IDBDatabase* aDatabase, IDBTransaction* aTransaction);
  55. static already_AddRefed<IDBRequest>
  56. Create(JSContext* aCx,
  57. IDBObjectStore* aSource,
  58. IDBDatabase* aDatabase,
  59. IDBTransaction* aTransaction);
  60. static already_AddRefed<IDBRequest>
  61. Create(JSContext* aCx,
  62. IDBIndex* aSource,
  63. IDBDatabase* aDatabase,
  64. IDBTransaction* aTransaction);
  65. static void
  66. CaptureCaller(JSContext* aCx, nsAString& aFilename, uint32_t* aLineNo,
  67. uint32_t* aColumn);
  68. static uint64_t
  69. NextSerialNumber();
  70. // nsIDOMEventTarget
  71. virtual nsresult
  72. GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  73. void
  74. GetSource(Nullable<OwningIDBObjectStoreOrIDBIndexOrIDBCursor>& aSource) const;
  75. void
  76. Reset();
  77. void
  78. DispatchNonTransactionError(nsresult aErrorCode);
  79. void
  80. SetResultCallback(ResultCallback* aCallback);
  81. void
  82. SetError(nsresult aRv);
  83. nsresult
  84. GetErrorCode() const
  85. #ifdef DEBUG
  86. ;
  87. #else
  88. {
  89. return mErrorCode;
  90. }
  91. #endif
  92. DOMError*
  93. GetErrorAfterResult() const
  94. #ifdef DEBUG
  95. ;
  96. #else
  97. {
  98. return mError;
  99. }
  100. #endif
  101. DOMError*
  102. GetError(ErrorResult& aRv);
  103. void
  104. GetCallerLocation(nsAString& aFilename, uint32_t* aLineNo,
  105. uint32_t* aColumn) const;
  106. bool
  107. IsPending() const
  108. {
  109. return !mHaveResultOrErrorCode;
  110. }
  111. uint64_t
  112. LoggingSerialNumber() const
  113. {
  114. AssertIsOnOwningThread();
  115. return mLoggingSerialNumber;
  116. }
  117. void
  118. SetLoggingSerialNumber(uint64_t aLoggingSerialNumber);
  119. nsPIDOMWindowInner*
  120. GetParentObject() const
  121. {
  122. return GetOwner();
  123. }
  124. void
  125. GetResult(JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) const;
  126. void
  127. GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
  128. ErrorResult& aRv) const
  129. {
  130. GetResult(aResult, aRv);
  131. }
  132. IDBTransaction*
  133. GetTransaction() const
  134. {
  135. AssertIsOnOwningThread();
  136. return mTransaction;
  137. }
  138. IDBRequestReadyState
  139. ReadyState() const;
  140. void
  141. SetSource(IDBCursor* aSource);
  142. IMPL_EVENT_HANDLER(success);
  143. IMPL_EVENT_HANDLER(error);
  144. void
  145. AssertIsOnOwningThread() const
  146. #ifdef DEBUG
  147. ;
  148. #else
  149. { }
  150. #endif
  151. NS_DECL_ISUPPORTS_INHERITED
  152. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(IDBRequest,
  153. IDBWrapperCache)
  154. // nsWrapperCache
  155. virtual JSObject*
  156. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  157. protected:
  158. explicit IDBRequest(IDBDatabase* aDatabase);
  159. explicit IDBRequest(nsPIDOMWindowInner* aOwner);
  160. ~IDBRequest();
  161. void
  162. InitMembers();
  163. void
  164. ConstructResult();
  165. };
  166. class NS_NO_VTABLE IDBRequest::ResultCallback
  167. {
  168. public:
  169. virtual nsresult
  170. GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) = 0;
  171. protected:
  172. ResultCallback()
  173. { }
  174. };
  175. class IDBOpenDBRequest final
  176. : public IDBRequest
  177. {
  178. class WorkerHolder;
  179. // Only touched on the owning thread.
  180. RefPtr<IDBFactory> mFactory;
  181. nsAutoPtr<WorkerHolder> mWorkerHolder;
  182. const bool mFileHandleDisabled;
  183. public:
  184. static already_AddRefed<IDBOpenDBRequest>
  185. CreateForWindow(JSContext* aCx,
  186. IDBFactory* aFactory,
  187. nsPIDOMWindowInner* aOwner,
  188. JS::Handle<JSObject*> aScriptOwner);
  189. static already_AddRefed<IDBOpenDBRequest>
  190. CreateForJS(JSContext* aCx,
  191. IDBFactory* aFactory,
  192. JS::Handle<JSObject*> aScriptOwner);
  193. bool
  194. IsFileHandleDisabled() const
  195. {
  196. return mFileHandleDisabled;
  197. }
  198. void
  199. SetTransaction(IDBTransaction* aTransaction);
  200. void
  201. NoteComplete();
  202. // nsIDOMEventTarget
  203. virtual nsresult
  204. PostHandleEvent(EventChainPostVisitor& aVisitor) override;
  205. IDBFactory*
  206. Factory() const
  207. {
  208. return mFactory;
  209. }
  210. IMPL_EVENT_HANDLER(blocked);
  211. IMPL_EVENT_HANDLER(upgradeneeded);
  212. NS_DECL_ISUPPORTS_INHERITED
  213. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBOpenDBRequest, IDBRequest)
  214. // nsWrapperCache
  215. virtual JSObject*
  216. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  217. private:
  218. IDBOpenDBRequest(IDBFactory* aFactory,
  219. nsPIDOMWindowInner* aOwner,
  220. bool aFileHandleDisabled);
  221. ~IDBOpenDBRequest();
  222. };
  223. } // namespace dom
  224. } // namespace mozilla
  225. #endif // mozilla_dom_idbrequest_h__