nsSimpleNestedURI.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "base/basictypes.h"
  6. #include "nsNetCID.h"
  7. #include "nsNetUtil.h"
  8. #include "nsSimpleNestedURI.h"
  9. #include "nsIObjectInputStream.h"
  10. #include "nsIObjectOutputStream.h"
  11. #include "mozilla/ipc/URIUtils.h"
  12. namespace mozilla {
  13. namespace net {
  14. NS_IMPL_ISUPPORTS_INHERITED(nsSimpleNestedURI, nsSimpleURI, nsINestedURI)
  15. nsSimpleNestedURI::nsSimpleNestedURI(nsIURI* innerURI)
  16. : mInnerURI(innerURI)
  17. {
  18. NS_ASSERTION(innerURI, "Must have inner URI");
  19. NS_TryToSetImmutable(innerURI);
  20. }
  21. // nsISerializable
  22. NS_IMETHODIMP
  23. nsSimpleNestedURI::Read(nsIObjectInputStream* aStream)
  24. {
  25. nsresult rv = nsSimpleURI::Read(aStream);
  26. if (NS_FAILED(rv)) return rv;
  27. NS_ASSERTION(!mMutable, "How did that happen?");
  28. nsCOMPtr<nsISupports> supports;
  29. rv = aStream->ReadObject(true, getter_AddRefs(supports));
  30. if (NS_FAILED(rv)) return rv;
  31. mInnerURI = do_QueryInterface(supports, &rv);
  32. if (NS_FAILED(rv)) return rv;
  33. NS_TryToSetImmutable(mInnerURI);
  34. return rv;
  35. }
  36. NS_IMETHODIMP
  37. nsSimpleNestedURI::Write(nsIObjectOutputStream* aStream)
  38. {
  39. nsCOMPtr<nsISerializable> serializable = do_QueryInterface(mInnerURI);
  40. if (!serializable) {
  41. // We can't serialize ourselves
  42. return NS_ERROR_NOT_AVAILABLE;
  43. }
  44. nsresult rv = nsSimpleURI::Write(aStream);
  45. if (NS_FAILED(rv)) return rv;
  46. rv = aStream->WriteCompoundObject(mInnerURI, NS_GET_IID(nsIURI),
  47. true);
  48. return rv;
  49. }
  50. // nsIIPCSerializableURI
  51. void
  52. nsSimpleNestedURI::Serialize(mozilla::ipc::URIParams& aParams)
  53. {
  54. using namespace mozilla::ipc;
  55. SimpleNestedURIParams params;
  56. URIParams simpleParams;
  57. nsSimpleURI::Serialize(simpleParams);
  58. params.simpleParams() = simpleParams;
  59. SerializeURI(mInnerURI, params.innerURI());
  60. aParams = params;
  61. }
  62. bool
  63. nsSimpleNestedURI::Deserialize(const mozilla::ipc::URIParams& aParams)
  64. {
  65. using namespace mozilla::ipc;
  66. if (aParams.type() != URIParams::TSimpleNestedURIParams) {
  67. NS_ERROR("Received unknown parameters from the other process!");
  68. return false;
  69. }
  70. const SimpleNestedURIParams& params = aParams.get_SimpleNestedURIParams();
  71. if (!nsSimpleURI::Deserialize(params.simpleParams()))
  72. return false;
  73. mInnerURI = DeserializeURI(params.innerURI());
  74. NS_TryToSetImmutable(mInnerURI);
  75. return true;
  76. }
  77. // nsINestedURI
  78. NS_IMETHODIMP
  79. nsSimpleNestedURI::GetInnerURI(nsIURI** uri)
  80. {
  81. NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
  82. return NS_EnsureSafeToReturn(mInnerURI, uri);
  83. }
  84. NS_IMETHODIMP
  85. nsSimpleNestedURI::GetInnermostURI(nsIURI** uri)
  86. {
  87. return NS_ImplGetInnermostURI(this, uri);
  88. }
  89. // nsSimpleURI overrides
  90. /* virtual */ nsresult
  91. nsSimpleNestedURI::EqualsInternal(nsIURI* other,
  92. nsSimpleURI::RefHandlingEnum refHandlingMode,
  93. bool* result)
  94. {
  95. *result = false;
  96. NS_ENSURE_TRUE(mInnerURI, NS_ERROR_NOT_INITIALIZED);
  97. if (other) {
  98. bool correctScheme;
  99. nsresult rv = other->SchemeIs(mScheme.get(), &correctScheme);
  100. NS_ENSURE_SUCCESS(rv, rv);
  101. if (correctScheme) {
  102. nsCOMPtr<nsINestedURI> nest = do_QueryInterface(other);
  103. if (nest) {
  104. nsCOMPtr<nsIURI> otherInner;
  105. rv = nest->GetInnerURI(getter_AddRefs(otherInner));
  106. NS_ENSURE_SUCCESS(rv, rv);
  107. return (refHandlingMode == eHonorRef) ?
  108. otherInner->Equals(mInnerURI, result) :
  109. otherInner->EqualsExceptRef(mInnerURI, result);
  110. }
  111. }
  112. }
  113. return NS_OK;
  114. }
  115. /* virtual */ nsSimpleURI*
  116. nsSimpleNestedURI::StartClone(nsSimpleURI::RefHandlingEnum refHandlingMode,
  117. const nsACString& newRef)
  118. {
  119. NS_ENSURE_TRUE(mInnerURI, nullptr);
  120. nsCOMPtr<nsIURI> innerClone;
  121. nsresult rv;
  122. if (refHandlingMode == eHonorRef) {
  123. rv = mInnerURI->Clone(getter_AddRefs(innerClone));
  124. } else if (refHandlingMode == eReplaceRef) {
  125. rv = mInnerURI->CloneWithNewRef(newRef, getter_AddRefs(innerClone));
  126. } else {
  127. rv = mInnerURI->CloneIgnoringRef(getter_AddRefs(innerClone));
  128. }
  129. if (NS_FAILED(rv)) {
  130. return nullptr;
  131. }
  132. nsSimpleNestedURI* url = new nsSimpleNestedURI(innerClone);
  133. SetRefOnClone(url, refHandlingMode, newRef);
  134. url->SetMutable(false);
  135. return url;
  136. }
  137. // nsIClassInfo overrides
  138. NS_IMETHODIMP
  139. nsSimpleNestedURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
  140. {
  141. static NS_DEFINE_CID(kSimpleNestedURICID, NS_SIMPLENESTEDURI_CID);
  142. *aClassIDNoAlloc = kSimpleNestedURICID;
  143. return NS_OK;
  144. }
  145. } // namespace net
  146. } // namespace mozilla