Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "Principal.h"
8 :
9 : #include "jsapi.h"
10 : #include "mozilla/Assertions.h"
11 :
12 : BEGIN_WORKERS_NAMESPACE
13 :
14 1 : struct WorkerPrincipal final : public JSPrincipals
15 : {
16 0 : bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) override {
17 0 : MOZ_CRASH("WorkerPrincipal::write not implemented");
18 : return false;
19 : }
20 : };
21 :
22 : JSPrincipals*
23 1 : GetWorkerPrincipal()
24 : {
25 1 : static WorkerPrincipal sPrincipal;
26 :
27 : /*
28 : * To make sure the the principals refcount is initialized to one, atomically
29 : * increment it on every pass though this function. If we discover this wasn't
30 : * the first time, decrement it again. This avoids the need for
31 : * synchronization.
32 : */
33 1 : int32_t prevRefcount = sPrincipal.refcount++;
34 1 : if (prevRefcount > 0) {
35 0 : --sPrincipal.refcount;
36 : } else {
37 : #ifdef DEBUG
38 1 : sPrincipal.debugToken = kJSPrincipalsDebugToken;
39 : #endif
40 : }
41 :
42 1 : return &sPrincipal;
43 : }
44 :
45 : void
46 0 : DestroyWorkerPrincipals(JSPrincipals* aPrincipals)
47 : {
48 0 : MOZ_ASSERT_UNREACHABLE("Worker principals refcount should never fall below one");
49 : }
50 :
51 : END_WORKERS_NAMESPACE
|