Index: chrome/browser/performance_monitor/performance_monitor.cc |
diff --git a/chrome/browser/performance_monitor/performance_monitor.cc b/chrome/browser/performance_monitor/performance_monitor.cc |
index 1e6d20b06e44a882935a3494e32169e4a889b437..ee0aaffbf5dc6af61938a9ed4ae359f8004c461b 100644 |
--- a/chrome/browser/performance_monitor/performance_monitor.cc |
+++ b/chrome/browser/performance_monitor/performance_monitor.cc |
@@ -4,15 +4,23 @@ |
#include "chrome/browser/performance_monitor/performance_monitor.h" |
+#include <set> |
+ |
#include "base/bind.h" |
#include "base/logging.h" |
#include "base/process_util.h" |
+#include "base/string_number_conversions.h" |
#include "base/threading/worker_pool.h" |
#include "base/time.h" |
+#include "chrome/browser/browser_process.h" |
#include "chrome/browser/browser_shutdown.h" |
#include "chrome/browser/performance_monitor/constants.h" |
#include "chrome/browser/performance_monitor/database.h" |
#include "chrome/browser/performance_monitor/performance_monitor_util.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_list.h" |
#include "chrome/browser/extensions/crx_installer.h" |
#include "chrome/common/chrome_notification_types.h" |
#include "chrome/common/chrome_version_info.h" |
@@ -25,6 +33,23 @@ |
using extensions::Extension; |
+namespace { |
+ |
+std::string TimeToString(base::Time time) { |
+ int64 time_int64 = time.ToInternalValue(); |
+ return base::Int64ToString(time_int64); |
+} |
+ |
+bool StringToTime(std::string time, base::Time* output) { |
+ int64 time_int64 = 0; |
+ if (!base::StringToInt64(time, &time_int64)) |
+ return false; |
+ *output = base::Time::FromInternalValue(time_int64); |
+ return true; |
+} |
+ |
+} // namespace |
+ |
namespace performance_monitor { |
PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
@@ -34,6 +59,7 @@ PerformanceMonitor::~PerformanceMonitor() { |
} |
void PerformanceMonitor::Start() { |
+ std::string time = TimeToString(base::Time::Now()); |
content::BrowserThread::PostBlockingPoolTaskAndReply( |
FROM_HERE, |
base::Bind(&PerformanceMonitor::InitOnBackgroundThread, |
@@ -43,7 +69,7 @@ void PerformanceMonitor::Start() { |
} |
void PerformanceMonitor::InitOnBackgroundThread() { |
- DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
database_ = Database::Create(database_path_); |
} |
@@ -58,9 +84,13 @@ bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { |
} |
void PerformanceMonitor::FinishInit() { |
- DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
RegisterForNotifications(); |
+ CheckForUncleanExits(); |
CheckForVersionUpdate(); |
+ |
+ timer_.Start(FROM_HERE, base::TimeDelta::FromMinutes(2), |
+ this, &PerformanceMonitor::DoTimedCollections); |
} |
void PerformanceMonitor::RegisterForNotifications() { |
@@ -79,6 +109,10 @@ void PerformanceMonitor::RegisterForNotifications() { |
content::NotificationService::AllSources()); |
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
content::NotificationService::AllSources()); |
+ |
+ // Profiles (for unclean exit) |
+ registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, |
+ content::NotificationService::AllSources()); |
} |
// Static |
@@ -102,7 +136,7 @@ void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { |
void PerformanceMonitor::GetStateValueOnBackgroundThread( |
const std::string& key, |
const StateValueCallback& callback) { |
- DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
std::string state_value = database_->GetStateValue(key); |
callback.Run(state_value); |
@@ -154,6 +188,79 @@ void PerformanceMonitor::CheckForVersionUpdateHelper( |
} |
} |
+// We check if profiles exited cleanly initialization time in case they were |
+// loaded prior to PerformanceMonitor's initialization. Later profiles will be |
+// checked through the PROFILE_ADDED notification. |
+void PerformanceMonitor::CheckForUncleanExits() { |
+ std::vector<Profile*> profiles = |
+ g_browser_process->profile_manager()->GetLoadedProfiles(); |
+ |
+ for (std::vector<Profile*>::const_iterator iter = profiles.begin(); |
+ iter != profiles.end(); ++iter) { |
+ if (!(*iter)->DidLastSessionExitCleanly()) { |
+ content::BrowserThread::PostBlockingPoolSequencedTask( |
+ Database::kDatabaseSequenceToken, |
+ FROM_HERE, |
+ base::Bind(&PerformanceMonitor::AddUncleanExitEvent, |
+ base::Unretained(this), |
+ (*iter)->GetDebugName())); |
+ } |
+ } |
+} |
+ |
+void PerformanceMonitor::AddUncleanExitEvent(std::string profile_name) { |
+ std::string database_key = kStateProfilePrefix + profile_name; |
+ std::string last_active_string = database_->GetStateValue(database_key); |
+ |
+ // Check if there was no previous time; this should only happen if the profile |
+ // was last used prior to PerformanceMonitor's integration. Do nothing in this |
+ // case, since the event was prior to the beginning of our recording. |
+ if (last_active_string.empty()) |
+ return; |
+ |
+ base::Time last_active_time; |
+ CHECK(StringToTime(last_active_string, &last_active_time)); |
+ |
+ scoped_ptr<Event> event = |
+ util::CreateUncleanExitEvent(last_active_time, profile_name); |
+ |
+ database_->AddEvent(*event.get()); |
+} |
+ |
+void PerformanceMonitor::UpdateLiveProfiles() { |
+ std::string time = TimeToString(base::Time::Now()); |
+ scoped_ptr<std::set<std::string> > active_profiles( |
+ new std::set<std::string>()); |
+ |
+ for (BrowserList::const_iterator iter = BrowserList::begin(); |
+ iter != BrowserList::end(); ++iter) { |
+ active_profiles->insert((*iter)->profile()->GetDebugName()); |
+ } |
+ |
+ content::BrowserThread::PostBlockingPoolSequencedTask( |
+ Database::kDatabaseSequenceToken, |
+ FROM_HERE, |
+ base::Bind(&PerformanceMonitor::UpdateLiveProfilesHelper, |
+ base::Unretained(this), |
+ base::Passed(active_profiles.Pass()), |
+ time)); |
+} |
+ |
+void PerformanceMonitor::UpdateLiveProfilesHelper( |
+ scoped_ptr<std::set<std::string> > active_profiles, |
+ std::string time) { |
+ CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
+ |
+ for (std::set<std::string>::const_iterator iter = active_profiles->begin(); |
+ iter != active_profiles->end(); ++iter) { |
+ database_->AddStateValue(kStateProfilePrefix + *iter, time); |
+ } |
+} |
+ |
+void PerformanceMonitor::DoTimedCollections() { |
+ UpdateLiveProfiles(); |
+} |
+ |
void PerformanceMonitor::Observe(int type, |
const content::NotificationSource& source, |
const content::NotificationDetails& details) { |
@@ -240,6 +347,18 @@ void PerformanceMonitor::Observe(int type, |
contents->GetURL().spec())); |
break; |
} |
+ case chrome::NOTIFICATION_PROFILE_ADDED: { |
+ Profile* profile = content::Source<Profile>(source).ptr(); |
+ if (!profile->DidLastSessionExitCleanly()) { |
+ content::BrowserThread::PostBlockingPoolSequencedTask( |
+ Database::kDatabaseSequenceToken, |
+ FROM_HERE, |
+ base::Bind(&PerformanceMonitor::AddUncleanExitEvent, |
+ base::Unretained(this), |
+ profile->GetDebugName())); |
+ } |
+ break; |
+ } |
default: { |
NOTREACHED(); |
break; |