1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
11 #include "base/atomic_sequence_num.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/time/time.h"
16 #include "chromeos/login_event_recorder.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/render_widget_host.h"
25 // BootTimesLoader loads the bootimes of Chrome OS from the file system.
26 // Loading is done asynchronously on the file thread. Once loaded,
27 // BootTimesLoader calls back to a method of your choice with the boot times.
28 // To use BootTimesLoader, do the following:
30 // . In your class define a member field of type chromeos::BootTimesLoader and
31 // base::CancelableTaskTracker.
32 // . Define the callback method, something like:
33 // void OnBootTimesLoaded(const BootTimesLoader::BootTimes& boot_times);
34 // . When you want the version invoke: loader.GetBootTimes(callback, &tracker_);
35 class BootTimesLoader : public content::NotificationObserver,
36 public LoginEventRecorder::Delegate {
39 virtual ~BootTimesLoader();
41 static BootTimesLoader* Get();
43 // LoginEventRecorder::Delegate override.
45 // Add a time marker for login. A timeline will be dumped to
46 // /tmp/login-times-sent after login is done. If |send_to_uma| is true
47 // the time between this marker and the last will be sent to UMA with
48 // the identifier BootTime.|marker_name|.
49 virtual void AddLoginTimeMarker(const std::string& marker_name,
50 bool send_to_uma) OVERRIDE;
52 // Add a time marker for logout. A timeline will be dumped to
53 // /tmp/logout-times-sent after logout is done. If |send_to_uma| is true
54 // the time between this marker and the last will be sent to UMA with
55 // the identifier ShutdownTime.|marker_name|.
56 void AddLogoutTimeMarker(const std::string& marker_name, bool send_to_uma);
58 // Records current uptime and disk usage for metrics use.
59 // Posts task to file thread.
60 // name will be used as part of file names in /tmp.
61 // Existing stats files will not be overwritten.
62 void RecordCurrentStats(const std::string& name);
64 // Saves away the stats at main, so the can be recorded later. At main() time
65 // the necessary threads don't exist yet for recording the data.
66 void SaveChromeMainStats();
68 // Records the data previously saved by SaveChromeMainStats(), using the
69 // file thread. Existing stats files will not be overwritten.
70 void RecordChromeMainStats();
72 // Records the time that a login was attempted. This will overwrite any
73 // previous login attempt times.
74 void RecordLoginAttempted();
76 // content::NotificationObserver implementation.
77 virtual void Observe(int type,
78 const content::NotificationSource& source,
79 const content::NotificationDetails& details) OVERRIDE;
81 // Records "LoginDone" event.
82 void LoginDone(bool is_user_new);
84 // Writes the logout times to a /tmp/logout-times-sent. Unlike login
85 // times, we manually call this function for logout times, as we cannot
86 // rely on notification service to tell when the logout is done.
87 void WriteLogoutTimes();
89 // Mark that WriteLogoutTimes should handle restart.
90 void set_restart_requested() { restart_requested_ = true; }
92 // This is called on Chrome process startup to write saved logout stats.
93 void OnChromeProcessStart();
95 // This saves logout-started metric to Local State.
96 void OnLogoutStarted(PrefService* state);
99 // BootTimesLoader calls into the Backend on the file thread to load
101 class Backend : public base::RefCountedThreadSafe<Backend> {
106 friend class base::RefCountedThreadSafe<Backend>;
110 DISALLOW_COPY_AND_ASSIGN(Backend);
115 TimeMarker(const std::string& name, bool send_to_uma)
117 time_(base::Time::NowFromSystemTime()),
118 send_to_uma_(send_to_uma) {}
119 std::string name() const { return name_; }
120 base::Time time() const { return time_; }
121 bool send_to_uma() const { return send_to_uma_; }
123 // comparitor for sorting
124 bool operator<(const TimeMarker& other) const {
125 return time_ < other.time_;
129 friend class std::vector<TimeMarker>;
137 // Initializes stats with current /proc values.
138 static Stats GetCurrentStats();
140 // Returns JSON representation.
141 std::string SerializeToString() const;
143 // Creates new object from JSON representation.
144 static Stats DeserializeFromString(const std::string& value);
146 const std::string& uptime() const { return uptime_; }
147 const std::string& disk() const { return disk_; }
149 // Writes "uptime in seconds" to result. (This is first field in uptime_.)
150 // Returns true on successful conversion.
151 bool UptimeDouble(double* result) const;
153 void RecordStats(const std::string& name) const;
154 void RecordStatsWithCallback(const std::string& name,
155 const base::Closure& callback) const;
158 // Runs on BlockingPool
159 void RecordStatsImpl(const std::string& name) const;
165 static void WriteTimes(const std::string base_name,
166 const std::string uma_name,
167 const std::string uma_prefix,
168 std::vector<TimeMarker> login_times);
169 static void AddMarker(std::vector<TimeMarker>* vector, TimeMarker marker);
171 // Clear saved logout-started metric in Local State.
172 // This method is called when logout-state was writen to file.
173 static void ClearLogoutStartedLastPreference();
175 // Used to hold the stats at main().
176 Stats chrome_main_stats_;
177 scoped_refptr<Backend> backend_;
179 // Used to track notifications for login.
180 content::NotificationRegistrar registrar_;
181 base::AtomicSequenceNumber num_tabs_;
182 bool have_registered_;
184 std::vector<TimeMarker> login_time_markers_;
185 std::vector<TimeMarker> logout_time_markers_;
186 std::set<content::RenderWidgetHost*> render_widget_hosts_loading_;
190 bool restart_requested_;
192 DISALLOW_COPY_AND_ASSIGN(BootTimesLoader);
195 } // namespace chromeos
197 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_