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
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/extensions_metrics_provider.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "components/metrics/metrics_log.h"
15 #include "components/metrics/metrics_state_manager.h"
16 #include "components/metrics/proto/system_profile.pb.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/extension_set.h"
19 #include "third_party/smhasher/src/City.h"
23 // 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 const size_t kExtensionListClientKeys = 4096;
28 // The number of hash buckets into which extension IDs are mapped. This sets
29 // the possible output range of the HashExtension function.
30 const size_t kExtensionListBuckets = 1024;
34 ExtensionsMetricsProvider::ExtensionsMetricsProvider(
35 metrics::MetricsStateManager* metrics_state_manager)
36 : metrics_state_manager_(metrics_state_manager), cached_profile_(NULL) {
37 DCHECK(metrics_state_manager_);
40 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() {
44 int ExtensionsMetricsProvider::HashExtension(const std::string& extension_id,
46 DCHECK_LE(client_key, kExtensionListClientKeys);
48 base::StringPrintf("%u:%s", client_key, extension_id.c_str());
49 uint64 output = CityHash64(message.data(), message.size());
50 return output % kExtensionListBuckets;
53 Profile* ExtensionsMetricsProvider::GetMetricsProfile() {
54 ProfileManager* profile_manager = g_browser_process->profile_manager();
58 // If there is a cached profile, reuse that. However, check that it is still
60 if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_))
61 return cached_profile_;
63 // Find a suitable profile to use, and cache it so that we continue to report
64 // statistics on the same profile. We would simply use
65 // ProfileManager::GetLastUsedProfile(), except that that has the side effect
66 // of creating a profile if it does not yet exist.
67 cached_profile_ = profile_manager->GetProfileByPath(
68 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()));
69 if (cached_profile_) {
70 // Ensure that the returned profile is not an incognito profile.
71 cached_profile_ = cached_profile_->GetOriginalProfile();
73 return cached_profile_;
76 scoped_ptr<extensions::ExtensionSet>
77 ExtensionsMetricsProvider::GetInstalledExtensions() {
78 #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
83 Profile* profile = GetMetricsProfile();
85 return extensions::ExtensionRegistry::Get(profile)
86 ->GenerateInstalledExtensionsSet();
88 #endif // defined(ENABLE_EXTENSIONS)
89 return scoped_ptr<extensions::ExtensionSet>();
92 uint64 ExtensionsMetricsProvider::GetClientID() {
93 // 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
96 return MetricsLog::Hash(metrics_state_manager_->client_id());
99 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics(
100 metrics::SystemProfileProto* system_profile) {
101 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions());
105 const int client_key = GetClientID() % kExtensionListClientKeys;
107 std::set<int> buckets;
108 for (extensions::ExtensionSet::const_iterator it = extensions->begin();
109 it != extensions->end();
111 buckets.insert(HashExtension((*it)->id(), client_key));
114 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end();
116 system_profile->add_occupied_extension_bucket(*it);