Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / boot_times_loader.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
7
8 #include <set>
9 #include <string>
10
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"
20
21 class PrefService;
22
23 namespace chromeos {
24
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:
29 //
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 {
37  public:
38   BootTimesLoader();
39   virtual ~BootTimesLoader();
40
41   static BootTimesLoader* Get();
42
43   // LoginEventRecorder::Delegate override.
44
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;
51
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);
57
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);
63
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();
67
68   // Records the data previously saved by SaveChromeMainStats(), using the
69   // file thread. Existing stats files will not be overwritten.
70   void RecordChromeMainStats();
71
72   // Records the time that a login was attempted. This will overwrite any
73   // previous login attempt times.
74   void RecordLoginAttempted();
75
76   // content::NotificationObserver implementation.
77   virtual void Observe(int type,
78                        const content::NotificationSource& source,
79                        const content::NotificationDetails& details) OVERRIDE;
80
81   // Records "LoginDone" event.
82   void LoginDone(bool is_user_new);
83
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();
88
89   // Mark that WriteLogoutTimes should handle restart.
90   void set_restart_requested() { restart_requested_ = true; }
91
92   // This is called on Chrome process startup to write saved logout stats.
93   void OnChromeProcessStart();
94
95   // This saves logout-started metric to Local State.
96   void OnLogoutStarted(PrefService* state);
97
98  private:
99   // BootTimesLoader calls into the Backend on the file thread to load
100   // the boot times.
101   class Backend : public base::RefCountedThreadSafe<Backend> {
102    public:
103     Backend() {}
104
105    private:
106     friend class base::RefCountedThreadSafe<Backend>;
107
108     ~Backend() {}
109
110     DISALLOW_COPY_AND_ASSIGN(Backend);
111   };
112
113   class TimeMarker {
114    public:
115     TimeMarker(const std::string& name, bool send_to_uma)
116         : name_(name),
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_; }
122
123     // comparitor for sorting
124     bool operator<(const TimeMarker& other) const {
125       return time_ < other.time_;
126     }
127
128    private:
129     friend class std::vector<TimeMarker>;
130     std::string name_;
131     base::Time time_;
132     bool send_to_uma_;
133   };
134
135   class Stats {
136    public:
137     // Initializes stats with current /proc values.
138     static Stats GetCurrentStats();
139
140     // Returns JSON representation.
141     std::string SerializeToString() const;
142
143     // Creates new object from JSON representation.
144     static Stats DeserializeFromString(const std::string& value);
145
146     const std::string& uptime() const { return uptime_; }
147     const std::string& disk() const { return disk_; }
148
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;
152
153     void RecordStats(const std::string& name) const;
154     void RecordStatsWithCallback(const std::string& name,
155                                  const base::Closure& callback) const;
156
157    private:
158     // Runs on BlockingPool
159     void RecordStatsImpl(const std::string& name) const;
160
161     std::string uptime_;
162     std::string disk_;
163   };
164
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);
170
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();
174
175   // Used to hold the stats at main().
176   Stats chrome_main_stats_;
177   scoped_refptr<Backend> backend_;
178
179   // Used to track notifications for login.
180   content::NotificationRegistrar registrar_;
181   base::AtomicSequenceNumber num_tabs_;
182   bool have_registered_;
183
184   std::vector<TimeMarker> login_time_markers_;
185   std::vector<TimeMarker> logout_time_markers_;
186   std::set<content::RenderWidgetHost*> render_widget_hosts_loading_;
187
188   bool login_done_;
189
190   bool restart_requested_;
191
192   DISALLOW_COPY_AND_ASSIGN(BootTimesLoader);
193 };
194
195 }  // namespace chromeos
196
197 #endif  // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_