Fix for a startup crash when restoring the session if a specific performance monitori...
[chromium-blink-merge.git] / chrome / browser / performance_monitor / startup_timer.cc
blob8948252b2a84830b8bdcacc00507d1866933b255
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 #include "chrome/browser/performance_monitor/startup_timer.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/performance_monitor/database.h"
12 #include "chrome/browser/performance_monitor/performance_monitor.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/notification_types.h"
19 namespace performance_monitor {
21 namespace {
22 // Needed because Database::AddMetric is overloaded, so base::Bind doesn't work.
23 void AddMetricToDatabaseOnBackgroundThread(Database* database,
24 const Metric& metric) {
25 database->AddMetric(metric);
28 } // namespace
30 // static
31 StartupTimer* StartupTimer::g_startup_timer_ = NULL;
33 StartupTimer::StartupTimer() : startup_begin_(base::TimeTicks::Now()),
34 startup_type_(STARTUP_NORMAL),
35 performance_monitor_initialized_(false) {
36 CHECK(!g_startup_timer_);
37 g_startup_timer_ = this;
39 // We need this check because, under certain rare circumstances,
40 // NotificationService::current() will return null, and this will cause a
41 // segfault in NotificationServiceImpl::AddObserver(). Currently, this only
42 // happens as a result of the child process launched by BrowserMainTest.
43 // WarmConnectionFieldTrial_Invalid.
44 if (content::NotificationService::current()) {
45 registrar_.Add(this, chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
46 content::NotificationService::AllSources());
50 StartupTimer::~StartupTimer() {
51 DCHECK(this == g_startup_timer_);
52 g_startup_timer_ = NULL;
55 bool StartupTimer::SignalStartupComplete(StartupType startup_type) {
56 DCHECK(elapsed_startup_time_ == base::TimeDelta());
58 startup_type_ = startup_type;
60 elapsed_startup_time_ =
61 base::TimeTicks::Now() - total_pause_ - startup_begin_;
63 if (performance_monitor_initialized_)
64 InsertElapsedStartupTime();
66 return true;
69 // static
70 void StartupTimer::PauseTimer() {
71 // Check that the timer is not already paused.
72 DCHECK(g_startup_timer_->pause_started_ == base::TimeTicks());
74 g_startup_timer_->pause_started_ = base::TimeTicks::Now();
77 // static
78 void StartupTimer::UnpauseTimer() {
79 // Check that the timer has been paused.
80 DCHECK(g_startup_timer_->pause_started_ != base::TimeTicks());
82 g_startup_timer_->total_pause_ += base::TimeTicks::Now() -
83 g_startup_timer_->pause_started_;
85 g_startup_timer_->pause_started_ = base::TimeTicks();
88 void StartupTimer::Observe(int type,
89 const content::NotificationSource& source,
90 const content::NotificationDetails& details) {
91 CHECK(type == chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED);
92 performance_monitor_initialized_ = true;
94 if (PerformanceMonitor::GetInstance()->database_logging_enabled()) {
95 if (elapsed_startup_time_ != base::TimeDelta())
96 InsertElapsedStartupTime();
97 if (elapsed_session_restore_times_.size())
98 InsertElapsedSessionRestoreTime();
102 // static
103 void StartupTimer::SetElapsedSessionRestoreTime(
104 const base::TimeDelta& elapsed_session_restore_time) {
105 if (PerformanceMonitor::GetInstance()->database_logging_enabled()) {
106 g_startup_timer_->elapsed_session_restore_times_.push_back(
107 elapsed_session_restore_time);
109 if (g_startup_timer_->performance_monitor_initialized_)
110 g_startup_timer_->InsertElapsedSessionRestoreTime();
114 void StartupTimer::InsertElapsedStartupTime() {
115 content::BrowserThread::PostBlockingPoolSequencedTask(
116 Database::kDatabaseSequenceToken,
117 FROM_HERE,
118 base::Bind(
119 &AddMetricToDatabaseOnBackgroundThread,
120 base::Unretained(PerformanceMonitor::GetInstance()->database()),
121 Metric(startup_type_ == STARTUP_NORMAL ? METRIC_STARTUP_TIME
122 : METRIC_TEST_STARTUP_TIME,
123 base::Time::Now(),
124 static_cast<double>(
125 elapsed_startup_time_.ToInternalValue()))));
128 void StartupTimer::InsertElapsedSessionRestoreTime() {
129 for (std::vector<base::TimeDelta>::const_iterator iter =
130 elapsed_session_restore_times_.begin();
131 iter != elapsed_session_restore_times_.end(); ++iter) {
132 content::BrowserThread::PostBlockingPoolSequencedTask(
133 Database::kDatabaseSequenceToken,
134 FROM_HERE,
135 base::Bind(
136 &AddMetricToDatabaseOnBackgroundThread,
137 base::Unretained(PerformanceMonitor::GetInstance()->database()),
138 Metric(METRIC_SESSION_RESTORE_TIME,
139 base::Time::Now(),
140 static_cast<double>(iter->ToInternalValue()))));
144 } // namespace performance_monitor