Bug 1933479 - Add tab close button on hover to vertical tabs when sidebar is collapse...
[gecko.git] / toolkit / components / contentanalysis / ContentAnalysisIPCTypes.h
blob48e650d778b396c3da6d0124b93737bc9f361869
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"
16 namespace mozilla {
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,
24 DENY_DUE_TO_CANCELED,
25 DENY_DUE_TO_INVALID_JSON_RESPONSE,
26 DENY_DUE_TO_OTHER_ERROR,
27 LAST_VALUE
30 class ContentAnalysisResult : public nsIContentAnalysisResult {
31 NS_DECL_THREADSAFE_ISUPPORTS
32 NS_DECL_NSICONTENTANALYSISRESULT
33 public:
34 static RefPtr<ContentAnalysisResult> FromAction(
35 nsIContentAnalysisResponse::Action aAction) {
36 auto result =
37 RefPtr<ContentAnalysisResult>(new ContentAnalysisResult(aAction));
38 return result;
41 static RefPtr<ContentAnalysisResult> FromNoResult(
42 NoContentAnalysisResult aResult) {
43 auto result =
44 RefPtr<ContentAnalysisResult>(new ContentAnalysisResult(aResult));
45 return result;
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();
58 return FromAction(
59 static_cast<nsIContentAnalysisResponse::Action>(actionNumber));
63 return FromNoResult(
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",
75 &shouldAllowValue)) {
76 if (shouldAllowValue.isTrue()) {
77 return FromAction(nsIContentAnalysisResponse::Action::eAllow);
78 } else if (shouldAllowValue.isFalse()) {
79 return FromAction(nsIContentAnalysisResponse::Action::eBlock);
80 } else {
81 return FromNoResult(NoContentAnalysisResult::DENY_DUE_TO_OTHER_ERROR);
85 return FromNoResult(
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);
97 } else {
98 return FromAction(nsIContentAnalysisResponse::Action::eBlock);
102 private:
103 explicit ContentAnalysisResult(nsIContentAnalysisResponse::Action aAction)
104 : mValue(aAction) {}
105 explicit ContentAnalysisResult(NoContentAnalysisResult aResult)
106 : mValue(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
117 namespace IPC {
119 template <>
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> {};
126 template <>
127 struct ParamTraits<nsIContentAnalysisResponse::Action>
128 : public ContiguousEnumSerializerInclusive<
129 nsIContentAnalysisResponse::Action,
130 nsIContentAnalysisResponse::Action::eUnspecified,
131 nsIContentAnalysisResponse::Action::eCanceled> {};
133 template <>
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));
146 template <>
147 struct ParamTraits<mozilla::contentanalysis::ContentAnalysisResult*> {
148 typedef mozilla::contentanalysis::ContentAnalysisResult paramType;
150 static void Write(MessageWriter* aWriter, const paramType* aParam) {
151 if (!aParam) {
152 WriteParam(aWriter, true);
153 return;
155 WriteParam(aWriter, false);
156 WriteParam(aWriter, aParam->mValue);
159 static bool Read(MessageReader* aReader, RefPtr<paramType>* aResult) {
160 bool isNull = false;
161 if (!ReadParam(aReader, &isNull)) {
162 return false;
164 if (isNull) {
165 *aResult = nullptr;
166 return true;
168 *aResult = mozilla::contentanalysis::ContentAnalysisResult::FromNoResult(
169 mozilla::contentanalysis::NoContentAnalysisResult::
170 DENY_DUE_TO_OTHER_ERROR);
171 return ReadParam(aReader, &((*aResult)->mValue));
175 } // namespace IPC
177 #endif // mozilla_ContentAnalysisIPCTypes_h