LoadContext.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/Assertions.h"
  6. #include "mozilla/BasePrincipal.h"
  7. #include "mozilla/LoadContext.h"
  8. #include "mozilla/Preferences.h"
  9. #include "mozilla/dom/ScriptSettings.h" // for AutoJSAPI
  10. #include "nsContentUtils.h"
  11. #include "xpcpublic.h"
  12. bool
  13. nsILoadContext::GetOriginAttributes(mozilla::DocShellOriginAttributes& aAttrs)
  14. {
  15. mozilla::dom::AutoJSAPI jsapi;
  16. bool ok = jsapi.Init(xpc::PrivilegedJunkScope());
  17. NS_ENSURE_TRUE(ok, false);
  18. JS::Rooted<JS::Value> v(jsapi.cx());
  19. nsresult rv = GetOriginAttributes(&v);
  20. NS_ENSURE_SUCCESS(rv, false);
  21. NS_ENSURE_TRUE(v.isObject(), false);
  22. JS::Rooted<JSObject*> obj(jsapi.cx(), &v.toObject());
  23. // If we're JS-implemented, the object will be left in a different (System-Principaled)
  24. // scope, so we may need to enter its compartment.
  25. MOZ_ASSERT(nsContentUtils::IsSystemPrincipal(nsContentUtils::ObjectPrincipal(obj)));
  26. JSAutoCompartment ac(jsapi.cx(), obj);
  27. mozilla::DocShellOriginAttributes attrs;
  28. ok = attrs.Init(jsapi.cx(), v);
  29. NS_ENSURE_TRUE(ok, false);
  30. aAttrs = attrs;
  31. return true;
  32. }
  33. namespace mozilla {
  34. NS_IMPL_ISUPPORTS(LoadContext, nsILoadContext, nsIInterfaceRequestor)
  35. LoadContext::LoadContext(nsIPrincipal* aPrincipal,
  36. nsILoadContext* aOptionalBase)
  37. : mTopFrameElement(nullptr)
  38. , mNestedFrameId(0)
  39. , mIsContent(true)
  40. , mUseRemoteTabs(false)
  41. #ifdef DEBUG
  42. , mIsNotNull(true)
  43. #endif
  44. {
  45. PrincipalOriginAttributes poa = BasePrincipal::Cast(aPrincipal)->OriginAttributesRef();
  46. mOriginAttributes.InheritFromDocToChildDocShell(poa);
  47. if (!aOptionalBase) {
  48. return;
  49. }
  50. MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetIsContent(&mIsContent));
  51. MOZ_ALWAYS_SUCCEEDS(aOptionalBase->GetUseRemoteTabs(&mUseRemoteTabs));
  52. }
  53. //-----------------------------------------------------------------------------
  54. // LoadContext::nsILoadContext
  55. //-----------------------------------------------------------------------------
  56. NS_IMETHODIMP
  57. LoadContext::GetAssociatedWindow(mozIDOMWindowProxy**)
  58. {
  59. MOZ_ASSERT(mIsNotNull);
  60. // can't support this in the parent process
  61. return NS_ERROR_UNEXPECTED;
  62. }
  63. NS_IMETHODIMP
  64. LoadContext::GetTopWindow(mozIDOMWindowProxy**)
  65. {
  66. MOZ_ASSERT(mIsNotNull);
  67. // can't support this in the parent process
  68. return NS_ERROR_UNEXPECTED;
  69. }
  70. NS_IMETHODIMP
  71. LoadContext::GetTopFrameElement(nsIDOMElement** aElement)
  72. {
  73. nsCOMPtr<nsIDOMElement> element = do_QueryReferent(mTopFrameElement);
  74. element.forget(aElement);
  75. return NS_OK;
  76. }
  77. NS_IMETHODIMP
  78. LoadContext::GetNestedFrameId(uint64_t* aId)
  79. {
  80. NS_ENSURE_ARG(aId);
  81. *aId = mNestedFrameId;
  82. return NS_OK;
  83. }
  84. NS_IMETHODIMP
  85. LoadContext::GetIsContent(bool* aIsContent)
  86. {
  87. MOZ_ASSERT(mIsNotNull);
  88. NS_ENSURE_ARG_POINTER(aIsContent);
  89. *aIsContent = mIsContent;
  90. return NS_OK;
  91. }
  92. NS_IMETHODIMP
  93. LoadContext::GetUsePrivateBrowsing(bool* aUsePrivateBrowsing)
  94. {
  95. MOZ_ASSERT(mIsNotNull);
  96. NS_ENSURE_ARG_POINTER(aUsePrivateBrowsing);
  97. *aUsePrivateBrowsing = mOriginAttributes.mPrivateBrowsingId > 0;
  98. return NS_OK;
  99. }
  100. NS_IMETHODIMP
  101. LoadContext::SetUsePrivateBrowsing(bool aUsePrivateBrowsing)
  102. {
  103. MOZ_ASSERT(mIsNotNull);
  104. // We shouldn't need this on parent...
  105. return NS_ERROR_UNEXPECTED;
  106. }
  107. NS_IMETHODIMP
  108. LoadContext::SetPrivateBrowsing(bool aUsePrivateBrowsing)
  109. {
  110. MOZ_ASSERT(mIsNotNull);
  111. // We shouldn't need this on parent...
  112. return NS_ERROR_UNEXPECTED;
  113. }
  114. NS_IMETHODIMP
  115. LoadContext::GetUseRemoteTabs(bool* aUseRemoteTabs)
  116. {
  117. MOZ_ASSERT(mIsNotNull);
  118. NS_ENSURE_ARG_POINTER(aUseRemoteTabs);
  119. *aUseRemoteTabs = mUseRemoteTabs;
  120. return NS_OK;
  121. }
  122. NS_IMETHODIMP
  123. LoadContext::SetRemoteTabs(bool aUseRemoteTabs)
  124. {
  125. MOZ_ASSERT(mIsNotNull);
  126. // We shouldn't need this on parent...
  127. return NS_ERROR_UNEXPECTED;
  128. }
  129. NS_IMETHODIMP
  130. LoadContext::GetIsInIsolatedMozBrowserElement(bool* aIsInIsolatedMozBrowserElement)
  131. {
  132. MOZ_ASSERT(mIsNotNull);
  133. NS_ENSURE_ARG_POINTER(aIsInIsolatedMozBrowserElement);
  134. *aIsInIsolatedMozBrowserElement = mOriginAttributes.mInIsolatedMozBrowser;
  135. return NS_OK;
  136. }
  137. NS_IMETHODIMP
  138. LoadContext::GetAppId(uint32_t* aAppId)
  139. {
  140. MOZ_ASSERT(mIsNotNull);
  141. NS_ENSURE_ARG_POINTER(aAppId);
  142. *aAppId = mOriginAttributes.mAppId;
  143. return NS_OK;
  144. }
  145. NS_IMETHODIMP
  146. LoadContext::GetOriginAttributes(JS::MutableHandleValue aAttrs)
  147. {
  148. JSContext* cx = nsContentUtils::GetCurrentJSContext();
  149. MOZ_ASSERT(cx);
  150. bool ok = ToJSValue(cx, mOriginAttributes, aAttrs);
  151. NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
  152. return NS_OK;
  153. }
  154. NS_IMETHODIMP
  155. LoadContext::IsTrackingProtectionOn(bool* aIsTrackingProtectionOn)
  156. {
  157. MOZ_ASSERT(mIsNotNull);
  158. if (Preferences::GetBool("privacy.trackingprotection.enabled", false)) {
  159. *aIsTrackingProtectionOn = true;
  160. } else if ((mOriginAttributes.mPrivateBrowsingId > 0) &&
  161. Preferences::GetBool("privacy.trackingprotection.pbmode.enabled", false)) {
  162. *aIsTrackingProtectionOn = true;
  163. } else {
  164. *aIsTrackingProtectionOn = false;
  165. }
  166. return NS_OK;
  167. }
  168. //-----------------------------------------------------------------------------
  169. // LoadContext::nsIInterfaceRequestor
  170. //-----------------------------------------------------------------------------
  171. NS_IMETHODIMP
  172. LoadContext::GetInterface(const nsIID& aIID, void** aResult)
  173. {
  174. NS_ENSURE_ARG_POINTER(aResult);
  175. *aResult = nullptr;
  176. if (aIID.Equals(NS_GET_IID(nsILoadContext))) {
  177. *aResult = static_cast<nsILoadContext*>(this);
  178. NS_ADDREF_THIS();
  179. return NS_OK;
  180. }
  181. return NS_NOINTERFACE;
  182. }
  183. } // namespace mozilla