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/extension_metrics.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 "chrome/common/metrics/proto/system_profile.pb.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/common/extension_set.h"
18 // From third_party/smhasher/src/City.h; that file can't be included here due
19 // to macro redefinitions (of UINT8_C, etc.) on Windows.
20 // TODO(mvrable): Clean up City.h so it can be included directly.
21 uint64 CityHash64(const char *buf, size_t len);
25 // The number of possible hash keys that a client may use. The UMA client_id
26 // value is reduced modulo this value to produce the key used by that
28 const size_t kExtensionListClientKeys = 4096;
30 // The number of hash buckets into which extension IDs are mapped. This sets
31 // the possible output range of the HashExtension function.
32 const size_t kExtensionListBuckets = 1024;
36 HashedExtensionMetrics::HashedExtensionMetrics(uint64 client_id)
37 : client_key_(client_id % kExtensionListClientKeys),
38 cached_profile_(NULL) {}
39 HashedExtensionMetrics::~HashedExtensionMetrics() {}
42 int HashedExtensionMetrics::HashExtension(const std::string& extension_id,
44 DCHECK_LE(client_key, kExtensionListClientKeys);
46 base::StringPrintf("%u:%s", client_key, extension_id.c_str());
47 uint64 output = CityHash64(message.data(), message.size());
48 return output % kExtensionListBuckets;
51 Profile* HashedExtensionMetrics::GetMetricsProfile() {
52 ProfileManager* profile_manager = g_browser_process->profile_manager();
56 // If there is a cached profile, reuse that. However, check that it is still
58 if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_))
59 return cached_profile_;
61 // Find a suitable profile to use, and cache it so that we continue to report
62 // statistics on the same profile. We would simply use
63 // ProfileManager::GetLastUsedProfile(), except that that has the side effect
64 // of creating a profile if it does not yet exist.
65 cached_profile_ = profile_manager->GetProfileByPath(
66 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir()));
67 if (cached_profile_) {
68 // Ensure that the returned profile is not an incognito profile.
69 cached_profile_ = cached_profile_->GetOriginalProfile();
71 return cached_profile_;
74 scoped_ptr<extensions::ExtensionSet>
75 HashedExtensionMetrics::GetInstalledExtensions() {
76 #if defined(ENABLE_EXTENSIONS)
77 // UMA reports do not support multiple profiles, but extensions are installed
78 // per-profile. We return the extensions installed in the primary profile.
79 // In the future, we might consider reporting data about extensions in all
81 Profile* profile = GetMetricsProfile();
83 return extensions::ExtensionRegistry::Get(profile)
84 ->GenerateInstalledExtensionsSet();
86 #endif // defined(ENABLE_EXTENSIONS)
87 return scoped_ptr<extensions::ExtensionSet>();
90 void HashedExtensionMetrics::WriteExtensionList(
91 metrics::SystemProfileProto* system_profile) {
92 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions());
96 std::set<int> buckets;
97 for (extensions::ExtensionSet::const_iterator it = extensions->begin();
98 it != extensions->end(); ++it) {
99 buckets.insert(HashExtension((*it)->id(), client_key_));
102 for (std::set<int>::const_iterator it = buckets.begin();
103 it != buckets.end(); ++it) {
104 system_profile->add_occupied_extension_bucket(*it);