IDBFactory.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_idbfactory_h__
  6. #define mozilla_dom_idbfactory_h__
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/dom/StorageTypeBinding.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsISupports.h"
  13. #include "nsString.h"
  14. #include "nsTArray.h"
  15. #include "nsWrapperCache.h"
  16. class nsIPrincipal;
  17. class nsPIDOMWindowInner;
  18. struct PRThread;
  19. namespace mozilla {
  20. class ErrorResult;
  21. namespace ipc {
  22. class PBackgroundChild;
  23. class PrincipalInfo;
  24. } // namespace ipc
  25. namespace dom {
  26. struct IDBOpenDBOptions;
  27. class IDBOpenDBRequest;
  28. template <typename> class Optional;
  29. class TabChild;
  30. namespace indexedDB {
  31. class BackgroundFactoryChild;
  32. class FactoryRequestParams;
  33. class LoggingInfo;
  34. }
  35. class IDBFactory final
  36. : public nsISupports
  37. , public nsWrapperCache
  38. {
  39. typedef mozilla::dom::StorageType StorageType;
  40. typedef mozilla::ipc::PBackgroundChild PBackgroundChild;
  41. typedef mozilla::ipc::PrincipalInfo PrincipalInfo;
  42. class BackgroundCreateCallback;
  43. struct PendingRequestInfo;
  44. nsAutoPtr<PrincipalInfo> mPrincipalInfo;
  45. // If this factory lives on a window then mWindow must be non-null. Otherwise
  46. // mOwningObject must be non-null.
  47. nsCOMPtr<nsPIDOMWindowInner> mWindow;
  48. JS::Heap<JSObject*> mOwningObject;
  49. // This will only be set if the factory belongs to a window in a child
  50. // process.
  51. RefPtr<TabChild> mTabChild;
  52. nsTArray<nsAutoPtr<PendingRequestInfo>> mPendingRequests;
  53. indexedDB::BackgroundFactoryChild* mBackgroundActor;
  54. #ifdef DEBUG
  55. PRThread* mOwningThread;
  56. #endif
  57. uint64_t mInnerWindowID;
  58. bool mBackgroundActorFailed;
  59. bool mPrivateBrowsingMode;
  60. public:
  61. static nsresult
  62. CreateForWindow(nsPIDOMWindowInner* aWindow,
  63. IDBFactory** aFactory);
  64. static nsresult
  65. CreateForMainThreadJS(JSContext* aCx,
  66. JS::Handle<JSObject*> aOwningObject,
  67. IDBFactory** aFactory);
  68. static nsresult
  69. CreateForWorker(JSContext* aCx,
  70. JS::Handle<JSObject*> aOwningObject,
  71. const PrincipalInfo& aPrincipalInfo,
  72. uint64_t aInnerWindowID,
  73. IDBFactory** aFactory);
  74. static bool
  75. AllowedForWindow(nsPIDOMWindowInner* aWindow);
  76. static bool
  77. AllowedForPrincipal(nsIPrincipal* aPrincipal,
  78. bool* aIsSystemPrincipal = nullptr);
  79. #ifdef DEBUG
  80. void
  81. AssertIsOnOwningThread() const;
  82. PRThread*
  83. OwningThread() const;
  84. #else
  85. void
  86. AssertIsOnOwningThread() const
  87. { }
  88. #endif
  89. void
  90. ClearBackgroundActor()
  91. {
  92. AssertIsOnOwningThread();
  93. mBackgroundActor = nullptr;
  94. }
  95. void
  96. IncrementParentLoggingRequestSerialNumber();
  97. nsPIDOMWindowInner*
  98. GetParentObject() const
  99. {
  100. return mWindow;
  101. }
  102. TabChild*
  103. GetTabChild() const
  104. {
  105. return mTabChild;
  106. }
  107. PrincipalInfo*
  108. GetPrincipalInfo() const
  109. {
  110. AssertIsOnOwningThread();
  111. return mPrincipalInfo;
  112. }
  113. uint64_t
  114. InnerWindowID() const
  115. {
  116. AssertIsOnOwningThread();
  117. return mInnerWindowID;
  118. }
  119. bool
  120. IsChrome() const;
  121. already_AddRefed<IDBOpenDBRequest>
  122. Open(JSContext* aCx,
  123. const nsAString& aName,
  124. uint64_t aVersion,
  125. ErrorResult& aRv);
  126. already_AddRefed<IDBOpenDBRequest>
  127. Open(JSContext* aCx,
  128. const nsAString& aName,
  129. const IDBOpenDBOptions& aOptions,
  130. ErrorResult& aRv);
  131. already_AddRefed<IDBOpenDBRequest>
  132. DeleteDatabase(JSContext* aCx,
  133. const nsAString& aName,
  134. const IDBOpenDBOptions& aOptions,
  135. ErrorResult& aRv);
  136. int16_t
  137. Cmp(JSContext* aCx,
  138. JS::Handle<JS::Value> aFirst,
  139. JS::Handle<JS::Value> aSecond,
  140. ErrorResult& aRv);
  141. already_AddRefed<IDBOpenDBRequest>
  142. OpenForPrincipal(JSContext* aCx,
  143. nsIPrincipal* aPrincipal,
  144. const nsAString& aName,
  145. uint64_t aVersion,
  146. ErrorResult& aRv);
  147. already_AddRefed<IDBOpenDBRequest>
  148. OpenForPrincipal(JSContext* aCx,
  149. nsIPrincipal* aPrincipal,
  150. const nsAString& aName,
  151. const IDBOpenDBOptions& aOptions,
  152. ErrorResult& aRv);
  153. already_AddRefed<IDBOpenDBRequest>
  154. DeleteForPrincipal(JSContext* aCx,
  155. nsIPrincipal* aPrincipal,
  156. const nsAString& aName,
  157. const IDBOpenDBOptions& aOptions,
  158. ErrorResult& aRv);
  159. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  160. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBFactory)
  161. // nsWrapperCache
  162. virtual JSObject*
  163. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  164. private:
  165. IDBFactory();
  166. ~IDBFactory();
  167. static nsresult
  168. CreateForMainThreadJSInternal(JSContext* aCx,
  169. JS::Handle<JSObject*> aOwningObject,
  170. nsAutoPtr<PrincipalInfo>& aPrincipalInfo,
  171. IDBFactory** aFactory);
  172. static nsresult
  173. CreateForJSInternal(JSContext* aCx,
  174. JS::Handle<JSObject*> aOwningObject,
  175. nsAutoPtr<PrincipalInfo>& aPrincipalInfo,
  176. uint64_t aInnerWindowID,
  177. IDBFactory** aFactory);
  178. static nsresult
  179. AllowedForWindowInternal(nsPIDOMWindowInner* aWindow,
  180. nsIPrincipal** aPrincipal);
  181. already_AddRefed<IDBOpenDBRequest>
  182. OpenInternal(JSContext* aCx,
  183. nsIPrincipal* aPrincipal,
  184. const nsAString& aName,
  185. const Optional<uint64_t>& aVersion,
  186. const Optional<StorageType>& aStorageType,
  187. bool aDeleting,
  188. ErrorResult& aRv);
  189. nsresult
  190. BackgroundActorCreated(PBackgroundChild* aBackgroundActor,
  191. const indexedDB::LoggingInfo& aLoggingInfo);
  192. void
  193. BackgroundActorFailed();
  194. nsresult
  195. InitiateRequest(IDBOpenDBRequest* aRequest,
  196. const indexedDB::FactoryRequestParams& aParams);
  197. };
  198. } // namespace dom
  199. } // namespace mozilla
  200. #endif // mozilla_dom_idbfactory_h__