| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/feature_provider.h" | 5 #include "extensions/common/features/feature_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" |
| 13 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 14 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 15 #include "extensions/common/extensions_client.h" | 17 #include "extensions/common/extensions_client.h" |
| 16 #include "extensions/common/features/feature.h" | 18 #include "extensions/common/features/feature.h" |
| 17 #include "extensions/common/features/feature_util.h" | 19 #include "extensions/common/features/feature_util.h" |
| 18 #include "extensions/common/switches.h" | 20 #include "extensions/common/switches.h" |
| 19 | 21 |
| 20 namespace extensions { | 22 namespace extensions { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 FeatureProvider::GetByName(provider_name)->GetFeature(feature_name); | 72 FeatureProvider::GetByName(provider_name)->GetFeature(feature_name); |
| 71 // We should always refer to existing features, but we can't CHECK here | 73 // We should always refer to existing features, but we can't CHECK here |
| 72 // due to flaky JSONReader fails, see: crbug.com/176381, crbug.com/602936 | 74 // due to flaky JSONReader fails, see: crbug.com/176381, crbug.com/602936 |
| 73 DCHECK(feature) << "Feature \"" << feature_name << "\" not found in " | 75 DCHECK(feature) << "Feature \"" << feature_name << "\" not found in " |
| 74 << "FeatureProvider \"" << provider_name << "\""; | 76 << "FeatureProvider \"" << provider_name << "\""; |
| 75 return feature; | 77 return feature; |
| 76 } | 78 } |
| 77 | 79 |
| 78 } // namespace | 80 } // namespace |
| 79 | 81 |
| 82 FeatureProvider::FeatureProvider() {} |
| 83 FeatureProvider::~FeatureProvider() {} |
| 84 |
| 80 // static | 85 // static |
| 81 const FeatureProvider* FeatureProvider::GetByName(const std::string& name) { | 86 const FeatureProvider* FeatureProvider::GetByName(const std::string& name) { |
| 82 return g_static.Get().GetFeatures(name); | 87 return g_static.Get().GetFeatures(name); |
| 83 } | 88 } |
| 84 | 89 |
| 85 // static | 90 // static |
| 86 const FeatureProvider* FeatureProvider::GetAPIFeatures() { | 91 const FeatureProvider* FeatureProvider::GetAPIFeatures() { |
| 87 return GetByName("api"); | 92 return GetByName("api"); |
| 88 } | 93 } |
| 89 | 94 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 115 // static | 120 // static |
| 116 const Feature* FeatureProvider::GetPermissionFeature(const std::string& name) { | 121 const Feature* FeatureProvider::GetPermissionFeature(const std::string& name) { |
| 117 return GetFeatureFromProviderByName("permission", name); | 122 return GetFeatureFromProviderByName("permission", name); |
| 118 } | 123 } |
| 119 | 124 |
| 120 // static | 125 // static |
| 121 const Feature* FeatureProvider::GetBehaviorFeature(const std::string& name) { | 126 const Feature* FeatureProvider::GetBehaviorFeature(const std::string& name) { |
| 122 return GetFeatureFromProviderByName("behavior", name); | 127 return GetFeatureFromProviderByName("behavior", name); |
| 123 } | 128 } |
| 124 | 129 |
| 130 Feature* FeatureProvider::GetFeature(const std::string& name) const { |
| 131 FeatureMap::const_iterator iter = features_.find(name); |
| 132 if (iter != features_.end()) |
| 133 return iter->second.get(); |
| 134 else |
| 135 return nullptr; |
| 136 } |
| 137 |
| 138 Feature* FeatureProvider::GetParent(Feature* feature) const { |
| 139 CHECK(feature); |
| 140 if (feature->no_parent()) |
| 141 return nullptr; |
| 142 |
| 143 std::vector<std::string> split = base::SplitString( |
| 144 feature->name(), ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 145 if (split.size() < 2) |
| 146 return nullptr; |
| 147 split.pop_back(); |
| 148 return GetFeature(base::JoinString(split, ".")); |
| 149 } |
| 150 |
| 151 // Children of a given API are named starting with parent.name()+".", which |
| 152 // means they'll be contiguous in the features_ std::map. |
| 153 std::vector<Feature*> FeatureProvider::GetChildren( |
| 154 const Feature& parent) const { |
| 155 std::string prefix = parent.name() + "."; |
| 156 const FeatureMap::const_iterator first_child = features_.lower_bound(prefix); |
| 157 |
| 158 // All children have names before (parent.name() + ('.'+1)). |
| 159 ++prefix.back(); |
| 160 const FeatureMap::const_iterator after_children = |
| 161 features_.lower_bound(prefix); |
| 162 |
| 163 std::vector<Feature*> result; |
| 164 result.reserve(std::distance(first_child, after_children)); |
| 165 for (FeatureMap::const_iterator it = first_child; it != after_children; |
| 166 ++it) { |
| 167 result.push_back(it->second.get()); |
| 168 } |
| 169 return result; |
| 170 } |
| 171 |
| 172 const FeatureMap& FeatureProvider::GetAllFeatures() const { |
| 173 return features_; |
| 174 } |
| 175 |
| 176 void FeatureProvider::AddFeature(base::StringPiece name, |
| 177 std::unique_ptr<Feature> feature) { |
| 178 features_[name.as_string()] = std::move(feature); |
| 179 } |
| 180 |
| 181 void FeatureProvider::AddFeature(base::StringPiece name, Feature* feature) { |
| 182 features_[name.as_string()] = std::unique_ptr<Feature>(feature); |
| 183 } |
| 184 |
| 125 } // namespace extensions | 185 } // namespace extensions |
| OLD | NEW |