2019-05-03 21:12:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/WindowGlobalActor.h"
|
|
|
|
|
2020-08-12 18:38:12 +03:00
|
|
|
#include "AutoplayPolicy.h"
|
2019-08-08 19:07:05 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-05-03 21:12:55 +03:00
|
|
|
#include "mozJSComponentLoader.h"
|
2021-04-12 21:04:12 +03:00
|
|
|
#include "mozilla/Components.h"
|
2020-03-06 19:36:01 +03:00
|
|
|
#include "mozilla/ContentBlockingAllowList.h"
|
2019-05-03 21:12:55 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2020-04-30 19:42:53 +03:00
|
|
|
#include "mozilla/dom/JSActorService.h"
|
2019-05-22 19:56:19 +03:00
|
|
|
#include "mozilla/dom/JSWindowActorParent.h"
|
|
|
|
#include "mozilla/dom/JSWindowActorChild.h"
|
2020-07-08 17:22:22 +03:00
|
|
|
#include "mozilla/dom/JSWindowActorProtocol.h"
|
2020-08-12 18:38:12 +03:00
|
|
|
#include "mozilla/dom/PopupBlocker.h"
|
2020-05-27 03:27:30 +03:00
|
|
|
#include "mozilla/net/CookieJarSettings.h"
|
2020-10-28 15:31:35 +03:00
|
|
|
#include "mozilla/dom/WindowGlobalChild.h"
|
|
|
|
#include "mozilla/dom/WindowGlobalParent.h"
|
|
|
|
|
|
|
|
#include "nsGlobalWindowInner.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsNetUtil.h"
|
2019-05-03 21:12:55 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2019-05-03 21:12:55 +03:00
|
|
|
|
2020-05-11 21:41:16 +03:00
|
|
|
// CORPP 3.1.3 https://mikewest.github.io/corpp/#integration-html
|
|
|
|
static nsILoadInfo::CrossOriginEmbedderPolicy InheritedPolicy(
|
|
|
|
dom::BrowsingContext* aBrowsingContext) {
|
|
|
|
WindowContext* inherit = aBrowsingContext->GetParentWindowContext();
|
|
|
|
if (inherit) {
|
|
|
|
return inherit->GetEmbedderPolicy();
|
|
|
|
}
|
|
|
|
|
2020-06-25 05:14:29 +03:00
|
|
|
return nsILoadInfo::EMBEDDER_POLICY_NULL;
|
2020-05-11 21:41:16 +03:00
|
|
|
}
|
|
|
|
|
2020-05-08 23:44:12 +03:00
|
|
|
// Common WindowGlobalInit creation code used by both `AboutBlankInitializer`
|
|
|
|
// and `WindowInitializer`.
|
2020-05-11 21:41:16 +03:00
|
|
|
WindowGlobalInit WindowGlobalActor::BaseInitializer(
|
|
|
|
dom::BrowsingContext* aBrowsingContext, uint64_t aInnerWindowId,
|
|
|
|
uint64_t aOuterWindowId) {
|
2020-05-08 23:44:12 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBrowsingContext);
|
|
|
|
|
|
|
|
WindowGlobalInit init;
|
|
|
|
auto& ctx = init.context();
|
|
|
|
ctx.mInnerWindowId = aInnerWindowId;
|
|
|
|
ctx.mOuterWindowId = aOuterWindowId;
|
|
|
|
ctx.mBrowsingContextId = aBrowsingContext->Id();
|
|
|
|
|
2020-05-11 21:41:16 +03:00
|
|
|
// If any synced fields need to be initialized from our BrowsingContext, we
|
|
|
|
// can initialize them here.
|
2020-07-22 17:07:26 +03:00
|
|
|
auto& fields = ctx.mFields;
|
|
|
|
fields.mEmbedderPolicy = InheritedPolicy(aBrowsingContext);
|
|
|
|
fields.mAutoplayPermission = nsIPermissionManager::UNKNOWN_ACTION;
|
2021-06-15 07:40:11 +03:00
|
|
|
fields.mAllowJavascript = true;
|
2020-05-08 23:44:12 +03:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2019-08-08 19:07:05 +03:00
|
|
|
WindowGlobalInit WindowGlobalActor::AboutBlankInitializer(
|
|
|
|
dom::BrowsingContext* aBrowsingContext, nsIPrincipal* aPrincipal) {
|
2020-05-08 23:44:12 +03:00
|
|
|
WindowGlobalInit init =
|
|
|
|
BaseInitializer(aBrowsingContext, nsContentUtils::GenerateWindowId(),
|
|
|
|
nsContentUtils::GenerateWindowId());
|
2019-08-08 19:07:05 +03:00
|
|
|
|
2020-05-08 23:44:12 +03:00
|
|
|
init.principal() = aPrincipal;
|
2021-05-26 10:14:03 +03:00
|
|
|
init.storagePrincipal() = aPrincipal;
|
2020-05-08 23:44:12 +03:00
|
|
|
Unused << NS_NewURI(getter_AddRefs(init.documentURI()), "about:blank");
|
2021-07-14 18:51:20 +03:00
|
|
|
init.isInitialDocument() = true;
|
2020-05-08 23:44:12 +03:00
|
|
|
|
|
|
|
return init;
|
|
|
|
}
|
2019-08-08 19:07:05 +03:00
|
|
|
|
2020-05-08 23:44:12 +03:00
|
|
|
WindowGlobalInit WindowGlobalActor::WindowInitializer(
|
|
|
|
nsGlobalWindowInner* aWindow) {
|
|
|
|
WindowGlobalInit init =
|
|
|
|
BaseInitializer(aWindow->GetBrowsingContext(), aWindow->WindowID(),
|
|
|
|
aWindow->GetOuterWindow()->WindowID());
|
2019-08-08 19:07:05 +03:00
|
|
|
|
2020-05-08 23:44:12 +03:00
|
|
|
init.principal() = aWindow->GetPrincipal();
|
2021-05-26 10:14:03 +03:00
|
|
|
init.storagePrincipal() = aWindow->GetEffectiveStoragePrincipal();
|
2020-05-08 23:44:12 +03:00
|
|
|
init.documentURI() = aWindow->GetDocumentURI();
|
2020-03-01 22:25:01 +03:00
|
|
|
|
2020-05-27 03:27:30 +03:00
|
|
|
Document* doc = aWindow->GetDocument();
|
|
|
|
|
2021-07-14 18:51:20 +03:00
|
|
|
init.isInitialDocument() = doc->IsInitialDocument();
|
2020-05-27 03:27:30 +03:00
|
|
|
init.blockAllMixedContent() = doc->GetBlockAllMixedContent(false);
|
|
|
|
init.upgradeInsecureRequests() = doc->GetUpgradeInsecureRequests(false);
|
|
|
|
init.sandboxFlags() = doc->GetSandboxFlags();
|
|
|
|
net::CookieJarSettings::Cast(doc->CookieJarSettings())
|
|
|
|
->Serialize(init.cookieJarSettings());
|
|
|
|
init.httpsOnlyStatus() = doc->HttpsOnlyStatus();
|
|
|
|
|
2020-07-22 17:07:26 +03:00
|
|
|
auto& fields = init.context().mFields;
|
|
|
|
fields.mCookieBehavior = Some(doc->CookieJarSettings()->GetCookieBehavior());
|
|
|
|
fields.mIsOnContentBlockingAllowList =
|
2020-05-27 03:27:30 +03:00
|
|
|
doc->CookieJarSettings()->GetIsOnContentBlockingAllowList();
|
2020-07-22 17:07:26 +03:00
|
|
|
fields.mIsThirdPartyWindow = doc->HasThirdPartyChannel();
|
|
|
|
fields.mIsThirdPartyTrackingResourceWindow =
|
2020-05-27 03:27:30 +03:00
|
|
|
nsContentUtils::IsThirdPartyTrackingResourceWindow(aWindow);
|
2020-07-22 17:07:26 +03:00
|
|
|
fields.mIsSecureContext = aWindow->IsSecureContext();
|
2020-05-27 03:27:30 +03:00
|
|
|
|
2020-08-12 18:38:12 +03:00
|
|
|
// Initialze permission fields
|
|
|
|
fields.mAutoplayPermission =
|
|
|
|
AutoplayPolicy::GetSiteAutoplayPermission(init.principal());
|
|
|
|
fields.mPopupPermission = PopupBlocker::GetPopupPermission(init.principal());
|
|
|
|
|
|
|
|
// Initialize top level permission fields
|
|
|
|
if (aWindow->GetBrowsingContext()->IsTop()) {
|
2021-04-12 21:04:12 +03:00
|
|
|
fields.mAllowMixedContent = [&] {
|
|
|
|
uint32_t permit = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
|
|
|
components::PermissionManager::Service();
|
|
|
|
|
|
|
|
if (permissionManager) {
|
|
|
|
permissionManager->TestPermissionFromPrincipal(
|
|
|
|
init.principal(), "mixed-content"_ns, &permit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return permit == nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}();
|
|
|
|
|
2020-08-12 18:38:12 +03:00
|
|
|
fields.mShortcutsPermission =
|
|
|
|
nsGlobalWindowInner::GetShortcutsPermission(init.principal());
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:04:12 +03:00
|
|
|
if (auto policy = doc->GetEmbedderPolicy()) {
|
2020-07-22 17:07:26 +03:00
|
|
|
fields.mEmbedderPolicy = *policy;
|
2020-05-27 03:27:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init Mixed Content Fields
|
2020-06-16 12:05:54 +03:00
|
|
|
nsCOMPtr<nsIURI> innerDocURI = NS_GetInnermostURI(doc->GetDocumentURI());
|
2021-04-12 21:04:12 +03:00
|
|
|
fields.mIsSecure = innerDocURI && innerDocURI->SchemeIs("https");
|
2020-05-27 03:27:30 +03:00
|
|
|
|
2020-05-27 03:28:59 +03:00
|
|
|
nsCOMPtr<nsITransportSecurityInfo> securityInfo;
|
|
|
|
if (nsCOMPtr<nsIChannel> channel = doc->GetChannel()) {
|
2020-09-22 20:41:34 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo(channel->LoadInfo());
|
|
|
|
fields.mIsOriginalFrameSource = loadInfo->GetOriginalFrameSrcLoad();
|
|
|
|
|
2020-05-27 03:28:59 +03:00
|
|
|
nsCOMPtr<nsISupports> securityInfoSupports;
|
|
|
|
channel->GetSecurityInfo(getter_AddRefs(securityInfoSupports));
|
|
|
|
securityInfo = do_QueryInterface(securityInfoSupports);
|
|
|
|
}
|
|
|
|
init.securityInfo() = securityInfo;
|
|
|
|
|
2020-12-10 23:45:54 +03:00
|
|
|
fields.mIsLocalIP = init.principal()->GetIsLocalIpAddress();
|
|
|
|
|
2020-05-27 03:27:30 +03:00
|
|
|
// Most data here is specific to the Document, which can change without
|
|
|
|
// creating a new WindowGlobal. Anything new added here which fits that
|
|
|
|
// description should also be synchronized in
|
|
|
|
// WindowGlobalChild::OnNewDocument.
|
2020-05-08 23:44:12 +03:00
|
|
|
return init;
|
2019-08-08 19:07:05 +03:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:22:22 +03:00
|
|
|
already_AddRefed<JSActorProtocol> WindowGlobalActor::MatchingJSActorProtocol(
|
|
|
|
JSActorService* aActorSvc, const nsACString& aName, ErrorResult& aRv) {
|
2020-04-30 19:42:53 +03:00
|
|
|
RefPtr<JSWindowActorProtocol> proto =
|
2020-07-08 17:22:22 +03:00
|
|
|
aActorSvc->GetJSWindowActorProtocol(aName);
|
2019-05-03 21:12:55 +03:00
|
|
|
if (!proto) {
|
2020-07-08 17:22:22 +03:00
|
|
|
aRv.ThrowNotFoundError(nsPrintfCString("No such JSWindowActor '%s'",
|
|
|
|
PromiseFlatCString(aName).get()));
|
|
|
|
return nullptr;
|
2019-05-03 21:12:55 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 00:18:49 +03:00
|
|
|
if (!proto->Matches(BrowsingContext(), GetDocumentURI(), GetRemoteType(),
|
|
|
|
aRv)) {
|
|
|
|
MOZ_ASSERT(aRv.Failed());
|
2020-07-08 17:22:22 +03:00
|
|
|
return nullptr;
|
2019-05-03 21:12:55 +03:00
|
|
|
}
|
2020-11-17 00:18:49 +03:00
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
2020-07-08 17:22:22 +03:00
|
|
|
return proto.forget();
|
2019-05-03 21:12:55 +03:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|