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

Side by Side Diff: extensions/common/features/base_feature_provider.cc

Issue 2669463002: [Extensions] Remove BaseFeatureProvider (Closed)
Patch Set: Created 3 years, 10 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/common/features/base_feature_provider.h"
6
7 #include <utility>
8
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "extensions/common/features/feature.h"
12
13 namespace extensions {
14
15 BaseFeatureProvider::BaseFeatureProvider() {}
16 BaseFeatureProvider::~BaseFeatureProvider() {}
17
18 const FeatureMap& BaseFeatureProvider::GetAllFeatures() const {
19 return features_;
20 }
21
22 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const {
23 FeatureMap::const_iterator iter = features_.find(name);
24 if (iter != features_.end())
25 return iter->second.get();
26 else
27 return nullptr;
28 }
29
30 Feature* BaseFeatureProvider::GetParent(Feature* feature) const {
31 CHECK(feature);
32 if (feature->no_parent())
33 return nullptr;
34
35 std::vector<std::string> split = base::SplitString(
36 feature->name(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
37 if (split.size() < 2)
38 return nullptr;
39 split.pop_back();
40 return GetFeature(base::JoinString(split, "."));
41 }
42
43 // Children of a given API are named starting with parent.name()+".", which
44 // means they'll be contiguous in the features_ std::map.
45 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent)
46 const {
47 std::string prefix = parent.name() + ".";
48 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix);
49
50 // All children have names before (parent.name() + ('.'+1)).
51 ++prefix.back();
52 const FeatureMap::const_iterator after_children =
53 features_.lower_bound(prefix);
54
55 std::vector<Feature*> result;
56 result.reserve(std::distance(first_child, after_children));
57 for (FeatureMap::const_iterator it = first_child; it != after_children;
58 ++it) {
59 result.push_back(it->second.get());
60 }
61 return result;
62 }
63
64 void BaseFeatureProvider::AddFeature(base::StringPiece name,
65 std::unique_ptr<Feature> feature) {
66 features_[name.as_string()] = std::move(feature);
67 }
68
69 void BaseFeatureProvider::AddFeature(base::StringPiece name, Feature* feature) {
70 features_[name.as_string()] = std::unique_ptr<Feature>(feature);
71 }
72
73 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698