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 "chrome/browser/metrics/extensions_metrics_provider.h" | 5 #include "chrome/browser/metrics/extensions_metrics_provider.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/extensions/install_verifier.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "components/metrics/metrics_log.h" | 15 #include "components/metrics/metrics_log.h" |
15 #include "components/metrics/metrics_state_manager.h" | 16 #include "components/metrics/metrics_state_manager.h" |
16 #include "components/metrics/proto/system_profile.pb.h" | 17 #include "components/metrics/proto/system_profile.pb.h" |
17 #include "extensions/browser/extension_registry.h" | 18 #include "extensions/browser/extension_registry.h" |
19 #include "extensions/browser/extension_system.h" | |
18 #include "extensions/common/extension_set.h" | 20 #include "extensions/common/extension_set.h" |
19 #include "third_party/smhasher/src/City.h" | 21 #include "third_party/smhasher/src/City.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 // The number of possible hash keys that a client may use. The UMA client_id | 25 // The number of possible hash keys that a client may use. The UMA client_id |
24 // value is reduced modulo this value to produce the key used by that | 26 // value is reduced modulo this value to produce the key used by that |
25 // particular client. | 27 // particular client. |
26 const size_t kExtensionListClientKeys = 4096; | 28 const size_t kExtensionListClientKeys = 4096; |
27 | 29 |
28 // The number of hash buckets into which extension IDs are mapped. This sets | 30 // The number of hash buckets into which extension IDs are mapped. This sets |
29 // the possible output range of the HashExtension function. | 31 // the possible output range of the HashExtension function. |
30 const size_t kExtensionListBuckets = 1024; | 32 const size_t kExtensionListBuckets = 1024; |
31 | 33 |
34 // Possible states for extensions. The order of these enum values is important, | |
35 // and is used when combining the state of multiple extensions and multiple | |
36 // profiles. Combining two states should always result in the higher state. | |
37 // Ex: One profile is in state FROM_STORE_VERIFIED, and another is in | |
38 // FROM_STORE_UNVERIFIED. The state of the two profiles together will be | |
39 // FROM_STORE_UNVERIFIED. | |
40 // This enum should be kept in sync with the corresponding enum in | |
41 // components/metrics/proto/system_profile.proto | |
42 enum ExtensionState { | |
43 NO_EXTENSIONS, | |
44 FROM_STORE_VERIFIED, | |
45 FROM_STORE_UNVERIFIED, | |
46 OFF_STORE | |
47 }; | |
48 | |
49 metrics::SystemProfileProto::ExtensionsState ExtensionStateAsProto( | |
50 ExtensionState value) { | |
51 switch (value) { | |
52 case NO_EXTENSIONS: | |
53 return metrics::SystemProfileProto::NO_EXTENSIONS; | |
54 case FROM_STORE_VERIFIED: | |
55 return metrics::SystemProfileProto::NO_OFFSTORE_VERIFIED; | |
56 case FROM_STORE_UNVERIFIED: | |
57 return metrics::SystemProfileProto::NO_OFFSTORE_UNVERIFIED; | |
58 case OFF_STORE: | |
59 return metrics::SystemProfileProto::HAS_OFFSTORE; | |
60 } | |
61 NOTREACHED(); | |
62 return metrics::SystemProfileProto::NO_EXTENSIONS; | |
63 } | |
64 | |
65 // Determines if the |extension| is an extension (can use extension APIs) and is | |
66 // not from the webstore. If local information claims the extension is from the | |
67 // webstore, we attempt to verify with |verifier| by checking if it has been | |
68 // explicitly deemed invalid. If |verifier| is inactive or if the extension is | |
69 // unknown to |verifier|, the local information is trusted. | |
70 ExtensionState IsOffStoreExtension( | |
71 const extensions::Extension& extension, | |
72 const extensions::InstallVerifier& verifier) { | |
73 if (!extension.is_extension() && !extension.is_legacy_packaged_app()) | |
74 return NO_EXTENSIONS; | |
75 | |
76 // Component extensions are considered safe. | |
77 if (extension.location() == extensions::Manifest::COMPONENT || | |
Devlin
2014/08/13 19:50:07
nit: Prefer extensions::Manifest::IsComponentLocat
jwd
2014/08/13 20:22:06
Oh, nice, hadn't noticed that.
Done.
| |
78 extension.location() == extensions::Manifest::EXTERNAL_COMPONENT) { | |
79 return NO_EXTENSIONS; | |
80 } | |
81 | |
82 if (verifier.AllowedByEnterprisePolicy(extension.id())) | |
83 return NO_EXTENSIONS; | |
84 | |
85 if (!extensions::InstallVerifier::FromStore(extension)) | |
86 return OFF_STORE; | |
87 | |
88 // Local information about the extension implies it is from the store. We try | |
89 // to use the install verifier to verify this. | |
90 if (!verifier.IsKnownId(extension.id())) | |
91 return FROM_STORE_UNVERIFIED; | |
92 | |
93 if (verifier.IsInvalid(extension.id())) | |
94 return OFF_STORE; | |
95 | |
96 return FROM_STORE_VERIFIED; | |
97 } | |
98 | |
99 // Finds the ExtensionState of |extensions|. The return value will be the | |
100 // highest (as defined by the order of ExtensionState) value of each extension | |
101 // in |extensions|. | |
102 ExtensionState CheckForOffStore(const extensions::ExtensionSet& extensions, | |
103 const extensions::InstallVerifier& verifier) { | |
104 ExtensionState state = NO_EXTENSIONS; | |
105 for (extensions::ExtensionSet::const_iterator it = extensions.begin(); | |
106 it != extensions.end() && state < OFF_STORE; | |
107 ++it) { | |
108 // Combine the state of each extension, always favoring the higher state as | |
109 // defined by the order of ExtensionState. | |
110 state = std::max(state, IsOffStoreExtension(**it, verifier)); | |
111 } | |
112 return state; | |
113 } | |
114 | |
32 } // namespace | 115 } // namespace |
33 | 116 |
34 ExtensionsMetricsProvider::ExtensionsMetricsProvider( | 117 ExtensionsMetricsProvider::ExtensionsMetricsProvider( |
35 metrics::MetricsStateManager* metrics_state_manager) | 118 metrics::MetricsStateManager* metrics_state_manager) |
36 : metrics_state_manager_(metrics_state_manager), cached_profile_(NULL) { | 119 : metrics_state_manager_(metrics_state_manager), cached_profile_(NULL) { |
37 DCHECK(metrics_state_manager_); | 120 DCHECK(metrics_state_manager_); |
38 } | 121 } |
39 | 122 |
40 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() { | 123 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() { |
41 } | 124 } |
(...skipping 25 matching lines...) Expand all Loading... | |
67 cached_profile_ = profile_manager->GetProfileByPath( | 150 cached_profile_ = profile_manager->GetProfileByPath( |
68 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); | 151 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); |
69 if (cached_profile_) { | 152 if (cached_profile_) { |
70 // Ensure that the returned profile is not an incognito profile. | 153 // Ensure that the returned profile is not an incognito profile. |
71 cached_profile_ = cached_profile_->GetOriginalProfile(); | 154 cached_profile_ = cached_profile_->GetOriginalProfile(); |
72 } | 155 } |
73 return cached_profile_; | 156 return cached_profile_; |
74 } | 157 } |
75 | 158 |
76 scoped_ptr<extensions::ExtensionSet> | 159 scoped_ptr<extensions::ExtensionSet> |
77 ExtensionsMetricsProvider::GetInstalledExtensions() { | 160 ExtensionsMetricsProvider::GetInstalledExtensions(Profile* profile) { |
78 #if defined(ENABLE_EXTENSIONS) | 161 #if defined(ENABLE_EXTENSIONS) |
79 // UMA reports do not support multiple profiles, but extensions are installed | |
80 // per-profile. We return the extensions installed in the primary profile. | |
81 // In the future, we might consider reporting data about extensions in all | |
82 // profiles. | |
83 Profile* profile = GetMetricsProfile(); | |
84 if (profile) { | 162 if (profile) { |
85 return extensions::ExtensionRegistry::Get(profile) | 163 return extensions::ExtensionRegistry::Get(profile) |
86 ->GenerateInstalledExtensionsSet(); | 164 ->GenerateInstalledExtensionsSet(); |
87 } | 165 } |
88 #endif // defined(ENABLE_EXTENSIONS) | 166 #endif // defined(ENABLE_EXTENSIONS) |
89 return scoped_ptr<extensions::ExtensionSet>(); | 167 return scoped_ptr<extensions::ExtensionSet>(); |
90 } | 168 } |
91 | 169 |
92 uint64 ExtensionsMetricsProvider::GetClientID() { | 170 uint64 ExtensionsMetricsProvider::GetClientID() { |
93 // TODO(blundell): Create a MetricsLog::ClientIDAsInt() API and call it | 171 // TODO(blundell): Create a MetricsLog::ClientIDAsInt() API and call it |
94 // here as well as in MetricsLog's population of the client_id field of | 172 // here as well as in MetricsLog's population of the client_id field of |
95 // the uma_proto. | 173 // the uma_proto. |
96 return MetricsLog::Hash(metrics_state_manager_->client_id()); | 174 return MetricsLog::Hash(metrics_state_manager_->client_id()); |
97 } | 175 } |
98 | 176 |
99 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics( | 177 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics( |
100 metrics::SystemProfileProto* system_profile) { | 178 metrics::SystemProfileProto* system_profile) { |
101 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions()); | 179 ProvideOffStoreMetric(system_profile); |
180 ProvideOccupiedBucketMetric(system_profile); | |
181 } | |
182 | |
183 void ExtensionsMetricsProvider::ProvideOffStoreMetric( | |
184 metrics::SystemProfileProto* system_profile) { | |
185 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
186 if (!profile_manager) | |
187 return; | |
188 | |
189 ExtensionState state = NO_EXTENSIONS; | |
190 | |
191 // The off-store metric includes information from all loaded profiles at the | |
192 // time when this metric is generated. | |
193 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | |
194 for (size_t i = 0u; i < profiles.size() && state < OFF_STORE; ++i) { | |
195 extensions::InstallVerifier* verifier = | |
196 extensions::ExtensionSystem::Get(profiles[i])->install_verifier(); | |
197 | |
198 scoped_ptr<extensions::ExtensionSet> extensions( | |
199 GetInstalledExtensions(profiles[i])); | |
200 | |
201 // Combine the state from each profile, always favoring the higher state as | |
202 // defined by the order of ExtensionState. | |
203 state = std::max(state, CheckForOffStore(*extensions.get(), *verifier)); | |
Devlin
2014/08/13 19:50:07
Do we not need to check |extensions| for NULL-ness
jwd
2014/08/13 20:22:06
Done.
| |
204 } | |
205 | |
206 system_profile->set_offstore_extensions_state(ExtensionStateAsProto(state)); | |
207 } | |
208 | |
209 void ExtensionsMetricsProvider::ProvideOccupiedBucketMetric( | |
210 metrics::SystemProfileProto* system_profile) { | |
211 // UMA reports do not support multiple profiles, but extensions are installed | |
212 // per-profile. We return the extensions installed in the primary profile. | |
213 // In the future, we might consider reporting data about extensions in all | |
214 // profiles. | |
215 Profile* profile = GetMetricsProfile(); | |
216 | |
217 scoped_ptr<extensions::ExtensionSet> extensions( | |
218 GetInstalledExtensions(profile)); | |
102 if (!extensions) | 219 if (!extensions) |
103 return; | 220 return; |
104 | 221 |
105 const int client_key = GetClientID() % kExtensionListClientKeys; | 222 const int client_key = GetClientID() % kExtensionListClientKeys; |
106 | 223 |
107 std::set<int> buckets; | 224 std::set<int> buckets; |
108 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); | 225 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); |
109 it != extensions->end(); | 226 it != extensions->end(); |
110 ++it) { | 227 ++it) { |
111 buckets.insert(HashExtension((*it)->id(), client_key)); | 228 buckets.insert(HashExtension((*it)->id(), client_key)); |
112 } | 229 } |
113 | 230 |
114 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end(); | 231 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end(); |
115 ++it) { | 232 ++it) { |
116 system_profile->add_occupied_extension_bucket(*it); | 233 system_profile->add_occupied_extension_bucket(*it); |
117 } | 234 } |
118 } | 235 } |
OLD | NEW |