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  virtual void ProvideSystemProfileMetrics(
38      metrics::SystemProfileProto* system_profile) OVERRIDE;
39
40 protected:
41  // Exposed for the sake of mocking in test code.
42
43  // Retrieves the set of extensions installed in the given |profile|.
44  virtual scoped_ptr<extensions::ExtensionSet> GetInstalledExtensions(
45      Profile* profile);
46
47  // Retrieves the client ID.
48  virtual uint64 GetClientID();
49
50  // Hashes the extension extension ID using the provided client key (which
51  // must be less than kExtensionListClientKeys) and to produce an output value
52  // between 0 and kExtensionListBuckets-1.
53  static int HashExtension(const std::string& extension_id, uint32 client_key);
54
55 private:
56  // Returns the profile for which extensions will be gathered.  Once a
57  // suitable profile has been found, future calls will continue to return the
58  // same value so that reported extensions are consistent.
59  Profile* GetMetricsProfile();
60
61  // Writes whether any loaded profiles have extensions not from the webstore.
62  void ProvideOffStoreMetric(metrics::SystemProfileProto* system_profile);
63
64  // Writes the hashed list of installed extensions into the specified
65  // SystemProfileProto object.
66  void ProvideOccupiedBucketMetric(metrics::SystemProfileProto* system_profile);
67
68  // The MetricsStateManager from which the client ID is obtained.
69  metrics::MetricsStateManager* metrics_state_manager_;
70
71  // The profile for which extensions are gathered for the occupied bucket
72  // metric. Once a profile is found its value is cached here so that
73  // GetMetricsProfile() can return a consistent value.
74  Profile* cached_profile_;
75
76  DISALLOW_COPY_AND_ASSIGN(ExtensionsMetricsProvider);
77};
78
79#endif  // CHROME_BROWSER_METRICS_EXTENSIONS_METRICS_PROVIDER_H_
80