extensions_metrics_provider.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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#ifndef CHROME_BROWSER_METRICS_EXTENSIONS_METRICS_PROVIDER_H_
6#define CHROME_BROWSER_METRICS_EXTENSIONS_METRICS_PROVIDER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "components/metrics/metrics_provider.h"
13
14class Profile;
15
16namespace extensions {
17class ExtensionSet;
18}
19
20namespace metrics {
21class MetricsStateManager;
22class SystemProfileProto;
23}
24
25// ExtensionsMetricsProvider groups various constants and functions used for
26// reporting extension IDs with UMA reports (after hashing the extension IDs
27// for privacy).
28class ExtensionsMetricsProvider : public metrics::MetricsProvider {
29 public:
30  // Holds on to |metrics_state_manager|, which must outlive this object, as a
31  // weak pointer.
32  explicit ExtensionsMetricsProvider(
33      metrics::MetricsStateManager* metrics_state_manager);
34  virtual ~ExtensionsMetricsProvider();
35
36  // metrics::MetricsProvider:
37
38  // Writes the hashed list of installed extensions into the specified
39  // SystemProfileProto object.
40  virtual void ProvideSystemProfileMetrics(
41      metrics::SystemProfileProto* system_profile) OVERRIDE;
42
43 protected:
44  // Exposed for the sake of mocking in test code.
45
46  // Retrieves the set of extensions installed in the current profile.
47  // TODO(mvrable): If metrics are ever converted to being per-profile, then
48  // this should be updated to return extensions installed in a specified
49  // profile.
50  virtual scoped_ptr<extensions::ExtensionSet> GetInstalledExtensions();
51
52  // Retrieves the client ID.
53  virtual uint64 GetClientID();
54
55  // Hashes the extension extension ID using the provided client key (which
56  // must be less than kExtensionListClientKeys) and to produce an output value
57  // between 0 and kExtensionListBuckets-1.
58  static int HashExtension(const std::string& extension_id, uint32 client_key);
59
60 private:
61  // Returns the profile for which extensions will be gathered.  Once a
62  // suitable profile has been found, future calls will continue to return the
63  // same value so that reported extensions are consistent.
64  Profile* GetMetricsProfile();
65
66  // The MetricsStateManager from which the client ID is obtained.
67  metrics::MetricsStateManager* metrics_state_manager_;
68
69  // The profile for which extensions are gathered.  Once a profile is found
70  // its value is cached here so that GetMetricsProfile() can return a
71  // consistent value.
72  Profile* cached_profile_;
73
74  DISALLOW_COPY_AND_ASSIGN(ExtensionsMetricsProvider);
75};
76
77#endif  // CHROME_BROWSER_METRICS_EXTENSIONS_METRICS_PROVIDER_H_
78