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/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
15 #include "extensions/common/extensions_client.h" | 15 #include "extensions/common/extensions_client.h" |
16 #include "extensions/common/features/feature.h" | 16 #include "extensions/common/features/feature.h" |
17 #include "extensions/common/features/feature_util.h" | 17 #include "extensions/common/features/feature_util.h" |
18 #include "extensions/common/switches.h" | 18 #include "extensions/common/switches.h" |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 class Static { | 24 class Static { |
25 public: | 25 public: |
26 FeatureProvider* GetFeatures(const std::string& name) const { | |
27 auto it = feature_providers_.find(name); | |
28 if (it == feature_providers_.end()) | |
29 CRASH_WITH_MINIDUMP("FeatureProvider \"" + name + "\" not found"); | |
30 return it->second.get(); | |
31 } | |
32 | |
33 private: | |
34 friend struct base::DefaultLazyInstanceTraits<Static>; | |
35 | |
36 Static() { | 26 Static() { |
37 TRACE_EVENT0("startup", "extensions::FeatureProvider::Static"); | 27 TRACE_EVENT0("startup", "extensions::FeatureProvider::Static"); |
38 base::Time begin_time = base::Time::Now(); | 28 base::Time begin_time = base::Time::Now(); |
39 | 29 |
40 ExtensionsClient* client = ExtensionsClient::Get(); | 30 ExtensionsClient* client = ExtensionsClient::Get(); |
41 feature_providers_["api"] = client->CreateFeatureProvider("api"); | 31 feature_providers_["api"] = client->CreateFeatureProvider("api"); |
42 feature_providers_["manifest"] = client->CreateFeatureProvider("manifest"); | 32 feature_providers_["manifest"] = client->CreateFeatureProvider("manifest"); |
43 feature_providers_["permission"] = | 33 feature_providers_["permission"] = |
44 client->CreateFeatureProvider("permission"); | 34 client->CreateFeatureProvider("permission"); |
45 feature_providers_["behavior"] = client->CreateFeatureProvider("behavior"); | 35 feature_providers_["behavior"] = client->CreateFeatureProvider("behavior"); |
46 | 36 |
47 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 37 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
48 std::string process_type = | 38 std::string process_type = |
49 command_line->GetSwitchValueASCII(::switches::kProcessType); | 39 command_line->GetSwitchValueASCII(::switches::kProcessType); |
50 | 40 |
51 // Measure time only for browser process. This method gets called by the | 41 // Measure time only for browser process. This method gets called by the |
52 // browser process on startup, as well as on renderer and extension | 42 // browser process on startup, as well as on renderer and extension |
53 // processes throughout the execution of the browser. We are more | 43 // processes throughout the execution of the browser. We are more |
54 // interested in how long this takes as a startup cost, so we are | 44 // interested in how long this takes as a startup cost, so we are |
55 // just measuring the time in the browser process. | 45 // just measuring the time in the browser process. |
56 if (process_type == std::string()) { | 46 if (process_type == std::string()) { |
57 UMA_HISTOGRAM_TIMES("Extensions.FeatureProviderStaticInitTime", | 47 UMA_HISTOGRAM_TIMES("Extensions.FeatureProviderStaticInitTime", |
58 base::Time::Now() - begin_time); | 48 base::Time::Now() - begin_time); |
59 } | 49 } |
60 } | 50 } |
61 | 51 |
| 52 FeatureProvider* GetFeatures(const std::string& name) const { |
| 53 auto it = feature_providers_.find(name); |
| 54 if (it == feature_providers_.end()) |
| 55 CRASH_WITH_MINIDUMP("FeatureProvider \"" + name + "\" not found"); |
| 56 return it->second.get(); |
| 57 } |
| 58 |
| 59 private: |
62 std::map<std::string, std::unique_ptr<FeatureProvider>> feature_providers_; | 60 std::map<std::string, std::unique_ptr<FeatureProvider>> feature_providers_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(Static); |
63 }; | 63 }; |
64 | 64 |
65 base::LazyInstance<Static> g_static = LAZY_INSTANCE_INITIALIZER; | 65 base::LazyInstance<Static>::Leaky g_static = LAZY_INSTANCE_INITIALIZER; |
66 | 66 |
67 const Feature* GetFeatureFromProviderByName(const std::string& provider_name, | 67 const Feature* GetFeatureFromProviderByName(const std::string& provider_name, |
68 const std::string& feature_name) { | 68 const std::string& feature_name) { |
69 const Feature* feature = | 69 const Feature* feature = |
70 FeatureProvider::GetByName(provider_name)->GetFeature(feature_name); | 70 FeatureProvider::GetByName(provider_name)->GetFeature(feature_name); |
71 // We should always refer to existing features, but we can't CHECK here | 71 // 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 | 72 // due to flaky JSONReader fails, see: crbug.com/176381, crbug.com/602936 |
73 DCHECK(feature) << "Feature \"" << feature_name << "\" not found in " | 73 DCHECK(feature) << "Feature \"" << feature_name << "\" not found in " |
74 << "FeatureProvider \"" << provider_name << "\""; | 74 << "FeatureProvider \"" << provider_name << "\""; |
75 return feature; | 75 return feature; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 const Feature* FeatureProvider::GetPermissionFeature(const std::string& name) { | 116 const Feature* FeatureProvider::GetPermissionFeature(const std::string& name) { |
117 return GetFeatureFromProviderByName("permission", name); | 117 return GetFeatureFromProviderByName("permission", name); |
118 } | 118 } |
119 | 119 |
120 // static | 120 // static |
121 const Feature* FeatureProvider::GetBehaviorFeature(const std::string& name) { | 121 const Feature* FeatureProvider::GetBehaviorFeature(const std::string& name) { |
122 return GetFeatureFromProviderByName("behavior", name); | 122 return GetFeatureFromProviderByName("behavior", name); |
123 } | 123 } |
124 | 124 |
125 } // namespace extensions | 125 } // namespace extensions |
OLD | NEW |