Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Unified Diff: extensions/common/features/base_feature_provider.cc

Issue 2133783002: [Extensions] Break up BaseFeatureProvider, create JSONFeatureProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/common/features/base_feature_provider.cc
diff --git a/extensions/common/features/base_feature_provider.cc b/extensions/common/features/base_feature_provider.cc
index 710a82749fa1f46a1abe601bfd150fdf0f1f9f9b..f662a725a15f0d4438fa59175f9a5ca89b8383e1 100644
--- a/extensions/common/features/base_feature_provider.cc
+++ b/extensions/common/features/base_feature_provider.cc
@@ -4,148 +4,14 @@
#include "extensions/common/features/base_feature_provider.h"
-#include <stddef.h>
-
-#include <stack>
-#include <utility>
-
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
-#include "base/values.h"
-#include "extensions/common/extensions_client.h"
-#include "extensions/common/features/complex_feature.h"
-#include "extensions/common/features/simple_feature.h"
+#include "extensions/common/features/feature.h"
namespace extensions {
-namespace {
-
-bool IsNocompile(const base::Value& value) {
- bool nocompile = false;
- const base::DictionaryValue* as_dict = nullptr;
- if (value.GetAsDictionary(&as_dict)) {
- as_dict->GetBoolean("nocompile", &nocompile);
- } else {
- // "nocompile" is not supported for any other feature type.
- }
- return nocompile;
-}
-
-bool ParseFeature(const base::DictionaryValue* value,
- const std::string& name,
- SimpleFeature* feature) {
- feature->set_name(name);
- std::string error = feature->Parse(value);
- if (!error.empty())
- LOG(ERROR) << error;
- return error.empty();
-}
-
-} // namespace
-
-BaseFeatureProvider::BaseFeatureProvider(const base::DictionaryValue& root,
- FeatureFactory factory)
- : factory_(factory) {
- for (base::DictionaryValue::Iterator iter(root); !iter.IsAtEnd();
- iter.Advance()) {
- if (IsNocompile(iter.value())) {
- continue;
- }
-
- if (iter.value().GetType() == base::Value::TYPE_DICTIONARY) {
- std::unique_ptr<SimpleFeature> feature((*factory_)());
-
- std::vector<std::string> split = base::SplitString(
- iter.key(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
-
- // Push parent features on the stack, starting with the current feature.
- // If one of the features has "noparent" set, stop pushing features on
- // the stack. The features will then be parsed in order, starting with
- // the farthest parent that is either top level or has "noparent" set.
- std::stack<std::pair<std::string, const base::DictionaryValue*> >
- parse_stack;
- while (!split.empty()) {
- std::string parent_name = base::JoinString(split, ".");
- split.pop_back();
- if (root.HasKey(parent_name)) {
- const base::DictionaryValue* parent = nullptr;
- if (!root.GetDictionaryWithoutPathExpansion(parent_name, &parent)) {
- // If the parent is a complex feature, find the parent with the
- // 'default_parent' flag.
- const base::ListValue* parent_list = nullptr;
- CHECK(root.GetListWithoutPathExpansion(parent_name, &parent_list));
- for (size_t i = 0; i < parent_list->GetSize(); ++i) {
- CHECK(parent_list->GetDictionary(i, &parent));
- if (parent->HasKey("default_parent"))
- break;
- parent = nullptr;
- }
- CHECK(parent) << parent_name << " must declare one of its features"
- << " the default parent, with {\"default_parent\": true}.";
- }
- parse_stack.push(std::make_pair(parent_name, parent));
- bool no_parent = false;
- parent->GetBoolean("noparent", &no_parent);
- if (no_parent)
- break;
- }
- }
-
- CHECK(!parse_stack.empty());
- // Parse all parent features.
- bool parse_error = false;
- while (!parse_stack.empty()) {
- if (!ParseFeature(parse_stack.top().second,
- parse_stack.top().first,
- feature.get())) {
- parse_error = true;
- break;
- }
- parse_stack.pop();
- }
-
- if (parse_error)
- continue;
-
- features_[iter.key()] = std::move(feature);
- } else if (iter.value().GetType() == base::Value::TYPE_LIST) {
- // This is a complex feature.
- const base::ListValue* list =
- static_cast<const base::ListValue*>(&iter.value());
- CHECK_GT(list->GetSize(), 0UL);
-
- std::unique_ptr<ComplexFeature::FeatureList> features(
- new ComplexFeature::FeatureList());
-
- // Parse and add all SimpleFeatures from the list.
- for (const auto& entry : *list) {
- base::DictionaryValue* dict;
- if (!entry->GetAsDictionary(&dict)) {
- LOG(ERROR) << iter.key() << ": Feature rules must be dictionaries.";
- continue;
- }
-
- std::unique_ptr<SimpleFeature> feature((*factory_)());
- if (!ParseFeature(dict, iter.key(), feature.get()))
- continue;
-
- features->push_back(std::move(feature));
- }
-
- std::unique_ptr<ComplexFeature> feature(
- new ComplexFeature(std::move(features)));
- feature->set_name(iter.key());
-
- features_[iter.key()] = std::move(feature);
- } else {
- LOG(ERROR) << iter.key() << ": Feature description must be dictionary or"
- << " list of dictionaries.";
- }
- }
-}
-
-BaseFeatureProvider::~BaseFeatureProvider() {
-}
+BaseFeatureProvider::BaseFeatureProvider() {}
+BaseFeatureProvider::~BaseFeatureProvider() {}
const FeatureMap& BaseFeatureProvider::GetAllFeatures() const {
return features_;

Powered by Google App Engine
This is Rietveld 408576698