Bug 1804093 - Part 5: Eliminate WorkerPrincipal and WorkletPrincipals, r=asuth

Differential Revision: https://phabricator.services.mozilla.com/D163856
This commit is contained in:
Nika Layzell 2022-12-12 22:08:36 +00:00
Родитель 6bde10f55f
Коммит 8351f7cc2d
17 изменённых файлов: 18 добавлений и 253 удалений

Просмотреть файл

@ -13,10 +13,6 @@
#include "mozilla/BasePrincipal.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/StructuredCloneTags.h"
// for mozilla::dom::workerinternals::kJSPrincipalsDebugToken
#include "mozilla/dom/workerinternals/JSSettings.h"
// for mozilla::dom::worklet::kJSPrincipalsDebugToken
#include "mozilla/dom/WorkletPrincipals.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
@ -85,10 +81,6 @@ JS_PUBLIC_API void JSPrincipals::dump() {
nsresult rv = static_cast<nsJSPrincipals*>(this)->GetScriptLocation(str);
fprintf(stderr, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
NS_SUCCEEDED(rv) ? str.get() : "(unknown)");
} else if (debugToken == dom::workerinternals::kJSPrincipalsDebugToken) {
fprintf(stderr, "Web Worker principal singleton (%p)\n", this);
} else if (debugToken == dom::WorkletPrincipals::kJSPrincipalsDebugToken) {
fprintf(stderr, "Web Worklet principal (%p)\n", this);
} else {
fprintf(stderr,
"!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
@ -109,10 +101,9 @@ bool nsJSPrincipals::ReadPrincipals(JSContext* aCx,
return false;
}
if (!(tag == SCTAG_DOM_NULL_PRINCIPAL || tag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
tag == SCTAG_DOM_CONTENT_PRINCIPAL ||
tag == SCTAG_DOM_EXPANDED_PRINCIPAL ||
tag == SCTAG_DOM_WORKER_PRINCIPAL)) {
if (tag != SCTAG_DOM_NULL_PRINCIPAL && tag != SCTAG_DOM_SYSTEM_PRINCIPAL &&
tag != SCTAG_DOM_CONTENT_PRINCIPAL &&
tag != SCTAG_DOM_EXPANDED_PRINCIPAL) {
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
return false;
}
@ -276,19 +267,6 @@ bool nsJSPrincipals::ReadPrincipalInfo(JSStructuredCloneReader* aReader,
return ::ReadPrincipalInfo(aReader, tag, aInfo);
}
static StaticRefPtr<nsIPrincipal> sActiveWorkerPrincipal;
nsJSPrincipals::AutoSetActiveWorkerPrincipal::AutoSetActiveWorkerPrincipal(
nsIPrincipal* aPrincipal) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_RELEASE_ASSERT(!sActiveWorkerPrincipal);
sActiveWorkerPrincipal = aPrincipal;
}
nsJSPrincipals::AutoSetActiveWorkerPrincipal::~AutoSetActiveWorkerPrincipal() {
sActiveWorkerPrincipal = nullptr;
}
/* static */
bool nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx,
JSStructuredCloneReader* aReader,
@ -297,20 +275,7 @@ bool nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx,
MOZ_ASSERT(aTag == SCTAG_DOM_NULL_PRINCIPAL ||
aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL ||
aTag == SCTAG_DOM_WORKER_PRINCIPAL);
if (aTag == SCTAG_DOM_WORKER_PRINCIPAL) {
// When reading principals which were written on a worker thread, we need to
// know the principal of the worker which did the write.
if (!sActiveWorkerPrincipal) {
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
return false;
}
RefPtr<nsJSPrincipals> retval = get(sActiveWorkerPrincipal);
retval.forget(aOutPrincipals);
return true;
}
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL);
PrincipalInfo info;
if (!::ReadPrincipalInfo(aReader, aTag, info)) {

Просмотреть файл

@ -42,17 +42,6 @@ class nsJSPrincipals : public nsIPrincipal, public JSPrincipals {
static bool WritePrincipalInfo(JSStructuredCloneWriter* aWriter,
const mozilla::ipc::PrincipalInfo& aInfo);
// This class is used on the main thread to specify which principal to use
// when reading principals data that was set on a DOM worker thread.
// DOM workers do not use principals from Gecko's point of view, and any
// JSPrincipals used internally will be a shared singleton object. When that
// singleton is written out and later read on the main thread, we substitute
// the principal specified with this class.
struct MOZ_RAII AutoSetActiveWorkerPrincipal {
explicit AutoSetActiveWorkerPrincipal(nsIPrincipal* aPrincipal);
~AutoSetActiveWorkerPrincipal();
};
bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) final;
bool isSystemOrAddonPrincipal() final;

Просмотреть файл

@ -78,15 +78,7 @@ JSObject* SerializedStackHolder::ReadStack(JSContext* aCx) {
JS::Rooted<JS::Value> stackValue(aCx);
{
Maybe<nsJSPrincipals::AutoSetActiveWorkerPrincipal> set;
if (mWorkerRef) {
set.emplace(mWorkerRef->Private()->GetPrincipal());
}
mHolder.Read(xpc::CurrentNativeGlobal(aCx), aCx, &stackValue,
IgnoreErrors());
}
mHolder.Read(xpc::CurrentNativeGlobal(aCx), aCx, &stackValue, IgnoreErrors());
return stackValue.isObject() ? &stackValue.toObject() : nullptr;
}

Просмотреть файл

@ -130,11 +130,6 @@ enum StructuredCloneTags : uint32_t {
// StructuredCloneHolder::{Read,Write}FullySerializableObjects. In theory they
// can be 'less' stable.
// Principal written out by worker threads when serializing objects. When
// reading on the main thread this principal will be converted to a normal
// principal object using nsJSPrincipals::AutoSetActiveWorkerPrincipal.
SCTAG_DOM_WORKER_PRINCIPAL,
SCTAG_DOM_IMAGEBITMAP,
SCTAG_DOM_MAP_MESSAGEPORT,
SCTAG_DOM_FORMDATA,

Просмотреть файл

@ -12,12 +12,12 @@
#include "jsapi.h"
#include "js/ForOfIterator.h"
#include "js/PropertyAndElement.h" // JS_GetProperty
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/AudioWorkletGlobalScopeBinding.h"
#include "mozilla/dom/AudioWorkletProcessor.h"
#include "mozilla/dom/BindingCallContext.h"
#include "mozilla/dom/MessagePort.h"
#include "mozilla/dom/StructuredCloneHolder.h"
#include "mozilla/dom/WorkletPrincipals.h"
#include "mozilla/dom/AudioParamDescriptorBinding.h"
#include "nsPrintfCString.h"
#include "nsTHashSet.h"
@ -59,9 +59,9 @@ bool AudioWorkletGlobalScope::WrapGlobalObject(
options.creationOptions().setDefineSharedArrayBufferConstructor(
IsSharedMemoryAllowed());
JS::AutoHoldPrincipals principals(aCx, new WorkletPrincipals(mImpl));
return AudioWorkletGlobalScope_Binding::Wrap(
aCx, this, this, options, principals.get(), true, aReflector);
aCx, this, this, options, BasePrincipal::Cast(mImpl->Principal()), true,
aReflector);
}
void AudioWorkletGlobalScope::RegisterProcessor(

Просмотреть файл

@ -1,36 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "Principal.h"
#include "JSSettings.h"
#include "jsapi.h"
#include "mozilla/Assertions.h"
#include "mozilla/dom/StructuredCloneTags.h"
#include "mozilla/dom/workerinternals/JSSettings.h"
namespace mozilla::dom {
WorkerPrincipal::WorkerPrincipal(bool aIsSystemOrAddonPrincipal)
: JSPrincipals(), mIsSystemOrAddonPrincipal(aIsSystemOrAddonPrincipal) {
setDebugToken(workerinternals::kJSPrincipalsDebugToken);
}
WorkerPrincipal::~WorkerPrincipal() = default;
bool WorkerPrincipal::write(JSContext* aCx, JSStructuredCloneWriter* aWriter) {
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_WORKER_PRINCIPAL, 0);
}
bool WorkerPrincipal::isSystemOrAddonPrincipal() {
return mIsSystemOrAddonPrincipal;
}
void WorkerPrincipal::Destroy(JSPrincipals* aPrincipals) {
delete static_cast<WorkerPrincipal*>(aPrincipals);
}
} // namespace mozilla::dom

Просмотреть файл

@ -1,34 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_workers_principal_h__
#define mozilla_dom_workers_principal_h__
#include "js/Principals.h"
namespace mozilla::dom {
struct MOZ_HEAP_CLASS WorkerPrincipal final : public JSPrincipals {
explicit WorkerPrincipal(bool aIsSystemOrAddonPrincipal);
bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) override;
// We don't distinguish between System or Addon because the only use
// case for this right now doesn't need to. When you need to distinguish
// add a second bool.
bool isSystemOrAddonPrincipal() override;
// Callback for JS_InitDestroyPrincipalsCallback()
static void Destroy(JSPrincipals* aPrincipals);
private:
~WorkerPrincipal();
bool mIsSystemOrAddonPrincipal;
};
} // namespace mozilla::dom
#endif /* mozilla_dom_workers_principal_h__ */

Просмотреть файл

@ -71,7 +71,6 @@
# include "nsMacUtilsImpl.h"
#endif
#include "Principal.h"
#include "WorkerDebuggerManager.h"
#include "WorkerError.h"
#include "WorkerLoadInfo.h"
@ -876,7 +875,7 @@ class WorkerJSContext final : public mozilla::CycleCollectedJSContext {
JSContext* cx = Context();
js::SetPreserveWrapperCallbacks(cx, PreserveWrapper, HasReleasedWrapper);
JS_InitDestroyPrincipalsCallback(cx, WorkerPrincipal::Destroy);
JS_InitDestroyPrincipalsCallback(cx, nsJSPrincipals::Destroy);
JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks);
if (mWorkerPrivate->IsDedicatedWorker()) {
JS_SetFutexCanWait(cx);

Просмотреть файл

@ -71,7 +71,6 @@
#include "mozilla/ResultExtensions.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/UniquePtr.h"
#include "Principal.h"
#include "WorkerRunnable.h"
#include "WorkerScope.h"

Просмотреть файл

@ -12,7 +12,6 @@
#include "Crypto.h"
#include "GeckoProfiler.h"
#include "MainThreadUtils.h"
#include "Principal.h"
#include "ScriptLoader.h"
#include "js/CompilationAndEvaluation.h"
#include "js/CompileOptions.h"
@ -916,9 +915,7 @@ bool DedicatedWorkerGlobalScope::WrapGlobalObject(
return DedicatedWorkerGlobalScope_Binding::Wrap(
aCx, this, this, options,
new WorkerPrincipal(usesSystemPrincipal ||
mWorkerPrivate->UsesAddonOrExpandedAddonPrincipal()),
true, aReflector);
nsJSPrincipals::get(mWorkerPrivate->GetPrincipal()), true, aReflector);
}
void DedicatedWorkerGlobalScope::PostMessage(
@ -1080,9 +1077,7 @@ bool SharedWorkerGlobalScope::WrapGlobalObject(
return SharedWorkerGlobalScope_Binding::Wrap(
aCx, this, this, options,
new WorkerPrincipal(mWorkerPrivate->UsesSystemPrincipal() ||
mWorkerPrivate->UsesAddonOrExpandedAddonPrincipal()),
true, aReflector);
nsJSPrincipals::get(mWorkerPrivate->GetPrincipal()), true, aReflector);
}
void SharedWorkerGlobalScope::Close() {
@ -1125,9 +1120,7 @@ bool ServiceWorkerGlobalScope::WrapGlobalObject(
return ServiceWorkerGlobalScope_Binding::Wrap(
aCx, this, this, options,
new WorkerPrincipal(mWorkerPrivate->UsesSystemPrincipal() ||
mWorkerPrivate->UsesAddonOrExpandedAddonPrincipal()),
true, aReflector);
nsJSPrincipals::get(mWorkerPrivate->GetPrincipal()), true, aReflector);
}
already_AddRefed<Clients> ServiceWorkerGlobalScope::GetClients() {
@ -1252,9 +1245,7 @@ bool WorkerDebuggerGlobalScope::WrapGlobalObject(
return WorkerDebuggerGlobalScope_Binding::Wrap(
aCx, this, this, options,
new WorkerPrincipal(mWorkerPrivate->UsesSystemPrincipal() ||
mWorkerPrivate->UsesAddonOrExpandedAddonPrincipal()),
true, aReflector);
nsJSPrincipals::get(mWorkerPrivate->GetPrincipal()), true, aReflector);
}
void WorkerDebuggerGlobalScope::GetGlobal(JSContext* aCx,

Просмотреть файл

@ -50,7 +50,6 @@ UNIFIED_SOURCES += [
"ChromeWorkerScope.cpp",
"JSExecutionManager.cpp",
"MessageEventRunnable.cpp",
"Principal.cpp",
"RegisterBindings.cpp",
"RuntimeService.cpp",
"ScriptLoader.cpp",

Просмотреть файл

@ -1,39 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "WorkletPrincipals.h"
#include "mozilla/dom/WorkletImpl.h"
#include "nsJSPrincipals.h"
namespace mozilla::dom {
WorkletPrincipals::WorkletPrincipals(WorkletImpl* aWorkletImpl)
: JSPrincipals(), mWorkletImpl(aWorkletImpl) {
setDebugToken(kJSPrincipalsDebugToken);
}
WorkletPrincipals::~WorkletPrincipals() = default;
bool WorkletPrincipals::write(JSContext* aCx,
JSStructuredCloneWriter* aWriter) {
// This is a serialization of the NullPrincipal corresponding to the worklet
// environment settings object for the WorkletGlobalScope.
// https://drafts.css-houdini.org/worklets/#set-up-a-worklet-environment-settings-object
return nsJSPrincipals::WritePrincipalInfo(aWriter,
mWorkletImpl->PrincipalInfo());
}
bool WorkletPrincipals::isSystemOrAddonPrincipal() {
// Per Bug 1578623 rev a83797ed249c - Worklets are always NullPrincipal
return false;
}
void WorkletPrincipals::Destroy(JSPrincipals* aPrincipals) {
delete static_cast<WorkletPrincipals*>(aPrincipals);
}
} // namespace mozilla::dom

Просмотреть файл

@ -1,40 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef DOM_WORKLET_WORKLETPRINCIPALS_H_
#define DOM_WORKLET_WORKLETPRINCIPALS_H_
#include "js/Principals.h"
#include "mozilla/RefPtr.h"
namespace mozilla {
class WorkletImpl;
namespace dom {
struct MOZ_HEAP_CLASS WorkletPrincipals final : public JSPrincipals {
// A new WorkletPrincipals has refcount zero.
explicit WorkletPrincipals(WorkletImpl* aWorkletImpl);
bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) override;
bool isSystemOrAddonPrincipal() override;
// Callback for JS_InitDestroyPrincipalsCallback()
static void Destroy(JSPrincipals* aPrincipals);
static const uint32_t kJSPrincipalsDebugToken = 0x7e2df9f4;
private:
~WorkletPrincipals();
RefPtr<WorkletImpl> mWorkletImpl;
};
} // namespace dom
} // namespace mozilla
#endif // DOM_WORKLET_WORKLETPRINCIPALS_H_

Просмотреть файл

@ -9,9 +9,9 @@
#include "nsContentUtils.h"
#include "nsCycleCollector.h"
#include "nsJSEnvironment.h"
#include "nsJSPrincipals.h"
#include "mozilla/dom/AtomList.h"
#include "mozilla/dom/WorkletGlobalScope.h"
#include "mozilla/dom/WorkletPrincipals.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/Attributes.h"
#include "mozilla/CycleCollectedJSRuntime.h"
@ -136,7 +136,7 @@ class WorkletJSContext final : public CycleCollectedJSContext {
JSContext* cx = Context();
js::SetPreserveWrapperCallbacks(cx, PreserveWrapper, HasReleasedWrapper);
JS_InitDestroyPrincipalsCallback(cx, WorkletPrincipals::Destroy);
JS_InitDestroyPrincipalsCallback(cx, nsJSPrincipals::Destroy);
JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks);
JS_SetFutexCanWait(cx);

Просмотреть файл

@ -11,7 +11,6 @@ EXPORTS.mozilla.dom += [
"Worklet.h",
"WorkletGlobalScope.h",
"WorkletImpl.h",
"WorkletPrincipals.h",
"WorkletThread.h",
]
@ -19,7 +18,6 @@ UNIFIED_SOURCES += [
"Worklet.cpp",
"WorkletGlobalScope.cpp",
"WorkletImpl.cpp",
"WorkletPrincipals.cpp",
"WorkletThread.cpp",
]

Просмотреть файл

@ -6,7 +6,6 @@
#include "PaintWorkletGlobalScope.h"
#include "mozilla/dom/WorkletPrincipals.h"
#include "mozilla/dom/PaintWorkletGlobalScopeBinding.h"
#include "mozilla/dom/FunctionBinding.h"
#include "PaintWorkletImpl.h"
@ -34,9 +33,9 @@ bool PaintWorkletGlobalScope::WrapGlobalObject(
options.creationOptions().setDefineSharedArrayBufferConstructor(
IsSharedMemoryAllowed());
JS::AutoHoldPrincipals principals(aCx, new WorkletPrincipals(mImpl));
return PaintWorkletGlobalScope_Binding::Wrap(
aCx, this, this, options, principals.get(), true, aReflector);
aCx, this, this, options, nsJSPrincipals::get(mImpl->Principal()), true,
aReflector);
}
void PaintWorkletGlobalScope::RegisterPaint(const nsAString& aType,

Просмотреть файл

@ -7,7 +7,6 @@
#include "ExtensionPort.h"
#include "mozilla/dom/FunctionBinding.h"
#include "nsJSPrincipals.h" // nsJSPrincipals::AutoSetActiveWorkerPrincipal
#include "nsThreadManager.h" // NS_IsMainThread
namespace mozilla {
@ -652,18 +651,7 @@ void ExtensionListenerCallPromiseResultHandler::WorkerRunCallback(
JS::Rooted<JS::Value> jsvalue(cx);
IgnoredErrorResult rv;
{
// Set the active worker principal while reading the result,
// needed to be sure to be able to successfully deserialize the
// SavedFrame part of a ClonedErrorHolder (in case that was the
// result stored in the StructuredCloneHolder).
Maybe<nsJSPrincipals::AutoSetActiveWorkerPrincipal> set;
if (workerRef) {
set.emplace(workerRef->Private()->GetPrincipal());
}
resHolder->Read(global, cx, &jsvalue, rv);
}
resHolder->Read(global, cx, &jsvalue, rv);
if (NS_WARN_IF(rv.Failed())) {
promiseResult->MaybeReject(rv.StealNSResult());