TypeUtils.h (5542B)
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_TypesUtils_h 8 #define mozilla_dom_cache_TypesUtils_h 9 10 #include "mozilla/AlreadyAddRefed.h" // for already_AddRefed 11 #include "mozilla/UniquePtr.h" // for UniquePtr 12 #include "mozilla/dom/HeadersBinding.h" // for HeadersGuardEnum, HeadersGua... 13 #include "mozilla/dom/SafeRefPtr.h" // for SafeRefPtr 14 #include "nsStringFwd.h" // for nsACString, nsAString 15 16 class nsIGlobalObject; 17 class nsIAsyncInputStream; 18 class nsIInputStream; 19 20 namespace mozilla { 21 22 namespace ipc { 23 class PBackgroundChild; 24 } // namespace ipc 25 26 namespace dom { 27 28 struct CacheQueryOptions; 29 struct MultiCacheQueryOptions; 30 class InternalHeaders; 31 class InternalRequest; 32 class InternalResponse; 33 class OwningRequestOrUTF8String; 34 class Request; 35 class RequestOrUTF8String; 36 class Response; 37 38 namespace cache { 39 class BoundStorageKeyChild; 40 class CacheChild; 41 class CacheStorageChild; 42 class CacheQueryParams; 43 class CacheReadStream; 44 class CacheRequest; 45 class CacheResponse; 46 class CacheStorageChild; 47 class HeadersEntry; 48 49 // common base class for below listeners 50 class Listener { 51 public: 52 virtual ~Listener() = default; 53 }; 54 55 // Cache registers itself as the listener of it's actor, CacheChild. 56 class CacheChildListener : public Listener { 57 public: 58 virtual void OnActorDestroy(CacheChild* aActor) = 0; 59 }; 60 61 // CacheStorage registers itself as the listener of it's actor, 62 // CacheStorageChild. 63 class CacheStorageChildListener : public Listener { 64 public: 65 virtual void OnActorDestroy(CacheStorageChild* aActor) = 0; 66 }; 67 68 // BoundStorageKey registers itself as the listener of it's actor, 69 // BoundStorageKeyChild. 70 class BoundStorageKeyChildListener : public Listener { 71 public: 72 virtual void OnActorDestroy(BoundStorageKeyChild* aActor) = 0; 73 }; 74 75 class TypeUtils { 76 public: 77 enum BodyAction { IgnoreBody, ReadBody }; 78 79 enum SchemeAction { IgnoreInvalidScheme, TypeErrorOnInvalidScheme }; 80 81 ~TypeUtils() = default; 82 virtual nsIGlobalObject* GetGlobalObject() const = 0; 83 #ifdef DEBUG 84 virtual void AssertOwningThread() const = 0; 85 #else 86 inline void AssertOwningThread() const {} 87 #endif 88 89 SafeRefPtr<InternalRequest> ToInternalRequest(JSContext* aCx, 90 const RequestOrUTF8String& aIn, 91 BodyAction aBodyAction, 92 ErrorResult& aRv); 93 94 SafeRefPtr<InternalRequest> ToInternalRequest( 95 JSContext* aCx, const OwningRequestOrUTF8String& aIn, 96 BodyAction aBodyAction, ErrorResult& aRv); 97 98 void ToCacheRequest(CacheRequest& aOut, const InternalRequest& aIn, 99 BodyAction aBodyAction, SchemeAction aSchemeAction, 100 ErrorResult& aRv); 101 102 void ToCacheResponseWithoutBody(CacheResponse& aOut, InternalResponse& aIn, 103 ErrorResult& aRv); 104 105 void ToCacheResponse(JSContext* aCx, CacheResponse& aOut, Response& aIn, 106 ErrorResult& aRv); 107 108 void ToCacheQueryParams(CacheQueryParams& aOut, const CacheQueryOptions& aIn); 109 110 void ToCacheQueryParams(CacheQueryParams& aOut, 111 const MultiCacheQueryOptions& aIn); 112 113 already_AddRefed<Response> ToResponse(const CacheResponse& aIn); 114 115 SafeRefPtr<InternalRequest> ToInternalRequest(const CacheRequest& aIn); 116 117 SafeRefPtr<Request> ToRequest(const CacheRequest& aIn); 118 119 // static methods 120 static already_AddRefed<InternalHeaders> ToInternalHeaders( 121 const nsTArray<HeadersEntry>& aHeadersEntryList, 122 HeadersGuardEnum aGuard = HeadersGuardEnum::None); 123 124 // Utility method for parsing a URL and doing associated operations. A mix 125 // of things are done in this one method to avoid duplicated parsing: 126 // 127 // 1) The aUrl argument is modified to strip the fragment 128 // 2) If aSchemaValidOut is set, then a boolean value is set indicating 129 // if the aUrl's scheme is valid or not for storing in the cache. 130 // 3) If aUrlWithoutQueryOut is set, then a url string is provided without 131 // the search section. 132 // 4) If aUrlQueryOut is set then its populated with the search section 133 // of the URL. Note, this parameter must be set if aUrlWithoutQueryOut 134 // is set. They must either both be nullptr or set to valid string 135 // pointers. 136 // 137 // Any errors are thrown on ErrorResult. 138 static void ProcessURL(nsACString& aUrl, bool* aSchemeValidOut, 139 nsACString* aUrlWithoutQueryOut, 140 nsACString* aUrlQueryOut, ErrorResult& aRv); 141 142 private: 143 void CheckAndSetBodyUsed(JSContext* aCx, Request& aRequest, 144 BodyAction aBodyAction, ErrorResult& aRv); 145 146 SafeRefPtr<InternalRequest> ToInternalRequest(const nsACString& aIn, 147 ErrorResult& aRv); 148 149 void SerializeCacheStream(nsIInputStream* aStream, 150 Maybe<CacheReadStream>* aStreamOut, 151 ErrorResult& aRv); 152 153 void SerializeSendStream(nsIInputStream* aStream, 154 CacheReadStream& aReadStreamOut, ErrorResult& aRv); 155 }; 156 157 } // namespace cache 158 } // namespace dom 159 } // namespace mozilla 160 161 #endif // mozilla_dom_cache_TypesUtils_h