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 <stdint.h> 9 10 #include <memory> 11 #include <string> 12 #include <vector> 13 14 #include "base/time/time.h" 15 #include "chrome/browser/metrics/cached_metrics_profile.h" 16 #include "components/metrics/metrics_provider.h" 17 #include "third_party/metrics_proto/extension_install.pb.h" 18 19 class Profile; 20 21 namespace extensions { 22 class Extension; 23 class ExtensionPrefs; 24 class ExtensionSet; 25 } 26 27 namespace metrics { 28 class MetricsStateManager; 29 class SystemProfileProto; 30 } 31 32 // ExtensionsMetricsProvider groups various constants and functions used for 33 // reporting extension IDs with UMA reports (after hashing the extension IDs 34 // for privacy). 35 class ExtensionsMetricsProvider : public metrics::MetricsProvider { 36 public: 37 // Holds on to |metrics_state_manager|, which must outlive this object, as a 38 // weak pointer. 39 explicit ExtensionsMetricsProvider( 40 metrics::MetricsStateManager* metrics_state_manager); 41 ExtensionsMetricsProvider(const ExtensionsMetricsProvider&) = delete; 42 ExtensionsMetricsProvider& operator=(const ExtensionsMetricsProvider&) = 43 delete; 44 ~ExtensionsMetricsProvider() override; 45 46 // metrics::MetricsProvider: 47 void ProvideSystemProfileMetrics( 48 metrics::SystemProfileProto* system_profile) override; 49 50 static metrics::ExtensionInstallProto ConstructInstallProtoForTesting( 51 const extensions::Extension& extension, 52 extensions::ExtensionPrefs* prefs, 53 base::Time last_sample_time); 54 static std::vector<metrics::ExtensionInstallProto> 55 GetInstallsForProfileForTesting(Profile* profile, 56 base::Time last_sample_time); 57 58 protected: 59 // Exposed for the sake of mocking in test code. 60 61 // Retrieves the set of extensions installed in the given |profile|. 62 virtual std::unique_ptr<extensions::ExtensionSet> GetInstalledExtensions( 63 Profile* profile); 64 65 // Retrieves the client ID. 66 virtual uint64_t GetClientID() const; 67 68 // Hashes the extension extension ID using the provided client key (which 69 // must be less than kExtensionListClientKeys) and to produce an output value 70 // between 0 and kExtensionListBuckets-1. 71 static int HashExtension(const std::string& extension_id, 72 uint32_t client_key); 73 74 private: 75 // Writes whether any loaded profiles have extensions not from the webstore. 76 void ProvideOffStoreMetric(metrics::SystemProfileProto* system_profile); 77 78 // Writes the hashed list of installed extensions into the specified 79 // SystemProfileProto object. 80 void ProvideOccupiedBucketMetric(metrics::SystemProfileProto* system_profile); 81 82 // Writes information about the installed extensions for all profiles into 83 // the proto. 84 void ProvideExtensionInstallsMetrics( 85 metrics::SystemProfileProto* system_profile); 86 87 // The MetricsStateManager from which the client ID is obtained. 88 metrics::MetricsStateManager* metrics_state_manager_; 89 90 metrics::CachedMetricsProfile cached_profile_; 91 92 // The time of our last recorded sample. 93 base::Time last_sample_time_; 94 }; 95 96 #endif // CHROME_BROWSER_METRICS_EXTENSIONS_METRICS_PROVIDER_H_ 97