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

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: Don't use base::Unretained. Rebased. 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 f7b88ff1d3504bf5ea5f538d4e6a5ce5be6fbfdf..66a0cf3796e18cd3e7b2dae8e3d836819cfe02b9 100644
--- a/chrome/browser/chromeos/system/statistics_provider.cc
+++ b/chrome/browser/chromeos/system/statistics_provider.cc
@@ -5,11 +5,13 @@
#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/chromeos/chromeos_version.h"
#include "base/file_path.h"
#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"
@@ -62,6 +64,15 @@ const char kVpdDelim[] = "\n";
// Timeout that we should wait for statistics to get loaded
const int kTimeoutSecs = 3;
+// Helper to invoke |callbacks| on the UI thread.
+void NotifyOnUI(std::vector<base::Closure>* callbacks) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ for (std::vector<base::Closure>::iterator it = callbacks->begin();
+ it != callbacks->end(); ++it) {
+ it->Run();
+ }
+}
+
} // namespace
// The StatisticsProvider implementation used in production.
@@ -71,6 +82,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:
@@ -86,6 +99,7 @@ class StatisticsProviderImpl : public StatisticsProvider {
NameValuePairsParser::NameValueMap machine_info_;
base::WaitableEvent on_statistics_loaded_;
+ std::vector<base::Closure> callbacks_;
DISALLOW_COPY_AND_ASSIGN(StatisticsProviderImpl);
};
@@ -123,6 +137,16 @@ bool StatisticsProviderImpl::GetMachineStatistic(
return false;
jar (doing other things) 2012/04/24 18:37:40 This API makes it impossible to tell whether there
Joao da Silva 2012/04/27 09:47:48 This problem doesn't exist with the async notifica
jar (doing other things) 2012/04/27 18:00:38 I think what you're saying is that you plan to rem
Joao da Silva 2012/05/15 13:52:47 This API is used in a couple of places that aren't
}
+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 */,
@@ -172,6 +196,14 @@ 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.
+ std::vector<base::Closure>* callbacks = new std::vector<base::Closure>();
+ callbacks->swap(callbacks_);
jar (doing other things) 2012/04/24 18:37:40 What thread are you running on? Are you ensured t
Joao da Silva 2012/04/27 09:47:48 This happens in the blocking pool, but the callbac
jar (doing other things) 2012/04/27 18:00:38 It appears as though the line: callbacks_.push_ba
Joao da Silva 2012/05/15 13:52:47 You're totally right. I tried to make this a stati
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(NotifyOnUI, base::Owned(callbacks)));
+
#if defined(GOOGLE_CHROME_BUILD)
// TODO(kochi): This is for providing a channel information to
// chrome::VersionInfo::GetChannel()/GetVersionStringModifier(),
@@ -210,6 +242,11 @@ class StatisticsProviderStubImpl : public StatisticsProvider {
return false;
}
+ virtual void WhenReady(const base::Closure& callback) OVERRIDE {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
+ }
+
static StatisticsProviderStubImpl* GetInstance() {
return Singleton<StatisticsProviderStubImpl,
DefaultSingletonTraits<StatisticsProviderStubImpl> >::get();

Powered by Google App Engine
This is Rietveld 408576698