| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/features/base_feature_provider.h" | 5 #include "extensions/common/features/base_feature_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <stack> | 9 #include <stack> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } else { | 142 } else { |
| 143 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" | 143 LOG(ERROR) << iter.key() << ": Feature description must be dictionary or" |
| 144 << " list of dictionaries."; | 144 << " list of dictionaries."; |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 BaseFeatureProvider::~BaseFeatureProvider() { | 149 BaseFeatureProvider::~BaseFeatureProvider() { |
| 150 } | 150 } |
| 151 | 151 |
| 152 const std::vector<std::string>& BaseFeatureProvider::GetAllFeatureNames() | 152 const FeatureMap& BaseFeatureProvider::GetAllFeatures() const { |
| 153 const { | 153 return features_; |
| 154 if (feature_names_.empty()) { | |
| 155 for (const auto& feature : features_) | |
| 156 feature_names_.push_back(feature.first); | |
| 157 // A std::map is sorted by its keys, so we don't need to sort feature_names_ | |
| 158 // now. | |
| 159 } | |
| 160 return feature_names_; | |
| 161 } | 154 } |
| 162 | 155 |
| 163 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const { | 156 Feature* BaseFeatureProvider::GetFeature(const std::string& name) const { |
| 164 const auto iter = features_.find(name); | 157 FeatureMap::const_iterator iter = features_.find(name); |
| 165 if (iter != features_.end()) | 158 if (iter != features_.end()) |
| 166 return iter->second.get(); | 159 return iter->second.get(); |
| 167 else | 160 else |
| 168 return nullptr; | 161 return nullptr; |
| 169 } | 162 } |
| 170 | 163 |
| 171 Feature* BaseFeatureProvider::GetParent(Feature* feature) const { | 164 Feature* BaseFeatureProvider::GetParent(Feature* feature) const { |
| 172 CHECK(feature); | 165 CHECK(feature); |
| 173 if (feature->no_parent()) | 166 if (feature->no_parent()) |
| 174 return nullptr; | 167 return nullptr; |
| 175 | 168 |
| 176 std::vector<std::string> split = base::SplitString( | 169 std::vector<std::string> split = base::SplitString( |
| 177 feature->name(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 170 feature->name(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 178 if (split.size() < 2) | 171 if (split.size() < 2) |
| 179 return nullptr; | 172 return nullptr; |
| 180 split.pop_back(); | 173 split.pop_back(); |
| 181 return GetFeature(base::JoinString(split, ".")); | 174 return GetFeature(base::JoinString(split, ".")); |
| 182 } | 175 } |
| 183 | 176 |
| 184 // Children of a given API are named starting with parent.name()+".", which | 177 // Children of a given API are named starting with parent.name()+".", which |
| 185 // means they'll be contiguous in the features_ std::map. | 178 // means they'll be contiguous in the features_ std::map. |
| 186 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent) | 179 std::vector<Feature*> BaseFeatureProvider::GetChildren(const Feature& parent) |
| 187 const { | 180 const { |
| 188 std::string prefix = parent.name() + "."; | 181 std::string prefix = parent.name() + "."; |
| 189 const auto first_child = features_.lower_bound(prefix); | 182 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix); |
| 190 | 183 |
| 191 // All children have names before (parent.name() + ('.'+1)). | 184 // All children have names before (parent.name() + ('.'+1)). |
| 192 ++prefix[prefix.size() - 1]; | 185 ++prefix[prefix.size() - 1]; |
| 193 const auto after_children = features_.lower_bound(prefix); | 186 const FeatureMap::const_iterator after_children = |
| 187 features_.lower_bound(prefix); |
| 194 | 188 |
| 195 std::vector<Feature*> result; | 189 std::vector<Feature*> result; |
| 196 result.reserve(std::distance(first_child, after_children)); | 190 result.reserve(std::distance(first_child, after_children)); |
| 197 for (auto it = first_child; it != after_children; ++it) { | 191 for (FeatureMap::const_iterator it = first_child; it != after_children; |
| 192 ++it) { |
| 198 result.push_back(it->second.get()); | 193 result.push_back(it->second.get()); |
| 199 } | 194 } |
| 200 return result; | 195 return result; |
| 201 } | 196 } |
| 202 | 197 |
| 203 } // namespace extensions | 198 } // namespace extensions |
| OLD | NEW |