1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/ArrayUtils.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/dom/BlobURL.h"
12 #include "mozilla/net/DefaultURI.h"
13 #include "mozilla/net/SubstitutingURL.h"
14 #include "mozilla/NullPrincipalURI.h"
15 #include "nsComponentManagerUtils.h"
19 #include "nsIIconURI.h"
20 #include "nsJSProtocolHandler.h"
22 #include "nsSimpleNestedURI.h"
23 #include "nsThreadUtils.h"
24 #include "nsIURIMutator.h"
26 using namespace mozilla::ipc
;
27 using mozilla::ArrayLength
;
31 NS_DEFINE_CID(kSimpleURIMutatorCID
, NS_SIMPLEURIMUTATOR_CID
);
32 NS_DEFINE_CID(kStandardURLMutatorCID
, NS_STANDARDURLMUTATOR_CID
);
33 NS_DEFINE_CID(kJARURIMutatorCID
, NS_JARURIMUTATOR_CID
);
34 NS_DEFINE_CID(kIconURIMutatorCID
, NS_MOZICONURIMUTATOR_CID
);
41 void SerializeURI(nsIURI
* aURI
, URIParams
& aParams
) {
42 MOZ_ASSERT(NS_IsMainThread());
45 aURI
->Serialize(aParams
);
46 if (aParams
.type() == URIParams::T__None
) {
47 MOZ_CRASH("Serialize failed!");
51 void SerializeURI(nsIURI
* aURI
, Maybe
<URIParams
>& aParams
) {
52 MOZ_ASSERT(NS_IsMainThread());
56 SerializeURI(aURI
, params
);
57 aParams
= Some(std::move(params
));
63 already_AddRefed
<nsIURI
> DeserializeURI(const URIParams
& aParams
) {
64 MOZ_ASSERT(NS_IsMainThread());
66 nsCOMPtr
<nsIURIMutator
> mutator
;
68 switch (aParams
.type()) {
69 case URIParams::TSimpleURIParams
:
70 mutator
= do_CreateInstance(kSimpleURIMutatorCID
);
73 case URIParams::TStandardURLParams
:
74 if (aParams
.get_StandardURLParams().isSubstituting()) {
75 mutator
= new net::SubstitutingURL::Mutator();
77 mutator
= do_CreateInstance(kStandardURLMutatorCID
);
81 case URIParams::TJARURIParams
:
82 mutator
= do_CreateInstance(kJARURIMutatorCID
);
85 case URIParams::TJSURIParams
:
86 mutator
= new nsJSURI::Mutator();
89 case URIParams::TIconURIParams
:
90 mutator
= do_CreateInstance(kIconURIMutatorCID
);
93 case URIParams::TNullPrincipalURIParams
:
94 mutator
= new NullPrincipalURI::Mutator();
97 case URIParams::TSimpleNestedURIParams
:
98 mutator
= new net::nsSimpleNestedURI::Mutator();
101 case URIParams::THostObjectURIParams
:
102 mutator
= new mozilla::dom::BlobURL::Mutator();
105 case URIParams::TDefaultURIParams
:
106 mutator
= new mozilla::net::DefaultURI::Mutator();
110 MOZ_CRASH("Unknown params!");
115 nsresult rv
= mutator
->Deserialize(aParams
);
117 MOZ_ASSERT(false, "Deserialize failed!");
121 nsCOMPtr
<nsIURI
> uri
;
122 DebugOnly
<nsresult
> rv2
= mutator
->Finalize(getter_AddRefs(uri
));
124 MOZ_ASSERT(NS_SUCCEEDED(rv2
));
129 already_AddRefed
<nsIURI
> DeserializeURI(const Maybe
<URIParams
>& aParams
) {
130 MOZ_ASSERT(NS_IsMainThread());
132 nsCOMPtr
<nsIURI
> uri
;
134 if (aParams
.isSome()) {
135 uri
= DeserializeURI(aParams
.ref());
142 } // namespace mozilla