Source code

Revision control

Other Tools

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*/
6
7
#ifndef nsJSPrincipals_h__
8
#define nsJSPrincipals_h__
9
#include "jsapi.h"
10
#include "nsIPrincipal.h"
11
12
namespace mozilla {
13
namespace ipc {
14
class PrincipalInfo;
15
} // namespace ipc
16
} // namespace mozilla
17
18
class nsJSPrincipals : public nsIPrincipal, public JSPrincipals {
19
public:
20
/* SpiderMonkey security callbacks. */
21
static bool Subsume(JSPrincipals* jsprin, JSPrincipals* other);
22
static void Destroy(JSPrincipals* jsprin);
23
24
/* JSReadPrincipalsOp for nsJSPrincipals */
25
static bool ReadPrincipals(JSContext* aCx, JSStructuredCloneReader* aReader,
26
JSPrincipals** aOutPrincipals);
27
28
static bool ReadKnownPrincipalType(JSContext* aCx,
29
JSStructuredCloneReader* aReader,
30
uint32_t aTag,
31
JSPrincipals** aOutPrincipals);
32
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();
45
};
46
47
bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) final;
48
49
bool isSystemOrAddonPrincipal() final;
50
51
/*
52
* Get a weak reference to nsIPrincipal associated with the given JS
53
* principal, and vice-versa.
54
*/
55
static nsJSPrincipals* get(JSPrincipals* principals) {
56
nsJSPrincipals* self = static_cast<nsJSPrincipals*>(principals);
57
MOZ_ASSERT_IF(self, self->debugToken == DEBUG_TOKEN);
58
return self;
59
}
60
static nsJSPrincipals* get(nsIPrincipal* principal) {
61
nsJSPrincipals* self = static_cast<nsJSPrincipals*>(principal);
62
MOZ_ASSERT_IF(self, self->debugToken == DEBUG_TOKEN);
63
return self;
64
}
65
66
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override;
67
NS_IMETHOD_(MozExternalRefCountType) Release(void) override;
68
69
nsJSPrincipals() {
70
refcount = 0;
71
setDebugToken(DEBUG_TOKEN);
72
}
73
74
/**
75
* Return a string that can be used as JS script filename in error reports.
76
*/
77
virtual nsresult GetScriptLocation(nsACString& aStr) = 0;
78
static const uint32_t DEBUG_TOKEN = 0x0bf41760;
79
80
protected:
81
virtual ~nsJSPrincipals() { setDebugToken(0); }
82
};
83
84
#endif /* nsJSPrincipals_h__ */