Commit d0cfca66 authored by dcheng@chromium.org's avatar dcheng@chromium.org

base::Bind() conversion for chrome/browser/search_engines

BUG=none
TEST=compiles and passes tests


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110342 0039d316-1c4b-4281-b951-d872f2087c98
parent 0f52f46c
...@@ -207,7 +207,6 @@ ...@@ -207,7 +207,6 @@
'sys_string_conversions_mac_unittest.mm', 'sys_string_conversions_mac_unittest.mm',
'sys_string_conversions_unittest.cc', 'sys_string_conversions_unittest.cc',
'system_monitor/system_monitor_unittest.cc', 'system_monitor/system_monitor_unittest.cc',
'task_queue_unittest.cc',
'task_unittest.cc', 'task_unittest.cc',
'template_util_unittest.cc', 'template_util_unittest.cc',
'test/trace_event_analyzer_unittest.cc', 'test/trace_event_analyzer_unittest.cc',
......
...@@ -300,8 +300,6 @@ ...@@ -300,8 +300,6 @@
'sys_string_conversions_win.cc', 'sys_string_conversions_win.cc',
'task.cc', 'task.cc',
'task.h', 'task.h',
'task_queue.cc',
'task_queue.h',
'template_util.h', 'template_util.h',
'threading/non_thread_safe.h', 'threading/non_thread_safe.h',
'threading/non_thread_safe_impl.cc', 'threading/non_thread_safe_impl.cc',
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task_queue.h"
#include "base/logging.h"
#include "base/stl_util.h"
TaskQueue::TaskQueue() {
}
TaskQueue::~TaskQueue() {
// We own all the pointes in |queue_|. It is our job to delete them.
STLDeleteElements(&queue_);
}
void TaskQueue::Push(Task* task) {
DCHECK(task);
// Add the task to the back of the queue.
queue_.push_back(task);
}
void TaskQueue::Clear() {
// Delete all the elements in the queue and clear the dead pointers.
STLDeleteElements(&queue_);
}
bool TaskQueue::IsEmpty() const {
return queue_.empty();
}
void TaskQueue::Run() {
// Nothing to run if our queue is empty.
if (queue_.empty())
return;
std::deque<Task*> ready;
queue_.swap(ready);
// Run the tasks that are ready.
std::deque<Task*>::const_iterator task;
for (task = ready.begin(); task != ready.end(); ++task) {
// Run the task and then delete it.
(*task)->Run();
delete (*task);
}
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TASK_QUEUE_H_
#define BASE_TASK_QUEUE_H_
#pragma once
#include <deque>
#include "base/base_export.h"
#include "base/task.h"
// A TaskQueue is a queue of tasks waiting to be run. To run the tasks, call
// the Run method. A task queue is itself a Task so that it can be placed in a
// message loop or another task queue.
class BASE_EXPORT TaskQueue : public Task {
public:
TaskQueue();
virtual ~TaskQueue();
// Push the specified task onto the queue. When the queue is run, the tasks
// will be run in the order they are pushed.
//
// This method takes ownership of |task| and will delete task after it is run
// (or when the TaskQueue is destroyed, if we never got a chance to run it).
void Push(Task* task);
// Remove all tasks from the queue. The tasks are deleted.
void Clear();
// Returns true if this queue contains no tasks.
bool IsEmpty() const;
// Run all the tasks in the queue. New tasks pushed onto the queue during
// a run will be run next time |Run| is called.
virtual void Run() OVERRIDE;
private:
// The list of tasks we are waiting to run.
std::deque<Task*> queue_;
};
#endif // BASE_TASK_QUEUE_H_
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/task.h"
#include "base/task_queue.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Sets bools according to whether Run or the destructor were called.
class TrackCallsTask : public Task {
public:
TrackCallsTask(bool* ran, bool* deleted)
: ran_(ran),
deleted_(deleted) {
*ran_ = false;
*deleted_ = false;
}
virtual ~TrackCallsTask() {
*deleted_ = true;
}
virtual void Run() {
*ran_ = true;
}
private:
bool* ran_;
bool* deleted_;
DISALLOW_COPY_AND_ASSIGN(TrackCallsTask);
};
// Adds a given task to the queue when run.
class TaskQueuerTask : public Task {
public:
TaskQueuerTask(TaskQueue* queue, Task* task_to_queue)
: queue_(queue),
task_to_queue_(task_to_queue) {
}
virtual void Run() {
queue_->Push(task_to_queue_);
}
private:
TaskQueue* queue_;
Task* task_to_queue_;
DISALLOW_COPY_AND_ASSIGN(TaskQueuerTask);
};
} // namespace
TEST(TaskQueueTest, RunNoTasks) {
TaskQueue queue;
EXPECT_TRUE(queue.IsEmpty());
queue.Run();
EXPECT_TRUE(queue.IsEmpty());
}
TEST(TaskQueueTest, RunTasks) {
TaskQueue queue;
bool ran_task1 = false;
bool deleted_task1 = false;
queue.Push(new TrackCallsTask(&ran_task1, &deleted_task1));
bool ran_task2 = false;
bool deleted_task2 = false;
queue.Push(new TrackCallsTask(&ran_task2, &deleted_task2));
queue.Run();
EXPECT_TRUE(ran_task1);
EXPECT_TRUE(deleted_task1);
EXPECT_TRUE(ran_task2);
EXPECT_TRUE(deleted_task2);
EXPECT_TRUE(queue.IsEmpty());
}
TEST(TaskQueueTest, ClearTasks) {
TaskQueue queue;
bool ran_task1 = false;
bool deleted_task1 = false;
queue.Push(new TrackCallsTask(&ran_task1, &deleted_task1));
bool ran_task2 = false;
bool deleted_task2 = false;
queue.Push(new TrackCallsTask(&ran_task2, &deleted_task2));
queue.Clear();
EXPECT_TRUE(queue.IsEmpty());
queue.Run();
EXPECT_FALSE(ran_task1);
EXPECT_TRUE(deleted_task1);
EXPECT_FALSE(ran_task2);
EXPECT_TRUE(deleted_task2);
EXPECT_TRUE(queue.IsEmpty());
}
TEST(TaskQueueTest, OneTaskQueuesMore) {
TaskQueue main_queue;
// Build a task which will queue two more when run.
scoped_ptr<TaskQueue> nested_queue(new TaskQueue());
bool ran_task1 = false;
bool deleted_task1 = false;
nested_queue->Push(
new TaskQueuerTask(&main_queue,
new TrackCallsTask(&ran_task1, &deleted_task1)));
bool ran_task2 = false;
bool deleted_task2 = false;
nested_queue->Push(
new TaskQueuerTask(&main_queue,
new TrackCallsTask(&ran_task2, &deleted_task2)));
main_queue.Push(nested_queue.release());
// Run the task which pushes two more tasks.
main_queue.Run();
// None of the pushed tasks shoudl have run yet.
EXPECT_FALSE(ran_task1);
EXPECT_FALSE(deleted_task1);
EXPECT_FALSE(ran_task2);
EXPECT_FALSE(deleted_task2);
EXPECT_FALSE(main_queue.IsEmpty());
// Now run the nested tasks.
main_queue.Run();
EXPECT_TRUE(ran_task1);
EXPECT_TRUE(deleted_task1);
EXPECT_TRUE(ran_task2);
EXPECT_TRUE(deleted_task2);
EXPECT_TRUE(main_queue.IsEmpty());
}
...@@ -4,12 +4,14 @@ ...@@ -4,12 +4,14 @@
#include "chrome/browser/search_engines/search_provider_install_data.h" #include "chrome/browser/search_engines/search_provider_install_data.h"
#include <algorithm>
#include <functional>
#include <vector> #include <vector>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/task.h"
#include "chrome/browser/search_engines/search_host_to_urls_map.h" #include "chrome/browser/search_engines/search_host_to_urls_map.h"
#include "chrome/browser/search_engines/search_terms_data.h" #include "chrome/browser/search_engines/search_terms_data.h"
#include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url.h"
...@@ -138,9 +140,9 @@ void GoogleURLObserver::Observe(int type, ...@@ -138,9 +140,9 @@ void GoogleURLObserver::Observe(int type,
const content::NotificationDetails& details) { const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) { if (type == chrome::NOTIFICATION_GOOGLE_URL_UPDATED) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
NewRunnableMethod(change_notifier_.get(), base::Bind(&GoogleURLChangeNotifier::OnChange,
&GoogleURLChangeNotifier::OnChange, change_notifier_.get(),
UIThreadSearchTermsData().GoogleBaseURLValue())); UIThreadSearchTermsData().GoogleBaseURLValue()));
} else { } else {
// This must be the death notification. // This must be the death notification.
delete this; delete this;
...@@ -185,16 +187,15 @@ SearchProviderInstallData::~SearchProviderInstallData() { ...@@ -185,16 +187,15 @@ SearchProviderInstallData::~SearchProviderInstallData() {
} }
} }
void SearchProviderInstallData::CallWhenLoaded(Task* task) { void SearchProviderInstallData::CallWhenLoaded(const base::Closure& closure) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (provider_map_.get()) { if (provider_map_.get()) {
task->Run(); closure.Run();
delete task;
return; return;
} }
task_queue_.Push(task); closure_queue_.push_back(closure);
if (load_handle_) if (load_handle_)
return; return;
...@@ -300,7 +301,12 @@ void SearchProviderInstallData::OnLoadFailed() { ...@@ -300,7 +301,12 @@ void SearchProviderInstallData::OnLoadFailed() {
void SearchProviderInstallData::NotifyLoaded() { void SearchProviderInstallData::NotifyLoaded() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
task_queue_.Run(); std::vector<base::Closure> closure_queue;
closure_queue.swap(closure_queue_);
std::for_each(closure_queue.begin(),
closure_queue.end(),
std::mem_fun_ref(&base::Closure::Run));
// Since we expect this request to be rare, clear out the information. This // Since we expect this request to be rare, clear out the information. This
// also keeps the responses current as the search providers change. // also keeps the responses current as the search providers change.
......
...@@ -9,16 +9,15 @@ ...@@ -9,16 +9,15 @@
#include <string> #include <string>
#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/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h" #include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/task_queue.h"
#include "chrome/browser/webdata/web_data_service.h" #include "chrome/browser/webdata/web_data_service.h"
class GURL; class GURL;
class SearchHostToURLsMap; class SearchHostToURLsMap;
class Task;
class TemplateURL; class TemplateURL;
namespace content { namespace content {
...@@ -53,10 +52,10 @@ class SearchProviderInstallData : public WebDataServiceConsumer, ...@@ -53,10 +52,10 @@ class SearchProviderInstallData : public WebDataServiceConsumer,
virtual ~SearchProviderInstallData(); virtual ~SearchProviderInstallData();
// Use to determine when the search provider information is loaded. The // Use to determine when the search provider information is loaded. The
// callback may happen synchronously or asynchronously. This takes ownership // callback may happen synchronously or asynchronously. There is no need to do
// of |task|. There is no need to do anything special to make it function // anything special to make it function (as it just relies on the normal I/O
// (as it just relies on the normal I/O thread message loop). // thread message loop).
void CallWhenLoaded(Task* task); void CallWhenLoaded(const base::Closure& closure);
// Returns the search provider install state for the given origin. // Returns the search provider install state for the given origin.
// This should only be called while a task is called back from CallWhenLoaded. // This should only be called while a task is called back from CallWhenLoaded.
...@@ -84,8 +83,8 @@ class SearchProviderInstallData : public WebDataServiceConsumer, ...@@ -84,8 +83,8 @@ class SearchProviderInstallData : public WebDataServiceConsumer,
// install state has been loaded. // install state has been loaded.
void NotifyLoaded(); void NotifyLoaded();
// The list of tasks to call after the load has finished. // The list of closures to call after the load has finished.
TaskQueue task_queue_; std::vector<base::Closure> closure_queue_;
// Service used to store entries. // Service used to store entries.
scoped_refptr<WebDataService> web_service_; scoped_refptr<WebDataService> web_service_;
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
#include <string> #include <string>
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/task.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/search_engines/search_provider_install_data.h" #include "chrome/browser/search_engines/search_provider_install_data.h"
#include "chrome/browser/search_engines/template_url.h" #include "chrome/browser/search_engines/template_url.h"
...@@ -94,7 +94,7 @@ bool TestGetInstallState::RunTests() { ...@@ -94,7 +94,7 @@ bool TestGetInstallState::RunTests() {
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask( BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask(
FROM_HERE, FROM_HERE,
NewRunnableMethod(this, &TestGetInstallState::StartTestOnIOThread)); base::Bind(&TestGetInstallState::StartTestOnIOThread, this));
// Run the current message loop. When the test is finished on the I/O thread, // Run the current message loop. When the test is finished on the I/O thread,
// it invokes Quit, which unblocks this. // it invokes Quit, which unblocks this.
MessageLoop::current()->Run(); MessageLoop::current()->Run();
...@@ -109,8 +109,7 @@ TestGetInstallState::~TestGetInstallState() { ...@@ -109,8 +109,7 @@ TestGetInstallState::~TestGetInstallState() {
void TestGetInstallState::StartTestOnIOThread() { void TestGetInstallState::StartTestOnIOThread() {
install_data_->CallWhenLoaded( install_data_->CallWhenLoaded(
NewRunnableMethod(this, base::Bind(&TestGetInstallState::DoInstallStateTests, this));
&TestGetInstallState::DoInstallStateTests));
} }
void TestGetInstallState::DoInstallStateTests() { void TestGetInstallState::DoInstallStateTests() {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/search_engines/search_provider_install_state_message_filter.h" #include "chrome/browser/search_engines/search_provider_install_state_message_filter.h"
#include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/render_messages.h" #include "chrome/common/render_messages.h"
...@@ -19,8 +20,7 @@ SearchProviderInstallStateMessageFilter:: ...@@ -19,8 +20,7 @@ SearchProviderInstallStateMessageFilter::
SearchProviderInstallStateMessageFilter( SearchProviderInstallStateMessageFilter(
int render_process_id, int render_process_id,
Profile* profile) Profile* profile)
: ALLOW_THIS_IN_INITIALIZER_LIST( : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
reply_with_provider_install_state_factory_(this)),
provider_data_(profile->GetWebDataService(Profile::EXPLICIT_ACCESS), provider_data_(profile->GetWebDataService(Profile::EXPLICIT_ACCESS),
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::Source<RenderProcessHost>( content::Source<RenderProcessHost>(
...@@ -88,9 +88,10 @@ SearchProviderInstallStateMessageFilter::OnMsgGetSearchProviderInstallState( ...@@ -88,9 +88,10 @@ SearchProviderInstallStateMessageFilter::OnMsgGetSearchProviderInstallState(
const GURL& requested_host, const GURL& requested_host,
IPC::Message* reply_msg) { IPC::Message* reply_msg) {
provider_data_.CallWhenLoaded( provider_data_.CallWhenLoaded(
reply_with_provider_install_state_factory_.NewRunnableMethod( base::Bind(
&SearchProviderInstallStateMessageFilter:: &SearchProviderInstallStateMessageFilter::
ReplyWithProviderInstallState, ReplyWithProviderInstallState,
weak_factory_.GetWeakPtr(),
page_location, page_location,
requested_host, requested_host,
reply_msg)); reply_msg));
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_STATE_MESSAGE_FILTER_H_ #ifndef CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_STATE_MESSAGE_FILTER_H_
#define CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_STATE_MESSAGE_FILTER_H_ #define CHROME_BROWSER_SEARCH_ENGINES_SEARCH_PROVIDER_INSTALL_STATE_MESSAGE_FILTER_H_
#include "base/memory/weak_ptr.h"
#include "chrome/browser/search_engines/search_provider_install_data.h" #include "chrome/browser/search_engines/search_provider_install_data.h"
#include "chrome/common/search_provider.h" #include "chrome/common/search_provider.h"
#include "content/browser/browser_message_filter.h" #include "content/browser/browser_message_filter.h"
...@@ -41,8 +42,7 @@ class SearchProviderInstallStateMessageFilter : public BrowserMessageFilter { ...@@ -41,8 +42,7 @@ class SearchProviderInstallStateMessageFilter : public BrowserMessageFilter {
IPC::Message* reply_msg); IPC::Message* reply_msg);
// Used to schedule invocations of ReplyWithProviderInstallState. // Used to schedule invocations of ReplyWithProviderInstallState.
ScopedRunnableMethodFactory<SearchProviderInstallStateMessageFilter> base::WeakPtrFactory<SearchProviderInstallStateMessageFilter> weak_factory_;
reply_with_provider_install_state_factory_;
// Used to do a load and get information about install states. // Used to do a load and get information about install states.
SearchProviderInstallData provider_data_; SearchProviderInstallData provider_data_;
......
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