LoadContextInfo.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "LoadContextInfo.h"
  5. #include "mozilla/dom/ToJSValue.h"
  6. #include "nsIChannel.h"
  7. #include "nsILoadContext.h"
  8. #include "nsIWebNavigation.h"
  9. #include "nsNetUtil.h"
  10. using namespace mozilla::dom;
  11. namespace mozilla {
  12. namespace net {
  13. // LoadContextInfo
  14. NS_IMPL_ISUPPORTS(LoadContextInfo, nsILoadContextInfo)
  15. LoadContextInfo::LoadContextInfo(bool aIsAnonymous, NeckoOriginAttributes aOriginAttributes)
  16. : mIsAnonymous(aIsAnonymous)
  17. , mOriginAttributes(aOriginAttributes)
  18. {
  19. }
  20. LoadContextInfo::~LoadContextInfo()
  21. {
  22. }
  23. NS_IMETHODIMP LoadContextInfo::GetIsPrivate(bool *aIsPrivate)
  24. {
  25. *aIsPrivate = mOriginAttributes.mPrivateBrowsingId > 0;
  26. return NS_OK;
  27. }
  28. NS_IMETHODIMP LoadContextInfo::GetIsAnonymous(bool *aIsAnonymous)
  29. {
  30. *aIsAnonymous = mIsAnonymous;
  31. return NS_OK;
  32. }
  33. NeckoOriginAttributes const* LoadContextInfo::OriginAttributesPtr()
  34. {
  35. return &mOriginAttributes;
  36. }
  37. NS_IMETHODIMP LoadContextInfo::GetOriginAttributes(JSContext *aCx,
  38. JS::MutableHandle<JS::Value> aVal)
  39. {
  40. if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) {
  41. return NS_ERROR_FAILURE;
  42. }
  43. return NS_OK;
  44. }
  45. // LoadContextInfoFactory
  46. NS_IMPL_ISUPPORTS(LoadContextInfoFactory, nsILoadContextInfoFactory)
  47. NS_IMETHODIMP LoadContextInfoFactory::GetDefault(nsILoadContextInfo * *aDefault)
  48. {
  49. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, NeckoOriginAttributes());
  50. info.forget(aDefault);
  51. return NS_OK;
  52. }
  53. NS_IMETHODIMP LoadContextInfoFactory::GetPrivate(nsILoadContextInfo * *aPrivate)
  54. {
  55. NeckoOriginAttributes attrs;
  56. attrs.SyncAttributesWithPrivateBrowsing(true);
  57. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(false, attrs);
  58. info.forget(aPrivate);
  59. return NS_OK;
  60. }
  61. NS_IMETHODIMP LoadContextInfoFactory::GetAnonymous(nsILoadContextInfo * *aAnonymous)
  62. {
  63. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(true, NeckoOriginAttributes());
  64. info.forget(aAnonymous);
  65. return NS_OK;
  66. }
  67. NS_IMETHODIMP LoadContextInfoFactory::Custom(bool aAnonymous,
  68. JS::HandleValue aOriginAttributes, JSContext *cx,
  69. nsILoadContextInfo * *_retval)
  70. {
  71. NeckoOriginAttributes attrs;
  72. bool status = attrs.Init(cx, aOriginAttributes);
  73. NS_ENSURE_TRUE(status, NS_ERROR_FAILURE);
  74. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aAnonymous, attrs);
  75. info.forget(_retval);
  76. return NS_OK;
  77. }
  78. NS_IMETHODIMP LoadContextInfoFactory::FromLoadContext(nsILoadContext *aLoadContext, bool aAnonymous,
  79. nsILoadContextInfo * *_retval)
  80. {
  81. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aLoadContext, aAnonymous);
  82. info.forget(_retval);
  83. return NS_OK;
  84. }
  85. NS_IMETHODIMP LoadContextInfoFactory::FromWindow(nsIDOMWindow *aWindow, bool aAnonymous,
  86. nsILoadContextInfo * *_retval)
  87. {
  88. nsCOMPtr<nsILoadContextInfo> info = GetLoadContextInfo(aWindow, aAnonymous);
  89. info.forget(_retval);
  90. return NS_OK;
  91. }
  92. // Helper functions
  93. LoadContextInfo *
  94. GetLoadContextInfo(nsIChannel * aChannel)
  95. {
  96. nsresult rv;
  97. DebugOnly<bool> pb = NS_UsePrivateBrowsing(aChannel);
  98. bool anon = false;
  99. nsLoadFlags loadFlags;
  100. rv = aChannel->GetLoadFlags(&loadFlags);
  101. if (NS_SUCCEEDED(rv)) {
  102. anon = !!(loadFlags & nsIChannel::LOAD_ANONYMOUS);
  103. }
  104. NeckoOriginAttributes oa;
  105. NS_GetOriginAttributes(aChannel, oa);
  106. MOZ_ASSERT(pb == (oa.mPrivateBrowsingId > 0));
  107. return new LoadContextInfo(anon, oa);
  108. }
  109. LoadContextInfo *
  110. GetLoadContextInfo(nsILoadContext *aLoadContext, bool aIsAnonymous)
  111. {
  112. if (!aLoadContext) {
  113. return new LoadContextInfo(aIsAnonymous,
  114. NeckoOriginAttributes(nsILoadContextInfo::NO_APP_ID, false));
  115. }
  116. DebugOnly<bool> pb = aLoadContext->UsePrivateBrowsing();
  117. DocShellOriginAttributes doa;
  118. aLoadContext->GetOriginAttributes(doa);
  119. MOZ_ASSERT(pb == (doa.mPrivateBrowsingId > 0));
  120. NeckoOriginAttributes noa;
  121. noa.InheritFromDocShellToNecko(doa);
  122. return new LoadContextInfo(aIsAnonymous, noa);
  123. }
  124. LoadContextInfo*
  125. GetLoadContextInfo(nsIDOMWindow *aWindow,
  126. bool aIsAnonymous)
  127. {
  128. nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
  129. nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav);
  130. return GetLoadContextInfo(loadContext, aIsAnonymous);
  131. }
  132. LoadContextInfo *
  133. GetLoadContextInfo(nsILoadContextInfo *aInfo)
  134. {
  135. return new LoadContextInfo(aInfo->IsAnonymous(),
  136. *aInfo->OriginAttributesPtr());
  137. }
  138. LoadContextInfo *
  139. GetLoadContextInfo(bool const aIsAnonymous,
  140. NeckoOriginAttributes const &aOriginAttributes)
  141. {
  142. return new LoadContextInfo(aIsAnonymous,
  143. aOriginAttributes);
  144. }
  145. } // namespace net
  146. } // namespace mozilla