ipc/glue/URIUtils.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:af105670367d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "URIUtils.h"
6
7 #include "nsIIPCSerializableURI.h"
8
9 #include "mozilla/ArrayUtils.h"
10 #include "mozilla/Assertions.h"
11 #include "nsComponentManagerUtils.h"
12 #include "nsDebug.h"
13 #include "nsID.h"
14 #include "nsJARURI.h"
15 #include "nsNetCID.h"
16 #include "nsNetUtil.h"
17 #include "nsThreadUtils.h"
18
19 using namespace mozilla::ipc;
20 using mozilla::ArrayLength;
21
22 namespace {
23
24 NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
25 NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
26 NS_DEFINE_CID(kJARURICID, NS_JARURI_CID);
27
28 struct StringWithLengh
29 {
30 const char* string;
31 size_t length;
32 };
33
34 #define STRING_WITH_LENGTH(_str) \
35 { _str, ArrayLength(_str) - 1 }
36
37 const StringWithLengh kGenericURIAllowedSchemes[] = {
38 STRING_WITH_LENGTH("about:"),
39 STRING_WITH_LENGTH("javascript:"),
40 STRING_WITH_LENGTH("javascript")
41 };
42
43 #undef STRING_WITH_LENGTH
44
45 } // anonymous namespace
46
47 namespace mozilla {
48 namespace ipc {
49
50 void
51 SerializeURI(nsIURI* aURI,
52 URIParams& aParams)
53 {
54 MOZ_ASSERT(NS_IsMainThread());
55 MOZ_ASSERT(aURI);
56
57 nsCOMPtr<nsIIPCSerializableURI> serializable = do_QueryInterface(aURI);
58 if (serializable) {
59 serializable->Serialize(aParams);
60 if (aParams.type() == URIParams::T__None) {
61 MOZ_CRASH("Serialize failed!");
62 }
63 return;
64 }
65
66 nsCString scheme;
67 if (NS_FAILED(aURI->GetScheme(scheme))) {
68 MOZ_CRASH("This must never fail!");
69 }
70
71 bool allowed = false;
72
73 for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) {
74 const StringWithLengh& entry = kGenericURIAllowedSchemes[i];
75 if (scheme.EqualsASCII(entry.string, entry.length)) {
76 allowed = true;
77 break;
78 }
79 }
80
81 if (!allowed) {
82 MOZ_CRASH("All IPDL URIs must be serializable or an allowed "
83 "scheme!");
84 }
85
86 GenericURIParams params;
87 if (NS_FAILED(aURI->GetSpec(params.spec())) ||
88 NS_FAILED(aURI->GetOriginCharset(params.charset()))) {
89 MOZ_CRASH("This must never fail!");
90 }
91
92 aParams = params;
93 }
94
95 void
96 SerializeURI(nsIURI* aURI,
97 OptionalURIParams& aParams)
98 {
99 MOZ_ASSERT(NS_IsMainThread());
100
101 if (aURI) {
102 URIParams params;
103 SerializeURI(aURI, params);
104 aParams = params;
105 }
106 else {
107 aParams = mozilla::void_t();
108 }
109 }
110
111 already_AddRefed<nsIURI>
112 DeserializeURI(const URIParams& aParams)
113 {
114 MOZ_ASSERT(NS_IsMainThread());
115
116 nsCOMPtr<nsIURI> uri;
117
118 if (aParams.type() != URIParams::TGenericURIParams) {
119 nsCOMPtr<nsIIPCSerializableURI> serializable;
120
121 switch (aParams.type()) {
122 case URIParams::TSimpleURIParams:
123 serializable = do_CreateInstance(kSimpleURICID);
124 break;
125
126 case URIParams::TStandardURLParams:
127 serializable = do_CreateInstance(kStandardURLCID);
128 break;
129
130 case URIParams::TJARURIParams:
131 serializable = do_CreateInstance(kJARURICID);
132 break;
133
134 default:
135 MOZ_CRASH("Unknown params!");
136 }
137
138 MOZ_ASSERT(serializable);
139
140 if (!serializable->Deserialize(aParams)) {
141 MOZ_ASSERT(false, "Deserialize failed!");
142 return nullptr;
143 }
144
145 uri = do_QueryInterface(serializable);
146 MOZ_ASSERT(uri);
147
148 return uri.forget();
149 }
150
151 MOZ_ASSERT(aParams.type() == URIParams::TGenericURIParams);
152
153 const GenericURIParams& params = aParams.get_GenericURIParams();
154
155 if (NS_FAILED(NS_NewURI(getter_AddRefs(uri), params.spec(),
156 params.charset().get()))) {
157 NS_WARNING("Failed to make new URI!");
158 return nullptr;
159 }
160
161 nsCString scheme;
162 if (NS_FAILED(uri->GetScheme(scheme))) {
163 MOZ_CRASH("This must never fail!");
164 }
165
166 bool allowed = false;
167
168 for (size_t i = 0; i < ArrayLength(kGenericURIAllowedSchemes); i++) {
169 const StringWithLengh& entry = kGenericURIAllowedSchemes[i];
170 if (scheme.EqualsASCII(entry.string, entry.length)) {
171 allowed = true;
172 break;
173 }
174 }
175
176 if (!allowed) {
177 MOZ_ASSERT(false, "This type of URI is not allowed!");
178 return nullptr;
179 }
180
181 return uri.forget();
182 }
183
184 already_AddRefed<nsIURI>
185 DeserializeURI(const OptionalURIParams& aParams)
186 {
187 MOZ_ASSERT(NS_IsMainThread());
188
189 nsCOMPtr<nsIURI> uri;
190
191 switch (aParams.type()) {
192 case OptionalURIParams::Tvoid_t:
193 break;
194
195 case OptionalURIParams::TURIParams:
196 uri = DeserializeURI(aParams.get_URIParams());
197 break;
198
199 default:
200 MOZ_CRASH("Unknown params!");
201 }
202
203 return uri.forget();
204 }
205
206 } // namespace ipc
207 } // namespace mozilla

mercurial