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"
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/switches.h"
19 namespace extensions
{
25 FeatureProvider
* GetFeatures(const std::string
& name
) const {
26 FeatureProviderMap::const_iterator it
= feature_providers_
.find(name
);
27 CHECK(it
!= feature_providers_
.end());
28 return it
->second
.get();
32 friend struct base::DefaultLazyInstanceTraits
<Static
>;
35 TRACE_EVENT0("startup", "extensions::FeatureProvider::Static");
36 base::Time begin_time
= base::Time::Now();
38 ExtensionsClient
* client
= ExtensionsClient::Get();
39 feature_providers_
["api"] =
40 make_linked_ptr(client
->CreateFeatureProvider("api").release());
41 feature_providers_
["manifest"] =
42 make_linked_ptr(client
->CreateFeatureProvider("manifest").release());
43 feature_providers_
["permission"] =
44 make_linked_ptr(client
->CreateFeatureProvider("permission").release());
45 feature_providers_
["behavior"] =
46 make_linked_ptr(client
->CreateFeatureProvider("behavior").release());
48 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
49 std::string process_type
=
50 command_line
->GetSwitchValueASCII(::switches::kProcessType
);
52 // Measure time only for browser process. This method gets called by the
53 // browser process on startup, as well as on renderer and extension
54 // processes throughout the execution of the browser. We are more
55 // interested in how long this takes as a startup cost, so we are
56 // just measuring the time in the browser process.
57 if (process_type
== std::string()) {
58 UMA_HISTOGRAM_TIMES("Extensions.FeatureProviderStaticInitTime",
59 base::Time::Now() - begin_time
);
63 typedef std::map
<std::string
, linked_ptr
<FeatureProvider
> >
66 FeatureProviderMap feature_providers_
;
69 base::LazyInstance
<Static
> g_static
= LAZY_INSTANCE_INITIALIZER
;
71 const Feature
* GetFeatureFromProviderByName(const std::string
& provider_name
,
72 const std::string
& feature_name
) {
73 const Feature
* feature
=
74 FeatureProvider::GetByName(provider_name
)->GetFeature(feature_name
);
75 CHECK(feature
) << "FeatureProvider '" << provider_name
76 << "' does not contain Feature '" << feature_name
<< "'";
83 const FeatureProvider
* FeatureProvider::GetByName(const std::string
& name
) {
84 const FeatureProvider
* feature_provider
= g_static
.Get().GetFeatures(name
);
85 CHECK(feature_provider
) << "FeatureProvider '" << name
<< "' not found";
86 return feature_provider
;
90 const FeatureProvider
* FeatureProvider::GetAPIFeatures() {
91 return GetByName("api");
95 const FeatureProvider
* FeatureProvider::GetManifestFeatures() {
96 return GetByName("manifest");
100 const FeatureProvider
* FeatureProvider::GetPermissionFeatures() {
101 return GetByName("permission");
105 const FeatureProvider
* FeatureProvider::GetBehaviorFeatures() {
106 return GetByName("behavior");
110 const Feature
* FeatureProvider::GetAPIFeature(const std::string
& name
) {
111 return GetFeatureFromProviderByName("api", name
);
115 const Feature
* FeatureProvider::GetManifestFeature(const std::string
& name
) {
116 return GetFeatureFromProviderByName("manifest", name
);
120 const Feature
* FeatureProvider::GetPermissionFeature(const std::string
& name
) {
121 return GetFeatureFromProviderByName("permission", name
);
125 const Feature
* FeatureProvider::GetBehaviorFeature(const std::string
& name
) {
126 return GetFeatureFromProviderByName("behavior", name
);
129 } // namespace extensions