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

Unified Diff: chrome/browser/chromeos/system/statistics_provider.cc

Issue 10078017: Added asynchronous notification of readiness to the StatisticsProvider, and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/system/statistics_provider.cc
diff --git a/chrome/browser/chromeos/system/statistics_provider.cc b/chrome/browser/chromeos/system/statistics_provider.cc
index 0d6798f8f6890f4ac82208f2c60eca24ffe59bfe..c93956cc6ecb6af6450a73a5f4213f59b7cc2297 100644
--- a/chrome/browser/chromeos/system/statistics_provider.cc
+++ b/chrome/browser/chromeos/system/statistics_provider.cc
@@ -10,6 +10,7 @@
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
+#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/time.h"
#include "base/chromeos/chromeos_version.h"
@@ -71,6 +72,8 @@ class StatisticsProviderImpl : public StatisticsProvider {
virtual bool GetMachineStatistic(const std::string& name,
std::string* result) OVERRIDE;
+ virtual void WhenReady(const base::Closure& callback) OVERRIDE;
+
static StatisticsProviderImpl* GetInstance();
private:
@@ -84,8 +87,12 @@ class StatisticsProviderImpl : public StatisticsProvider {
// Loads the machine statistcs by examining the system.
void LoadMachineStatistics();
+ // Invokes all the callbacks.
+ void NotifyOnUI();
+
NameValuePairsParser::NameValueMap machine_info_;
base::WaitableEvent on_statistics_loaded_;
+ std::vector<base::Closure> callbacks_;
DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
};
@@ -117,6 +124,16 @@ bool StatisticsProviderImpl::GetMachineStatistic(
return false;
}
+void StatisticsProviderImpl::WhenReady(const base::Closure& callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (on_statistics_loaded_.IsSignaled()) {
+ // Don't assume the caller is re-entrant.
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
+ } else {
+ callbacks_.push_back(callback);
+ }
+}
+
// manual_reset needs to be true, as we want to keep the signaled state.
StatisticsProviderImpl::StatisticsProviderImpl()
: on_statistics_loaded_(true /* manual_reset */,
@@ -166,6 +183,13 @@ void StatisticsProviderImpl::LoadMachineStatistics() {
on_statistics_loaded_.Signal();
VLOG(1) << "Finished loading statistics";
+ // Any callbacks registed before Signal() are in |callbacks_|. Any registed
+ // after Signal() will be immediately posted. NotifyOnUI() posts the callbacks
+ // in |callbacks_| from the UI loop.
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&StatisticsProviderImpl::NotifyOnUI,
+ base::Unretained(this)));
+
#if defined(GOOGLE_CHROME_BUILD)
// TODO(kochi): This is for providing a channel information to
// chrome::VersionInfo::GetChannel()/GetVersionStringModifier(),
@@ -190,6 +214,15 @@ void StatisticsProviderImpl::LoadMachineStatistics() {
#endif
}
+void StatisticsProviderImpl::NotifyOnUI() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ for (std::vector<base::Closure>::iterator it = callbacks_.begin();
+ it != callbacks_.end(); ++it) {
+ it->Run();
+ }
+ callbacks_.clear();
+}
+
StatisticsProviderImpl* StatisticsProviderImpl::GetInstance() {
return Singleton<StatisticsProviderImpl,
DefaultSingletonTraits<StatisticsProviderImpl> >::get();
@@ -204,6 +237,10 @@ class StatisticsProviderStubImpl : public StatisticsProvider {
return false;
}
+ virtual void WhenReady(const base::Closure& callback) OVERRIDE {
+ callback.Run();
+ }
+
static StatisticsProviderStubImpl* GetInstance() {
return Singleton<StatisticsProviderStubImpl,
DefaultSingletonTraits<StatisticsProviderStubImpl> >::get();

Powered by Google App Engine
This is Rietveld 408576698