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(); |