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/. */
7 #ifndef mozilla_dom_cache_CacheStorage_h
8 #define mozilla_dom_cache_CacheStorage_h
10 #include "mozilla/UniquePtr.h"
11 #include "mozilla/dom/cache/Types.h"
12 #include "mozilla/dom/cache/TypeUtils.h"
14 #include "nsISupportsImpl.h"
16 #include "nsWrapperCache.h"
18 class nsIGlobalObject
;
23 enum UseCounter
: int16_t;
24 enum class UseCounterWorker
: int16_t;
32 enum class CacheStorageNamespace
: uint8_t;
38 class CacheStorageChild
;
41 class CacheStorage final
: public nsISupports
,
42 public nsWrapperCache
,
44 using PBackgroundChild
= mozilla::ipc::PBackgroundChild
;
47 static already_AddRefed
<CacheStorage
> CreateOnMainThread(
48 Namespace aNamespace
, nsIGlobalObject
* aGlobal
, nsIPrincipal
* aPrincipal
,
49 bool aForceTrustedOrigin
, ErrorResult
& aRv
);
51 static already_AddRefed
<CacheStorage
> CreateOnWorker(
52 Namespace aNamespace
, nsIGlobalObject
* aGlobal
,
53 WorkerPrivate
* aWorkerPrivate
, ErrorResult
& aRv
);
55 static bool DefineCaches(JSContext
* aCx
, JS::Handle
<JSObject
*> aGlobal
);
57 // webidl interface methods
58 already_AddRefed
<Promise
> Match(JSContext
* aCx
,
59 const RequestOrUSVString
& aRequest
,
60 const MultiCacheQueryOptions
& aOptions
,
62 already_AddRefed
<Promise
> Has(const nsAString
& aKey
, ErrorResult
& aRv
);
63 already_AddRefed
<Promise
> Open(const nsAString
& aKey
, ErrorResult
& aRv
);
64 already_AddRefed
<Promise
> Delete(const nsAString
& aKey
, ErrorResult
& aRv
);
65 already_AddRefed
<Promise
> Keys(ErrorResult
& aRv
);
67 // chrome-only webidl interface methods
68 static already_AddRefed
<CacheStorage
> Constructor(
69 const GlobalObject
& aGlobal
, CacheStorageNamespace aNamespace
,
70 nsIPrincipal
* aPrincipal
, ErrorResult
& aRv
);
73 nsISupports
* GetParentObject() const;
74 virtual JSObject
* WrapObject(JSContext
* aContext
,
75 JS::Handle
<JSObject
*> aGivenProto
) override
;
77 // Called when CacheStorageChild actor is being destroyed
78 void DestroyInternal(CacheStorageChild
* aActor
);
81 virtual nsIGlobalObject
* GetGlobalObject() const override
;
83 virtual void AssertOwningThread() const override
;
86 virtual mozilla::ipc::PBackgroundChild
* GetIPCManager() override
;
89 CacheStorage(Namespace aNamespace
, nsIGlobalObject
* aGlobal
,
90 const mozilla::ipc::PrincipalInfo
& aPrincipalInfo
,
91 SafeRefPtr
<CacheWorkerRef
> aWorkerRef
);
92 explicit CacheStorage(nsresult aFailureResult
);
96 void RunRequest(UniquePtr
<Entry
> aEntry
);
98 OpenMode
GetOpenMode() const;
100 bool HasStorageAccess(UseCounter aLabel
, UseCounterWorker aLabelWorker
) const;
102 const Namespace mNamespace
;
103 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
104 const UniquePtr
<mozilla::ipc::PrincipalInfo
> mPrincipalInfo
;
106 // weak ref cleared in DestroyInternal
107 CacheStorageChild
* mActor
;
112 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
113 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(CacheStorage
)
118 } // namespace mozilla
120 #endif // mozilla_dom_cache_CacheStorage_h