| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/metrics/extension_metrics.h" | |
| 6 | |
| 7 #include <set> | |
| 8 | |
| 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/proto/system_profile.pb.h" | |
| 15 #include "extensions/browser/extension_registry.h" | |
| 16 #include "extensions/common/extension_set.h" | |
| 17 #include "third_party/smhasher/src/City.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // The number of possible hash keys that a client may use. The UMA client_id | |
| 22 // value is reduced modulo this value to produce the key used by that | |
| 23 // particular client. | |
| 24 const size_t kExtensionListClientKeys = 4096; | |
| 25 | |
| 26 // The number of hash buckets into which extension IDs are mapped. This sets | |
| 27 // the possible output range of the HashExtension function. | |
| 28 const size_t kExtensionListBuckets = 1024; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 HashedExtensionMetrics::HashedExtensionMetrics(uint64 client_id) | |
| 33 : client_key_(client_id % kExtensionListClientKeys), | |
| 34 cached_profile_(NULL) {} | |
| 35 HashedExtensionMetrics::~HashedExtensionMetrics() {} | |
| 36 | |
| 37 // static | |
| 38 int HashedExtensionMetrics::HashExtension(const std::string& extension_id, | |
| 39 uint32 client_key) { | |
| 40 DCHECK_LE(client_key, kExtensionListClientKeys); | |
| 41 std::string message = | |
| 42 base::StringPrintf("%u:%s", client_key, extension_id.c_str()); | |
| 43 uint64 output = CityHash64(message.data(), message.size()); | |
| 44 return output % kExtensionListBuckets; | |
| 45 } | |
| 46 | |
| 47 Profile* HashedExtensionMetrics::GetMetricsProfile() { | |
| 48 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 49 if (!profile_manager) | |
| 50 return NULL; | |
| 51 | |
| 52 // If there is a cached profile, reuse that. However, check that it is still | |
| 53 // valid first. | |
| 54 if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_)) | |
| 55 return cached_profile_; | |
| 56 | |
| 57 // Find a suitable profile to use, and cache it so that we continue to report | |
| 58 // statistics on the same profile. We would simply use | |
| 59 // ProfileManager::GetLastUsedProfile(), except that that has the side effect | |
| 60 // of creating a profile if it does not yet exist. | |
| 61 cached_profile_ = profile_manager->GetProfileByPath( | |
| 62 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); | |
| 63 if (cached_profile_) { | |
| 64 // Ensure that the returned profile is not an incognito profile. | |
| 65 cached_profile_ = cached_profile_->GetOriginalProfile(); | |
| 66 } | |
| 67 return cached_profile_; | |
| 68 } | |
| 69 | |
| 70 scoped_ptr<extensions::ExtensionSet> | |
| 71 HashedExtensionMetrics::GetInstalledExtensions() { | |
| 72 #if defined(ENABLE_EXTENSIONS) | |
| 73 // UMA reports do not support multiple profiles, but extensions are installed | |
| 74 // per-profile. We return the extensions installed in the primary profile. | |
| 75 // In the future, we might consider reporting data about extensions in all | |
| 76 // profiles. | |
| 77 Profile* profile = GetMetricsProfile(); | |
| 78 if (profile) { | |
| 79 return extensions::ExtensionRegistry::Get(profile) | |
| 80 ->GenerateInstalledExtensionsSet(); | |
| 81 } | |
| 82 #endif // defined(ENABLE_EXTENSIONS) | |
| 83 return scoped_ptr<extensions::ExtensionSet>(); | |
| 84 } | |
| 85 | |
| 86 void HashedExtensionMetrics::WriteExtensionList( | |
| 87 metrics::SystemProfileProto* system_profile) { | |
| 88 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions()); | |
| 89 if (!extensions) | |
| 90 return; | |
| 91 | |
| 92 std::set<int> buckets; | |
| 93 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); | |
| 94 it != extensions->end(); ++it) { | |
| 95 buckets.insert(HashExtension((*it)->id(), client_key_)); | |
| 96 } | |
| 97 | |
| 98 for (std::set<int>::const_iterator it = buckets.begin(); | |
| 99 it != buckets.end(); ++it) { | |
| 100 system_profile->add_occupied_extension_bucket(*it); | |
| 101 } | |
| 102 } | |
| OLD | NEW |