Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / features / feature_provider.cc
blob79ecf731e6081953c61979b7aef48a7bf8e33d3a
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
3 // found in the LICENSE file.
5 #include "extensions/common/features/feature_provider.h"
7 #include <map>
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/trace_event/trace_event.h"
15 #include "content/public/common/content_switches.h"
16 #include "extensions/common/extensions_client.h"
17 #include "extensions/common/features/feature_util.h"
18 #include "extensions/common/switches.h"
20 namespace extensions {
22 namespace {
24 class Static {
25 public:
26 FeatureProvider* GetFeatures(const std::string& name) const {
27 FeatureProviderMap::const_iterator it = feature_providers_.find(name);
28 if (it == feature_providers_.end())
29 CRASH_WITH_MINIDUMP("FeatureProvider \"" + name + "\" not found");
30 return it->second.get();
33 private:
34 friend struct base::DefaultLazyInstanceTraits<Static>;
36 Static() {
37 TRACE_EVENT0("startup", "extensions::FeatureProvider::Static");
38 base::Time begin_time = base::Time::Now();
40 ExtensionsClient* client = ExtensionsClient::Get();
41 feature_providers_["api"] =
42 make_linked_ptr(client->CreateFeatureProvider("api").release());
43 feature_providers_["manifest"] =
44 make_linked_ptr(client->CreateFeatureProvider("manifest").release());
45 feature_providers_["permission"] =
46 make_linked_ptr(client->CreateFeatureProvider("permission").release());
47 feature_providers_["behavior"] =
48 make_linked_ptr(client->CreateFeatureProvider("behavior").release());
50 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
51 std::string process_type =
52 command_line->GetSwitchValueASCII(::switches::kProcessType);
54 // Measure time only for browser process. This method gets called by the
55 // browser process on startup, as well as on renderer and extension
56 // processes throughout the execution of the browser. We are more
57 // interested in how long this takes as a startup cost, so we are
58 // just measuring the time in the browser process.
59 if (process_type == std::string()) {
60 UMA_HISTOGRAM_TIMES("Extensions.FeatureProviderStaticInitTime",
61 base::Time::Now() - begin_time);
65 typedef std::map<std::string, linked_ptr<FeatureProvider> >
66 FeatureProviderMap;
68 FeatureProviderMap feature_providers_;
71 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER;
73 const Feature* GetFeatureFromProviderByName(const std::string& provider_name,
74 const std::string& feature_name) {
75 const Feature* feature =
76 FeatureProvider::GetByName(provider_name)->GetFeature(feature_name);
77 if (!feature) {
78 CRASH_WITH_MINIDUMP("Feature \"" + feature_name + "\" not found in " +
79 "FeatureProvider \"" + provider_name + "\"");
81 return feature;
84 } // namespace
86 // static
87 const FeatureProvider* FeatureProvider::GetByName(const std::string& name) {
88 return g_static.Get().GetFeatures(name);
91 // static
92 const FeatureProvider* FeatureProvider::GetAPIFeatures() {
93 return GetByName("api");
96 // static
97 const FeatureProvider* FeatureProvider::GetManifestFeatures() {
98 return GetByName("manifest");
101 // static
102 const FeatureProvider* FeatureProvider::GetPermissionFeatures() {
103 return GetByName("permission");
106 // static
107 const FeatureProvider* FeatureProvider::GetBehaviorFeatures() {
108 return GetByName("behavior");
111 // static
112 const Feature* FeatureProvider::GetAPIFeature(const std::string& name) {
113 return GetFeatureFromProviderByName("api", name);
116 // static
117 const Feature* FeatureProvider::GetManifestFeature(const std::string& name) {
118 return GetFeatureFromProviderByName("manifest", name);
121 // static
122 const Feature* FeatureProvider::GetPermissionFeature(const std::string& name) {
123 return GetFeatureFromProviderByName("permission", name);
126 // static
127 const Feature* FeatureProvider::GetBehaviorFeature(const std::string& name) {
128 return GetFeatureFromProviderByName("behavior", name);
131 } // namespace extensions