1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_ContentAnalysisIPCTypes_h
#define mozilla_ContentAnalysisIPCTypes_h
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "js/PropertyAndElement.h"
#include "mozilla/Variant.h"
#include "nsIContentAnalysis.h"
namespace mozilla {
namespace contentanalysis {
enum class NoContentAnalysisResult : uint8_t {
ALLOW_DUE_TO_CONTENT_ANALYSIS_NOT_ACTIVE,
ALLOW_DUE_TO_CONTEXT_EXEMPT_FROM_CONTENT_ANALYSIS,
ALLOW_DUE_TO_SAME_TAB_SOURCE,
ALLOW_DUE_TO_COULD_NOT_GET_DATA,
DENY_DUE_TO_CANCELED,
DENY_DUE_TO_INVALID_JSON_RESPONSE,
DENY_DUE_TO_OTHER_ERROR,
LAST_VALUE
};
class ContentAnalysisResult : public nsIContentAnalysisResult {
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSICONTENTANALYSISRESULT
public:
static RefPtr<ContentAnalysisResult> FromAction(
nsIContentAnalysisResponse::Action aAction) {
auto result =
RefPtr<ContentAnalysisResult>(new ContentAnalysisResult(aAction));
return result;
}
static RefPtr<ContentAnalysisResult> FromNoResult(
NoContentAnalysisResult aResult) {
auto result =
RefPtr<ContentAnalysisResult>(new ContentAnalysisResult(aResult));
return result;
}
static RefPtr<ContentAnalysisResult> FromJSONResponse(
const JS::Handle<JS::Value>& aValue, JSContext* aCx) {
if (aValue.isObject()) {
auto* obj = aValue.toObjectOrNull();
JS::Handle<JSObject*> handle =
JS::Handle<JSObject*>::fromMarkedLocation(&obj);
JS::Rooted<JS::Value> actionValue(aCx);
if (JS_GetProperty(aCx, handle, "action", &actionValue)) {
if (actionValue.isNumber()) {
double actionNumber = actionValue.toNumber();
return FromAction(
static_cast<nsIContentAnalysisResponse::Action>(actionNumber));
}
}
}
return FromNoResult(
NoContentAnalysisResult::DENY_DUE_TO_INVALID_JSON_RESPONSE);
}
static RefPtr<ContentAnalysisResult> FromJSONContentAnalysisResponse(
const JS::Handle<JS::Value>& aValue, JSContext* aCx) {
if (aValue.isObject()) {
auto* obj = aValue.toObjectOrNull();
JS::Handle<JSObject*> handle =
JS::Handle<JSObject*>::fromMarkedLocation(&obj);
JS::Rooted<JS::Value> shouldAllowValue(aCx);
if (JS_GetProperty(aCx, handle, "shouldAllowContent",
&shouldAllowValue)) {
if (shouldAllowValue.isTrue()) {
return FromAction(nsIContentAnalysisResponse::Action::eAllow);
} else if (shouldAllowValue.isFalse()) {
return FromAction(nsIContentAnalysisResponse::Action::eBlock);
} else {
return FromNoResult(NoContentAnalysisResult::DENY_DUE_TO_OTHER_ERROR);
}
}
}
return FromNoResult(
NoContentAnalysisResult::DENY_DUE_TO_INVALID_JSON_RESPONSE);
}
static RefPtr<ContentAnalysisResult> FromContentAnalysisResponse(
nsIContentAnalysisResponse* aResponse) {
bool shouldAllowContent = false;
DebugOnly<nsresult> rv =
aResponse->GetShouldAllowContent(&shouldAllowContent);
MOZ_ASSERT(NS_SUCCEEDED(rv));
if (shouldAllowContent) {
return FromAction(nsIContentAnalysisResponse::Action::eAllow);
} else {
return FromAction(nsIContentAnalysisResponse::Action::eBlock);
}
}
private:
explicit ContentAnalysisResult(nsIContentAnalysisResponse::Action aAction)
: mValue(aAction) {}
explicit ContentAnalysisResult(NoContentAnalysisResult aResult)
: mValue(aResult) {}
virtual ~ContentAnalysisResult() = default;
Variant<nsIContentAnalysisResponse::Action, NoContentAnalysisResult> mValue;
friend struct IPC::ParamTraits<ContentAnalysisResult>;
friend struct IPC::ParamTraits<ContentAnalysisResult*>;
friend struct IPC::ParamTraits<RefPtr<ContentAnalysisResult>>;
};
} // namespace contentanalysis
} // namespace mozilla
namespace IPC {
template <>
struct ParamTraits<mozilla::contentanalysis::NoContentAnalysisResult>
: public ContiguousEnumSerializer<
mozilla::contentanalysis::NoContentAnalysisResult,
static_cast<mozilla::contentanalysis::NoContentAnalysisResult>(0),
mozilla::contentanalysis::NoContentAnalysisResult::LAST_VALUE> {};
template <>
struct ParamTraits<nsIContentAnalysisResponse::Action>
: public ContiguousEnumSerializerInclusive<
nsIContentAnalysisResponse::Action,
nsIContentAnalysisResponse::Action::eUnspecified,
nsIContentAnalysisResponse::Action::eCanceled> {};
template <>
struct ParamTraits<mozilla::contentanalysis::ContentAnalysisResult> {
typedef mozilla::contentanalysis::ContentAnalysisResult paramType;
static void Write(MessageWriter* aWriter, const paramType& aParam) {
WriteParam(aWriter, aParam.mValue);
}
static bool Read(MessageReader* aReader, paramType* aResult) {
return ReadParam(aReader, &(aResult->mValue));
}
};
template <>
struct ParamTraits<mozilla::contentanalysis::ContentAnalysisResult*> {
typedef mozilla::contentanalysis::ContentAnalysisResult paramType;
static void Write(MessageWriter* aWriter, const paramType* aParam) {
if (!aParam) {
WriteParam(aWriter, true);
return;
}
WriteParam(aWriter, false);
WriteParam(aWriter, aParam->mValue);
}
static bool Read(MessageReader* aReader, RefPtr<paramType>* aResult) {
bool isNull = false;
if (!ReadParam(aReader, &isNull)) {
return false;
}
if (isNull) {
*aResult = nullptr;
return true;
}
*aResult = mozilla::contentanalysis::ContentAnalysisResult::FromNoResult(
mozilla::contentanalysis::NoContentAnalysisResult::
DENY_DUE_TO_OTHER_ERROR);
return ReadParam(aReader, &((*aResult)->mValue));
}
};
} // namespace IPC
#endif // mozilla_ContentAnalysisIPCTypes_h
|