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