CacheStorage.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_cache_CacheStorage_h
  6. #define mozilla_dom_cache_CacheStorage_h
  7. #include "mozilla/dom/cache/Types.h"
  8. #include "mozilla/dom/cache/TypeUtils.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsISupportsImpl.h"
  12. #include "nsTArray.h"
  13. #include "nsWrapperCache.h"
  14. #include "nsIIPCBackgroundChildCreateCallback.h"
  15. class nsIGlobalObject;
  16. namespace mozilla {
  17. class ErrorResult;
  18. namespace ipc {
  19. class PrincipalInfo;
  20. } // namespace ipc
  21. namespace dom {
  22. enum class CacheStorageNamespace : uint32_t;
  23. class Promise;
  24. namespace workers {
  25. class WorkerPrivate;
  26. } // namespace workers
  27. namespace cache {
  28. class CacheStorageChild;
  29. class CacheWorkerHolder;
  30. class CacheStorage final : public nsIIPCBackgroundChildCreateCallback
  31. , public nsWrapperCache
  32. , public TypeUtils
  33. {
  34. typedef mozilla::ipc::PBackgroundChild PBackgroundChild;
  35. public:
  36. static already_AddRefed<CacheStorage>
  37. CreateOnMainThread(Namespace aNamespace, nsIGlobalObject* aGlobal,
  38. nsIPrincipal* aPrincipal, bool aStorageDisabled,
  39. bool aForceTrustedOrigin, ErrorResult& aRv);
  40. static already_AddRefed<CacheStorage>
  41. CreateOnWorker(Namespace aNamespace, nsIGlobalObject* aGlobal,
  42. workers::WorkerPrivate* aWorkerPrivate, ErrorResult& aRv);
  43. static bool
  44. DefineCaches(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
  45. // webidl interface methods
  46. already_AddRefed<Promise> Match(const RequestOrUSVString& aRequest,
  47. const CacheQueryOptions& aOptions,
  48. ErrorResult& aRv);
  49. already_AddRefed<Promise> Has(const nsAString& aKey, ErrorResult& aRv);
  50. already_AddRefed<Promise> Open(const nsAString& aKey, ErrorResult& aRv);
  51. already_AddRefed<Promise> Delete(const nsAString& aKey, ErrorResult& aRv);
  52. already_AddRefed<Promise> Keys(ErrorResult& aRv);
  53. // chrome-only webidl interface methods
  54. static already_AddRefed<CacheStorage>
  55. Constructor(const GlobalObject& aGlobal, CacheStorageNamespace aNamespace,
  56. nsIPrincipal* aPrincipal, ErrorResult& aRv);
  57. // binding methods
  58. static bool PrefEnabled(JSContext* aCx, JSObject* aObj);
  59. nsISupports* GetParentObject() const;
  60. virtual JSObject* WrapObject(JSContext* aContext, JS::Handle<JSObject*> aGivenProto) override;
  61. // nsIIPCbackgroundChildCreateCallback methods
  62. virtual void ActorCreated(PBackgroundChild* aActor) override;
  63. virtual void ActorFailed() override;
  64. // Called when CacheStorageChild actor is being destroyed
  65. void DestroyInternal(CacheStorageChild* aActor);
  66. // TypeUtils methods
  67. virtual nsIGlobalObject* GetGlobalObject() const override;
  68. #ifdef DEBUG
  69. virtual void AssertOwningThread() const override;
  70. #endif
  71. virtual mozilla::ipc::PBackgroundChild*
  72. GetIPCManager() override;
  73. private:
  74. CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal,
  75. const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
  76. CacheWorkerHolder* aWorkerHolder);
  77. explicit CacheStorage(nsresult aFailureResult);
  78. ~CacheStorage();
  79. void MaybeRunPendingRequests();
  80. const Namespace mNamespace;
  81. nsCOMPtr<nsIGlobalObject> mGlobal;
  82. UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
  83. RefPtr<CacheWorkerHolder> mWorkerHolder;
  84. // weak ref cleared in DestroyInternal
  85. CacheStorageChild* mActor;
  86. struct Entry;
  87. nsTArray<nsAutoPtr<Entry>> mPendingRequests;
  88. nsresult mStatus;
  89. public:
  90. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  91. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(CacheStorage,
  92. nsIIPCBackgroundChildCreateCallback)
  93. };
  94. } // namespace cache
  95. } // namespace dom
  96. } // namespace mozilla
  97. #endif // mozilla_dom_cache_CacheStorage_h