1 /* -*- Mode: C++; tab-width: 2; 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 /* describes principals by their orginating uris*/
7 #ifndef nsJSPrincipals_h__
8 #define nsJSPrincipals_h__
10 #include "nsIPrincipal.h"
16 } // namespace mozilla
18 class nsJSPrincipals
: public nsIPrincipal
, public JSPrincipals
{
20 /* SpiderMonkey security callbacks. */
21 static bool Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
);
22 static void Destroy(JSPrincipals
* jsprin
);
24 /* JSReadPrincipalsOp for nsJSPrincipals */
25 static bool ReadPrincipals(JSContext
* aCx
, JSStructuredCloneReader
* aReader
,
26 JSPrincipals
** aOutPrincipals
);
28 static bool ReadKnownPrincipalType(JSContext
* aCx
,
29 JSStructuredCloneReader
* aReader
,
31 JSPrincipals
** aOutPrincipals
);
33 /* For write() implementations of off-main-thread JSPrincipals. */
34 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
35 const mozilla::ipc::PrincipalInfo
& aInfo
);
36 // This class is used on the main thread to specify which principal to use
37 // when reading principals data that was set on a DOM worker thread.
38 // DOM workers do not use principals from Gecko's point of view, and any
39 // JSPrincipals used internally will be a shared singleton object. When that
40 // singleton is written out and later read on the main thread, we substitute
41 // the principal specified with this class.
42 struct MOZ_RAII AutoSetActiveWorkerPrincipal
{
43 explicit AutoSetActiveWorkerPrincipal(nsIPrincipal
* aPrincipal
);
44 ~AutoSetActiveWorkerPrincipal();
47 bool write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) final
;
49 bool isSystemOrAddonPrincipal() final
;
52 * Get a weak reference to nsIPrincipal associated with the given JS
53 * principal, and vice-versa.
55 static nsJSPrincipals
* get(JSPrincipals
* principals
) {
56 nsJSPrincipals
* self
= static_cast<nsJSPrincipals
*>(principals
);
57 MOZ_ASSERT_IF(self
, self
->debugToken
== DEBUG_TOKEN
);
60 static nsJSPrincipals
* get(nsIPrincipal
* principal
) {
61 nsJSPrincipals
* self
= static_cast<nsJSPrincipals
*>(principal
);
62 MOZ_ASSERT_IF(self
, self
->debugToken
== DEBUG_TOKEN
);
66 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void) override
;
67 NS_IMETHOD_(MozExternalRefCountType
) Release(void) override
;
71 setDebugToken(DEBUG_TOKEN
);
75 * Return a string that can be used as JS script filename in error reports.
77 virtual nsresult
GetScriptLocation(nsACString
& aStr
) = 0;
78 static const uint32_t DEBUG_TOKEN
= 0x0bf41760;
81 virtual ~nsJSPrincipals() { setDebugToken(0); }
84 #endif /* nsJSPrincipals_h__ */