Skip to content

Commit

Permalink
Bug 1804093 - Part 5: Eliminate WorkerPrincipal and WorkletPrincipals…
Browse files Browse the repository at this point in the history
…, r=asuth

Differential Revision: https://phabricator.services.mozilla.com/D163856
  • Loading branch information
mystor committed Dec 12, 2022
1 parent 6bde10f commit 8351f7c
Show file tree
Hide file tree
Showing 17 changed files with 18 additions and 253 deletions.
43 changes: 4 additions & 39 deletions caps/nsJSPrincipals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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: "
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)) {
Expand Down
11 changes: 0 additions & 11 deletions caps/nsJSPrincipals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 1 addition & 9 deletions dom/base/SerializedStackHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 0 additions & 5 deletions dom/base/StructuredCloneTags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions dom/media/webaudio/AudioWorkletGlobalScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
36 changes: 0 additions & 36 deletions dom/workers/Principal.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions dom/workers/Principal.h

This file was deleted.

3 changes: 1 addition & 2 deletions dom/workers/RuntimeService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
# include "nsMacUtilsImpl.h"
#endif

#include "Principal.h"
#include "WorkerDebuggerManager.h"
#include "WorkerError.h"
#include "WorkerLoadInfo.h"
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion dom/workers/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
17 changes: 4 additions & 13 deletions dom/workers/WorkerScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion dom/workers/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ UNIFIED_SOURCES += [
"ChromeWorkerScope.cpp",
"JSExecutionManager.cpp",
"MessageEventRunnable.cpp",
"Principal.cpp",
"RegisterBindings.cpp",
"RuntimeService.cpp",
"ScriptLoader.cpp",
Expand Down
39 changes: 0 additions & 39 deletions dom/worklet/WorkletPrincipals.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions dom/worklet/WorkletPrincipals.h

This file was deleted.

Loading

0 comments on commit 8351f7c

Please sign in to comment.