1 // Copyright 2020 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_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_METRICS_H_
6 #define CHROME_BROWSER_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_METRICS_H_
7 
8 #include "base/scoped_observer.h"
9 #include "base/time/time.h"
10 #include "base/timer/timer.h"
11 #include "chrome/browser/extensions/forced_extensions/force_installed_tracker.h"
12 #include "chrome/browser/extensions/forced_extensions/install_stage_tracker.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/updater/extension_downloader_delegate.h"
15 #include "extensions/common/extension.h"
16 
17 class Profile;
18 
19 namespace extensions {
20 
21 // Used to report force-installed extension stats to UMA.
22 // ExtensionService owns this class and outlives it.
23 class ForceInstalledMetrics : public ForceInstalledTracker::Observer {
24  public:
25   ForceInstalledMetrics(ExtensionRegistry* registry,
26                         Profile* profile,
27                         ForceInstalledTracker* tracker,
28                         std::unique_ptr<base::OneShotTimer> timer =
29                             std::make_unique<base::OneShotTimer>());
30 
31   ForceInstalledMetrics(const ForceInstalledMetrics&) = delete;
32   ForceInstalledMetrics& operator=(const ForceInstalledMetrics&) = delete;
33 
34   ~ForceInstalledMetrics() override;
35 
36   // Note: enum used for UMA. Do NOT reorder or remove entries. Don't forget to
37   // update enums.xml (name: SessionType) when adding new
38   // entries.
39   // Type of session for current user. This enum is required as UserType enum
40   // doesn't support new regular users. See user_manager::UserType enum for
41   // description of session types other than new and existing regular users.
42   enum class UserType {
43     // Session with Regular existing user, which has a user name and password.
44     USER_TYPE_REGULAR_EXISTING = 0,
45     USER_TYPE_GUEST = 1,
46     // Session with Regular new user, which has a user name and password.
47     USER_TYPE_REGULAR_NEW = 2,
48     USER_TYPE_PUBLIC_ACCOUNT = 3,
49     USER_TYPE_SUPERVISED = 4,
50     USER_TYPE_KIOSK_APP = 5,
51     USER_TYPE_CHILD = 6,
52     USER_TYPE_ARC_KIOSK_APP = 7,
53     USER_TYPE_ACTIVE_DIRECTORY = 8,
54     USER_TYPE_WEB_KIOSK_APP = 9,
55     // Maximum histogram value.
56     kMaxValue = USER_TYPE_WEB_KIOSK_APP
57   };
58 
59   // ForceInstalledTracker::Observer overrides:
60   //
61   // Calls ReportMetrics method if there is a non-empty list of
62   // force-installed extensions, and is responsible for cleanup of
63   // observers.
64   void OnForceInstalledExtensionsLoaded() override;
65 
66   // Calls ReportMetricsOnExtensionsReady method if there is a non-empty list of
67   // force-installed extensions.
68   void OnForceInstalledExtensionsReady() override;
69 
70   // Reports cache status for the force installed extensions.
71   void OnExtensionDownloadCacheStatusRetrieved(
72       const ExtensionId& id,
73       ExtensionDownloaderDelegate::CacheStatus cache_status) override;
74 
75  private:
76   // Returns false if the extension status corresponds to a missing extension
77   // which is not yet installed or loaded.
78   bool IsStatusGood(ForceInstalledTracker::ExtensionStatus status);
79 
80   // Reports disable reasons for the extensions which are installed but not
81   // loaded.
82   void ReportDisableReason(const ExtensionId& extension_id);
83 
84   // If |kInstallationTimeout| report time elapsed for extensions load,
85   // otherwise amount of not yet loaded extensions and reasons
86   // why they were not installed.
87   void ReportMetrics();
88 
89   // Reports metrics for sessions when all force installed extensions are ready
90   // for use.
91   void ReportMetricsOnExtensionsReady();
92 
93   ExtensionRegistry* const registry_;
94   Profile* const profile_;
95   ForceInstalledTracker* const tracker_;
96 
97   // Moment when the class was initialized.
98   base::Time start_time_;
99 
100   // Tracks whether extensions load stats were already for the session.
101   bool load_reported_ = false;
102 
103   // Tracks whether extensions ready stats were already reported for the
104   // session.
105   bool ready_reported_ = false;
106 
107   ScopedObserver<ForceInstalledTracker, ForceInstalledTracker::Observer>
108       tracker_observer_{this};
109 
110   // Tracks installation reporting timeout.
111   std::unique_ptr<base::OneShotTimer> timer_;
112 };
113 
114 }  // namespace extensions
115 
116 #endif  // CHROME_BROWSER_EXTENSIONS_FORCED_EXTENSIONS_FORCE_INSTALLED_METRICS_H_
117