OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/atomic_sequence_num.h" | 11 #include "base/atomic_sequence_num.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 #include "content/public/browser/render_widget_host.h" | 18 #include "content/public/browser/render_widget_host.h" |
19 | 19 |
| 20 class PrefService; |
| 21 |
20 namespace chromeos { | 22 namespace chromeos { |
21 | 23 |
22 // BootTimesLoader loads the bootimes of Chrome OS from the file system. | 24 // BootTimesLoader loads the bootimes of Chrome OS from the file system. |
23 // Loading is done asynchronously on the file thread. Once loaded, | 25 // Loading is done asynchronously on the file thread. Once loaded, |
24 // BootTimesLoader calls back to a method of your choice with the boot times. | 26 // BootTimesLoader calls back to a method of your choice with the boot times. |
25 // To use BootTimesLoader, do the following: | 27 // To use BootTimesLoader, do the following: |
26 // | 28 // |
27 // . In your class define a member field of type chromeos::BootTimesLoader and | 29 // . In your class define a member field of type chromeos::BootTimesLoader and |
28 // base::CancelableTaskTracker. | 30 // base::CancelableTaskTracker. |
29 // . Define the callback method, something like: | 31 // . Define the callback method, something like: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 void LoginDone(bool is_user_new); | 77 void LoginDone(bool is_user_new); |
76 | 78 |
77 // Writes the logout times to a /tmp/logout-times-sent. Unlike login | 79 // Writes the logout times to a /tmp/logout-times-sent. Unlike login |
78 // times, we manually call this function for logout times, as we cannot | 80 // times, we manually call this function for logout times, as we cannot |
79 // rely on notification service to tell when the logout is done. | 81 // rely on notification service to tell when the logout is done. |
80 void WriteLogoutTimes(); | 82 void WriteLogoutTimes(); |
81 | 83 |
82 // Mark that WriteLogoutTimes should handle restart. | 84 // Mark that WriteLogoutTimes should handle restart. |
83 void set_restart_requested() { restart_requested_ = true; } | 85 void set_restart_requested() { restart_requested_ = true; } |
84 | 86 |
| 87 // This is called on Chrome process startup to write saved logout stats. |
| 88 void OnChromeProcessStart(); |
| 89 |
| 90 // This saves logout-started metric to Local State. |
| 91 void OnLogoutStarted(PrefService* state); |
| 92 |
85 private: | 93 private: |
86 // BootTimesLoader calls into the Backend on the file thread to load | 94 // BootTimesLoader calls into the Backend on the file thread to load |
87 // the boot times. | 95 // the boot times. |
88 class Backend : public base::RefCountedThreadSafe<Backend> { | 96 class Backend : public base::RefCountedThreadSafe<Backend> { |
89 public: | 97 public: |
90 Backend() {} | 98 Backend() {} |
91 | 99 |
92 private: | 100 private: |
93 friend class base::RefCountedThreadSafe<Backend>; | 101 friend class base::RefCountedThreadSafe<Backend>; |
94 | 102 |
(...skipping 17 matching lines...) Expand all Loading... |
112 return time_ < other.time_; | 120 return time_ < other.time_; |
113 } | 121 } |
114 | 122 |
115 private: | 123 private: |
116 friend class std::vector<TimeMarker>; | 124 friend class std::vector<TimeMarker>; |
117 std::string name_; | 125 std::string name_; |
118 base::Time time_; | 126 base::Time time_; |
119 bool send_to_uma_; | 127 bool send_to_uma_; |
120 }; | 128 }; |
121 | 129 |
122 struct Stats { | 130 class Stats { |
123 public: | 131 public: |
124 std::string uptime; | 132 // Initializes stats with current /proc values. |
125 std::string disk; | 133 static Stats GetCurrentStats(); |
| 134 |
| 135 // Returns JSON representation. |
| 136 std::string SerializeToString() const; |
| 137 |
| 138 // Creates new object from JSON representation. |
| 139 static Stats DeserializeFromString(const std::string& value); |
| 140 |
| 141 const std::string& uptime() const { return uptime_; } |
| 142 const std::string& disk() const { return disk_; } |
| 143 |
| 144 // Writes "uptime in seconds" to result. (This is first field in uptime_.) |
| 145 // Returns true on successful conversion. |
| 146 bool UptimeDouble(double* result) const; |
| 147 |
| 148 void RecordStats(const std::string& name) const; |
| 149 void RecordStatsWithCallback(const std::string& name, |
| 150 const base::Closure& callback) const; |
| 151 |
| 152 private: |
| 153 // Runs on BlockingPool |
| 154 void RecordStatsImpl(const std::string& name) const; |
| 155 |
| 156 std::string uptime_; |
| 157 std::string disk_; |
126 }; | 158 }; |
127 | 159 |
128 static void RecordStats( | |
129 const std::string& name, const Stats& stats); | |
130 static Stats GetCurrentStats(); | |
131 static void WriteTimes(const std::string base_name, | 160 static void WriteTimes(const std::string base_name, |
132 const std::string uma_name, | 161 const std::string uma_name, |
133 const std::string uma_prefix, | 162 const std::string uma_prefix, |
134 std::vector<TimeMarker> login_times); | 163 std::vector<TimeMarker> login_times); |
135 static void AddMarker(std::vector<TimeMarker>* vector, TimeMarker marker); | 164 static void AddMarker(std::vector<TimeMarker>* vector, TimeMarker marker); |
136 | 165 |
| 166 // Clear saved logout-started metric in Local State. |
| 167 // This method is called when logout-state was writen to file. |
| 168 static void ClearLogoutStartedLastPreference(); |
| 169 |
137 // Used to hold the stats at main(). | 170 // Used to hold the stats at main(). |
138 Stats chrome_main_stats_; | 171 Stats chrome_main_stats_; |
139 scoped_refptr<Backend> backend_; | 172 scoped_refptr<Backend> backend_; |
140 | 173 |
141 // Used to track notifications for login. | 174 // Used to track notifications for login. |
142 content::NotificationRegistrar registrar_; | 175 content::NotificationRegistrar registrar_; |
143 base::AtomicSequenceNumber num_tabs_; | 176 base::AtomicSequenceNumber num_tabs_; |
144 bool have_registered_; | 177 bool have_registered_; |
145 | 178 |
146 std::vector<TimeMarker> login_time_markers_; | 179 std::vector<TimeMarker> login_time_markers_; |
147 std::vector<TimeMarker> logout_time_markers_; | 180 std::vector<TimeMarker> logout_time_markers_; |
148 std::set<content::RenderWidgetHost*> render_widget_hosts_loading_; | 181 std::set<content::RenderWidgetHost*> render_widget_hosts_loading_; |
149 | 182 |
150 bool login_done_; | 183 bool login_done_; |
151 | 184 |
152 bool restart_requested_; | 185 bool restart_requested_; |
153 | 186 |
154 DISALLOW_COPY_AND_ASSIGN(BootTimesLoader); | 187 DISALLOW_COPY_AND_ASSIGN(BootTimesLoader); |
155 }; | 188 }; |
156 | 189 |
157 } // namespace chromeos | 190 } // namespace chromeos |
158 | 191 |
159 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ | 192 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_ |
OLD | NEW |