Cache.h (4650B)
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_Cache_h 8 #define mozilla_dom_cache_Cache_h 9 10 #include "mozilla/dom/cache/TypeUtils.h" 11 #include "mozilla/dom/cache/Types.h" 12 #include "nsCOMPtr.h" 13 #include "nsISupportsImpl.h" 14 #include "nsString.h" 15 #include "nsWrapperCache.h" 16 17 class nsIGlobalObject; 18 19 namespace mozilla { 20 21 class ErrorResult; 22 23 namespace dom { 24 25 class OwningRequestOrUTF8String; 26 class Promise; 27 struct CacheQueryOptions; 28 class RequestOrUTF8String; 29 class Response; 30 template <typename T> 31 class Optional; 32 template <typename T> 33 class Sequence; 34 enum class CallerType : uint32_t; 35 36 namespace cache { 37 38 class AutoChildOpArgs; 39 class CacheChild; 40 41 enum class PutStatusPolicy { Default, RequireOK }; 42 bool IsValidPutRequestURL(const nsACString& aUrl, ErrorResult& aRv); 43 bool IsValidPutRequestMethod(const RequestOrUTF8String& aRequest, 44 ErrorResult& aRv); 45 bool IsValidPutRequestMethod(const Request& aRequest, ErrorResult& aRv); 46 bool IsValidPutResponseStatus(Response& aResponse, PutStatusPolicy aPolicy, 47 ErrorResult& aRv); 48 49 class Cache final : public nsISupports, 50 public nsWrapperCache, 51 public TypeUtils, 52 public CacheChildListener { 53 public: 54 Cache(nsIGlobalObject* aGlobal, CacheChild* aActor, Namespace aNamespace); 55 56 static bool CachesEnabled(JSContext* aCx, JSObject*); 57 58 // webidl interface methods 59 already_AddRefed<Promise> Match(JSContext* aCx, 60 const RequestOrUTF8String& aRequest, 61 const CacheQueryOptions& aOptions, 62 ErrorResult& aRv); 63 already_AddRefed<Promise> MatchAll( 64 JSContext* aCx, const Optional<RequestOrUTF8String>& aRequest, 65 const CacheQueryOptions& aOptions, ErrorResult& aRv); 66 already_AddRefed<Promise> Add(JSContext* aContext, 67 const RequestOrUTF8String& aRequest, 68 CallerType aCallerType, ErrorResult& aRv); 69 already_AddRefed<Promise> AddAll( 70 JSContext* aContext, const Sequence<OwningRequestOrUTF8String>& aRequests, 71 CallerType aCallerType, ErrorResult& aRv); 72 already_AddRefed<Promise> Put(JSContext* aCx, 73 const RequestOrUTF8String& aRequest, 74 Response& aResponse, ErrorResult& aRv); 75 already_AddRefed<Promise> Delete(JSContext* aCx, 76 const RequestOrUTF8String& aRequest, 77 const CacheQueryOptions& aOptions, 78 ErrorResult& aRv); 79 already_AddRefed<Promise> Keys(JSContext* aCx, 80 const Optional<RequestOrUTF8String>& aRequest, 81 const CacheQueryOptions& aParams, 82 ErrorResult& aRv); 83 84 // binding methods 85 nsISupports* GetParentObject() const; 86 virtual JSObject* WrapObject(JSContext* aContext, 87 JS::Handle<JSObject*> aGivenProto) override; 88 89 // Called when CacheChild actor is being destroyed. 90 // Overrides CacheChildListener method 91 void OnActorDestroy(CacheChild* aActor) override; 92 93 // TypeUtils methods 94 virtual nsIGlobalObject* GetGlobalObject() const override; 95 96 #ifdef DEBUG 97 virtual void AssertOwningThread() const override; 98 #endif 99 100 private: 101 class FetchHandler; 102 103 ~Cache(); 104 105 // Called when we're destroyed or CCed. 106 void DisconnectFromActor(); 107 108 already_AddRefed<Promise> ExecuteOp(AutoChildOpArgs& aOpArgs, 109 ErrorResult& aRv); 110 111 already_AddRefed<Promise> AddAll(const GlobalObject& aGlobal, 112 nsTArray<SafeRefPtr<Request>>&& aRequestList, 113 CallerType aCallerType, ErrorResult& aRv); 114 115 already_AddRefed<Promise> PutAll( 116 JSContext* aCx, const nsTArray<SafeRefPtr<Request>>& aRequestList, 117 const nsTArray<RefPtr<Response>>& aResponseList, ErrorResult& aRv); 118 119 OpenMode GetOpenMode() const; 120 121 nsCOMPtr<nsIGlobalObject> mGlobal; 122 CacheChild* mActor; 123 const Namespace mNamespace; 124 125 public: 126 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 127 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Cache) 128 }; 129 130 } // namespace cache 131 } // namespace dom 132 } // namespace mozilla 133 134 #endif // mozilla_dom_Cache_h