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