Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/base/nsSimpleNestedURI.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "nsSimpleNestedURI.h"
11
#include "nsIObjectInputStream.h"
12
#include "nsIObjectOutputStream.h"
13
14
#include "mozilla/ipc/URIUtils.h"
15
16
namespace mozilla {
17
namespace net {
18
19
NS_IMPL_ISUPPORTS_INHERITED(nsSimpleNestedURI, nsSimpleURI, nsINestedURI)
20
21
nsSimpleNestedURI::nsSimpleNestedURI(nsIURI* innerURI)
22
    : mInnerURI(innerURI)
23
0
{
24
0
    NS_ASSERTION(innerURI, "Must have inner URI");
25
0
}
26
27
nsresult
28
nsSimpleNestedURI::SetPathQueryRef(const nsACString &aPathQueryRef)
29
0
{
30
0
    NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
31
0
32
0
    nsCOMPtr<nsIURI> inner;
33
0
    nsresult rv = NS_MutateURI(mInnerURI)
34
0
                    .SetPathQueryRef(aPathQueryRef)
35
0
                    .Finalize(inner);
36
0
    NS_ENSURE_SUCCESS(rv, rv);
37
0
    rv = nsSimpleURI::SetPathQueryRef(aPathQueryRef);
38
0
    NS_ENSURE_SUCCESS(rv, rv);
39
0
    // If the regular SetPathQueryRef worked, also set it on the inner URI
40
0
    mInnerURI = inner;
41
0
    return NS_OK;
42
0
}
43
44
nsresult
45
nsSimpleNestedURI::SetQuery(const nsACString &aQuery)
46
0
{
47
0
    NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
48
0
49
0
    nsCOMPtr<nsIURI> inner;
50
0
    nsresult rv = NS_MutateURI(mInnerURI)
51
0
                    .SetQuery(aQuery)
52
0
                    .Finalize(inner);
53
0
    NS_ENSURE_SUCCESS(rv, rv);
54
0
    rv = nsSimpleURI::SetQuery(aQuery);
55
0
    NS_ENSURE_SUCCESS(rv, rv);
56
0
    // If the regular SetQuery worked, also set it on the inner URI
57
0
    mInnerURI = inner;
58
0
    return NS_OK;
59
0
}
60
61
nsresult
62
nsSimpleNestedURI::SetRef(const nsACString &aRef)
63
0
{
64
0
    NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
65
0
66
0
    nsCOMPtr<nsIURI> inner;
67
0
    nsresult rv = NS_MutateURI(mInnerURI)
68
0
                    .SetRef(aRef)
69
0
                    .Finalize(inner);
70
0
    NS_ENSURE_SUCCESS(rv, rv);
71
0
    rv = nsSimpleURI::SetRef(aRef);
72
0
    NS_ENSURE_SUCCESS(rv, rv);
73
0
    // If the regular SetRef worked, also set it on the inner URI
74
0
    mInnerURI = inner;
75
0
    return NS_OK;
76
0
}
77
78
// nsISerializable
79
80
NS_IMETHODIMP
81
nsSimpleNestedURI::Read(nsIObjectInputStream *aStream)
82
0
{
83
0
    MOZ_ASSERT_UNREACHABLE("Use nsIURIMutator.read() instead");
84
0
    return NS_ERROR_NOT_IMPLEMENTED;
85
0
}
86
87
nsresult
88
nsSimpleNestedURI::ReadPrivate(nsIObjectInputStream *aStream)
89
0
{
90
0
    nsresult rv = nsSimpleURI::ReadPrivate(aStream);
91
0
    if (NS_FAILED(rv)) return rv;
92
0
93
0
    nsCOMPtr<nsISupports> supports;
94
0
    rv = aStream->ReadObject(true, getter_AddRefs(supports));
95
0
    if (NS_FAILED(rv)) return rv;
96
0
97
0
    mInnerURI = do_QueryInterface(supports, &rv);
98
0
    if (NS_FAILED(rv)) return rv;
99
0
100
0
    return rv;
101
0
}
102
103
NS_IMETHODIMP
104
nsSimpleNestedURI::Write(nsIObjectOutputStream* aStream)
105
0
{
106
0
    nsCOMPtr<nsISerializable> serializable = do_QueryInterface(mInnerURI);
107
0
    if (!serializable) {
108
0
        // We can't serialize ourselves
109
0
        return NS_ERROR_NOT_AVAILABLE;
110
0
    }
111
0
112
0
    nsresult rv = nsSimpleURI::Write(aStream);
113
0
    if (NS_FAILED(rv)) return rv;
114
0
115
0
    rv = aStream->WriteCompoundObject(mInnerURI, NS_GET_IID(nsIURI),
116
0
                                      true);
117
0
    return rv;
118
0
}
119
120
// nsIIPCSerializableURI
121
void
122
nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams& aParams)
123
0
{
124
0
    using namespace mozilla::ipc;
125
0
126
0
    SimpleNestedURIParams params;
127
0
    URIParams simpleParams;
128
0
129
0
    nsSimpleURI::Serialize(simpleParams);
130
0
    params.simpleParams() = simpleParams;
131
0
132
0
    SerializeURI(mInnerURI, params.innerURI());
133
0
134
0
    aParams = params;
135
0
}
136
137
bool
138
nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams& aParams)
139
0
{
140
0
    using namespace mozilla::ipc;
141
0
142
0
    if (aParams.type() != URIParams::TSimpleNestedURIParams) {
143
0
        NS_ERROR("Received unknown parameters from the other process!");
144
0
        return false;
145
0
    }
146
0
147
0
    const SimpleNestedURIParams& params = aParams.get_SimpleNestedURIParams();
148
0
    if (!nsSimpleURI::Deserialize(params.simpleParams()))
149
0
        return false;
150
0
151
0
    mInnerURI = DeserializeURI(params.innerURI());
152
0
    return true;
153
0
}
154
155
// nsINestedURI
156
157
NS_IMETHODIMP
158
nsSimpleNestedURI::GetInnerURI(nsIURI** aURI)
159
0
{
160
0
    NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
161
0
162
0
    nsCOMPtr<nsIURI> uri = mInnerURI;
163
0
    uri.forget(aURI);
164
0
    return NS_OK;
165
0
}
166
167
NS_IMETHODIMP
168
nsSimpleNestedURI::GetInnermostURI(nsIURI** uri)
169
0
{
170
0
    return NS_ImplGetInnermostURI(this, uri);
171
0
}
172
173
// nsSimpleURI overrides
174
/* virtual */ nsresult
175
nsSimpleNestedURI::EqualsInternal(nsIURI* other,
176
                                  nsSimpleURI::RefHandlingEnum refHandlingMode,
177
                                  bool* result)
178
0
{
179
0
    *result = false;
180
0
    NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
181
0
182
0
    if (other) {
183
0
        bool correctScheme;
184
0
        nsresult rv = other->SchemeIs(mScheme.get(), &correctScheme);
185
0
        NS_ENSURE_SUCCESS(rv, rv);
186
0
187
0
        if (correctScheme) {
188
0
            nsCOMPtr<nsINestedURI> nest = do_QueryInterface(other);
189
0
            if (nest) {
190
0
                nsCOMPtr<nsIURI> otherInner;
191
0
                rv = nest->GetInnerURI(getter_AddRefs(otherInner));
192
0
                NS_ENSURE_SUCCESS(rv, rv);
193
0
194
0
                return (refHandlingMode == eHonorRef) ?
195
0
                    otherInner->Equals(mInnerURI, result) :
196
0
                    otherInner->EqualsExceptRef(mInnerURI, result);
197
0
            }
198
0
        }
199
0
    }
200
0
201
0
    return NS_OK;
202
0
}
203
204
/* virtual */ nsSimpleURI*
205
nsSimpleNestedURI::StartClone(nsSimpleURI::RefHandlingEnum refHandlingMode,
206
                              const nsACString& newRef)
207
0
{
208
0
    NS_ENSURE_TRUE(mInnerURI, nullptr);
209
0
210
0
    nsCOMPtr<nsIURI> innerClone;
211
0
    nsresult rv = NS_OK;
212
0
    if (refHandlingMode == eHonorRef) {
213
0
        innerClone = mInnerURI;
214
0
    } else if (refHandlingMode == eReplaceRef) {
215
0
        rv = NS_GetURIWithNewRef(mInnerURI, newRef, getter_AddRefs(innerClone));
216
0
    } else {
217
0
        rv = NS_GetURIWithoutRef(mInnerURI, getter_AddRefs(innerClone));
218
0
    }
219
0
220
0
    if (NS_FAILED(rv)) {
221
0
        return nullptr;
222
0
    }
223
0
224
0
    nsSimpleNestedURI* url = new nsSimpleNestedURI(innerClone);
225
0
    SetRefOnClone(url, refHandlingMode, newRef);
226
0
227
0
    return url;
228
0
}
229
230
// nsIClassInfo overrides
231
232
NS_IMETHODIMP
233
nsSimpleNestedURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
234
0
{
235
0
    static NS_DEFINE_CID(kSimpleNestedURICID, NS_SIMPLENESTEDURI_CID);
236
0
237
0
    *aClassIDNoAlloc = kSimpleNestedURICID;
238
0
    return NS_OK;
239
0
}
240
241
// Queries this list of interfaces. If none match, it queries mURI.
242
NS_IMPL_NSIURIMUTATOR_ISUPPORTS(nsSimpleNestedURI::Mutator,
243
                                nsIURISetters,
244
                                nsIURIMutator,
245
                                nsISerializable,
246
                                nsINestedURIMutator)
247
248
NS_IMETHODIMP
249
nsSimpleNestedURI::Mutate(nsIURIMutator** aMutator)
250
0
{
251
0
    RefPtr<nsSimpleNestedURI::Mutator> mutator = new nsSimpleNestedURI::Mutator();
252
0
    nsresult rv = mutator->InitFromURI(this);
253
0
    if (NS_FAILED(rv)) {
254
0
        return rv;
255
0
    }
256
0
    mutator.forget(aMutator);
257
0
    return NS_OK;
258
0
}
259
260
} // namespace net
261
} // namespace mozilla