nsSimpleNestedURI.cpp (6237B)
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/. */ 5 6 #include "base/basictypes.h" 7 8 #include "nsNetCID.h" 9 #include "nsNetUtil.h" 10 #include "nsIClassInfoImpl.h" 11 #include "nsSimpleNestedURI.h" 12 #include "nsIObjectInputStream.h" 13 #include "nsIObjectOutputStream.h" 14 15 #include "mozilla/ipc/URIUtils.h" 16 17 namespace mozilla { 18 namespace net { 19 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) 24 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) 29 30 nsSimpleNestedURI::nsSimpleNestedURI(nsIURI* innerURI) : mInnerURI(innerURI) { 31 NS_ASSERTION(innerURI, "Must have inner URI"); 32 } 33 34 nsresult nsSimpleNestedURI::SetPathQueryRef(const nsACString& aPathQueryRef) { 35 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED); 36 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; 46 } 47 48 nsresult nsSimpleNestedURI::SetQuery(const nsACString& aQuery) { 49 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED); 50 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; 59 } 60 61 nsresult nsSimpleNestedURI::SetRef(const nsACString& aRef) { 62 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED); 63 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; 72 } 73 74 // nsISerializable 75 76 NS_IMETHODIMP 77 nsSimpleNestedURI::Read(nsIObjectInputStream* aStream) { 78 MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead"); 79 return NS_ERROR_NOT_IMPLEMENTED; 80 } 81 82 nsresult nsSimpleNestedURI::ReadPrivate(nsIObjectInputStream* aStream) { 83 nsresult rv = nsSimpleURI::ReadPrivate(aStream); 84 if (NS_FAILED(rv)) return rv; 85 86 nsCOMPtr<nsISupports> supports; 87 rv = aStream->ReadObject(true, getter_AddRefs(supports)); 88 if (NS_FAILED(rv)) return rv; 89 90 mInnerURI = do_QueryInterface(supports, &rv); 91 if (NS_FAILED(rv)) return rv; 92 93 return rv; 94 } 95 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; 102 } 103 104 nsresult rv = nsSimpleURI::Write(aStream); 105 if (NS_FAILED(rv)) return rv; 106 107 rv = aStream->WriteCompoundObject(mInnerURI, NS_GET_IID(nsIURI), true); 108 return rv; 109 } 110 111 NS_IMETHODIMP_(void) 112 nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams& aParams) { 113 using namespace mozilla::ipc; 114 115 SimpleNestedURIParams params; 116 URIParams simpleParams; 117 118 nsSimpleURI::Serialize(simpleParams); 119 params.simpleParams() = simpleParams; 120 121 SerializeURI(mInnerURI, params.innerURI()); 122 123 aParams = params; 124 } 125 126 bool nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams& aParams) { 127 using namespace mozilla::ipc; 128 129 if (aParams.type() != URIParams::TSimpleNestedURIParams) { 130 NS_ERROR("Received unknown parameters from the other process!"); 131 return false; 132 } 133 134 const SimpleNestedURIParams& params = aParams.get_SimpleNestedURIParams(); 135 if (!nsSimpleURI::Deserialize(params.simpleParams())) return false; 136 137 mInnerURI = DeserializeURI(params.innerURI()); 138 return true; 139 } 140 141 // nsINestedURI 142 143 NS_IMETHODIMP 144 nsSimpleNestedURI::GetInnerURI(nsIURI** aURI) { 145 NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED); 146 147 nsCOMPtr<nsIURI> uri = mInnerURI; 148 uri.forget(aURI); 149 return NS_OK; 150 } 151 152 NS_IMETHODIMP 153 nsSimpleNestedURI::GetInnermostURI(nsIURI** uri) { 154 return NS_ImplGetInnermostURI(this, uri); 155 } 156 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); 163 164 if (other) { 165 bool correctScheme; 166 nsresult rv = 167 other->SchemeIs(PromiseFlatCString(Scheme()).get(), &correctScheme); 168 NS_ENSURE_SUCCESS(rv, rv); 169 170 if (correctScheme) { 171 nsCOMPtr<nsINestedURI> nest = do_QueryInterface(other); 172 if (nest) { 173 nsCOMPtr<nsIURI> otherInner; 174 rv = nest->GetInnerURI(getter_AddRefs(otherInner)); 175 NS_ENSURE_SUCCESS(rv, rv); 176 177 return (refHandlingMode == eHonorRef) 178 ? otherInner->Equals(mInnerURI, result) 179 : otherInner->EqualsExceptRef(mInnerURI, result); 180 } 181 } 182 } 183 184 return NS_OK; 185 } 186 187 /* virtual */ 188 already_AddRefed<nsSimpleURI> nsSimpleNestedURI::StartClone() { 189 NS_ENSURE_TRUE(mInnerURI, nullptr); 190 191 RefPtr<nsSimpleNestedURI> url = new nsSimpleNestedURI(mInnerURI); 192 193 return url.forget(); 194 } 195 196 // Queries this list of interfaces. If none match, it queries mURI. 197 NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator, nsIURISetters, 198 nsIURIMutator, nsISerializable, 199 nsINestedURIMutator) 200 201 NS_IMETHODIMP 202 nsSimpleNestedURI::Mutate(nsIURIMutator** aMutator) { 203 RefPtr<nsSimpleNestedURI::Mutator> mutator = new nsSimpleNestedURI::Mutator(); 204 nsresult rv = mutator->InitFromURI(this); 205 if (NS_FAILED(rv)) { 206 return rv; 207 } 208 mutator.forget(aMutator); 209 return NS_OK; 210 } 211 212 } // namespace net 213 } // namespace mozilla