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