CacheStorage.h (4049B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_cache_CacheStorage_h 8 #define mozilla_dom_cache_CacheStorage_h 9 10 #include "mozilla/UniquePtr.h" 11 #include "mozilla/dom/cache/TypeUtils.h" 12 #include "mozilla/dom/cache/Types.h" 13 #include "nsCOMPtr.h" 14 #include "nsISupportsImpl.h" 15 #include "nsTArray.h" 16 #include "nsWrapperCache.h" 17 18 class nsIGlobalObject; 19 20 namespace mozilla { 21 22 class ErrorResult; 23 enum UseCounter : int16_t; 24 enum class UseCounterWorker : int16_t; 25 26 namespace ipc { 27 class PrincipalInfo; 28 } // namespace ipc 29 30 namespace dom { 31 32 enum class CacheStorageNamespace : uint8_t; 33 class Promise; 34 class WorkerPrivate; 35 36 namespace cache { 37 38 class CacheStorageChild; 39 class CacheWorkerRef; 40 41 bool IsTrusted(const mozilla::ipc::PrincipalInfo& aPrincipalInfo, 42 bool aTestingPrefEnabled); 43 44 class CacheStorage final : public nsISupports, 45 public nsWrapperCache, 46 public TypeUtils, 47 public CacheStorageChildListener { 48 using PBackgroundChild = mozilla::ipc::PBackgroundChild; 49 50 public: 51 static already_AddRefed<CacheStorage> CreateOnMainThread( 52 Namespace aNamespace, nsIGlobalObject* aGlobal, nsIPrincipal* aPrincipal, 53 bool aForceTrustedOrigin, ErrorResult& aRv); 54 55 static already_AddRefed<CacheStorage> CreateOnWorker( 56 Namespace aNamespace, nsIGlobalObject* aGlobal, 57 WorkerPrivate* aWorkerPrivate, ErrorResult& aRv); 58 59 static bool DefineCachesForSandbox(JSContext* aCx, 60 JS::Handle<JSObject*> aGlobal); 61 62 static bool CachesEnabled(JSContext* aCx, JSObject*); 63 64 // webidl interface methods 65 already_AddRefed<Promise> Match(JSContext* aCx, 66 const RequestOrUTF8String& aRequest, 67 const MultiCacheQueryOptions& aOptions, 68 ErrorResult& aRv); 69 already_AddRefed<Promise> Has(const nsAString& aKey, ErrorResult& aRv); 70 already_AddRefed<Promise> Open(const nsAString& aKey, ErrorResult& aRv); 71 already_AddRefed<Promise> Delete(const nsAString& aKey, ErrorResult& aRv); 72 already_AddRefed<Promise> Keys(ErrorResult& aRv); 73 74 // chrome-only webidl interface methods 75 static already_AddRefed<CacheStorage> Constructor( 76 const GlobalObject& aGlobal, CacheStorageNamespace aNamespace, 77 nsIPrincipal* aPrincipal, ErrorResult& aRv); 78 79 // binding methods 80 nsISupports* GetParentObject() const; 81 virtual JSObject* WrapObject(JSContext* aContext, 82 JS::Handle<JSObject*> aGivenProto) override; 83 84 // Called when CacheStorageChild actor is being destroyed 85 // Override Observer method 86 void OnActorDestroy(CacheStorageChild* aActor) override; 87 88 // TypeUtils methods 89 virtual nsIGlobalObject* GetGlobalObject() const override; 90 #ifdef DEBUG 91 virtual void AssertOwningThread() const override; 92 #endif 93 94 private: 95 CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal, 96 const mozilla::ipc::PrincipalInfo& aPrincipalInfo, 97 SafeRefPtr<CacheWorkerRef> aWorkerRef); 98 explicit CacheStorage(nsresult aFailureResult); 99 ~CacheStorage(); 100 101 struct Entry; 102 void RunRequest(UniquePtr<Entry> aEntry); 103 104 OpenMode GetOpenMode() const; 105 106 bool HasStorageAccess(UseCounter aLabel, UseCounterWorker aLabelWorker) const; 107 108 const Namespace mNamespace; 109 nsCOMPtr<nsIGlobalObject> mGlobal; 110 const UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo; 111 112 // weak ref cleared in OnActorDestroy 113 CacheStorageChild* mActor; 114 115 nsresult mStatus; 116 117 public: 118 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 119 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(CacheStorage) 120 }; 121 122 } // namespace cache 123 } // namespace dom 124 } // namespace mozilla 125 126 #endif // mozilla_dom_cache_CacheStorage_h