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 "nsIClassInfoImpl.h"
11 #include "nsSimpleNestedURI.h"
12 #include "nsIObjectInputStream.h"
13 #include "nsIObjectOutputStream.h"
15 #include "mozilla/ipc/URIUtils.h"
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
,
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
;
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
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
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
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
;
97 nsSimpleNestedURI::Write(nsIObjectOutputStream
* aStream
) {
98 nsCOMPtr
<nsISerializable
> serializable
= do_QueryInterface(mInnerURI
);
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);
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());
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!");
134 const SimpleNestedURIParams
& params
= aParams
.get_SimpleNestedURIParams();
135 if (!nsSimpleURI::Deserialize(params
.simpleParams())) return false;
137 mInnerURI
= DeserializeURI(params
.innerURI());
144 nsSimpleNestedURI::GetInnerURI(nsIURI
** aURI
) {
145 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
147 nsCOMPtr
<nsIURI
> uri
= mInnerURI
;
153 nsSimpleNestedURI::GetInnermostURI(nsIURI
** uri
) {
154 return NS_ImplGetInnermostURI(this, uri
);
157 // nsSimpleURI overrides
159 nsresult
nsSimpleNestedURI::EqualsInternal(
160 nsIURI
* other
, nsSimpleURI::RefHandlingEnum refHandlingMode
, bool* result
) {
162 NS_ENSURE_TRUE(mInnerURI
, NS_ERROR_NOT_INITIALIZED
);
166 nsresult rv
= other
->SchemeIs(mScheme
.get(), &correctScheme
);
167 NS_ENSURE_SUCCESS(rv
, rv
);
170 nsCOMPtr
<nsINestedURI
> nest
= do_QueryInterface(other
);
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
);
187 nsSimpleURI
* nsSimpleNestedURI::StartClone(
188 nsSimpleURI::RefHandlingEnum refHandlingMode
, const nsACString
& newRef
) {
189 NS_ENSURE_TRUE(mInnerURI
, nullptr);
191 nsCOMPtr
<nsIURI
> innerClone
;
193 if (refHandlingMode
== eHonorRef
) {
194 innerClone
= mInnerURI
;
195 } else if (refHandlingMode
== eReplaceRef
) {
196 rv
= NS_GetURIWithNewRef(mInnerURI
, newRef
, getter_AddRefs(innerClone
));
198 rv
= NS_GetURIWithoutRef(mInnerURI
, getter_AddRefs(innerClone
));
205 nsSimpleNestedURI
* url
= new nsSimpleNestedURI(innerClone
);
206 SetRefOnClone(url
, refHandlingMode
, newRef
);
211 // Queries this list of interfaces. If none match, it queries mURI.
212 NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator
, nsIURISetters
,
213 nsIURIMutator
, nsISerializable
,
217 nsSimpleNestedURI::Mutate(nsIURIMutator
** aMutator
) {
218 RefPtr
<nsSimpleNestedURI::Mutator
> mutator
= new nsSimpleNestedURI::Mutator();
219 nsresult rv
= mutator
->InitFromURI(this);
223 mutator
.forget(aMutator
);
228 } // namespace mozilla