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 "js/Principals.h"
11 #include "nsIPrincipal.h"
14 struct JSStructuredCloneReader
;
15 struct JSStructuredCloneWriter
;
21 } // namespace mozilla
23 class nsJSPrincipals
: public nsIPrincipal
, public JSPrincipals
{
25 /* SpiderMonkey security callbacks. */
26 static bool Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
);
27 static void Destroy(JSPrincipals
* jsprin
);
29 /* JSReadPrincipalsOp for nsJSPrincipals */
30 static bool ReadPrincipals(JSContext
* aCx
, JSStructuredCloneReader
* aReader
,
31 JSPrincipals
** aOutPrincipals
);
33 static bool ReadKnownPrincipalType(JSContext
* aCx
,
34 JSStructuredCloneReader
* aReader
,
36 JSPrincipals
** aOutPrincipals
);
38 /* For write() implementations of off-main-thread JSPrincipals. */
39 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
40 const mozilla::ipc::PrincipalInfo
& aInfo
);
41 // This class is used on the main thread to specify which principal to use
42 // when reading principals data that was set on a DOM worker thread.
43 // DOM workers do not use principals from Gecko's point of view, and any
44 // JSPrincipals used internally will be a shared singleton object. When that
45 // singleton is written out and later read on the main thread, we substitute
46 // the principal specified with this class.
47 struct MOZ_RAII AutoSetActiveWorkerPrincipal
{
48 explicit AutoSetActiveWorkerPrincipal(nsIPrincipal
* aPrincipal
);
49 ~AutoSetActiveWorkerPrincipal();
52 bool write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) final
;
54 bool isSystemOrAddonPrincipal() final
;
57 * Get a weak reference to nsIPrincipal associated with the given JS
58 * principal, and vice-versa.
60 static nsJSPrincipals
* get(JSPrincipals
* principals
) {
61 nsJSPrincipals
* self
= static_cast<nsJSPrincipals
*>(principals
);
62 MOZ_ASSERT_IF(self
, self
->debugToken
== DEBUG_TOKEN
);
65 static nsJSPrincipals
* get(nsIPrincipal
* principal
) {
66 nsJSPrincipals
* self
= static_cast<nsJSPrincipals
*>(principal
);
67 MOZ_ASSERT_IF(self
, self
->debugToken
== DEBUG_TOKEN
);
71 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void) override
;
72 NS_IMETHOD_(MozExternalRefCountType
) Release(void) override
;
76 setDebugToken(DEBUG_TOKEN
);
80 * Return a string that can be used as JS script filename in error reports.
82 virtual nsresult
GetScriptLocation(nsACString
& aStr
) = 0;
83 static const uint32_t DEBUG_TOKEN
= 0x0bf41760;
86 virtual ~nsJSPrincipals() { setDebugToken(0); }
89 #endif /* nsJSPrincipals_h__ */