This source file includes following definitions.
- cached_profile_
- HashExtension
- GetMetricsProfile
- GetInstalledExtensions
- WriteExtensionList
#include "chrome/browser/metrics/extension_metrics.h"
#include <set>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/metrics/proto/system_profile.pb.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_set.h"
uint64 CityHash64(const char *buf, size_t len);
namespace {
const size_t kExtensionListClientKeys = 4096;
const size_t kExtensionListBuckets = 1024;
}
HashedExtensionMetrics::HashedExtensionMetrics(uint64 client_id)
: client_key_(client_id % kExtensionListClientKeys),
cached_profile_(NULL) {}
HashedExtensionMetrics::~HashedExtensionMetrics() {}
int HashedExtensionMetrics::HashExtension(const std::string& extension_id,
uint32 client_key) {
DCHECK_LE(client_key, kExtensionListClientKeys);
std::string message =
base::StringPrintf("%u:%s", client_key, extension_id.c_str());
uint64 output = CityHash64(message.data(), message.size());
return output % kExtensionListBuckets;
}
Profile* HashedExtensionMetrics::GetMetricsProfile() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
if (!profile_manager)
return NULL;
if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_))
return cached_profile_;
cached_profile_ = ProfileManager::GetLastUsedProfile();
if (cached_profile_) {
cached_profile_ = cached_profile_->GetOriginalProfile();
}
return cached_profile_;
}
scoped_ptr<extensions::ExtensionSet>
HashedExtensionMetrics::GetInstalledExtensions() {
#if defined(ENABLE_EXTENSIONS)
Profile* profile = GetMetricsProfile();
if (profile) {
return extensions::ExtensionRegistry::Get(profile)
->GenerateInstalledExtensionsSet();
}
#endif
return scoped_ptr<extensions::ExtensionSet>();
}
void HashedExtensionMetrics::WriteExtensionList(
metrics::SystemProfileProto* system_profile) {
scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions());
if (!extensions)
return;
std::set<int> buckets;
for (extensions::ExtensionSet::const_iterator it = extensions->begin();
it != extensions->end(); ++it) {
buckets.insert(HashExtension((*it)->id(), client_key_));
}
for (std::set<int>::const_iterator it = buckets.begin();
it != buckets.end(); ++it) {
system_profile->add_occupied_extension_bucket(*it);
}
}