Bug 1908670 - Home and newtab topic personalization needs region controls r=home...
[gecko.git] / ipc / glue / URIUtils.cpp
blob690509ceccbd28500e22b3e9d9326116f7da2772
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/. */
7 #include "URIUtils.h"
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/SubstitutingJARURI.h"
14 #include "mozilla/net/SubstitutingURL.h"
15 #include "nsAboutProtocolHandler.h"
16 #include "nsComponentManagerUtils.h"
17 #include "nsDebug.h"
18 #include "nsID.h"
19 #include "nsJARURI.h"
20 #include "nsIIconURI.h"
21 #include "nsJSProtocolHandler.h"
22 #include "nsNetCID.h"
23 #include "nsSimpleNestedURI.h"
24 #include "nsThreadUtils.h"
25 #include "nsIURIMutator.h"
27 using namespace mozilla::ipc;
28 using mozilla::ArrayLength;
30 namespace {
32 NS_DEFINE_CID(kSimpleURIMutatorCID, NS_SIMPLEURIMUTATOR_CID);
33 NS_DEFINE_CID(kStandardURLMutatorCID, NS_STANDARDURLMUTATOR_CID);
34 NS_DEFINE_CID(kJARURIMutatorCID, NS_JARURIMUTATOR_CID);
35 NS_DEFINE_CID(kIconURIMutatorCID, NS_MOZICONURIMUTATOR_CID);
37 } // namespace
39 namespace mozilla {
40 namespace ipc {
42 void SerializeURI(nsIURI* aURI, URIParams& aParams) {
43 MOZ_ASSERT(aURI);
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 if (aURI) {
53 URIParams params;
54 SerializeURI(aURI, params);
55 aParams = Some(std::move(params));
56 } else {
57 aParams = Nothing();
61 already_AddRefed<nsIURI> DeserializeURI(const URIParams& aParams) {
62 nsCOMPtr<nsIURIMutator> mutator;
64 switch (aParams.type()) {
65 case URIParams::TSimpleURIParams:
66 mutator = do_CreateInstance(kSimpleURIMutatorCID);
67 break;
69 case URIParams::TStandardURLParams:
70 if (aParams.get_StandardURLParams().isSubstituting()) {
71 mutator = new net::SubstitutingURL::Mutator();
72 } else {
73 mutator = do_CreateInstance(kStandardURLMutatorCID);
75 break;
77 case URIParams::TJARURIParams:
78 mutator = do_CreateInstance(kJARURIMutatorCID);
79 break;
81 case URIParams::TJSURIParams:
82 mutator = new nsJSURI::Mutator();
83 break;
85 case URIParams::TIconURIParams:
86 mutator = do_CreateInstance(kIconURIMutatorCID);
87 break;
89 case URIParams::TSimpleNestedURIParams:
90 mutator = new net::nsSimpleNestedURI::Mutator();
91 break;
93 case URIParams::THostObjectURIParams:
94 mutator = new mozilla::dom::BlobURL::Mutator();
95 break;
97 case URIParams::TDefaultURIParams:
98 mutator = new mozilla::net::DefaultURI::Mutator();
99 break;
101 case URIParams::TNestedAboutURIParams:
102 mutator = new net::nsNestedAboutURI::Mutator();
103 break;
105 case URIParams::TSubstitutingJARURIParams:
106 mutator = new net::SubstitutingJARURI::Mutator();
107 break;
109 default:
110 MOZ_CRASH("Unknown params!");
113 MOZ_ASSERT(mutator);
115 nsresult rv = mutator->Deserialize(aParams);
116 if (NS_FAILED(rv)) {
117 MOZ_ASSERT(false, "Deserialize failed!");
118 return nullptr;
121 nsCOMPtr<nsIURI> uri;
122 DebugOnly<nsresult> rv2 = mutator->Finalize(getter_AddRefs(uri));
123 MOZ_ASSERT(uri);
124 MOZ_ASSERT(NS_SUCCEEDED(rv2));
126 return uri.forget();
129 already_AddRefed<nsIURI> DeserializeURI(const Maybe<URIParams>& aParams) {
130 nsCOMPtr<nsIURI> uri;
132 if (aParams.isSome()) {
133 uri = DeserializeURI(aParams.ref());
136 return uri.forget();
139 } // namespace ipc
140 } // namespace mozilla