nsNullPrincipalURI.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. * vim: sw=2 ts=2 sts=2 expandtab
  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/. */
  6. #include "nsNullPrincipalURI.h"
  7. #include "mozilla/ArrayUtils.h"
  8. #include "mozilla/DebugOnly.h"
  9. #include "mozilla/MemoryReporting.h"
  10. #include "mozilla/Services.h"
  11. #include "mozilla/Unused.h"
  12. #include "mozilla/ipc/URIParams.h"
  13. #include "nsEscape.h"
  14. #include "nsCRT.h"
  15. #include "nsIUUIDGenerator.h"
  16. using namespace mozilla;
  17. ////////////////////////////////////////////////////////////////////////////////
  18. //// nsNullPrincipalURI
  19. nsNullPrincipalURI::nsNullPrincipalURI()
  20. : mPath(mPathBytes, ArrayLength(mPathBytes), ArrayLength(mPathBytes) - 1)
  21. {
  22. }
  23. nsNullPrincipalURI::nsNullPrincipalURI(const nsNullPrincipalURI& aOther)
  24. : mPath(mPathBytes, ArrayLength(mPathBytes), ArrayLength(mPathBytes) - 1)
  25. {
  26. mPath.Assign(aOther.mPath);
  27. }
  28. nsresult
  29. nsNullPrincipalURI::Init()
  30. {
  31. // FIXME: bug 327161 -- make sure the uuid generator is reseeding-resistant.
  32. nsCOMPtr<nsIUUIDGenerator> uuidgen = services::GetUUIDGenerator();
  33. NS_ENSURE_TRUE(uuidgen, NS_ERROR_NOT_AVAILABLE);
  34. nsID id;
  35. nsresult rv = uuidgen->GenerateUUIDInPlace(&id);
  36. NS_ENSURE_SUCCESS(rv, rv);
  37. MOZ_ASSERT(mPathBytes == mPath.BeginWriting());
  38. id.ToProvidedString(mPathBytes);
  39. MOZ_ASSERT(mPath.Length() == NSID_LENGTH - 1);
  40. MOZ_ASSERT(strlen(mPath.get()) == NSID_LENGTH - 1);
  41. return NS_OK;
  42. }
  43. /* static */
  44. already_AddRefed<nsNullPrincipalURI>
  45. nsNullPrincipalURI::Create()
  46. {
  47. RefPtr<nsNullPrincipalURI> uri = new nsNullPrincipalURI();
  48. nsresult rv = uri->Init();
  49. NS_ENSURE_SUCCESS(rv, nullptr);
  50. return uri.forget();
  51. }
  52. static NS_DEFINE_CID(kNullPrincipalURIImplementationCID,
  53. NS_NULLPRINCIPALURI_IMPLEMENTATION_CID);
  54. NS_IMPL_ADDREF(nsNullPrincipalURI)
  55. NS_IMPL_RELEASE(nsNullPrincipalURI)
  56. NS_INTERFACE_MAP_BEGIN(nsNullPrincipalURI)
  57. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURI)
  58. if (aIID.Equals(kNullPrincipalURIImplementationCID))
  59. foundInterface = static_cast<nsIURI *>(this);
  60. else
  61. NS_INTERFACE_MAP_ENTRY(nsIURI)
  62. NS_INTERFACE_MAP_ENTRY(nsISizeOf)
  63. NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableURI)
  64. NS_INTERFACE_MAP_END
  65. ////////////////////////////////////////////////////////////////////////////////
  66. //// nsIURI
  67. NS_IMETHODIMP
  68. nsNullPrincipalURI::GetAsciiHost(nsACString &_host)
  69. {
  70. _host.Truncate();
  71. return NS_OK;
  72. }
  73. NS_IMETHODIMP
  74. nsNullPrincipalURI::GetAsciiHostPort(nsACString &_hostport)
  75. {
  76. return NS_ERROR_NOT_IMPLEMENTED;
  77. }
  78. NS_IMETHODIMP
  79. nsNullPrincipalURI::GetAsciiSpec(nsACString &_spec)
  80. {
  81. nsAutoCString buffer;
  82. // Ignore the return value -- nsNullPrincipalURI::GetSpec() is infallible.
  83. Unused << GetSpec(buffer);
  84. // This uses the infallible version of |NS_EscapeURL| as |GetSpec| is
  85. // already infallible.
  86. NS_EscapeURL(buffer, esc_OnlyNonASCII | esc_AlwaysCopy, _spec);
  87. return NS_OK;
  88. }
  89. NS_IMETHODIMP
  90. nsNullPrincipalURI::GetHost(nsACString &_host)
  91. {
  92. _host.Truncate();
  93. return NS_OK;
  94. }
  95. NS_IMETHODIMP
  96. nsNullPrincipalURI::SetHost(const nsACString &aHost)
  97. {
  98. return NS_ERROR_NOT_IMPLEMENTED;
  99. }
  100. NS_IMETHODIMP
  101. nsNullPrincipalURI::GetHostPort(nsACString &_host)
  102. {
  103. return NS_ERROR_NOT_IMPLEMENTED;
  104. }
  105. NS_IMETHODIMP
  106. nsNullPrincipalURI::SetHostPort(const nsACString &aHost)
  107. {
  108. return NS_ERROR_NOT_IMPLEMENTED;
  109. }
  110. NS_IMETHODIMP
  111. nsNullPrincipalURI::SetHostAndPort(const nsACString &aHost)
  112. {
  113. return NS_ERROR_NOT_IMPLEMENTED;
  114. }
  115. NS_IMETHODIMP
  116. nsNullPrincipalURI::GetOriginCharset(nsACString &_charset)
  117. {
  118. _charset.Truncate();
  119. return NS_OK;
  120. }
  121. NS_IMETHODIMP
  122. nsNullPrincipalURI::GetPassword(nsACString &_password)
  123. {
  124. return NS_ERROR_NOT_IMPLEMENTED;
  125. }
  126. NS_IMETHODIMP
  127. nsNullPrincipalURI::SetPassword(const nsACString &aPassword)
  128. {
  129. return NS_ERROR_NOT_IMPLEMENTED;
  130. }
  131. NS_IMETHODIMP
  132. nsNullPrincipalURI::GetPath(nsACString &_path)
  133. {
  134. _path = mPath;
  135. return NS_OK;
  136. }
  137. NS_IMETHODIMP
  138. nsNullPrincipalURI::SetPath(const nsACString &aPath)
  139. {
  140. return NS_ERROR_NOT_IMPLEMENTED;
  141. }
  142. NS_IMETHODIMP
  143. nsNullPrincipalURI::GetFilePath(nsACString &aFilePath)
  144. {
  145. aFilePath.Truncate();
  146. return NS_ERROR_NOT_IMPLEMENTED;
  147. }
  148. NS_IMETHODIMP
  149. nsNullPrincipalURI::SetFilePath(const nsACString &aFilePath)
  150. {
  151. return NS_ERROR_NOT_IMPLEMENTED;
  152. }
  153. NS_IMETHODIMP
  154. nsNullPrincipalURI::GetQuery(nsACString &aQuery)
  155. {
  156. aQuery.Truncate();
  157. return NS_ERROR_NOT_IMPLEMENTED;
  158. }
  159. NS_IMETHODIMP
  160. nsNullPrincipalURI::SetQuery(const nsACString &aQuery)
  161. {
  162. return NS_ERROR_NOT_IMPLEMENTED;
  163. }
  164. NS_IMETHODIMP
  165. nsNullPrincipalURI::GetRef(nsACString &_ref)
  166. {
  167. _ref.Truncate();
  168. return NS_ERROR_NOT_IMPLEMENTED;
  169. }
  170. NS_IMETHODIMP
  171. nsNullPrincipalURI::SetRef(const nsACString &aRef)
  172. {
  173. return NS_ERROR_NOT_IMPLEMENTED;
  174. }
  175. NS_IMETHODIMP
  176. nsNullPrincipalURI::GetPrePath(nsACString &_prePath)
  177. {
  178. _prePath = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":");
  179. return NS_OK;
  180. }
  181. NS_IMETHODIMP
  182. nsNullPrincipalURI::GetPort(int32_t *_port)
  183. {
  184. return NS_ERROR_NOT_IMPLEMENTED;
  185. }
  186. NS_IMETHODIMP
  187. nsNullPrincipalURI::SetPort(int32_t aPort)
  188. {
  189. return NS_ERROR_NOT_IMPLEMENTED;
  190. }
  191. NS_IMETHODIMP
  192. nsNullPrincipalURI::GetScheme(nsACString &_scheme)
  193. {
  194. _scheme = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME);
  195. return NS_OK;
  196. }
  197. NS_IMETHODIMP
  198. nsNullPrincipalURI::SetScheme(const nsACString &aScheme)
  199. {
  200. return NS_ERROR_NOT_IMPLEMENTED;
  201. }
  202. NS_IMETHODIMP
  203. nsNullPrincipalURI::GetSpec(nsACString &_spec)
  204. {
  205. _spec = NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":") + mPath;
  206. return NS_OK;
  207. }
  208. // result may contain unescaped UTF-8 characters
  209. NS_IMETHODIMP
  210. nsNullPrincipalURI::GetSpecIgnoringRef(nsACString &result)
  211. {
  212. return GetSpec(result);
  213. }
  214. NS_IMETHODIMP
  215. nsNullPrincipalURI::GetHasRef(bool *result)
  216. {
  217. *result = false;
  218. return NS_OK;
  219. }
  220. NS_IMETHODIMP
  221. nsNullPrincipalURI::SetSpec(const nsACString &aSpec)
  222. {
  223. return NS_ERROR_NOT_IMPLEMENTED;
  224. }
  225. NS_IMETHODIMP
  226. nsNullPrincipalURI::GetUsername(nsACString &_username)
  227. {
  228. return NS_ERROR_NOT_IMPLEMENTED;
  229. }
  230. NS_IMETHODIMP
  231. nsNullPrincipalURI::SetUsername(const nsACString &aUsername)
  232. {
  233. return NS_ERROR_NOT_IMPLEMENTED;
  234. }
  235. NS_IMETHODIMP
  236. nsNullPrincipalURI::GetUserPass(nsACString &_userPass)
  237. {
  238. return NS_ERROR_NOT_IMPLEMENTED;
  239. }
  240. NS_IMETHODIMP
  241. nsNullPrincipalURI::SetUserPass(const nsACString &aUserPass)
  242. {
  243. return NS_ERROR_NOT_IMPLEMENTED;
  244. }
  245. NS_IMETHODIMP
  246. nsNullPrincipalURI::Clone(nsIURI **_newURI)
  247. {
  248. nsCOMPtr<nsIURI> uri = new nsNullPrincipalURI(*this);
  249. uri.forget(_newURI);
  250. return NS_OK;
  251. }
  252. NS_IMETHODIMP
  253. nsNullPrincipalURI::CloneIgnoringRef(nsIURI **_newURI)
  254. {
  255. // GetRef/SetRef not supported by nsNullPrincipalURI, so
  256. // CloneIgnoringRef() is the same as Clone().
  257. return Clone(_newURI);
  258. }
  259. NS_IMETHODIMP
  260. nsNullPrincipalURI::CloneWithNewRef(const nsACString& newRef, nsIURI **_newURI)
  261. {
  262. // GetRef/SetRef not supported by nsNullPrincipalURI, so
  263. // CloneWithNewRef() is the same as Clone().
  264. return Clone(_newURI);
  265. }
  266. NS_IMETHODIMP
  267. nsNullPrincipalURI::Equals(nsIURI *aOther, bool *_equals)
  268. {
  269. *_equals = false;
  270. RefPtr<nsNullPrincipalURI> otherURI;
  271. nsresult rv = aOther->QueryInterface(kNullPrincipalURIImplementationCID,
  272. getter_AddRefs(otherURI));
  273. if (NS_SUCCEEDED(rv)) {
  274. *_equals = mPath == otherURI->mPath;
  275. }
  276. return NS_OK;
  277. }
  278. NS_IMETHODIMP
  279. nsNullPrincipalURI::EqualsExceptRef(nsIURI *aOther, bool *_equals)
  280. {
  281. // GetRef/SetRef not supported by nsNullPrincipalURI, so
  282. // EqualsExceptRef() is the same as Equals().
  283. return Equals(aOther, _equals);
  284. }
  285. NS_IMETHODIMP
  286. nsNullPrincipalURI::Resolve(const nsACString &aRelativePath,
  287. nsACString &_resolvedURI)
  288. {
  289. _resolvedURI = aRelativePath;
  290. return NS_OK;
  291. }
  292. NS_IMETHODIMP
  293. nsNullPrincipalURI::SchemeIs(const char *aScheme, bool *_schemeIs)
  294. {
  295. *_schemeIs = (0 == nsCRT::strcasecmp(NS_NULLPRINCIPAL_SCHEME, aScheme));
  296. return NS_OK;
  297. }
  298. ////////////////////////////////////////////////////////////////////////////////
  299. //// nsIIPCSerializableURI
  300. void
  301. nsNullPrincipalURI::Serialize(mozilla::ipc::URIParams &aParams)
  302. {
  303. aParams = mozilla::ipc::NullPrincipalURIParams();
  304. }
  305. bool
  306. nsNullPrincipalURI::Deserialize(const mozilla::ipc::URIParams &aParams)
  307. {
  308. if (aParams.type() != mozilla::ipc::URIParams::TNullPrincipalURIParams) {
  309. MOZ_ASSERT_UNREACHABLE("unexpected URIParams type");
  310. return false;
  311. }
  312. nsresult rv = Init();
  313. NS_ENSURE_SUCCESS(rv, false);
  314. return true;
  315. }
  316. ////////////////////////////////////////////////////////////////////////////////
  317. //// nsISizeOf
  318. size_t
  319. nsNullPrincipalURI::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
  320. {
  321. return mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  322. }
  323. size_t
  324. nsNullPrincipalURI::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
  325. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  326. }