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"
10 #include "nsSimpleNestedURI.h"
11 #include "nsIObjectInputStream.h"
12 #include "nsIObjectOutputStream.h"
14 #include "mozilla/ipc/URIUtils.h"
19 NS_IMPL_CLASSINFO(nsSimpleNestedURI
, nullptr, nsIClassInfo::THREADSAFE
,
20 NS_SIMPLENESTEDURI_CID
)
21 // Empty CI getter. We only need nsIClassInfo for Serialization
22 NS_IMPL_CI_INTERFACE_GETTER0(nsSimpleNestedURI
)
24 NS_IMPL_ADDREF_INHERITED(nsSimpleNestedURI
, nsSimpleURI
)
25 NS_IMPL_RELEASE_INHERITED(nsSimpleNestedURI
, nsSimpleURI
)
26 NS_IMPL_QUERY_INTERFACE_CI_INHERITED(nsSimpleNestedURI
, nsSimpleURI
,
29 nsSimpleNestedURI::nsSimpleNestedURI(nsIURI
* innerURI
) : mInnerURI(innerURI
) {
30 NS_ASSERTION(innerURI
, "Must have inner URI");
33 nsresult
nsSimpleNestedURI::SetPathQueryRef(const nsACString
& aPathQueryRef
) {
34 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
36 nsCOMPtr
<nsIURI
> inner
;
38 NS_MutateURI(mInnerURI
).SetPathQueryRef(aPathQueryRef
).Finalize(inner
);
39 NS_ENSURE_SUCCESS(rv
, rv
);
40 rv
= nsSimpleURI::SetPathQueryRef(aPathQueryRef
);
41 NS_ENSURE_SUCCESS(rv
, rv
);
42 // If the regular SetPathQueryRef worked, also set it on the inner URI
47 nsresult
nsSimpleNestedURI::SetQuery(const nsACString
& aQuery
) {
48 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
50 nsCOMPtr
<nsIURI
> inner
;
51 nsresult rv
= NS_MutateURI(mInnerURI
).SetQuery(aQuery
).Finalize(inner
);
52 NS_ENSURE_SUCCESS(rv
, rv
);
53 rv
= nsSimpleURI::SetQuery(aQuery
);
54 NS_ENSURE_SUCCESS(rv
, rv
);
55 // If the regular SetQuery worked, also set it on the inner URI
60 nsresult
nsSimpleNestedURI::SetRef(const nsACString
& aRef
) {
61 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
63 nsCOMPtr
<nsIURI
> inner
;
64 nsresult rv
= NS_MutateURI(mInnerURI
).SetRef(aRef
).Finalize(inner
);
65 NS_ENSURE_SUCCESS(rv
, rv
);
66 rv
= nsSimpleURI::SetRef(aRef
);
67 NS_ENSURE_SUCCESS(rv
, rv
);
68 // If the regular SetRef worked, also set it on the inner URI
76 nsSimpleNestedURI::Read(nsIObjectInputStream
* aStream
) {
77 MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
78 return NS_ERROR_NOT_IMPLEMENTED
;
81 nsresult
nsSimpleNestedURI::ReadPrivate(nsIObjectInputStream
* aStream
) {
82 nsresult rv
= nsSimpleURI::ReadPrivate(aStream
);
83 if (NS_FAILED(rv
)) return rv
;
85 nsCOMPtr
<nsISupports
> supports
;
86 rv
= aStream
->ReadObject(true, getter_AddRefs(supports
));
87 if (NS_FAILED(rv
)) return rv
;
89 mInnerURI
= do_QueryInterface(supports
, &rv
);
90 if (NS_FAILED(rv
)) return rv
;
96 nsSimpleNestedURI::Write(nsIObjectOutputStream
* aStream
) {
97 nsCOMPtr
<nsISerializable
> serializable
= do_QueryInterface(mInnerURI
);
99 // We can't serialize ourselves
100 return NS_ERROR_NOT_AVAILABLE
;
103 nsresult rv
= nsSimpleURI::Write(aStream
);
104 if (NS_FAILED(rv
)) return rv
;
106 rv
= aStream
->WriteCompoundObject(mInnerURI
, NS_GET_IID(nsIURI
), true);
111 nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams
& aParams
) {
112 using namespace mozilla::ipc
;
114 SimpleNestedURIParams params
;
115 URIParams simpleParams
;
117 nsSimpleURI::Serialize(simpleParams
);
118 params
.simpleParams() = simpleParams
;
120 SerializeURI(mInnerURI
, params
.innerURI());
125 bool nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams
& aParams
) {
126 using namespace mozilla::ipc
;
128 if (aParams
.type() != URIParams::TSimpleNestedURIParams
) {
129 NS_ERROR("Received unknown parameters from the other process!");
133 const SimpleNestedURIParams
& params
= aParams
.get_SimpleNestedURIParams();
134 if (!nsSimpleURI::Deserialize(params
.simpleParams())) return false;
136 mInnerURI
= DeserializeURI(params
.innerURI());
143 nsSimpleNestedURI::GetInnerURI(nsIURI
** aURI
) {
144 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
146 nsCOMPtr
<nsIURI
> uri
= mInnerURI
;
152 nsSimpleNestedURI::GetInnermostURI(nsIURI
** uri
) {
153 return NS_ImplGetInnermostURI(this, uri
);
156 // nsSimpleURI overrides
158 nsresult
nsSimpleNestedURI::EqualsInternal(
159 nsIURI
* other
, nsSimpleURI::RefHandlingEnum refHandlingMode
, bool* result
) {
161 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
165 nsresult rv
= other
->SchemeIs(mScheme
.get(), &correctScheme
);
166 NS_ENSURE_SUCCESS(rv
, rv
);
169 nsCOMPtr
<nsINestedURI
> nest
= do_QueryInterface(other
);
171 nsCOMPtr
<nsIURI
> otherInner
;
172 rv
= nest
->GetInnerURI(getter_AddRefs(otherInner
));
173 NS_ENSURE_SUCCESS(rv
, rv
);
175 return (refHandlingMode
== eHonorRef
)
176 ? otherInner
->Equals(mInnerURI
, result
)
177 : otherInner
->EqualsExceptRef(mInnerURI
, result
);
186 nsSimpleURI
* nsSimpleNestedURI::StartClone(
187 nsSimpleURI::RefHandlingEnum refHandlingMode
, const nsACString
& newRef
) {
188 NS_ENSURE_TRUE(mInnerURI
, nullptr);
190 nsCOMPtr
<nsIURI
> innerClone
;
192 if (refHandlingMode
== eHonorRef
) {
193 innerClone
= mInnerURI
;
194 } else if (refHandlingMode
== eReplaceRef
) {
195 rv
= NS_GetURIWithNewRef(mInnerURI
, newRef
, getter_AddRefs(innerClone
));
197 rv
= NS_GetURIWithoutRef(mInnerURI
, getter_AddRefs(innerClone
));
204 nsSimpleNestedURI
* url
= new nsSimpleNestedURI(innerClone
);
205 SetRefOnClone(url
, refHandlingMode
, newRef
);
210 // Queries this list of interfaces. If none match, it queries mURI.
211 NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator
, nsIURISetters
,
212 nsIURIMutator
, nsISerializable
,
216 nsSimpleNestedURI::Mutate(nsIURIMutator
** aMutator
) {
217 RefPtr
<nsSimpleNestedURI::Mutator
> mutator
= new nsSimpleNestedURI::Mutator();
218 nsresult rv
= mutator
->InitFromURI(this);
222 mutator
.forget(aMutator
);
227 } // namespace mozilla