ChannelInfo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 "mozilla/dom/ChannelInfo.h"
  6. #include "nsCOMPtr.h"
  7. #include "nsContentUtils.h"
  8. #include "nsIChannel.h"
  9. #include "nsIDocument.h"
  10. #include "nsIHttpChannel.h"
  11. #include "nsSerializationHelper.h"
  12. #include "mozilla/net/HttpBaseChannel.h"
  13. #include "mozilla/ipc/ChannelInfo.h"
  14. #include "nsNetUtil.h"
  15. using namespace mozilla;
  16. using namespace mozilla::dom;
  17. void
  18. ChannelInfo::InitFromDocument(nsIDocument* aDoc)
  19. {
  20. MOZ_ASSERT(NS_IsMainThread());
  21. MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
  22. nsCOMPtr<nsISupports> securityInfo = aDoc->GetSecurityInfo();
  23. if (securityInfo) {
  24. SetSecurityInfo(securityInfo);
  25. }
  26. mInited = true;
  27. }
  28. void
  29. ChannelInfo::InitFromChannel(nsIChannel* aChannel)
  30. {
  31. MOZ_ASSERT(NS_IsMainThread());
  32. MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
  33. nsCOMPtr<nsISupports> securityInfo;
  34. aChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
  35. if (securityInfo) {
  36. SetSecurityInfo(securityInfo);
  37. }
  38. mInited = true;
  39. }
  40. void
  41. ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal)
  42. {
  43. MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
  44. MOZ_ASSERT(aGlobal);
  45. MOZ_RELEASE_ASSERT(
  46. nsContentUtils::IsSystemPrincipal(aGlobal->PrincipalOrNull()));
  47. mSecurityInfo.Truncate();
  48. mInited = true;
  49. }
  50. void
  51. ChannelInfo::InitFromIPCChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo)
  52. {
  53. MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
  54. mSecurityInfo = aChannelInfo.securityInfo();
  55. mInited = true;
  56. }
  57. void
  58. ChannelInfo::SetSecurityInfo(nsISupports* aSecurityInfo)
  59. {
  60. MOZ_ASSERT(mSecurityInfo.IsEmpty(), "security info should only be set once");
  61. nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aSecurityInfo);
  62. if (!serializable) {
  63. NS_WARNING("A non-serializable object was passed to InternalResponse::SetSecurityInfo");
  64. return;
  65. }
  66. NS_SerializeToString(serializable, mSecurityInfo);
  67. }
  68. nsresult
  69. ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel)
  70. {
  71. MOZ_ASSERT(NS_IsMainThread());
  72. MOZ_ASSERT(mInited);
  73. if (!mSecurityInfo.IsEmpty()) {
  74. nsCOMPtr<nsISupports> infoObj;
  75. nsresult rv = NS_DeserializeObject(mSecurityInfo, getter_AddRefs(infoObj));
  76. if (NS_WARN_IF(NS_FAILED(rv))) {
  77. return rv;
  78. }
  79. nsCOMPtr<nsIHttpChannel> httpChannel =
  80. do_QueryInterface(aChannel);
  81. MOZ_ASSERT(httpChannel);
  82. net::HttpBaseChannel* httpBaseChannel =
  83. static_cast<net::HttpBaseChannel*>(httpChannel.get());
  84. rv = httpBaseChannel->OverrideSecurityInfo(infoObj);
  85. if (NS_WARN_IF(NS_FAILED(rv))) {
  86. return rv;
  87. }
  88. }
  89. return NS_OK;
  90. }
  91. mozilla::ipc::IPCChannelInfo
  92. ChannelInfo::AsIPCChannelInfo() const
  93. {
  94. // This may be called when mInited is false, for example if we try to store
  95. // a synthesized Response object into the Cache. Uninitialized and empty
  96. // ChannelInfo objects are indistinguishable at the IPC level, so this is
  97. // fine.
  98. IPCChannelInfo ipcInfo;
  99. ipcInfo.securityInfo() = mSecurityInfo;
  100. return ipcInfo;
  101. }