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
;
23 class PBackgroundChild
;
28 struct CacheQueryOptions
;
29 struct MultiCacheQueryOptions
;
30 class InternalHeaders
;
31 class InternalRequest
;
32 class InternalResponse
;
33 class OwningRequestOrUTF8String
;
35 class RequestOrUTF8String
;
40 class CacheQueryParams
;
41 class CacheReadStream
;
48 enum BodyAction
{ IgnoreBody
, ReadBody
};
50 enum SchemeAction
{ IgnoreInvalidScheme
, TypeErrorOnInvalidScheme
};
52 ~TypeUtils() = default;
53 virtual nsIGlobalObject
* GetGlobalObject() const = 0;
55 virtual void AssertOwningThread() const = 0;
57 inline void AssertOwningThread() const {}
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
64 virtual mozilla::ipc::PBackgroundChild
* GetIPCManager() = 0;
66 SafeRefPtr
<InternalRequest
> ToInternalRequest(JSContext
* aCx
,
67 const RequestOrUTF8String
& aIn
,
68 BodyAction aBodyAction
,
71 SafeRefPtr
<InternalRequest
> ToInternalRequest(
72 JSContext
* aCx
, const OwningRequestOrUTF8String
& aIn
,
73 BodyAction aBodyAction
, ErrorResult
& aRv
);
75 void ToCacheRequest(CacheRequest
& aOut
, const InternalRequest
& aIn
,
76 BodyAction aBodyAction
, SchemeAction aSchemeAction
,
79 void ToCacheResponseWithoutBody(CacheResponse
& aOut
, InternalResponse
& aIn
,
82 void ToCacheResponse(JSContext
* aCx
, CacheResponse
& aOut
, Response
& aIn
,
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
);
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
114 // Any errors are thrown on ErrorResult.
115 static void ProcessURL(nsACString
& aUrl
, bool* aSchemeValidOut
,
116 nsACString
* aUrlWithoutQueryOut
,
117 nsACString
* aUrlQueryOut
, ErrorResult
& aRv
);
120 void CheckAndSetBodyUsed(JSContext
* aCx
, Request
& aRequest
,
121 BodyAction aBodyAction
, ErrorResult
& aRv
);
123 SafeRefPtr
<InternalRequest
> ToInternalRequest(const nsACString
& aIn
,
126 void SerializeCacheStream(nsIInputStream
* aStream
,
127 Maybe
<CacheReadStream
>* aStreamOut
,
130 void SerializeSendStream(nsIInputStream
* aStream
,
131 CacheReadStream
& aReadStreamOut
, ErrorResult
& aRv
);
136 } // namespace mozilla
138 #endif // mozilla_dom_cache_TypesUtils_h