Commit 2226c22b authored by dcheng@chromium.org's avatar dcheng@chromium.org

base::Bind() conversion for MetricsService.

BUG=none
TEST=trybots


Review URL: http://codereview.chromium.org/8603013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111047 0039d316-1c4b-4281-b951-d872f2087c98
parent f519295f
...@@ -28,7 +28,6 @@ static const int kNeverUsableSequenceNumber = -2; ...@@ -28,7 +28,6 @@ static const int kNeverUsableSequenceNumber = -2;
HistogramSynchronizer::HistogramSynchronizer() HistogramSynchronizer::HistogramSynchronizer()
: lock_(), : lock_(),
received_all_renderer_histograms_(&lock_), received_all_renderer_histograms_(&lock_),
callback_task_(NULL),
callback_thread_(NULL), callback_thread_(NULL),
last_used_sequence_number_(kNeverUsableSequenceNumber), last_used_sequence_number_(kNeverUsableSequenceNumber),
async_sequence_number_(kNeverUsableSequenceNumber), async_sequence_number_(kNeverUsableSequenceNumber),
...@@ -41,7 +40,7 @@ HistogramSynchronizer::HistogramSynchronizer() ...@@ -41,7 +40,7 @@ HistogramSynchronizer::HistogramSynchronizer()
HistogramSynchronizer::~HistogramSynchronizer() { HistogramSynchronizer::~HistogramSynchronizer() {
// Just in case we have any pending tasks, clear them out. // Just in case we have any pending tasks, clear them out.
SetCallbackTaskAndThread(NULL, NULL); SetCallbackTaskAndThread(NULL, base::Closure());
histogram_synchronizer_ = NULL; histogram_synchronizer_ = NULL;
} }
...@@ -78,21 +77,21 @@ void HistogramSynchronizer::FetchRendererHistogramsSynchronously( ...@@ -78,21 +77,21 @@ void HistogramSynchronizer::FetchRendererHistogramsSynchronously(
// static // static
void HistogramSynchronizer::FetchRendererHistogramsAsynchronously( void HistogramSynchronizer::FetchRendererHistogramsAsynchronously(
MessageLoop* callback_thread, MessageLoop* callback_thread,
Task* callback_task, const base::Closure& callback,
int wait_time) { int wait_time) {
DCHECK(callback_thread != NULL); DCHECK(callback_thread != NULL);
DCHECK(callback_task != NULL); DCHECK(!callback.is_null());
HistogramSynchronizer* current_synchronizer = CurrentSynchronizer(); HistogramSynchronizer* current_synchronizer = CurrentSynchronizer();
if (current_synchronizer == NULL) { if (current_synchronizer == NULL) {
// System teardown is happening. // System teardown is happening.
callback_thread->PostTask(FROM_HERE, callback_task); callback_thread->PostTask(FROM_HERE, callback);
return; return;
} }
current_synchronizer->SetCallbackTaskAndThread(callback_thread, current_synchronizer->SetCallbackTaskAndThread(callback_thread,
callback_task); callback);
int sequence_number = int sequence_number =
current_synchronizer->NotifyAllRenderers(ASYNC_HISTOGRAMS); current_synchronizer->NotifyAllRenderers(ASYNC_HISTOGRAMS);
...@@ -176,16 +175,16 @@ void HistogramSynchronizer::DecrementPendingRenderers(int sequence_number) { ...@@ -176,16 +175,16 @@ void HistogramSynchronizer::DecrementPendingRenderers(int sequence_number) {
void HistogramSynchronizer::SetCallbackTaskAndThread( void HistogramSynchronizer::SetCallbackTaskAndThread(
MessageLoop* callback_thread, MessageLoop* callback_thread,
Task* callback_task) { const base::Closure& callback) {
Task* old_task = NULL; base::Closure old_callback;
MessageLoop* old_thread = NULL; MessageLoop* old_thread = NULL;
TimeTicks old_start_time; TimeTicks old_start_time;
int unresponsive_renderers; int unresponsive_renderers;
const TimeTicks now = TimeTicks::Now(); const TimeTicks now = TimeTicks::Now();
{ {
base::AutoLock auto_lock(lock_); base::AutoLock auto_lock(lock_);
old_task = callback_task_; old_callback = callback_;
callback_task_ = callback_task; callback_ = callback;
old_thread = callback_thread_; old_thread = callback_thread_;
callback_thread_ = callback_thread; callback_thread_ = callback_thread;
unresponsive_renderers = async_renderers_pending_; unresponsive_renderers = async_renderers_pending_;
...@@ -195,13 +194,13 @@ void HistogramSynchronizer::SetCallbackTaskAndThread( ...@@ -195,13 +194,13 @@ void HistogramSynchronizer::SetCallbackTaskAndThread(
async_sequence_number_ = kNeverUsableSequenceNumber; async_sequence_number_ = kNeverUsableSequenceNumber;
} }
// Just in case there was a task pending.... // Just in case there was a task pending....
InternalPostTask(old_thread, old_task, unresponsive_renderers, InternalPostTask(old_thread, old_callback, unresponsive_renderers,
old_start_time); old_start_time);
} }
void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback( void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
int sequence_number) { int sequence_number) {
Task* task = NULL; base::Closure callback;
MessageLoop* thread = NULL; MessageLoop* thread = NULL;
TimeTicks started; TimeTicks started;
int unresponsive_renderers; int unresponsive_renderers;
...@@ -209,20 +208,21 @@ void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback( ...@@ -209,20 +208,21 @@ void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback(
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
if (sequence_number != async_sequence_number_) if (sequence_number != async_sequence_number_)
return; return;
task = callback_task_; callback = callback_;
thread = callback_thread_; thread = callback_thread_;
callback_task_ = NULL; callback_.Reset();
callback_thread_ = NULL; callback_thread_ = NULL;
started = async_callback_start_time_; started = async_callback_start_time_;
unresponsive_renderers = async_renderers_pending_; unresponsive_renderers = async_renderers_pending_;
} }
InternalPostTask(thread, task, unresponsive_renderers, started); InternalPostTask(thread, callback, unresponsive_renderers, started);
} }
void HistogramSynchronizer::InternalPostTask(MessageLoop* thread, Task* task, void HistogramSynchronizer::InternalPostTask(MessageLoop* thread,
const base::Closure& callback,
int unresponsive_renderers, int unresponsive_renderers,
const base::TimeTicks& started) { const base::TimeTicks& started) {
if (!task || !thread) if (callback.is_null() || !thread)
return; return;
UMA_HISTOGRAM_COUNTS("Histogram.RendersNotRespondingAsynchronous", UMA_HISTOGRAM_COUNTS("Histogram.RendersNotRespondingAsynchronous",
unresponsive_renderers); unresponsive_renderers);
...@@ -231,7 +231,7 @@ void HistogramSynchronizer::InternalPostTask(MessageLoop* thread, Task* task, ...@@ -231,7 +231,7 @@ void HistogramSynchronizer::InternalPostTask(MessageLoop* thread, Task* task,
TimeTicks::Now() - started); TimeTicks::Now() - started);
} }
thread->PostTask(FROM_HERE, task); thread->PostTask(FROM_HERE, callback);
} }
int HistogramSynchronizer::GetNextAvailableSequenceNumber( int HistogramSynchronizer::GetNextAvailableSequenceNumber(
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
#include <vector> #include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/synchronization/condition_variable.h" #include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "base/time.h" #include "base/time.h"
class MessageLoop; class MessageLoop;
class Task;
// This class maintains state that is used to upload histogram data from the // This class maintains state that is used to upload histogram data from the
// various renderer processes, into the browser process. Such transactions are // various renderer processes, into the browser process. Such transactions are
...@@ -75,10 +75,12 @@ class HistogramSynchronizer : public ...@@ -75,10 +75,12 @@ class HistogramSynchronizer : public
// Contact all renderers, and get them to upload to the browser any/all // Contact all renderers, and get them to upload to the browser any/all
// changes to histograms. When all changes have been acquired, or when the // changes to histograms. When all changes have been acquired, or when the
// wait time expires (whichever is sooner), post the callback_task to the // wait time expires (whichever is sooner), post the callback to the
// specified thread. Note the callback_task is posted exactly once. // specified message loop. Note the callback is posted exactly once.
static void FetchRendererHistogramsAsynchronously( static void FetchRendererHistogramsAsynchronously(
MessageLoop* callback_thread, Task* callback_task, int wait_time); MessageLoop* callback_thread,
const base::Closure& callback,
int wait_time);
// This method is called on the IO thread. Deserializes the histograms and // This method is called on the IO thread. Deserializes the histograms and
// records that we have received histograms from a renderer process. // records that we have received histograms from a renderer process.
...@@ -102,13 +104,13 @@ class HistogramSynchronizer : public ...@@ -102,13 +104,13 @@ class HistogramSynchronizer : public
// either signal the waiting process or call the callback function. // either signal the waiting process or call the callback function.
void DecrementPendingRenderers(int sequence_number); void DecrementPendingRenderers(int sequence_number);
// Set the callback_thread_ and callback_task_ members. If these members // Set the callback_thread_ and callback_ members. If these members already
// already had values, then as a side effect, post the old callback_task_ to // had values, then as a side effect, post the old callback_ to the old
// the old callaback_thread_. This side effect should not generally happen, // callaback_thread_. This side effect should not generally happen, but is in
// but is in place to assure correctness (that any tasks that were set, are // place to assure correctness (that any tasks that were set, are eventually
// eventually called, and never merely discarded). // called, and never merely discarded).
void SetCallbackTaskAndThread(MessageLoop* callback_thread, void SetCallbackTaskAndThread(MessageLoop* callback_thread,
Task* callback_task); const base::Closure& callback);
void ForceHistogramSynchronizationDoneCallback(int sequence_number); void ForceHistogramSynchronizationDoneCallback(int sequence_number);
...@@ -118,8 +120,10 @@ class HistogramSynchronizer : public ...@@ -118,8 +120,10 @@ class HistogramSynchronizer : public
int renderer_count); int renderer_count);
// Internal helper function, to post task, and record callback stats. // Internal helper function, to post task, and record callback stats.
void InternalPostTask(MessageLoop* thread, Task* task, void InternalPostTask(MessageLoop* thread,
int unresponsive_renderers, const base::TimeTicks& started); const base::Closure& callback,
int unresponsive_renderers,
const base::TimeTicks& started);
// This lock_ protects access to all members. // This lock_ protects access to all members.
base::Lock lock_; base::Lock lock_;
...@@ -130,8 +134,8 @@ class HistogramSynchronizer : public ...@@ -130,8 +134,8 @@ class HistogramSynchronizer : public
// When a request is made to asynchronously update the histograms, we store // When a request is made to asynchronously update the histograms, we store
// the task and thread we use to post a completion notification in // the task and thread we use to post a completion notification in
// callback_task_ and callback_thread_. // callback_ and callback_thread_.
Task* callback_task_; base::Closure callback_;
MessageLoop* callback_thread_; MessageLoop* callback_thread_;
// We don't track the actual renderers that are contacted for an update, only // We don't track the actual renderers that are contacted for an update, only
......
...@@ -264,16 +264,17 @@ struct MetricsService::ChildProcessStats { ...@@ -264,16 +264,17 @@ struct MetricsService::ChildProcessStats {
// Will run the provided task after finished. // Will run the provided task after finished.
class MetricsMemoryDetails : public MemoryDetails { class MetricsMemoryDetails : public MemoryDetails {
public: public:
explicit MetricsMemoryDetails(Task* completion) : completion_(completion) {} explicit MetricsMemoryDetails(const base::Closure& callback)
: callback_(callback) {}
virtual void OnDetailsAvailable() { virtual void OnDetailsAvailable() {
MessageLoop::current()->PostTask(FROM_HERE, completion_); MessageLoop::current()->PostTask(FROM_HERE, callback_);
} }
private: private:
~MetricsMemoryDetails() {} ~MetricsMemoryDetails() {}
Task* completion_; base::Closure callback_;
DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails);
}; };
...@@ -875,10 +876,12 @@ void MetricsService::StartScheduledUpload() { ...@@ -875,10 +876,12 @@ void MetricsService::StartScheduledUpload() {
DCHECK(!waiting_for_asynchronus_reporting_step_); DCHECK(!waiting_for_asynchronus_reporting_step_);
waiting_for_asynchronus_reporting_step_ = true; waiting_for_asynchronus_reporting_step_ = true;
Task* task = log_sender_factory_. base::Closure callback =
NewRunnableMethod(&MetricsService::OnMemoryDetailCollectionDone); base::Bind(&MetricsService::OnMemoryDetailCollectionDone,
log_sender_factory_.GetWeakPtr());
scoped_refptr<MetricsMemoryDetails> details(new MetricsMemoryDetails(task)); scoped_refptr<MetricsMemoryDetails> details(
new MetricsMemoryDetails(callback));
details->StartFetch(); details->StartFetch();
// Collect WebCore cache information to put into a histogram. // Collect WebCore cache information to put into a histogram.
...@@ -902,8 +905,9 @@ void MetricsService::OnMemoryDetailCollectionDone() { ...@@ -902,8 +905,9 @@ void MetricsService::OnMemoryDetailCollectionDone() {
// OnHistogramSynchronizationDone to continue processing. // OnHistogramSynchronizationDone to continue processing.
// Create a callback_task for OnHistogramSynchronizationDone. // Create a callback_task for OnHistogramSynchronizationDone.
Task* callback_task = log_sender_factory_.NewRunnableMethod( base::Closure callback = base::Bind(
&MetricsService::OnHistogramSynchronizationDone); &MetricsService::OnHistogramSynchronizationDone,
log_sender_factory_.GetWeakPtr());
base::StatisticsRecorder::CollectHistogramStats("Browser"); base::StatisticsRecorder::CollectHistogramStats("Browser");
...@@ -911,7 +915,7 @@ void MetricsService::OnMemoryDetailCollectionDone() { ...@@ -911,7 +915,7 @@ void MetricsService::OnMemoryDetailCollectionDone() {
// renderer processes. Wait time specifies how long to wait before absolutely // renderer processes. Wait time specifies how long to wait before absolutely
// calling us back on the task. // calling us back on the task.
HistogramSynchronizer::FetchRendererHistogramsAsynchronously( HistogramSynchronizer::FetchRendererHistogramsAsynchronously(
MessageLoop::current(), callback_task, MessageLoop::current(), callback,
kMaxHistogramGatheringWaitDuration); kMaxHistogramGatheringWaitDuration);
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/process_util.h" #include "base/process_util.h"
#include "chrome/browser/io_thread.h" #include "chrome/browser/io_thread.h"
#include "chrome/common/metrics_helpers.h" #include "chrome/common/metrics_helpers.h"
...@@ -369,7 +370,7 @@ class MetricsService : public content::NotificationObserver, ...@@ -369,7 +370,7 @@ class MetricsService : public content::NotificationObserver,
struct ChildProcessStats; struct ChildProcessStats;
std::map<string16, ChildProcessStats> child_process_stats_buffer_; std::map<string16, ChildProcessStats> child_process_stats_buffer_;
ScopedRunnableMethodFactory<MetricsService> log_sender_factory_; base::WeakPtrFactory<MetricsService> log_sender_factory_;
base::WeakPtrFactory<MetricsService> state_saver_factory_; base::WeakPtrFactory<MetricsService> state_saver_factory_;
// Dictionary containing all the profile specific metrics. This is set // Dictionary containing all the profile specific metrics. This is set
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment