1 // Copyright 2018 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_PERF_PROFILE_PROVIDER_CHROMEOS_H_
6 #define CHROME_BROWSER_METRICS_PERF_PROFILE_PROVIDER_CHROMEOS_H_
7 
8 #include <vector>
9 
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/sessions/session_restore.h"
13 #include "chromeos/dbus/power/power_manager_client.h"
14 #include "chromeos/login/login_state/login_state.h"
15 #include "content/public/browser/jank_monitor.h"
16 
17 namespace metrics {
18 
19 class MetricProvider;
20 class SampledProfile;
21 
22 // Provides access to ChromeOS profile data using different metric collectors.
23 // It detects certain system triggers, such as device resuming from suspend
24 // mode, or user logging in, which it forwards to the registered collectors.
25 class ProfileProvider : public chromeos::PowerManagerClient::Observer,
26                         public chromeos::LoginState::Observer,
27                         public content::JankMonitor::Observer {
28  public:
29   ProfileProvider();
30   ~ProfileProvider() override;
31 
32   void Init();
33 
34   // Stores collected perf data protobufs in |sampled_profiles|. Clears all the
35   // stored profile data. Returns true if it wrote to |sampled_profiles|.
36   bool GetSampledProfiles(std::vector<SampledProfile>* sampled_profiles);
37 
38  protected:
39   // Called when either the login state or the logged in user type changes.
40   // Activates the registered collectors to start collecting. Inherited from
41   // LoginState::Observer.
42   void LoggedInStateChanged() override;
43 
44   // Called when a suspend finishes. This is either a successful suspend
45   // followed by a resume, or a suspend that was canceled. Inherited from
46   // PowerManagerClient::Observer.
47   void SuspendDone(const base::TimeDelta& sleep_duration) override;
48 
49   // Called when a session restore has finished.
50   void OnSessionRestoreDone(int num_tabs_restored);
51 
52   // Called when a jank is observed by the JankMonitor. Note that these 2
53   // methods don't run on the UI thread.
54   void OnJankStarted() override;
55   void OnJankStopped() override;
56 
57   // For testing.
jank_monitor()58   scoped_refptr<content::JankMonitor> jank_monitor() const {
59     return jank_monitor_;
60   }
61   // For testing.
jankiness_collection_min_interval()62   base::TimeDelta jankiness_collection_min_interval() const {
63     return jankiness_collection_min_interval_;
64   }
65 
66   // Vector of registered metric collectors.
67   std::vector<std::unique_ptr<MetricProvider>> collectors_;
68 
69  private:
70   // Points to the on-session-restored callback that was registered with
71   // SessionRestore's callback list. When objects of this class are destroyed,
72   // the subscription object's destructor will automatically unregister the
73   // callback in SessionRestore, so that the callback list does not contain any
74   // obsolete callbacks.
75   SessionRestore::CallbackSubscription
76       on_session_restored_callback_subscription_;
77 
78   scoped_refptr<content::JankMonitor> jank_monitor_;
79 
80   // Timestamp of the most recent jank observed.
81   base::TimeTicks last_jank_start_time_;
82 
83   const base::TimeDelta jankiness_collection_min_interval_;
84 
85   // To pass around the "this" pointer across threads safely.
86   base::WeakPtrFactory<ProfileProvider> weak_factory_{this};
87 
88   DISALLOW_COPY_AND_ASSIGN(ProfileProvider);
89 };
90 
91 }  // namespace metrics
92 
93 #endif  // CHROME_BROWSER_METRICS_PERF_PROFILE_PROVIDER_CHROMEOS_H_
94