Bug 1826136 [wpt PR 39338] - Update wpt metadata, a=testonly
[gecko.git] / dom / cache / TypeUtils.h
blob5342ea3d7e227c23eeca0f66ccb2a37e11cf9a75
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_TypesUtils_h
8 #define mozilla_dom_cache_TypesUtils_h
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
16 class nsIGlobalObject;
17 class nsIAsyncInputStream;
18 class nsIInputStream;
20 namespace mozilla {
22 namespace ipc {
23 class PBackgroundChild;
24 } // namespace ipc
26 namespace dom {
28 struct CacheQueryOptions;
29 struct MultiCacheQueryOptions;
30 class InternalHeaders;
31 class InternalRequest;
32 class InternalResponse;
33 class OwningRequestOrUSVString;
34 class Request;
35 class RequestOrUSVString;
36 class Response;
38 namespace cache {
40 class CacheQueryParams;
41 class CacheReadStream;
42 class CacheRequest;
43 class CacheResponse;
44 class HeadersEntry;
46 class TypeUtils {
47 public:
48 enum BodyAction { IgnoreBody, ReadBody };
50 enum SchemeAction { IgnoreInvalidScheme, TypeErrorOnInvalidScheme };
52 ~TypeUtils() = default;
53 virtual nsIGlobalObject* GetGlobalObject() const = 0;
54 #ifdef DEBUG
55 virtual void AssertOwningThread() const = 0;
56 #else
57 inline void AssertOwningThread() const {}
58 #endif
60 // This is mainly declared to support serializing body streams. Some
61 // TypeUtils implementations do not expect to be used for this kind of
62 // serialization. These classes will MOZ_CRASH() if you try to call
63 // GetIPCManager().
64 virtual mozilla::ipc::PBackgroundChild* GetIPCManager() = 0;
66 SafeRefPtr<InternalRequest> ToInternalRequest(JSContext* aCx,
67 const RequestOrUSVString& aIn,
68 BodyAction aBodyAction,
69 ErrorResult& aRv);
71 SafeRefPtr<InternalRequest> ToInternalRequest(
72 JSContext* aCx, const OwningRequestOrUSVString& aIn,
73 BodyAction aBodyAction, ErrorResult& aRv);
75 void ToCacheRequest(CacheRequest& aOut, const InternalRequest& aIn,
76 BodyAction aBodyAction, SchemeAction aSchemeAction,
77 ErrorResult& aRv);
79 void ToCacheResponseWithoutBody(CacheResponse& aOut, InternalResponse& aIn,
80 ErrorResult& aRv);
82 void ToCacheResponse(JSContext* aCx, CacheResponse& aOut, Response& aIn,
83 ErrorResult& aRv);
85 void ToCacheQueryParams(CacheQueryParams& aOut, const CacheQueryOptions& aIn);
87 void ToCacheQueryParams(CacheQueryParams& aOut,
88 const MultiCacheQueryOptions& aIn);
90 already_AddRefed<Response> ToResponse(const CacheResponse& aIn);
92 SafeRefPtr<InternalRequest> ToInternalRequest(const CacheRequest& aIn);
94 SafeRefPtr<Request> ToRequest(const CacheRequest& aIn);
96 // static methods
97 static already_AddRefed<InternalHeaders> ToInternalHeaders(
98 const nsTArray<HeadersEntry>& aHeadersEntryList,
99 HeadersGuardEnum aGuard = HeadersGuardEnum::None);
101 // Utility method for parsing a URL and doing associated operations. A mix
102 // of things are done in this one method to avoid duplicated parsing:
104 // 1) The aUrl argument is modified to strip the fragment
105 // 2) If aSchemaValidOut is set, then a boolean value is set indicating
106 // if the aUrl's scheme is valid or not for storing in the cache.
107 // 3) If aUrlWithoutQueryOut is set, then a url string is provided without
108 // the search section.
109 // 4) If aUrlQueryOut is set then its populated with the search section
110 // of the URL. Note, this parameter must be set if aUrlWithoutQueryOut
111 // is set. They must either both be nullptr or set to valid string
112 // pointers.
114 // Any errors are thrown on ErrorResult.
115 static void ProcessURL(nsACString& aUrl, bool* aSchemeValidOut,
116 nsACString* aUrlWithoutQueryOut,
117 nsACString* aUrlQueryOut, ErrorResult& aRv);
119 private:
120 void CheckAndSetBodyUsed(JSContext* aCx, Request& aRequest,
121 BodyAction aBodyAction, ErrorResult& aRv);
123 SafeRefPtr<InternalRequest> ToInternalRequest(const nsAString& aIn,
124 ErrorResult& aRv);
126 void SerializeCacheStream(nsIInputStream* aStream,
127 Maybe<CacheReadStream>* aStreamOut,
128 ErrorResult& aRv);
130 void SerializeSendStream(nsIInputStream* aStream,
131 CacheReadStream& aReadStreamOut, ErrorResult& aRv);
134 } // namespace cache
135 } // namespace dom
136 } // namespace mozilla
138 #endif // mozilla_dom_cache_TypesUtils_h