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 } |
38 | |
39 ExtensionsMetricsProvider::~ExtensionsMetricsProvider() { | |
40 } | |
36 | 41 |
37 // static | 42 // static |
38 int HashedExtensionMetrics::HashExtension(const std::string& extension_id, | 43 int ExtensionsMetricsProvider::HashExtension(const std::string& extension_id, |
39 uint32 client_key) { | 44 uint32 client_key) { |
40 DCHECK_LE(client_key, kExtensionListClientKeys); | 45 DCHECK_LE(client_key, kExtensionListClientKeys); |
41 std::string message = | 46 std::string message = |
42 base::StringPrintf("%u:%s", client_key, extension_id.c_str()); | 47 base::StringPrintf("%u:%s", client_key, extension_id.c_str()); |
43 uint64 output = CityHash64(message.data(), message.size()); | 48 uint64 output = CityHash64(message.data(), message.size()); |
44 return output % kExtensionListBuckets; | 49 return output % kExtensionListBuckets; |
45 } | 50 } |
46 | 51 |
47 Profile* HashedExtensionMetrics::GetMetricsProfile() { | 52 Profile* ExtensionsMetricsProvider::GetMetricsProfile() { |
48 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 53 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
49 if (!profile_manager) | 54 if (!profile_manager) |
50 return NULL; | 55 return NULL; |
51 | 56 |
52 // If there is a cached profile, reuse that. However, check that it is still | 57 // If there is a cached profile, reuse that. However, check that it is still |
53 // valid first. | 58 // valid first. |
54 if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_)) | 59 if (cached_profile_ && profile_manager->IsValidProfile(cached_profile_)) |
55 return cached_profile_; | 60 return cached_profile_; |
56 | 61 |
57 // Find a suitable profile to use, and cache it so that we continue to report | 62 // 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 | 63 // statistics on the same profile. We would simply use |
59 // ProfileManager::GetLastUsedProfile(), except that that has the side effect | 64 // ProfileManager::GetLastUsedProfile(), except that that has the side effect |
60 // of creating a profile if it does not yet exist. | 65 // of creating a profile if it does not yet exist. |
61 cached_profile_ = profile_manager->GetProfileByPath( | 66 cached_profile_ = profile_manager->GetProfileByPath( |
62 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); | 67 profile_manager->GetLastUsedProfileDir(profile_manager->user_data_dir())); |
63 if (cached_profile_) { | 68 if (cached_profile_) { |
64 // Ensure that the returned profile is not an incognito profile. | 69 // Ensure that the returned profile is not an incognito profile. |
65 cached_profile_ = cached_profile_->GetOriginalProfile(); | 70 cached_profile_ = cached_profile_->GetOriginalProfile(); |
66 } | 71 } |
67 return cached_profile_; | 72 return cached_profile_; |
68 } | 73 } |
69 | 74 |
70 scoped_ptr<extensions::ExtensionSet> | 75 scoped_ptr<extensions::ExtensionSet> |
71 HashedExtensionMetrics::GetInstalledExtensions() { | 76 ExtensionsMetricsProvider::GetInstalledExtensions() { |
72 #if defined(ENABLE_EXTENSIONS) | 77 #if defined(ENABLE_EXTENSIONS) |
73 // UMA reports do not support multiple profiles, but extensions are installed | 78 // UMA reports do not support multiple profiles, but extensions are installed |
74 // per-profile. We return the extensions installed in the primary profile. | 79 // per-profile. We return the extensions installed in the primary profile. |
75 // In the future, we might consider reporting data about extensions in all | 80 // In the future, we might consider reporting data about extensions in all |
76 // profiles. | 81 // profiles. |
77 Profile* profile = GetMetricsProfile(); | 82 Profile* profile = GetMetricsProfile(); |
78 if (profile) { | 83 if (profile) { |
79 return extensions::ExtensionRegistry::Get(profile) | 84 return extensions::ExtensionRegistry::Get(profile) |
80 ->GenerateInstalledExtensionsSet(); | 85 ->GenerateInstalledExtensionsSet(); |
81 } | 86 } |
82 #endif // defined(ENABLE_EXTENSIONS) | 87 #endif // defined(ENABLE_EXTENSIONS) |
83 return scoped_ptr<extensions::ExtensionSet>(); | 88 return scoped_ptr<extensions::ExtensionSet>(); |
84 } | 89 } |
85 | 90 |
86 void HashedExtensionMetrics::WriteExtensionList( | 91 uint64 ExtensionsMetricsProvider::GetClientID() { |
92 return metrics::MetricsLogBase::Hash(metrics_state_manager_->client_id()); | |
blundell
2014/05/21 11:02:37
Do you think it's worth having MetricsLogBase expo
Alexei Svitkine (slow)
2014/05/21 12:23:21
I'm OK with punting this for now and putting a TOD
blundell
2014/05/21 13:03:29
Done.
| |
93 } | |
94 | |
95 void ExtensionsMetricsProvider::ProvideSystemProfileMetrics( | |
87 metrics::SystemProfileProto* system_profile) { | 96 metrics::SystemProfileProto* system_profile) { |
88 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions()); | 97 scoped_ptr<extensions::ExtensionSet> extensions(GetInstalledExtensions()); |
89 if (!extensions) | 98 if (!extensions) |
90 return; | 99 return; |
91 | 100 |
101 const int kClientKey = GetClientID() % kExtensionListClientKeys; | |
Alexei Svitkine (slow)
2014/05/21 12:23:21
Nit: kNamingConvention actually shouldn't be used
blundell
2014/05/21 13:03:29
Done.
| |
102 | |
92 std::set<int> buckets; | 103 std::set<int> buckets; |
93 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); | 104 for (extensions::ExtensionSet::const_iterator it = extensions->begin(); |
94 it != extensions->end(); ++it) { | 105 it != extensions->end(); |
95 buckets.insert(HashExtension((*it)->id(), client_key_)); | 106 ++it) { |
107 buckets.insert(HashExtension((*it)->id(), kClientKey)); | |
96 } | 108 } |
97 | 109 |
98 for (std::set<int>::const_iterator it = buckets.begin(); | 110 for (std::set<int>::const_iterator it = buckets.begin(); it != buckets.end(); |
99 it != buckets.end(); ++it) { | 111 ++it) { |
100 system_profile->add_occupied_extension_bucket(*it); | 112 system_profile->add_occupied_extension_bucket(*it); |
101 } | 113 } |
102 } | 114 } |
OLD | NEW |