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/. */
7 #ifndef mozilla_ContentAnalysisIPCTypes_h
8 #define mozilla_ContentAnalysisIPCTypes_h
10 #include "ipc/EnumSerializer.h"
11 #include "ipc/IPCMessageUtilsSpecializations.h"
12 #include "js/PropertyAndElement.h"
13 #include "mozilla/Variant.h"
14 #include "nsIContentAnalysis.h"
17 namespace contentanalysis
{
19 enum class NoContentAnalysisResult
: uint8_t {
20 ALLOW_DUE_TO_CONTENT_ANALYSIS_NOT_ACTIVE
,
21 ALLOW_DUE_TO_CONTEXT_EXEMPT_FROM_CONTENT_ANALYSIS
,
22 ALLOW_DUE_TO_SAME_TAB_SOURCE
,
23 ALLOW_DUE_TO_COULD_NOT_GET_DATA
,
25 DENY_DUE_TO_INVALID_JSON_RESPONSE
,
26 DENY_DUE_TO_OTHER_ERROR
,
30 class ContentAnalysisResult
: public nsIContentAnalysisResult
{
31 NS_DECL_THREADSAFE_ISUPPORTS
32 NS_DECL_NSICONTENTANALYSISRESULT
34 static RefPtr
<ContentAnalysisResult
> FromAction(
35 nsIContentAnalysisResponse::Action aAction
) {
37 RefPtr
<ContentAnalysisResult
>(new ContentAnalysisResult(aAction
));
41 static RefPtr
<ContentAnalysisResult
> FromNoResult(
42 NoContentAnalysisResult aResult
) {
44 RefPtr
<ContentAnalysisResult
>(new ContentAnalysisResult(aResult
));
48 static RefPtr
<ContentAnalysisResult
> FromJSONResponse(
49 const JS::Handle
<JS::Value
>& aValue
, JSContext
* aCx
) {
50 if (aValue
.isObject()) {
51 auto* obj
= aValue
.toObjectOrNull();
52 JS::Handle
<JSObject
*> handle
=
53 JS::Handle
<JSObject
*>::fromMarkedLocation(&obj
);
54 JS::Rooted
<JS::Value
> actionValue(aCx
);
55 if (JS_GetProperty(aCx
, handle
, "action", &actionValue
)) {
56 if (actionValue
.isNumber()) {
57 double actionNumber
= actionValue
.toNumber();
59 static_cast<nsIContentAnalysisResponse::Action
>(actionNumber
));
64 NoContentAnalysisResult::DENY_DUE_TO_INVALID_JSON_RESPONSE
);
67 static RefPtr
<ContentAnalysisResult
> FromJSONContentAnalysisResponse(
68 const JS::Handle
<JS::Value
>& aValue
, JSContext
* aCx
) {
69 if (aValue
.isObject()) {
70 auto* obj
= aValue
.toObjectOrNull();
71 JS::Handle
<JSObject
*> handle
=
72 JS::Handle
<JSObject
*>::fromMarkedLocation(&obj
);
73 JS::Rooted
<JS::Value
> shouldAllowValue(aCx
);
74 if (JS_GetProperty(aCx
, handle
, "shouldAllowContent",
76 if (shouldAllowValue
.isTrue()) {
77 return FromAction(nsIContentAnalysisResponse::Action::eAllow
);
78 } else if (shouldAllowValue
.isFalse()) {
79 return FromAction(nsIContentAnalysisResponse::Action::eBlock
);
81 return FromNoResult(NoContentAnalysisResult::DENY_DUE_TO_OTHER_ERROR
);
86 NoContentAnalysisResult::DENY_DUE_TO_INVALID_JSON_RESPONSE
);
89 static RefPtr
<ContentAnalysisResult
> FromContentAnalysisResponse(
90 nsIContentAnalysisResponse
* aResponse
) {
91 bool shouldAllowContent
= false;
92 DebugOnly
<nsresult
> rv
=
93 aResponse
->GetShouldAllowContent(&shouldAllowContent
);
94 MOZ_ASSERT(NS_SUCCEEDED(rv
));
95 if (shouldAllowContent
) {
96 return FromAction(nsIContentAnalysisResponse::Action::eAllow
);
98 return FromAction(nsIContentAnalysisResponse::Action::eBlock
);
103 explicit ContentAnalysisResult(nsIContentAnalysisResponse::Action aAction
)
105 explicit ContentAnalysisResult(NoContentAnalysisResult aResult
)
107 virtual ~ContentAnalysisResult() = default;
108 Variant
<nsIContentAnalysisResponse::Action
, NoContentAnalysisResult
> mValue
;
109 friend struct IPC::ParamTraits
<ContentAnalysisResult
>;
110 friend struct IPC::ParamTraits
<ContentAnalysisResult
*>;
111 friend struct IPC::ParamTraits
<RefPtr
<ContentAnalysisResult
>>;
114 } // namespace contentanalysis
115 } // namespace mozilla
120 struct ParamTraits
<mozilla::contentanalysis::NoContentAnalysisResult
>
121 : public ContiguousEnumSerializer
<
122 mozilla::contentanalysis::NoContentAnalysisResult
,
123 static_cast<mozilla::contentanalysis::NoContentAnalysisResult
>(0),
124 mozilla::contentanalysis::NoContentAnalysisResult::LAST_VALUE
> {};
127 struct ParamTraits
<nsIContentAnalysisResponse::Action
>
128 : public ContiguousEnumSerializerInclusive
<
129 nsIContentAnalysisResponse::Action
,
130 nsIContentAnalysisResponse::Action::eUnspecified
,
131 nsIContentAnalysisResponse::Action::eCanceled
> {};
134 struct ParamTraits
<mozilla::contentanalysis::ContentAnalysisResult
> {
135 typedef mozilla::contentanalysis::ContentAnalysisResult paramType
;
137 static void Write(MessageWriter
* aWriter
, const paramType
& aParam
) {
138 WriteParam(aWriter
, aParam
.mValue
);
141 static bool Read(MessageReader
* aReader
, paramType
* aResult
) {
142 return ReadParam(aReader
, &(aResult
->mValue
));
147 struct ParamTraits
<mozilla::contentanalysis::ContentAnalysisResult
*> {
148 typedef mozilla::contentanalysis::ContentAnalysisResult paramType
;
150 static void Write(MessageWriter
* aWriter
, const paramType
* aParam
) {
152 WriteParam(aWriter
, true);
155 WriteParam(aWriter
, false);
156 WriteParam(aWriter
, aParam
->mValue
);
159 static bool Read(MessageReader
* aReader
, RefPtr
<paramType
>* aResult
) {
161 if (!ReadParam(aReader
, &isNull
)) {
168 *aResult
= mozilla::contentanalysis::ContentAnalysisResult::FromNoResult(
169 mozilla::contentanalysis::NoContentAnalysisResult::
170 DENY_DUE_TO_OTHER_ERROR
);
171 return ReadParam(aReader
, &((*aResult
)->mValue
));
177 #endif // mozilla_ContentAnalysisIPCTypes_h