Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: chrome/browser/chromeos/boot_times_loader.cc

Issue 11410073: Add function to CancelableTaskTracker and convert BootTimeLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase again and add tests Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/chromeos/boot_times_loader.h" 5 #include "chrome/browser/chromeos/boot_times_loader.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/location.h"
14 #include "base/message_loop.h" 15 #include "base/message_loop.h"
16 #include "base/message_loop_proxy.h"
15 #include "base/metrics/histogram.h" 17 #include "base/metrics/histogram.h"
16 #include "base/process_util.h" 18 #include "base/process_util.h"
17 #include "base/string_number_conversions.h" 19 #include "base/string_number_conversions.h"
18 #include "base/string_util.h" 20 #include "base/string_util.h"
19 #include "base/stringprintf.h" 21 #include "base/stringprintf.h"
20 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
21 #include "base/threading/thread_restrictions.h" 23 #include "base/threading/thread_restrictions.h"
22 #include "base/time.h" 24 #include "base/time.h"
23 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/chromeos/login/authentication_notification_details.h" 26 #include "chrome/browser/chromeos/login/authentication_notification_details.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 Browser* browser = *it; 62 Browser* browser = *it;
61 for (int i = 0, tab_count = browser->tab_count(); i < tab_count; ++i) { 63 for (int i = 0, tab_count = browser->tab_count(); i < tab_count; ++i) {
62 WebContents* tab = chrome::GetWebContentsAt(browser, i); 64 WebContents* tab = chrome::GetWebContentsAt(browser, i);
63 if (tab->GetRenderWidgetHostView() == rwhv) { 65 if (tab->GetRenderWidgetHostView() == rwhv) {
64 return tab->GetURL().spec(); 66 return tab->GetURL().spec();
65 } 67 }
66 } 68 }
67 } 69 }
68 return std::string(); 70 return std::string();
69 } 71 }
72
73 void PostCallbackIfNotCanceled(
74 const CancelableTaskTracker::IsCanceledCallback& is_canceled_cb,
75 base::TaskRunner* task_runner,
76 const chromeos::BootTimesLoader::GetBootTimesCallback& callback,
77 const chromeos::BootTimesLoader::BootTimes& boot_times) {
78 if (is_canceled_cb.Run())
79 return;
80 task_runner->PostTask(FROM_HERE, base::Bind(callback, boot_times));
70 } 81 }
71 82
83 } // namespace
84
72 namespace chromeos { 85 namespace chromeos {
73 86
74 #define FPL(value) FILE_PATH_LITERAL(value) 87 #define FPL(value) FILE_PATH_LITERAL(value)
75 88
76 // Dir uptime & disk logs are located in. 89 // Dir uptime & disk logs are located in.
77 static const FilePath::CharType kLogPath[] = FPL("/tmp"); 90 static const FilePath::CharType kLogPath[] = FPL("/tmp");
78 // Dir log{in,out} logs are located in. 91 // Dir log{in,out} logs are located in.
79 static const FilePath::CharType kLoginLogPath[] = FPL("/home/chronos/user"); 92 static const FilePath::CharType kLoginLogPath[] = FPL("/home/chronos/user");
80 // Prefix for the time measurement files. 93 // Prefix for the time measurement files.
81 static const FilePath::CharType kUptimePrefix[] = FPL("uptime-"); 94 static const FilePath::CharType kUptimePrefix[] = FPL("uptime-");
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 logout_time_markers_.reserve(30); 128 logout_time_markers_.reserve(30);
116 } 129 }
117 130
118 BootTimesLoader::~BootTimesLoader() {} 131 BootTimesLoader::~BootTimesLoader() {}
119 132
120 // static 133 // static
121 BootTimesLoader* BootTimesLoader::Get() { 134 BootTimesLoader* BootTimesLoader::Get() {
122 return g_boot_times_loader.Pointer(); 135 return g_boot_times_loader.Pointer();
123 } 136 }
124 137
125 BootTimesLoader::Handle BootTimesLoader::GetBootTimes( 138 CancelableTaskTracker::TaskId BootTimesLoader::GetBootTimes(
126 CancelableRequestConsumerBase* consumer, 139 const GetBootTimesCallback& callback,
127 const GetBootTimesCallback& callback) { 140 CancelableTaskTracker* tracker) {
128 if (!BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) { 141 if (!BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
129 // This should only happen if Chrome is shutting down, so we don't do 142 // This should only happen if Chrome is shutting down, so we don't do
130 // anything. 143 // anything.
131 return 0; 144 return CancelableTaskTracker::kBadTaskId;
132 } 145 }
133 146
134 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 147 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
135 if (command_line.HasSwitch(switches::kTestType)) { 148 if (command_line.HasSwitch(switches::kTestType)) {
136 // TODO(davemoore) This avoids boottimes for tests. This needs to be 149 // TODO(davemoore) This avoids boottimes for tests. This needs to be
137 // replaced with a mock of BootTimesLoader. 150 // replaced with a mock of BootTimesLoader.
138 return 0; 151 return CancelableTaskTracker::kBadTaskId;
139 } 152 }
140 153
141 scoped_refptr<CancelableRequest<GetBootTimesCallback> > request( 154 CancelableTaskTracker::IsCanceledCallback is_canceled;
142 new CancelableRequest<GetBootTimesCallback>(callback)); 155 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled);
143 AddRequest(request, consumer);
144 156
157 GetBootTimesCallback callback_runner =
158 base::Bind(&PostCallbackIfNotCanceled,
159 is_canceled, base::MessageLoopProxy::current(), callback);
145 BrowserThread::PostTask( 160 BrowserThread::PostTask(
146 BrowserThread::FILE, 161 BrowserThread::FILE,
147 FROM_HERE, 162 FROM_HERE,
148 base::Bind(&Backend::GetBootTimes, backend_, request)); 163 base::Bind(&Backend::GetBootTimesAndRunCallback,
149 return request->handle(); 164 backend_, is_canceled, callback_runner));
165 return id;
150 } 166 }
151 167
152 // Extracts the uptime value from files located in /tmp, returning the 168 // Extracts the uptime value from files located in /tmp, returning the
153 // value as a double in value. 169 // value as a double in value.
154 static bool GetTime(const FilePath::StringType& log, double* value) { 170 static bool GetTime(const FilePath::StringType& log, double* value) {
155 FilePath log_dir(kLogPath); 171 FilePath log_dir(kLogPath);
156 FilePath log_file = log_dir.Append(log); 172 FilePath log_file = log_dir.Append(log);
157 std::string contents; 173 std::string contents;
158 *value = 0.0; 174 *value = 0.0;
159 if (file_util::ReadFileToString(log_file, &contents)) { 175 if (file_util::ReadFileToString(log_file, &contents)) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 "chrome: %.2f\n", 223 "chrome: %.2f\n",
208 boot_times.total, 224 boot_times.total,
209 boot_times.firmware, 225 boot_times.firmware,
210 boot_times.pre_startup, 226 boot_times.pre_startup,
211 boot_times.system, 227 boot_times.system,
212 boot_times.chrome); 228 boot_times.chrome);
213 file_util::WriteFile(sent, boot_times_text.data(), boot_times_text.size()); 229 file_util::WriteFile(sent, boot_times_text.data(), boot_times_text.size());
214 DCHECK(file_util::PathExists(sent)); 230 DCHECK(file_util::PathExists(sent));
215 } 231 }
216 232
217 void BootTimesLoader::Backend::GetBootTimes( 233 void BootTimesLoader::Backend::GetBootTimesAndRunCallback(
218 const scoped_refptr<GetBootTimesRequest>& request) { 234 const CancelableTaskTracker::IsCanceledCallback& is_canceled_cb,
235 const GetBootTimesCallback& callback) {
236 if (is_canceled_cb.Run())
237 return;
238
219 const FilePath::CharType kFirmwareBootTime[] = FPL("firmware-boot-time"); 239 const FilePath::CharType kFirmwareBootTime[] = FPL("firmware-boot-time");
220 const FilePath::CharType kPreStartup[] = FPL("pre-startup"); 240 const FilePath::CharType kPreStartup[] = FPL("pre-startup");
221 const FilePath::CharType kChromeExec[] = FPL("chrome-exec"); 241 const FilePath::CharType kChromeExec[] = FPL("chrome-exec");
222 const FilePath::CharType kChromeMain[] = FPL("chrome-main"); 242 const FilePath::CharType kChromeMain[] = FPL("chrome-main");
223 const FilePath::CharType kXStarted[] = FPL("x-started"); 243 const FilePath::CharType kXStarted[] = FPL("x-started");
224 const FilePath::CharType kLoginPromptReady[] = FPL("login-prompt-ready"); 244 const FilePath::CharType kLoginPromptReady[] = FPL("login-prompt-ready");
225 const FilePath::StringType uptime_prefix = kUptimePrefix; 245 const FilePath::StringType uptime_prefix = kUptimePrefix;
226 246
227 if (request->canceled())
228 return;
229
230 // Wait until firmware-boot-time file exists by reposting. 247 // Wait until firmware-boot-time file exists by reposting.
231 FilePath log_dir(kLogPath); 248 FilePath log_dir(kLogPath);
232 FilePath log_file = log_dir.Append(kFirmwareBootTime); 249 FilePath log_file = log_dir.Append(kFirmwareBootTime);
233 if (!file_util::PathExists(log_file)) { 250 if (!file_util::PathExists(log_file)) {
234 BrowserThread::PostDelayedTask( 251 BrowserThread::PostDelayedTask(
235 BrowserThread::FILE, 252 BrowserThread::FILE,
236 FROM_HERE, 253 FROM_HERE,
237 base::Bind(&Backend::GetBootTimes, this, request), 254 base::Bind(&Backend::GetBootTimesAndRunCallback,
255 this, is_canceled_cb, callback),
238 base::TimeDelta::FromMilliseconds(kReadAttemptDelayMs)); 256 base::TimeDelta::FromMilliseconds(kReadAttemptDelayMs));
239 return; 257 return;
240 } 258 }
241 259
242 BootTimes boot_times; 260 BootTimes boot_times;
243 261
244 GetTime(kFirmwareBootTime, &boot_times.firmware); 262 GetTime(kFirmwareBootTime, &boot_times.firmware);
245 GetTime(uptime_prefix + kPreStartup, &boot_times.pre_startup); 263 GetTime(uptime_prefix + kPreStartup, &boot_times.pre_startup);
246 GetTime(uptime_prefix + kXStarted, &boot_times.x_started); 264 GetTime(uptime_prefix + kXStarted, &boot_times.x_started);
247 GetTime(uptime_prefix + kChromeExec, &boot_times.chrome_exec); 265 GetTime(uptime_prefix + kChromeExec, &boot_times.chrome_exec);
248 GetTime(uptime_prefix + kChromeMain, &boot_times.chrome_main); 266 GetTime(uptime_prefix + kChromeMain, &boot_times.chrome_main);
249 GetTime(uptime_prefix + kLoginPromptReady, &boot_times.login_prompt_ready); 267 GetTime(uptime_prefix + kLoginPromptReady, &boot_times.login_prompt_ready);
250 268
251 boot_times.total = boot_times.firmware + boot_times.login_prompt_ready; 269 boot_times.total = boot_times.firmware + boot_times.login_prompt_ready;
252 if (boot_times.chrome_exec > 0) { 270 if (boot_times.chrome_exec > 0) {
253 boot_times.system = boot_times.chrome_exec - boot_times.pre_startup; 271 boot_times.system = boot_times.chrome_exec - boot_times.pre_startup;
254 boot_times.chrome = boot_times.login_prompt_ready - boot_times.chrome_exec; 272 boot_times.chrome = boot_times.login_prompt_ready - boot_times.chrome_exec;
255 } else { 273 } else {
256 boot_times.system = boot_times.login_prompt_ready - boot_times.pre_startup; 274 boot_times.system = boot_times.login_prompt_ready - boot_times.pre_startup;
257 } 275 }
258 276
259 SendBootTimesToUMA(boot_times); 277 SendBootTimesToUMA(boot_times);
260 278
261 request->ForwardResult(request->handle(), boot_times); 279 callback.Run(boot_times);
262 } 280 }
263 281
264 // Appends the given buffer into the file. Returns the number of bytes 282 // Appends the given buffer into the file. Returns the number of bytes
265 // written, or -1 on error. 283 // written, or -1 on error.
266 // TODO(satorux): Move this to file_util. 284 // TODO(satorux): Move this to file_util.
267 static int AppendFile(const FilePath& file_path, 285 static int AppendFile(const FilePath& file_path,
268 const char* data, 286 const char* data,
269 int size) { 287 int size) {
270 FILE* file = file_util::OpenFile(file_path, "a"); 288 FILE* file = file_util::OpenFile(file_path, "a");
271 if (!file) { 289 if (!file) {
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 GetRenderWidgetHost(&web_contents->GetController()); 533 GetRenderWidgetHost(&web_contents->GetController());
516 render_widget_hosts_loading_.erase(render_widget_host); 534 render_widget_hosts_loading_.erase(render_widget_host);
517 break; 535 break;
518 } 536 }
519 default: 537 default:
520 break; 538 break;
521 } 539 }
522 } 540 }
523 541
524 } // namespace chromeos 542 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/boot_times_loader.h ('k') | chrome/browser/chromeos/login/version_info_updater.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698