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 178
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/api/declarative/declarative_manifest_data.h"
#include <stddef.h>
#include <concepts>
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/api/declarative/declarative_constants.h"
#include "extensions/common/manifest_constants.h"
namespace extensions {
namespace {
class ErrorBuilder {
public:
explicit ErrorBuilder(std::u16string* error) : error_(error) {}
ErrorBuilder(const ErrorBuilder&) = delete;
ErrorBuilder& operator=(const ErrorBuilder&) = delete;
// Appends a single error consisting of one or more strings.
template <typename... Ts>
requires(sizeof...(Ts) > 0) &&
(... && std::convertible_to<Ts, std::string_view>)
void Append(Ts... errors) {
if (!error_->empty())
error_->append(u"; ");
(..., error_->append(base::UTF8ToUTF16(errors)));
}
private:
const raw_ptr<std::u16string> error_;
};
// Converts a rule defined in the manifest into a JSON internal format. The
// difference is that actions and conditions use a "type" key to define the
// type of rule/condition, while the internal format uses a "instanceType" key
// for this. This function walks through all the conditions and rules to swap
// the manifest key for the internal key.
bool ConvertManifestRule(DeclarativeManifestData::Rule& rule,
ErrorBuilder* error_builder) {
auto convert_list = [error_builder](base::Value::List& list) {
for (base::Value& value : list) {
base::Value::Dict* dictionary = value.GetIfDict();
if (!dictionary) {
error_builder->Append("expected dictionary, got ",
base::Value::GetTypeName(value.type()));
return false;
}
std::string* type = dictionary->FindString("type");
if (!type) {
error_builder->Append("'type' is required and must be a string");
return false;
}
if (*type == declarative_content_constants::kLegacyShowAction) {
dictionary->Set("instanceType",
declarative_content_constants::kShowAction);
} else {
dictionary->Set("instanceType", std::move(*type));
}
dictionary->Remove("type");
}
return true;
};
return convert_list(rule.actions) && convert_list(rule.conditions);
}
} // namespace
DeclarativeManifestData::DeclarativeManifestData() {
}
DeclarativeManifestData::~DeclarativeManifestData() {
}
// static
DeclarativeManifestData* DeclarativeManifestData::Get(
const Extension* extension) {
return static_cast<DeclarativeManifestData*>(
extension->GetManifestData(manifest_keys::kEventRules));
}
// static
std::unique_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue(
const base::Value& value,
std::u16string* error) {
// The following is an example of how an event programmatic rule definition
// translates to a manifest definition.
//
// From javascript:
//
// chrome.declarativeContent.onPageChanged.addRules([{
// actions: [
// new chrome.declarativeContent.ShowPageAction()
// ],
// conditions: [
// new chrome.declarativeContent.PageStateMatcher({css: ["video"]})
// ]
// }]);
//
// In manifest:
//
// "event_rules": [{
// "event" : "declarativeContent.onPageChanged",
// "actions" : [{
// "type": "declarativeContent.ShowPageAction"
// }],
// "conditions" : [{
// "css": ["video"],
// "type" : "declarativeContent.PageStateMatcher"
// }]
// }]
//
// The javascript objects get translated into JSON objects with a "type"
// field to indicate the instance type. Instead of adding rules to a
// specific event list, each rule has an "event" field to indicate which
// event it applies to.
//
ErrorBuilder error_builder(error);
std::unique_ptr<DeclarativeManifestData> result(
new DeclarativeManifestData());
if (!value.is_list()) {
error_builder.Append("'event_rules' expected list, got ",
base::Value::GetTypeName(value.type()));
return nullptr;
}
for (const auto& element : value.GetList()) {
if (!element.is_dict()) {
error_builder.Append("expected dictionary, got ",
base::Value::GetTypeName(element.type()));
return nullptr;
}
const base::Value::Dict& dict = element.GetDict();
const std::string* event = dict.FindString("event");
if (!event) {
error_builder.Append("'event' is required");
return nullptr;
}
auto rule = Rule::FromValue(dict);
if (!rule) {
error_builder.Append("rule failed to populate");
return nullptr;
}
if (!ConvertManifestRule(*rule, &error_builder)) {
return nullptr;
}
result->event_rules_map_[*event].push_back(std::move(rule).value());
}
return result;
}
std::vector<DeclarativeManifestData::Rule>
DeclarativeManifestData::RulesForEvent(const std::string& event) {
const auto& rules = event_rules_map_[event];
std::vector<DeclarativeManifestData::Rule> result;
result.reserve(rules.size());
for (const auto& rule : rules) {
// TODO(rdevlin.cronin): It would be nice if we could have the RulesRegistry
// reference the rules owned here, but the ownership issues are a bit
// tricky. Revisit this.
result.push_back(rule.Clone());
}
return result;
}
} // namespace extensions
|