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