Bug 1875768 - Call the appropriate postfork handler on MacOS r=glandium
[gecko.git] / netwerk / base / nsSimpleNestedURI.cpp
blob54f57b462516b093ac21564b5c5cf131901dce4a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "base/basictypes.h"
8 #include "nsNetCID.h"
9 #include "nsNetUtil.h"
10 #include "nsIClassInfoImpl.h"
11 #include "nsSimpleNestedURI.h"
12 #include "nsIObjectInputStream.h"
13 #include "nsIObjectOutputStream.h"
15 #include "mozilla/ipc/URIUtils.h"
17 namespace mozilla {
18 namespace net {
20 NS_IMPL_CLASSINFO(nsSimpleNestedURI, nullptr, nsIClassInfo::THREADSAFE,
21 NS_SIMPLENESTEDURI_CID)
22 // Empty CI getter. We only need nsIClassInfo for Serialization
23 NS_IMPL_CI_INTERFACE_GETTER0(nsSimpleNestedURI)
25 NS_IMPL_ADDREF_INHERITED(nsSimpleNestedURI, nsSimpleURI)
26 NS_IMPL_RELEASE_INHERITED(nsSimpleNestedURI, nsSimpleURI)
27 NS_IMPL_QUERY_INTERFACE_CI_INHERITED(nsSimpleNestedURI, nsSimpleURI,
28 nsINestedURI)
30 nsSimpleNestedURI::nsSimpleNestedURI(nsIURI* innerURI) : mInnerURI(innerURI) {
31 NS_ASSERTION(innerURI, "Must have inner URI");
34 nsresult nsSimpleNestedURI::SetPathQueryRef(const nsACString& aPathQueryRef) {
35 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
37 nsCOMPtr<nsIURI> inner;
38 nsresult rv =
39 NS_MutateURI(mInnerURI).SetPathQueryRef(aPathQueryRef).Finalize(inner);
40 NS_ENSURE_SUCCESS(rv, rv);
41 rv = nsSimpleURI::SetPathQueryRef(aPathQueryRef);
42 NS_ENSURE_SUCCESS(rv, rv);
43 // If the regular SetPathQueryRef worked, also set it on the inner URI
44 mInnerURI = inner;
45 return NS_OK;
48 nsresult nsSimpleNestedURI::SetQuery(const nsACString& aQuery) {
49 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
51 nsCOMPtr<nsIURI> inner;
52 nsresult rv = NS_MutateURI(mInnerURI).SetQuery(aQuery).Finalize(inner);
53 NS_ENSURE_SUCCESS(rv, rv);
54 rv = nsSimpleURI::SetQuery(aQuery);
55 NS_ENSURE_SUCCESS(rv, rv);
56 // If the regular SetQuery worked, also set it on the inner URI
57 mInnerURI = inner;
58 return NS_OK;
61 nsresult nsSimpleNestedURI::SetRef(const nsACString& aRef) {
62 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
64 nsCOMPtr<nsIURI> inner;
65 nsresult rv = NS_MutateURI(mInnerURI).SetRef(aRef).Finalize(inner);
66 NS_ENSURE_SUCCESS(rv, rv);
67 rv = nsSimpleURI::SetRef(aRef);
68 NS_ENSURE_SUCCESS(rv, rv);
69 // If the regular SetRef worked, also set it on the inner URI
70 mInnerURI = inner;
71 return NS_OK;
74 // nsISerializable
76 NS_IMETHODIMP
77 nsSimpleNestedURI::Read(nsIObjectInputStream* aStream) {
78 MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
79 return NS_ERROR_NOT_IMPLEMENTED;
82 nsresult nsSimpleNestedURI::ReadPrivate(nsIObjectInputStream* aStream) {
83 nsresult rv = nsSimpleURI::ReadPrivate(aStream);
84 if (NS_FAILED(rv)) return rv;
86 nsCOMPtr<nsISupports> supports;
87 rv = aStream->ReadObject(true, getter_AddRefs(supports));
88 if (NS_FAILED(rv)) return rv;
90 mInnerURI = do_QueryInterface(supports, &rv);
91 if (NS_FAILED(rv)) return rv;
93 return rv;
96 NS_IMETHODIMP
97 nsSimpleNestedURI::Write(nsIObjectOutputStream* aStream) {
98 nsCOMPtr<nsISerializable> serializable = do_QueryInterface(mInnerURI);
99 if (!serializable) {
100 // We can't serialize ourselves
101 return NS_ERROR_NOT_AVAILABLE;
104 nsresult rv = nsSimpleURI::Write(aStream);
105 if (NS_FAILED(rv)) return rv;
107 rv = aStream->WriteCompoundObject(mInnerURI, NS_GET_IID(nsIURI), true);
108 return rv;
111 NS_IMETHODIMP_(void)
112 nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams& aParams) {
113 using namespace mozilla::ipc;
115 SimpleNestedURIParams params;
116 URIParams simpleParams;
118 nsSimpleURI::Serialize(simpleParams);
119 params.simpleParams() = simpleParams;
121 SerializeURI(mInnerURI, params.innerURI());
123 aParams = params;
126 bool nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams& aParams) {
127 using namespace mozilla::ipc;
129 if (aParams.type() != URIParams::TSimpleNestedURIParams) {
130 NS_ERROR("Received unknown parameters from the other process!");
131 return false;
134 const SimpleNestedURIParams& params = aParams.get_SimpleNestedURIParams();
135 if (!nsSimpleURI::Deserialize(params.simpleParams())) return false;
137 mInnerURI = DeserializeURI(params.innerURI());
138 return true;
141 // nsINestedURI
143 NS_IMETHODIMP
144 nsSimpleNestedURI::GetInnerURI(nsIURI** aURI) {
145 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
147 nsCOMPtr<nsIURI> uri = mInnerURI;
148 uri.forget(aURI);
149 return NS_OK;
152 NS_IMETHODIMP
153 nsSimpleNestedURI::GetInnermostURI(nsIURI** uri) {
154 return NS_ImplGetInnermostURI(this, uri);
157 // nsSimpleURI overrides
158 /* virtual */
159 nsresult nsSimpleNestedURI::EqualsInternal(
160 nsIURI* other, nsSimpleURI::RefHandlingEnum refHandlingMode, bool* result) {
161 *result = false;
162 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
164 if (other) {
165 bool correctScheme;
166 nsresult rv = other->SchemeIs(mScheme.get(), &correctScheme);
167 NS_ENSURE_SUCCESS(rv, rv);
169 if (correctScheme) {
170 nsCOMPtr<nsINestedURI> nest = do_QueryInterface(other);
171 if (nest) {
172 nsCOMPtr<nsIURI> otherInner;
173 rv = nest->GetInnerURI(getter_AddRefs(otherInner));
174 NS_ENSURE_SUCCESS(rv, rv);
176 return (refHandlingMode == eHonorRef)
177 ? otherInner->Equals(mInnerURI, result)
178 : otherInner->EqualsExceptRef(mInnerURI, result);
183 return NS_OK;
186 /* virtual */
187 nsSimpleURI* nsSimpleNestedURI::StartClone(
188 nsSimpleURI::RefHandlingEnum refHandlingMode, const nsACString& newRef) {
189 NS_ENSURE_TRUE(mInnerURI, nullptr);
191 nsCOMPtr<nsIURI> innerClone;
192 nsresult rv = NS_OK;
193 if (refHandlingMode == eHonorRef) {
194 innerClone = mInnerURI;
195 } else if (refHandlingMode == eReplaceRef) {
196 rv = NS_GetURIWithNewRef(mInnerURI, newRef, getter_AddRefs(innerClone));
197 } else {
198 rv = NS_GetURIWithoutRef(mInnerURI, getter_AddRefs(innerClone));
201 if (NS_FAILED(rv)) {
202 return nullptr;
205 nsSimpleNestedURI* url = new nsSimpleNestedURI(innerClone);
206 SetRefOnClone(url, refHandlingMode, newRef);
208 return url;
211 // Queries this list of interfaces. If none match, it queries mURI.
212 NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator, nsIURISetters,
213 nsIURIMutator, nsISerializable,
214 nsINestedURIMutator)
216 NS_IMETHODIMP
217 nsSimpleNestedURI::Mutate(nsIURIMutator** aMutator) {
218 RefPtr<nsSimpleNestedURI::Mutator> mutator = new nsSimpleNestedURI::Mutator();
219 nsresult rv = mutator->InitFromURI(this);
220 if (NS_FAILED(rv)) {
221 return rv;
223 mutator.forget(aMutator);
224 return NS_OK;
227 } // namespace net
228 } // namespace mozilla