chromeos: Lock proxy settings UI for policy managed network.
[chromium-blink-merge.git] / chrome / browser / chromeos / boot_times_loader.h
blob0cae34f1d35762f1ede1ebfdad8229e37f208493
1 // Copyright (c) 2011 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_
7 #pragma once
9 #include <set>
10 #include <string>
12 #include "base/atomic_sequence_num.h"
13 #include "base/callback_forward.h"
14 #include "base/compiler_specific.h"
15 #include "base/time.h"
16 #include "chrome/browser/cancelable_request.h"
17 #include "content/browser/renderer_host/render_widget_host.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
21 namespace chromeos {
23 // BootTimesLoader loads the bootimes of Chrome OS from the file system.
24 // Loading is done asynchronously on the file thread. Once loaded,
25 // BootTimesLoader calls back to a method of your choice with the boot times.
26 // To use BootTimesLoader do the following:
28 // . In your class define a member field of type chromeos::BootTimesLoader and
29 // CancelableRequestConsumerBase.
30 // . Define the callback method, something like:
31 // void OnBootTimesLoader(chromeos::BootTimesLoader::Handle,
32 // BootTimesLoader::BootTimes boot_times);
33 // . When you want the version invoke: loader.GetBootTimes(&consumer, callback);
34 class BootTimesLoader
35 : public CancelableRequestProvider,
36 public content::NotificationObserver {
37 public:
38 BootTimesLoader();
39 virtual ~BootTimesLoader();
41 // All fields are 0.0 if they couldn't be found.
42 typedef struct BootTimes {
43 double firmware; // Time from power button to kernel being loaded.
44 double pre_startup; // Time from kernel to system code being called.
45 double x_started; // Time X server is ready to be connected to.
46 double chrome_exec; // Time session manager executed Chrome.
47 double chrome_main; // Time chrome's main() was called.
48 double login_prompt_ready; // Time login (or OOB) panel is displayed.
49 double system; // Time system took to start chrome.
50 double chrome; // Time chrome took to display login panel.
51 double total; // Time from power button to login panel.
53 BootTimes() : firmware(0),
54 pre_startup(0),
55 x_started(0),
56 chrome_exec(0),
57 chrome_main(0),
58 login_prompt_ready(0),
59 system(0),
60 chrome(0),
61 total(0) {}
62 } BootTimes;
64 // Signature
65 typedef base::Callback<void(Handle, BootTimes)> GetBootTimesCallback;
67 typedef CancelableRequest<GetBootTimesCallback> GetBootTimesRequest;
69 static BootTimesLoader* Get();
71 // Asynchronously requests the info.
72 Handle GetBootTimes(
73 CancelableRequestConsumerBase* consumer,
74 const GetBootTimesCallback& callback);
76 // Add a time marker for login. A timeline will be dumped to
77 // /tmp/login-times-sent after login is done. If |send_to_uma| is true
78 // the time between this marker and the last will be sent to UMA with
79 // the identifier BootTime.|marker_name|.
80 void AddLoginTimeMarker(const std::string& marker_name, bool send_to_uma);
82 // Add a time marker for logout. A timeline will be dumped to
83 // /tmp/logout-times-sent after logout is done. If |send_to_uma| is true
84 // the time between this marker and the last will be sent to UMA with
85 // the identifier ShutdownTime.|marker_name|.
86 void AddLogoutTimeMarker(const std::string& marker_name, bool send_to_uma);
88 // Records current uptime and disk usage for metrics use.
89 // Posts task to file thread.
90 // name will be used as part of file names in /tmp.
91 // Existing stats files will not be overwritten.
92 void RecordCurrentStats(const std::string& name);
94 // Saves away the stats at main, so the can be recorded later. At main() time
95 // the necessary threads don't exist yet for recording the data.
96 void SaveChromeMainStats();
98 // Records the data previously saved by SaveChromeMainStats(), using the
99 // file thread. Existing stats files will not be overwritten.
100 void RecordChromeMainStats();
102 // Records the time that a login was attempted. This will overwrite any
103 // previous login attempt times.
104 void RecordLoginAttempted();
106 // content::NotificationObserver implementation.
107 virtual void Observe(int type,
108 const content::NotificationSource& source,
109 const content::NotificationDetails& details) OVERRIDE;
111 // Writes the logout times to a /tmp/logout-times-sent. Unlike login
112 // times, we manually call this function for logout times, as we cannot
113 // rely on notification service to tell when the logout is done.
114 void WriteLogoutTimes();
116 private:
117 // BootTimesLoader calls into the Backend on the file thread to load
118 // the boot times.
119 class Backend : public base::RefCountedThreadSafe<Backend> {
120 public:
121 Backend() {}
123 void GetBootTimes(const scoped_refptr<GetBootTimesRequest>& request);
125 private:
126 friend class base::RefCountedThreadSafe<Backend>;
128 ~Backend() {}
130 DISALLOW_COPY_AND_ASSIGN(Backend);
133 class TimeMarker {
134 public:
135 TimeMarker(const std::string& name, bool send_to_uma)
136 : name_(name),
137 time_(base::Time::NowFromSystemTime()),
138 send_to_uma_(send_to_uma) {}
139 std::string name() const { return name_; }
140 base::Time time() const { return time_; }
141 bool send_to_uma() const { return send_to_uma_; }
143 private:
144 friend class std::vector<TimeMarker>;
145 std::string name_;
146 base::Time time_;
147 bool send_to_uma_;
150 struct Stats {
151 public:
152 std::string uptime;
153 std::string disk;
156 static void RecordStats(
157 const std::string& name, const Stats& stats);
158 static Stats GetCurrentStats();
159 static void WriteTimes(const std::string base_name,
160 const std::string uma_name,
161 const std::string uma_prefix,
162 const std::vector<TimeMarker> login_times);
163 void LoginDone();
165 // Used to hold the stats at main().
166 Stats chrome_main_stats_;
167 scoped_refptr<Backend> backend_;
169 // Used to track notifications for login.
170 content::NotificationRegistrar registrar_;
171 base::AtomicSequenceNumber num_tabs_;
172 bool have_registered_;
174 std::vector<TimeMarker> login_time_markers_;
175 std::vector<TimeMarker> logout_time_markers_;
176 std::set<RenderWidgetHost*> render_widget_hosts_loading_;
178 DISALLOW_COPY_AND_ASSIGN(BootTimesLoader);
181 } // namespace chromeos
183 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_