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 importante, | |
Alexei Svitkine (slow)
2014/08/12 19:17:13
Nit: important
jwd
2014/08/12 21:24:16
Done.
| |
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 enum ExtensionState { | |
41 NO_EXTENSIONS, | |
42 FROM_STORE_VERIFIED, | |
43 FROM_STORE_UNVERIFIED, | |
44 OFF_STORE | |
45 }; | |
46 | |
47 metrics::SystemProfileProto::ExtensionsState ExtensionStateAsProto( | |
48 ExtensionState value) { | |
49 switch (value) { | |
50 case NO_EXTENSIONS: | |
51 return metrics::SystemProfileProto::NO_EXTENSIONS; | |
52 case FROM_STORE_VERIFIED: | |
53 return metrics::SystemProfileProto::NO_OFFSTORE_VERIFIED; | |
54 case FROM_STORE_UNVERIFIED: | |
55 return metrics::SystemProfileProto::NO_OFFSTORE_UNVERIFIED; | |
56 case OFF_STORE: | |
57 return metrics::SystemProfileProto::HAS_OFFSTORE; | |
58 } | |
59 NOTREACHED(); | |
60 return metrics::SystemProfileProto::NO_EXTENSIONS; | |
61 } | |
62 | |
63 // Determines if the |extension| is an extension (can use extension APIs) and is | |
64 // not from the webstore. If local information claims the extension is from the | |
65 // webstore, we attempt to verify with |verifier| by checking if it has been | |
66 // explicitly deemed invalid. If |verifier| is inactive or if the extension is | |
67 // unknown to |verifier|, the local information is trusted. | |
68 ExtensionState IsOffStoreExtension( | |
69 const extensions::Extension& extension, | |
70 const extensions::InstallVerifier& verifier) { | |
71 if (!extension.is_extension() && !extension.is_legacy_packaged_app()) | |
72 return NO_EXTENSIONS; | |
73 | |
74 // Component extensions are considered safe. | |
75 if (extension.location() == extensions::Manifest::COMPONENT || | |
76 extension.location() == extensions::Manifest::EXTERNAL_COMPONENT) { | |
77 return NO_EXTENSIONS; | |
78 } | |
79 | |
80 if (verifier.AllowedByEnterprisePolicy(extension.id())) | |
81 return NO_EXTENSIONS; | |
82 | |
83 if (!extensions::InstallVerifier::FromStore(extension)) | |
84 return OFF_STORE; | |
85 | |
86 // Local information about the extension implies it is from the store. We try | |
87 // to use the install verifier to vereify this. | |
88 if (!verifier.IsKnownId(extension.id())) | |
89 return FROM_STORE_UNVERIFIED; | |
90 | |
91 if (verifier.IsInvalid(extension.id())) | |
92 return OFF_STORE; | |
93 | |
94 return FROM_STORE_VERIFIED; | |
95 } | |
96 | |
97 ExtensionState CheckForOffStore(const extensions::ExtensionSet& extensions, | |
Alexei Svitkine (slow)
2014/08/12 19:17:13
Nit: Add a comment.
jwd
2014/08/12 21:24:16
Done.
| |
98 const extensions::InstallVerifier& verifier) { | |
99 ExtensionState extensions_state = NO_EXTENSIONS; | |
100 for (extensions::ExtensionSet::const_iterator it = extensions.begin(); | |
101 it != extensions.end(); | |
Alexei Svitkine (slow)
2014/08/12 19:17:13
Nit: Can add && state <= OFF_STORE, so that we can
jwd
2014/08/12 21:24:17
Done.
| |
102 ++it) { | |
103 // Combine the state of each extension, always favoring the higher state as | |
104 // defined by the order of ExtensionState. | |
105 extensions_state = | |
106 std::max(extensions_state, IsOffStoreExtension(**it, verifier)); | |
107 } | |
108 return extensions_state; | |
109 } | |
110 | |
32 } // namespace | 111 } // namespace |
33 | 112 |
34 ExtensionsMetricsProvider::ExtensionsMetricsProvider( | 113 ExtensionsMetricsProvider::ExtensionsMetricsProvider( |
35 metrics::MetricsStateManager* metrics_state_manager) | 114 metrics::MetricsStateManager* metrics_state_manager) |
36 : metrics_state_manager_(metrics_state_manager), cached_profile_(NULL) { | 115 : metrics_state_manager_(metrics_state_manager), cached_profile_(NULL) { |
37 DCHECK(metrics_state_manager_); | 116 DCHECK(metrics_state_manager_); |
38 } | 117 } |
39 | 118 |
40 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() { | 119 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() { |
41 } | 120 } |
(...skipping 25 matching lines...) Expand all Loading... | |
67 cached_profile_ = profile_manager->GetProfileByPath( | 146 cached_profile_ = profile_manager->GetProfileByPath( |
68 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); | 147 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); |
69 if (cached_profile_) { | 148 if (cached_profile_) { |
70 // Ensure that the returned profile is not an incognito profile. | 149 // Ensure that the returned profile is not an incognito profile. |
71 cached_profile_ = cached_profile_->GetOriginalProfile(); | 150 cached_profile_ = cached_profile_->GetOriginalProfile(); |
72 } | 151 } |
73 return cached_profile_; | 152 return cached_profile_; |
74 } | 153 } |
75 | 154 |
76 scoped_ptr<extensions::ExtensionSet> | 155 scoped_ptr<extensions::ExtensionSet> |
77 ExtensionsMetricsProvider::GetInstalledExtensions() { | 156 ExtensionsMetricsProvider::GetInstalledExtensions(Profile* profile) { |
78 #if defined(ENABLE_EXTENSIONS) | 157 #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) { | 158 if (profile) { |
85 return extensions::ExtensionRegistry::Get(profile) | 159 return extensions::ExtensionRegistry::Get(profile) |
86 ->GenerateInstalledExtensionsSet(); | 160 ->GenerateInstalledExtensionsSet(); |
87 } | 161 } |
88 #endif // defined(ENABLE_EXTENSIONS) | 162 #endif // defined(ENABLE_EXTENSIONS) |
89 return scoped_ptr<extensions::ExtensionSet>(); | 163 return scoped_ptr<extensions::ExtensionSet>(); |
90 } | 164 } |
91 | 165 |
92 uint64 ExtensionsMetricsProvider::GetClientID() { | 166 uint64 ExtensionsMetricsProvider::GetClientID() { |
93 // TODO(blundell): Create a MetricsLog::ClientIDAsInt() API and call it | 167 // 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 | 168 // here as well as in MetricsLog's population of the client_id field of |
95 // the uma_proto. | 169 // the uma_proto. |
96 return MetricsLog::Hash(metrics_state_manager_->client_id()); | 170 return MetricsLog::Hash(metrics_state_manager_->client_id()); |
97 } | 171 } |
98 | 172 |
99 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics( | 173 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics( |
100 metrics::SystemProfileProto* system_profile) { | 174 metrics::SystemProfileProto* system_profile) { |
101 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions()); | 175 ProvideOffStoreMetric(system_profile); |
176 ProvideOccupiedBucketMetric(system_profile); | |
177 } | |
178 | |
179 void ExtensionsMetricsProvider::ProvideOffStoreMetric( | |
180 metrics::SystemProfileProto* system_profile) { | |
181 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
182 if (!profile_manager) | |
183 return; | |
184 | |
185 ExtensionState extensions_state = NO_EXTENSIONS; | |
186 | |
187 // The off store metric includes information from all loaded profiles at the | |
188 // time when this metric is generated. | |
189 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); | |
190 for (size_t i = 0; i < profiles.size(); ++i) { | |
191 extensions::InstallVerifier* verifier = | |
Alexei Svitkine (slow)
2014/08/12 19:17:13
Does all this code build on iOS and Android or do
jwd
2014/08/12 21:24:17
Done.
| |
192 extensions::ExtensionSystem::Get(profiles[i])->install_verifier(); | |
193 | |
194 scoped_ptr<extensions::ExtensionSet> extensions( | |
195 GetInstalledExtensions(profiles[i])); | |
196 | |
197 // Combine the state from each profile, always favoring the higher state as | |
198 // defined by the order of ExtensionState. | |
199 extensions_state = std::max(extensions_state, | |
200 CheckForOffStore(*extensions.get(), *verifier)); | |
201 } | |
202 | |
203 system_profile->set_offstore_extensions_state( | |
Alexei Svitkine (slow)
2014/08/12 19:17:13
Probably we shouldn't bother setting this on platf
jwd
2014/08/12 21:24:17
Done.
| |
204 ExtensionStateAsProto(extensions_state)); | |
205 } | |
206 | |
207 void ExtensionsMetricsProvider::ProvideOccupiedBucketMetric( | |
208 metrics::SystemProfileProto* system_profile) { | |
209 // UMA reports do not support multiple profiles, but extensions are installed | |
210 // per-profile. We return the extensions installed in the primary profile. | |
211 // In the future, we might consider reporting data about extensions in all | |
212 // profiles. | |
213 Profile* profile = GetMetricsProfile(); | |
214 | |
215 scoped_ptr<extensions::ExtensionSet> extensions( | |
216 GetInstalledExtensions(profile)); | |
102 if (!extensions) | 217 if (!extensions) |
103 return; | 218 return; |
104 | 219 |
105 const int client_key = GetClientID() % kExtensionListClientKeys; | 220 const int client_key = GetClientID() % kExtensionListClientKeys; |
106 | 221 |
107 std::set<int> buckets; | 222 std::set<int> buckets; |
108 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); | 223 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); |
109 it != extensions->end(); | 224 it != extensions->end(); |
110 ++it) { | 225 ++it) { |
111 buckets.insert(HashExtension((*it)->id(), client_key)); | 226 buckets.insert(HashExtension((*it)->id(), client_key)); |
112 } | 227 } |
113 | 228 |
114 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end(); | 229 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end(); |
115 ++it) { | 230 ++it) { |
116 system_profile->add_occupied_extension_bucket(*it); | 231 system_profile->add_occupied_extension_bucket(*it); |
117 } | 232 } |
118 } | 233 } |
OLD | NEW |