summaryrefslogtreecommitdiffstats
path: root/dom/cache/TypeUtils.h
blob: 731ef9506b820e07244778f2fb1dd0f7589afada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_cache_TypesUtils_h
#define mozilla_dom_cache_TypesUtils_h

#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/InternalHeaders.h"
#include "nsError.h"

class nsIGlobalObject;
class nsIAsyncInputStream;
class nsIInputStream;

namespace mozilla {

namespace ipc {
class PBackgroundChild;
class SendStreamChild;
class AutoIPCStream;
}

namespace dom {

struct CacheQueryOptions;
class InternalRequest;
class InternalResponse;
class OwningRequestOrUSVString;
class Request;
class RequestOrUSVString;
class Response;

namespace cache {

class CacheQueryParams;
class CacheReadStream;
class CacheReadStreamOrVoid;
class CacheRequest;
class CacheResponse;
class HeadersEntry;

class TypeUtils
{
public:
  enum BodyAction
  {
    IgnoreBody,
    ReadBody
  };

  enum SchemeAction
  {
    IgnoreInvalidScheme,
    TypeErrorOnInvalidScheme
  };

  ~TypeUtils() { }
  virtual nsIGlobalObject* GetGlobalObject() const = 0;
#ifdef DEBUG
  virtual void AssertOwningThread() const = 0;
#else
  inline void AssertOwningThread() const { }
#endif

  // This is mainly declared to support serializing body streams.  Some
  // TypeUtils implementations do not expect to be used for this kind of
  // serialization.  These classes will MOZ_CRASH() if you try to call
  // GetIPCManager().
  virtual mozilla::ipc::PBackgroundChild*
  GetIPCManager() = 0;

  already_AddRefed<InternalRequest>
  ToInternalRequest(const RequestOrUSVString& aIn, BodyAction aBodyAction,
                    ErrorResult& aRv);

  already_AddRefed<InternalRequest>
  ToInternalRequest(const OwningRequestOrUSVString& aIn, BodyAction aBodyAction,
                    ErrorResult& aRv);

  void
  ToCacheRequest(CacheRequest& aOut, InternalRequest* aIn,
                 BodyAction aBodyAction, SchemeAction aSchemeAction,
                 nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
                 ErrorResult& aRv);

  void
  ToCacheResponseWithoutBody(CacheResponse& aOut, InternalResponse& aIn,
                             ErrorResult& aRv);

  void
  ToCacheResponse(CacheResponse& aOut, Response& aIn,
                  nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
                  ErrorResult& aRv);

  void
  ToCacheQueryParams(CacheQueryParams& aOut, const CacheQueryOptions& aIn);

  already_AddRefed<Response>
  ToResponse(const CacheResponse& aIn);

  already_AddRefed<InternalRequest>
  ToInternalRequest(const CacheRequest& aIn);

  already_AddRefed<Request>
  ToRequest(const CacheRequest& aIn);

  // static methods
  static already_AddRefed<InternalHeaders>
  ToInternalHeaders(const nsTArray<HeadersEntry>& aHeadersEntryList,
                    HeadersGuardEnum aGuard = HeadersGuardEnum::None);

  // Utility method for parsing a URL and doing associated operations.  A mix
  // of things are done in this one method to avoid duplicated parsing:
  //
  //  1) The aUrl argument is modified to strip the fragment
  //  2) If aSchemaValidOut is set, then a boolean value is set indicating
  //     if the aUrl's scheme is valid or not for storing in the cache.
  //  3) If aUrlWithoutQueryOut is set, then a url string is provided without
  //     the search section.
  //  4) If aUrlQueryOut is set then its populated with the search section
  //     of the URL.  Note, this parameter must be set if aUrlWithoutQueryOut
  //     is set.  They must either both be nullptr or set to valid string
  //     pointers.
  //
  // Any errors are thrown on ErrorResult.
  static void
  ProcessURL(nsACString& aUrl, bool* aSchemeValidOut,
             nsACString* aUrlWithoutQueryOut, nsACString* aUrlQueryOut,
             ErrorResult& aRv);

private:
  void
  CheckAndSetBodyUsed(Request* aRequest, BodyAction aBodyAction,
                      ErrorResult& aRv);

  already_AddRefed<InternalRequest>
  ToInternalRequest(const nsAString& aIn, ErrorResult& aRv);

  void
  SerializeCacheStream(nsIInputStream* aStream, CacheReadStreamOrVoid* aStreamOut,
                       nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
                       ErrorResult& aRv);

  void
  SerializeSendStream(nsIInputStream* aStream, CacheReadStream& aReadStreamOut,
                      ErrorResult& aRv);
};

} // namespace cache
} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_cache_TypesUtils_h