summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-17 01:20:36 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-17 01:20:36 +0000
commit8c6517e5e15b7e9a3d3c95f697f674d221968acc (patch)
tree315dfbae873b9b79e2aa62bc1b51de1c307189b7 /chrome/browser/automation
parent83fc28d5f4523f516154f63c08a084d26d9aab6e (diff)
downloadchromium_src-8c6517e5e15b7e9a3d3c95f697f674d221968acc.zip
chromium_src-8c6517e5e15b7e9a3d3c95f697f674d221968acc.tar.gz
chromium_src-8c6517e5e15b7e9a3d3c95f697f674d221968acc.tar.bz2
base::Bind: Cleanup in automation.
BUG=none TEST=none R=csilv@chromium.org Review URL: http://codereview.chromium.org/8212006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105761 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/automation')
-rw-r--r--chrome/browser/automation/automation_provider_gtk.cc121
-rw-r--r--chrome/browser/automation/automation_provider_win.cc3
-rw-r--r--chrome/browser/automation/automation_resource_message_filter.cc31
-rw-r--r--chrome/browser/automation/automation_resource_message_filter.h2
-rw-r--r--chrome/browser/automation/automation_util.cc34
-rw-r--r--chrome/browser/automation/testing_automation_provider.cc11
-rw-r--r--chrome/browser/automation/testing_automation_provider_views.cc12
-rw-r--r--chrome/browser/automation/ui_controls.h24
-rw-r--r--chrome/browser/automation/ui_controls_gtk.cc30
-rw-r--r--chrome/browser/automation/ui_controls_internal.cc14
-rw-r--r--chrome/browser/automation/ui_controls_internal.h20
-rw-r--r--chrome/browser/automation/ui_controls_mac.mm82
-rw-r--r--chrome/browser/automation/ui_controls_win.cc47
-rw-r--r--chrome/browser/automation/url_request_automation_job.cc15
-rw-r--r--chrome/browser/automation/url_request_automation_job.h5
15 files changed, 188 insertions, 263 deletions
diff --git a/chrome/browser/automation/automation_provider_gtk.cc b/chrome/browser/automation/automation_provider_gtk.cc
index 11b6d31..0dfec43 100644
--- a/chrome/browser/automation/automation_provider_gtk.cc
+++ b/chrome/browser/automation/automation_provider_gtk.cc
@@ -6,6 +6,9 @@
#include <gtk/gtk.h>
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/callback.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/automation/automation_browser_tracker.h"
#include "chrome/browser/automation/automation_window_tracker.h"
@@ -18,90 +21,21 @@
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
-void AutomationProvider::PrintAsync(int tab_handle) {
- NOTIMPLEMENTED();
-}
-
-// This task sends a WindowDragResponse message with the appropriate
-// routing ID to the automation proxy. This is implemented as a task so that
-// we know that the mouse events (and any tasks that they spawn on the message
-// loop) have been processed by the time this is sent.
-class WindowDragResponseTask : public Task {
- public:
- WindowDragResponseTask(AutomationProvider* provider,
- IPC::Message* reply_message)
- : provider_(provider),
- reply_message_(reply_message) {
- DCHECK(provider_);
- DCHECK(reply_message_);
- }
-
- virtual ~WindowDragResponseTask() {
- }
+namespace {
- virtual void Run() {
- AutomationMsg_WindowDrag::WriteReplyParams(reply_message_, true);
- provider_->Send(reply_message_);
- }
-
- private:
- AutomationProvider* provider_;
- IPC::Message* reply_message_;
-
- DISALLOW_COPY_AND_ASSIGN(WindowDragResponseTask);
-};
-
-// A task that just runs a SendMouseEvent and performs another task when done.
-class MouseEventTask : public Task {
- public:
- MouseEventTask(Task* next_task, ui_controls::MouseButtonState state)
- : next_task_(next_task),
- state_(state) {}
-
- virtual ~MouseEventTask() {
- }
-
- virtual void Run() {
- ui_controls::SendMouseEventsNotifyWhenDone(ui_controls::LEFT, state_,
- next_task_);
- }
-
- private:
- // The task to execute when we are done.
- Task* next_task_;
-
- // Mouse press or mouse release.
- ui_controls::MouseButtonState state_;
-
- DISALLOW_COPY_AND_ASSIGN(MouseEventTask);
-};
-
-// A task that just runs a SendMouseMove and performs another task when done.
-class MouseMoveTask : public Task {
- public:
- MouseMoveTask(Task* next_task, int absolute_x, int absolute_y)
- : next_task_(next_task),
- x_(absolute_x),
- y_(absolute_y) {
- }
-
- virtual ~MouseMoveTask() {
- }
-
- virtual void Run() {
- ui_controls::SendMouseMoveNotifyWhenDone(x_, y_, next_task_);
- }
-
- private:
- // The task to execute when we are done.
- Task* next_task_;
+// This function sends a WindowDragResponse message with the appropriate routing
+// ID to the automation proxy.
+void SendWindowDragResponse(AutomationProvider* provider,
+ IPC::Message* reply_message) {
+ AutomationMsg_WindowDrag::WriteReplyParams(reply_message, true);
+ provider->Send(reply_message);
+}
- // Coordinates of the press.
- int x_;
- int y_;
+} // namespace
- DISALLOW_COPY_AND_ASSIGN(MouseMoveTask);
-};
+void AutomationProvider::PrintAsync(int tab_handle) {
+ NOTIMPLEMENTED();
+}
void AutomationProvider::WindowSimulateDrag(
int handle,
@@ -117,9 +51,14 @@ void AutomationProvider::WindowSimulateDrag(
gdk_window_get_position(GTK_WIDGET(window)->window, &x, &y);
// Create a nested stack of tasks to run.
- Task* next_task = new WindowDragResponseTask(this, reply_message);
- next_task = new MouseEventTask(next_task, ui_controls::UP);
- next_task = new MouseEventTask(next_task, ui_controls::UP);
+ base::Closure drag_response_cb = base::Bind(
+ &SendWindowDragResponse, make_scoped_refptr(this), reply_message);
+ base::Closure move_chain_cb = base::IgnoreReturn<bool>(
+ base::Bind(&ui_controls::SendMouseEventsNotifyWhenDone,
+ ui_controls::LEFT, ui_controls::UP, drag_response_cb));
+ move_chain_cb = base::IgnoreReturn<bool>(
+ base::Bind(&ui_controls::SendMouseEventsNotifyWhenDone,
+ ui_controls::LEFT, ui_controls::UP, move_chain_cb));
for (size_t i = drag_path.size() - 1; i > 0; --i) {
// Smooth out the mouse movements by adding intermediate points. This
// better simulates a real user drag.
@@ -128,14 +67,20 @@ void AutomationProvider::WindowSimulateDrag(
int half_step_x = (dest_x + drag_path[i - 1].x() + x) / 2;
int half_step_y = (dest_y + drag_path[i - 1].y() + y) / 2;
- next_task = new MouseMoveTask(next_task, dest_x, dest_y);
- next_task = new MouseMoveTask(next_task, half_step_x, half_step_y);
+ move_chain_cb = base::IgnoreReturn<bool>(
+ base::Bind(&ui_controls::SendMouseMoveNotifyWhenDone, dest_x, dest_y,
+ move_chain_cb));
+ move_chain_cb = base::IgnoreReturn<bool>(
+ base::Bind(&ui_controls::SendMouseMoveNotifyWhenDone, half_step_x,
+ half_step_y, move_chain_cb));
}
- next_task = new MouseEventTask(next_task, ui_controls::DOWN);
+ move_chain_cb = base::IgnoreReturn<bool>(
+ base::Bind(&ui_controls::SendMouseEventsNotifyWhenDone,
+ ui_controls::LEFT, ui_controls::DOWN, move_chain_cb));
ui_controls::SendMouseMoveNotifyWhenDone(x + drag_path[0].x(),
y + drag_path[0].y(),
- next_task);
+ move_chain_cb);
} else {
AutomationMsg_WindowDrag::WriteReplyParams(reply_message, false);
Send(reply_message);
diff --git a/chrome/browser/automation/automation_provider_win.cc b/chrome/browser/automation/automation_provider_win.cc
index e624962..95e28ea 100644
--- a/chrome/browser/automation/automation_provider_win.cc
+++ b/chrome/browser/automation/automation_provider_win.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/automation/automation_provider.h"
+#include "base/callback.h"
#include "base/debug/trace_event.h"
#include "base/json/json_reader.h"
#include "base/utf_string_conversions.h"
@@ -211,7 +212,7 @@ void AutomationProvider::WindowSimulateDrag(
ui::EF_SHIFT_DOWN),
((flags & ui::EF_ALT_DOWN) == ui::EF_ALT_DOWN),
false,
- new MessageLoop::QuitTask());
+ MessageLoop::QuitClosure());
MessageLoopForUI* loop = MessageLoopForUI::current();
bool did_allow_task_nesting = loop->NestableTasksAllowed();
loop->SetNestableTasksAllowed(true);
diff --git a/chrome/browser/automation/automation_resource_message_filter.cc b/chrome/browser/automation/automation_resource_message_filter.cc
index 664efcf..9206d24 100644
--- a/chrome/browser/automation/automation_resource_message_filter.cc
+++ b/chrome/browser/automation/automation_resource_message_filter.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/automation/automation_resource_message_filter.h"
+#include "base/bind.h"
#include "base/path_service.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
@@ -64,8 +65,7 @@ AutomationResourceMessageFilter::AutomationResourceMessageFilter()
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(
- URLRequestAutomationJob::EnsureProtocolFactoryRegistered));
+ base::Bind(&URLRequestAutomationJob::EnsureProtocolFactoryRegistered));
}
AutomationResourceMessageFilter::~AutomationResourceMessageFilter() {
@@ -223,13 +223,9 @@ bool AutomationResourceMessageFilter::RegisterRenderView(
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(
- AutomationResourceMessageFilter::RegisterRenderViewInIOThread,
- renderer_pid,
- renderer_id,
- tab_handle,
- make_scoped_refptr(filter),
- pending_view));
+ base::Bind(&AutomationResourceMessageFilter::RegisterRenderViewInIOThread,
+ renderer_pid, renderer_id, tab_handle,
+ make_scoped_refptr(filter), pending_view));
return true;
}
@@ -237,8 +233,8 @@ void AutomationResourceMessageFilter::UnRegisterRenderView(
int renderer_pid, int renderer_id) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(
- AutomationResourceMessageFilter::UnRegisterRenderViewInIOThread,
+ base::Bind(
+ &AutomationResourceMessageFilter::UnRegisterRenderViewInIOThread,
renderer_pid, renderer_id));
}
@@ -252,12 +248,9 @@ bool AutomationResourceMessageFilter::ResumePendingRenderView(
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(
- AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread,
- renderer_pid,
- renderer_id,
- tab_handle,
- make_scoped_refptr(filter)));
+ base::Bind(
+ &AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread,
+ renderer_pid, renderer_id, tab_handle, make_scoped_refptr(filter)));
return true;
}
@@ -299,7 +292,7 @@ void AutomationResourceMessageFilter::UnRegisterRenderViewInIOThread(
}
// static
-bool AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread(
+void AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread(
int renderer_pid, int renderer_id, int tab_handle,
AutomationResourceMessageFilter* filter) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
@@ -314,7 +307,6 @@ bool AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread(
<< renderer_pid
<< ", render view id:"
<< renderer_id;
- return false;
}
DCHECK(automation_details_iter->second.is_pending_render_view);
@@ -327,7 +319,6 @@ bool AutomationResourceMessageFilter::ResumePendingRenderViewInIOThread(
AutomationDetails(tab_handle, filter, false);
ResumeJobsForPendingView(tab_handle, old_filter, filter);
- return true;
}
bool AutomationResourceMessageFilter::LookupRegisteredRenderView(
diff --git a/chrome/browser/automation/automation_resource_message_filter.h b/chrome/browser/automation/automation_resource_message_filter.h
index c4b1f4d..d95886d 100644
--- a/chrome/browser/automation/automation_resource_message_filter.h
+++ b/chrome/browser/automation/automation_resource_message_filter.h
@@ -140,7 +140,7 @@ class AutomationResourceMessageFilter
bool pending_view);
static void UnRegisterRenderViewInIOThread(int renderer_pid, int renderer_id);
- static bool ResumePendingRenderViewInIOThread(
+ static void ResumePendingRenderViewInIOThread(
int renderer_pid, int renderer_id, int tab_handle,
AutomationResourceMessageFilter* filter);
diff --git a/chrome/browser/automation/automation_util.cc b/chrome/browser/automation/automation_util.cc
index 132ad14..8c91ce0 100644
--- a/chrome/browser/automation/automation_util.cc
+++ b/chrome/browser/automation/automation_util.cc
@@ -152,9 +152,8 @@ void GetCookies(const GURL& url,
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
CHECK(BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&GetCookiesOnIOThread,
- url, context_getter, &event, value)));
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&GetCookiesOnIOThread, url, context_getter, &event, value)));
event.Wait();
*value_size = static_cast<int>(value->size());
@@ -174,10 +173,9 @@ void SetCookie(const GURL& url,
false /* not initially signaled */);
bool success = false;
CHECK(BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&SetCookieOnIOThread,
- url, value, context_getter, &event,
- &success)));
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&SetCookieOnIOThread, url, value, context_getter, &event,
+ &success)));
event.Wait();
if (success)
*response_value = 1;
@@ -195,9 +193,9 @@ void DeleteCookie(const GURL& url,
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
CHECK(BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableFunction(&DeleteCookieOnIOThread,
- url, cookie_name, context_getter, &event)));
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&DeleteCookieOnIOThread, url, cookie_name, context_getter,
+ &event)));
event.Wait();
*success = true;
}
@@ -220,9 +218,8 @@ void GetCookiesJSON(AutomationProvider* provider,
net::CookieList cookie_list;
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
- Task* task = NewRunnableFunction(
- &GetCanonicalCookiesOnIOThread,
- GURL(url), context_getter, &event, &cookie_list);
+ base::Closure task = base::Bind(&GetCanonicalCookiesOnIOThread, GURL(url),
+ context_getter, &event, &cookie_list);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to get the cookies");
return;
@@ -268,9 +265,8 @@ void DeleteCookieJSON(AutomationProvider* provider,
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
- Task* task = NewRunnableFunction(
- &DeleteCookieOnIOThread,
- GURL(url), name, context_getter, &event);
+ base::Closure task = base::Bind(&DeleteCookieOnIOThread, GURL(url), name,
+ context_getter, &event);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to delete the cookie");
return;
@@ -358,9 +354,9 @@ void SetCookieJSON(AutomationProvider* provider,
base::WaitableEvent event(true /* manual reset */,
false /* not initially signaled */);
bool success = false;
- Task* task = NewRunnableFunction(
- &SetCookieWithDetailsOnIOThread,
- GURL(url), *cookie.get(), domain, context_getter, &event, &success);
+ base::Closure task = base::Bind(
+ &SetCookieWithDetailsOnIOThread, GURL(url), *cookie.get(), domain,
+ context_getter, &event, &success);
if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task)) {
reply.SendError("Couldn't post task to set the cookie");
return;
diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc
index ac2e654..58a0474 100644
--- a/chrome/browser/automation/testing_automation_provider.cc
+++ b/chrome/browser/automation/testing_automation_provider.cc
@@ -216,8 +216,9 @@ void TestingAutomationProvider::OnBrowserRemoved(const Browser* browser) {
switches::kKeepAliveForTest)) {
// If you change this, update Observer for chrome::SESSION_END
// below.
- MessageLoop::current()->PostTask(FROM_HERE,
- NewRunnableMethod(this, &TestingAutomationProvider::OnRemoveProvider));
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&TestingAutomationProvider::OnRemoveProvider, this));
}
}
@@ -876,7 +877,7 @@ void TestingAutomationProvider::WindowSimulateClick(const IPC::Message& message,
if (window_tracker_->ContainsHandle(handle)) {
// TODO(phajdan.jr): This is flaky. We should wait for the final click.
ui_controls::SendMouseMoveNotifyWhenDone(
- click.x(), click.y(), NewRunnableFunction(&SendMouseClick, flags));
+ click.x(), click.y(), base::Bind(&SendMouseClick, flags));
}
}
@@ -5542,8 +5543,8 @@ void TestingAutomationProvider::SendOSLevelKeyEventToTab(
if (!ui_controls::SendKeyPressNotifyWhenDone(
window, static_cast<ui::KeyboardCode>(keycode),
control, shift, alt, meta,
- NewRunnableMethod(this,
- &TestingAutomationProvider::SendSuccessReply, reply_message))) {
+ base::Bind(&TestingAutomationProvider::SendSuccessReply, this,
+ reply_message))) {
AutomationJSONReply(this, reply_message)
.SendError("Could not send the native key event");
}
diff --git a/chrome/browser/automation/testing_automation_provider_views.cc b/chrome/browser/automation/testing_automation_provider_views.cc
index 40af25b..9a02bb0 100644
--- a/chrome/browser/automation/testing_automation_provider_views.cc
+++ b/chrome/browser/automation/testing_automation_provider_views.cc
@@ -4,6 +4,8 @@
#include "chrome/browser/automation/testing_automation_provider.h"
+#include "base/bind.h"
+#include "base/memory/weak_ptr.h"
#include "base/compiler_specific.h"
#include "chrome/browser/automation/automation_browser_tracker.h"
#include "chrome/browser/automation/automation_window_tracker.h"
@@ -30,7 +32,7 @@ class ViewFocusChangeWaiter : public views::FocusChangeListener {
previous_view_id_(previous_view_id),
automation_(automation),
reply_message_(reply_message),
- ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
focus_manager_->AddFocusChangeListener(this);
// Call the focus change notification once in case the focus has
// already changed.
@@ -48,10 +50,8 @@ class ViewFocusChangeWaiter : public views::FocusChangeListener {
// that will get run after focus changes.
MessageLoop::current()->PostTask(
FROM_HERE,
- method_factory_.NewRunnableMethod(
- &ViewFocusChangeWaiter::FocusChanged,
- focused_before,
- focused_now));
+ base::Bind(&ViewFocusChangeWaiter::FocusChanged,
+ weak_factory_.GetWeakPtr(), focused_before, focused_now));
}
private:
@@ -70,7 +70,7 @@ class ViewFocusChangeWaiter : public views::FocusChangeListener {
int previous_view_id_;
AutomationProvider* automation_;
IPC::Message* reply_message_;
- ScopedRunnableMethodFactory<ViewFocusChangeWaiter> method_factory_;
+ base::WeakPtrFactory<ViewFocusChangeWaiter> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ViewFocusChangeWaiter);
};
diff --git a/chrome/browser/automation/ui_controls.h b/chrome/browser/automation/ui_controls.h
index 7d7b0bf..210dfec 100644
--- a/chrome/browser/automation/ui_controls.h
+++ b/chrome/browser/automation/ui_controls.h
@@ -12,6 +12,7 @@
#include <wtypes.h>
#endif
+#include "base/callback.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/point.h"
@@ -22,16 +23,14 @@ class View;
}
#endif
-class Task;
-
namespace ui_controls {
-// Many of the functions in this class include a variant that takes a Task.
-// The version that takes a Task waits until the generated event is processed.
-// Once the generated event is processed the Task is Run (and deleted). Note
-// that this is a somewhat fragile process in that any event of the correct
-// type (key down, mouse click, etc.) will trigger the Task to be run. Hence
-// a usage such as
+// Many of the functions in this class include a variant that takes a Closure.
+// The version that takes a Closure waits until the generated event is
+// processed. Once the generated event is processed the Closure is Run (and
+// deleted). Note that this is a somewhat fragile process in that any event of
+// the correct type (key down, mouse click, etc.) will trigger the Closure to be
+// run. Hence a usage such as
//
// SendKeyPress(...);
// SendKeyPressNotifyWhenDone(..., task);
@@ -57,11 +56,11 @@ bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
bool shift,
bool alt,
bool command,
- Task* task);
+ const base::Closure& task);
// Simulate a mouse move. (x,y) are absolute screen coordinates.
bool SendMouseMove(long x, long y);
-bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task);
+bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task);
enum MouseButton {
LEFT = 0,
@@ -79,7 +78,8 @@ enum MouseButtonState {
// the cursor currently is, so be sure to move the cursor before calling this
// (and be sure the cursor has arrived!).
bool SendMouseEvents(MouseButton type, int state);
-bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, Task* task);
+bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
+ const base::Closure& task);
// Same as SendMouseEvents with UP | DOWN.
bool SendMouseClick(MouseButton type);
@@ -95,7 +95,7 @@ void MoveMouseToCenterAndPress(
#endif
MouseButton button,
int state,
- Task* task);
+ const base::Closure& task);
} // ui_controls
diff --git a/chrome/browser/automation/ui_controls_gtk.cc b/chrome/browser/automation/ui_controls_gtk.cc
index db52ea2..1505b73 100644
--- a/chrome/browser/automation/ui_controls_gtk.cc
+++ b/chrome/browser/automation/ui_controls_gtk.cc
@@ -7,6 +7,7 @@
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
+#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome/browser/automation/ui_controls_internal.h"
@@ -31,7 +32,7 @@ guint32 XTimeNow() {
class EventWaiter : public MessageLoopForUI::Observer {
public:
- EventWaiter(Task* task, GdkEventType type, int count)
+ EventWaiter(const base::Closure& task, GdkEventType type, int count)
: task_(task),
type_(type),
count_(count) {
@@ -71,9 +72,7 @@ class EventWaiter : public MessageLoopForUI::Observer {
#endif
private:
- // We pass ownership of task_ to MessageLoop when the current event is
- // received.
- Task* task_;
+ base::Closure task_;
GdkEventType type_;
// The number of events of this type to wait for.
int count_;
@@ -161,7 +160,7 @@ bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
bool shift,
bool alt,
bool command,
- Task* task) {
+ const base::Closure& task) {
DCHECK(!command); // No command key on Linux
int release_count = 1;
if (control)
@@ -185,7 +184,7 @@ bool SendMouseMove(long x, long y) {
return true;
}
-bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) {
+bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) {
bool rv = SendMouseMove(x, y);
new EventWaiter(task, GDK_MOTION_NOTIFY, 1);
return rv;
@@ -241,7 +240,8 @@ bool SendMouseEvents(MouseButton type, int state) {
return false;
}
-bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, Task* task) {
+bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
+ const base::Closure& task) {
bool rv = SendMouseEvents(type, state);
GdkEventType wait_type;
if (state & UP) {
@@ -291,7 +291,7 @@ void SynchronizeWidgetSize(views::Widget* widget) {
#endif
void MoveMouseToCenterAndPress(views::View* view, MouseButton button,
- int state, Task* task) {
+ int state, const base::Closure& task) {
#if defined(OS_LINUX) && !defined(USE_AURA)
// X is asynchronous and we need to wait until the window gets
// resized to desired size.
@@ -300,18 +300,20 @@ void MoveMouseToCenterAndPress(views::View* view, MouseButton button,
gfx::Point view_center(view->width() / 2, view->height() / 2);
views::View::ConvertPointToScreen(view, &view_center);
- SendMouseMoveNotifyWhenDone(view_center.x(), view_center.y(),
- new ClickTask(button, state, task));
+ SendMouseMoveNotifyWhenDone(
+ view_center.x(), view_center.y(),
+ base::Bind(&ui_controls::ClickTask, button, state, task));
}
#else
void MoveMouseToCenterAndPress(GtkWidget* widget,
MouseButton button,
int state,
- Task* task) {
+ const base::Closure& task) {
gfx::Rect bounds = gtk_util::GetWidgetScreenBounds(widget);
- SendMouseMoveNotifyWhenDone(bounds.x() + bounds.width() / 2,
- bounds.y() + bounds.height() / 2,
- new ClickTask(button, state, task));
+ SendMouseMoveNotifyWhenDone(
+ bounds.x() + bounds.width() / 2,
+ bounds.y() + bounds.height() / 2,
+ base::Bind(&ui_controls::ClickTask, button, state, task));
}
#endif
diff --git a/chrome/browser/automation/ui_controls_internal.cc b/chrome/browser/automation/ui_controls_internal.cc
index 25598e8..8cb5ec3 100644
--- a/chrome/browser/automation/ui_controls_internal.cc
+++ b/chrome/browser/automation/ui_controls_internal.cc
@@ -6,17 +6,11 @@
namespace ui_controls {
-ClickTask::ClickTask(MouseButton button, int state, Task* followup)
- : button_(button), state_(state), followup_(followup) {
-}
-
-ClickTask::~ClickTask() {}
-
-void ClickTask::Run() {
- if (followup_)
- SendMouseEventsNotifyWhenDone(button_, state_, followup_);
+void ClickTask(MouseButton button, int state, const base::Closure& followup) {
+ if (!followup.is_null())
+ SendMouseEventsNotifyWhenDone(button, state, followup);
else
- SendMouseEvents(button_, state_);
+ SendMouseEvents(button, state);
}
} // ui_controls
diff --git a/chrome/browser/automation/ui_controls_internal.h b/chrome/browser/automation/ui_controls_internal.h
index 5ec9a81..39d0c7df 100644
--- a/chrome/browser/automation/ui_controls_internal.h
+++ b/chrome/browser/automation/ui_controls_internal.h
@@ -5,26 +5,14 @@
#ifndef CHROME_BROWSER_AUTOMATION_UI_CONTROLS_INTERNAL_H_
#define CHROME_BROWSER_AUTOMATION_UI_CONTROLS_INTERNAL_H_
-#include "base/task.h"
+#include "base/callback.h"
#include "chrome/browser/automation/ui_controls.h"
namespace ui_controls {
-// A utility class to send a mouse click event in a task.
-// It's shared by ui_controls_linux.cc and ui_controls_mac.cc
-class ClickTask : public Task {
- public:
- // A |followup| Task can be specified to notify the caller when the mouse
- // click event is sent. If can be NULL if the caller does not care about it.
- ClickTask(MouseButton button, int state, Task* followup);
- virtual ~ClickTask();
- virtual void Run();
-
- private:
- MouseButton button_;
- int state_;
- Task* followup_;
-};
+// A utility function to send a mouse click event in a closure. It's shared by
+// ui_controls_linux.cc and ui_controls_mac.cc
+void ClickTask(MouseButton button, int state, const base::Closure& followup);
} // namespace ui_controls
diff --git a/chrome/browser/automation/ui_controls_mac.mm b/chrome/browser/automation/ui_controls_mac.mm
index 6e38ef7..4dd9918 100644
--- a/chrome/browser/automation/ui_controls_mac.mm
+++ b/chrome/browser/automation/ui_controls_mac.mm
@@ -8,6 +8,8 @@
#include <mach/mach_time.h>
#include <vector>
+#include "base/bind.h"
+#include "base/callback.h"
#include "base/message_loop.h"
#include "chrome/browser/automation/ui_controls_internal.h"
#include "content/browser/browser_thread.h"
@@ -187,29 +189,22 @@ void SynthesizeKeyEventsSequence(NSWindow* window,
}
}
-// A task class to watch for the event queue. The specific task will be fired
-// when there is no more event in the queue.
-class EventQueueWatcher : public Task {
- public:
- EventQueueWatcher(Task* task) : task_(task) {}
-
- virtual ~EventQueueWatcher() {}
-
- virtual void Run() {
- NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
- untilDate:nil
- inMode:NSDefaultRunLoopMode
- dequeue:NO];
- // If there is still event in the queue, then we need to check again.
- if (event)
- MessageLoop::current()->PostTask(FROM_HERE, new EventQueueWatcher(task_));
- else
- MessageLoop::current()->PostTask(FROM_HERE, task_);
+// A helper function to watch for the event queue. The specific task will be
+// fired when there is no more event in the queue.
+void EventQueueWatcher(const base::Closure& task) {
+ NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
+ untilDate:nil
+ inMode:NSDefaultRunLoopMode
+ dequeue:NO];
+ // If there is still event in the queue, then we need to check again.
+ if (event) {
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&EventQueueWatcher, task));
+ } else {
+ MessageLoop::current()->PostTask(FROM_HERE, task);
}
-
- private:
- Task* task_;
-};
+}
// Stores the current mouse location on the screen. So that we can use it
// when firing keyboard and mouse click events.
@@ -217,7 +212,6 @@ NSPoint g_mouse_location = { 0, 0 };
} // anonymous namespace
-
namespace ui_controls {
bool SendKeyPress(gfx::NativeWindow window,
@@ -228,7 +222,7 @@ bool SendKeyPress(gfx::NativeWindow window,
bool command) {
return SendKeyPressNotifyWhenDone(window, key,
control, shift, alt, command,
- NULL);
+ base::Closure());
}
// Win and Linux implement a SendKeyPress() this as a
@@ -239,7 +233,7 @@ bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
bool shift,
bool alt,
bool command,
- Task* task) {
+ const base::Closure& task) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
std::vector<NSEvent*> events;
@@ -255,21 +249,23 @@ bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
iter != events.end(); ++iter)
[[NSApplication sharedApplication] sendEvent:*iter];
- if (task)
- MessageLoop::current()->PostTask(FROM_HERE, new EventQueueWatcher(task));
+ if (!task.is_null()) {
+ MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&EventQueueWatcher, task));
+ }
return true;
}
bool SendMouseMove(long x, long y) {
- return SendMouseMoveNotifyWhenDone(x, y, NULL);
+ return SendMouseMoveNotifyWhenDone(x, y, base::Closure());
}
// Input position is in screen coordinates. However, NSMouseMoved
// events require them window-relative, so we adjust. We *DO* flip
// the coordinate space, so input events can be the same for all
// platforms. E.g. (0,0) is upper-left.
-bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) {
+bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) {
NSWindow* window = [[NSApplication sharedApplication] keyWindow];
CGFloat screenHeight =
[[[NSScreen screens] objectAtIndex:0] frame].size.height;
@@ -291,21 +287,24 @@ bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) {
pressure:0.0];
[[NSApplication sharedApplication] postEvent:event atStart:NO];
- if (task)
- MessageLoop::current()->PostTask(FROM_HERE, new EventQueueWatcher(task));
+ if (!task.is_null()) {
+ MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&EventQueueWatcher, task));
+ }
return true;
}
bool SendMouseEvents(MouseButton type, int state) {
- return SendMouseEventsNotifyWhenDone(type, state, NULL);
+ return SendMouseEventsNotifyWhenDone(type, state, base::Closure());
}
-bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, Task* task) {
+bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
+ const base::Closure& task) {
// On windows it appears state can be (UP|DOWN). It is unclear if
// that'll happen here but prepare for it just in case.
if (state == (UP|DOWN)) {
- return (SendMouseEventsNotifyWhenDone(type, DOWN, NULL) &&
+ return (SendMouseEventsNotifyWhenDone(type, DOWN, base::Closure()) &&
SendMouseEventsNotifyWhenDone(type, UP, task));
}
NSEventType etype = 0;
@@ -347,21 +346,23 @@ bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, Task* task) {
pressure:(state == DOWN ? 1.0 : 0.0 )];
[[NSApplication sharedApplication] postEvent:event atStart:NO];
- if (task)
- MessageLoop::current()->PostTask(FROM_HERE, new EventQueueWatcher(task));
+ if (!task.is_null()) {
+ MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&EventQueueWatcher, task));
+ }
return true;
}
bool SendMouseClick(MouseButton type) {
- return SendMouseEventsNotifyWhenDone(type, UP|DOWN, NULL);
+ return SendMouseEventsNotifyWhenDone(type, UP|DOWN, base::Closure());
}
void MoveMouseToCenterAndPress(
NSView* view,
MouseButton button,
int state,
- Task* task) {
+ const base::Closure& task) {
DCHECK(view);
NSWindow* window = [view window];
DCHECK(window);
@@ -376,8 +377,9 @@ void MoveMouseToCenterAndPress(
center = [window convertBaseToScreen:center];
center = NSMakePoint(center.x, [screen frame].size.height - center.y);
- SendMouseMoveNotifyWhenDone(center.x, center.y,
- new ClickTask(button, state, task));
+ SendMouseMoveNotifyWhenDone(
+ center.x, center.y,
+ base::Bind(&ui_controls::ClickTask, button, state, task));
}
} // ui_controls
diff --git a/chrome/browser/automation/ui_controls_win.cc b/chrome/browser/automation/ui_controls_win.cc
index 1cfcbaf..9385df5 100644
--- a/chrome/browser/automation/ui_controls_win.cc
+++ b/chrome/browser/automation/ui_controls_win.cc
@@ -4,6 +4,8 @@
#include "chrome/browser/automation/ui_controls.h"
+#include "base/bind.h"
+#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
@@ -22,7 +24,7 @@ namespace {
// appropriate event is received the task is notified.
class InputDispatcher : public base::RefCounted<InputDispatcher> {
public:
- InputDispatcher(Task* task, WPARAM message_waiting_for);
+ InputDispatcher(const base::Closure& task, WPARAM message_waiting_for);
// Invoked from the hook. If mouse_message matches message_waiting_for_
// MatchingMessageFound is invoked.
@@ -41,7 +43,7 @@ class InputDispatcher : public base::RefCounted<InputDispatcher> {
void NotifyTask();
// The task we notify.
- scoped_ptr<Task> task_;
+ base::Closure task_;
// Message we're waiting for. Not used for keyboard events.
const WPARAM message_waiting_for_;
@@ -107,7 +109,8 @@ void UninstallHook(InputDispatcher* dispatcher) {
}
}
-InputDispatcher::InputDispatcher(Task* task, UINT message_waiting_for)
+InputDispatcher::InputDispatcher(const base::Closure& task,
+ UINT message_waiting_for)
: task_(task), message_waiting_for_(message_waiting_for) {
InstallHook(this, message_waiting_for == WM_KEYUP);
}
@@ -126,12 +129,12 @@ void InputDispatcher::MatchingMessageFound() {
UninstallHook(this);
// At the time we're invoked the event has not actually been processed.
// Use PostTask to make sure the event has been processed before notifying.
- MessageLoop::current()->PostDelayedTask(
- FROM_HERE, NewRunnableMethod(this, &InputDispatcher::NotifyTask), 0);
+ MessageLoop::current()->PostTask(
+ FROM_HERE, base::Bind(&InputDispatcher::NotifyTask, this));
}
void InputDispatcher::NotifyTask() {
- task_->Run();
+ task_.Run();
Release();
}
@@ -164,9 +167,9 @@ bool SendKeyEvent(ui::KeyboardCode key, bool up) {
bool SendKeyPressImpl(ui::KeyboardCode key,
bool control, bool shift, bool alt,
- Task* task) {
+ const base::Closure& task) {
scoped_refptr<InputDispatcher> dispatcher(
- task ? new InputDispatcher(task, WM_KEYUP) : NULL);
+ !task.is_null() ? new InputDispatcher(task, WM_KEYUP) : NULL);
// If a pop-up menu is open, it won't receive events sent using SendInput.
// Check for a pop-up menu using its window class (#32768) and if one
@@ -239,12 +242,12 @@ bool SendKeyPressImpl(ui::KeyboardCode key,
return true;
}
-bool SendMouseMoveImpl(long x, long y, Task* task) {
+bool SendMouseMoveImpl(long x, long y, const base::Closure& task) {
// First check if the mouse is already there.
POINT current_pos;
::GetCursorPos(&current_pos);
if (x == current_pos.x && y == current_pos.y) {
- if (task)
+ if (!task.is_null())
MessageLoop::current()->PostTask(FROM_HERE, task);
return true;
}
@@ -262,7 +265,7 @@ bool SendMouseMoveImpl(long x, long y, Task* task) {
input.mi.dy = pixel_y;
scoped_refptr<InputDispatcher> dispatcher(
- task ? new InputDispatcher(task, WM_MOUSEMOVE) : NULL);
+ !task.is_null() ? new InputDispatcher(task, WM_MOUSEMOVE) : NULL);
if (!::SendInput(1, &input, sizeof(INPUT)))
return false;
@@ -273,7 +276,8 @@ bool SendMouseMoveImpl(long x, long y, Task* task) {
return true;
}
-bool SendMouseEventsImpl(MouseButton type, int state, Task* task) {
+bool SendMouseEventsImpl(MouseButton type, int state,
+ const base::Closure& task) {
DWORD down_flags = MOUSEEVENTF_ABSOLUTE;
DWORD up_flags = MOUSEEVENTF_ABSOLUTE;
UINT last_event;
@@ -303,7 +307,7 @@ bool SendMouseEventsImpl(MouseButton type, int state, Task* task) {
}
scoped_refptr<InputDispatcher> dispatcher(
- task ? new InputDispatcher(task, last_event) : NULL);
+ !task.is_null() ? new InputDispatcher(task, last_event) : NULL);
INPUT input = { 0 };
input.type = INPUT_MOUSE;
@@ -332,7 +336,7 @@ bool SendKeyPress(gfx::NativeWindow window,
bool alt,
bool command) {
DCHECK(!command); // No command key on Windows
- return SendKeyPressImpl(key, control, shift, alt, NULL);
+ return SendKeyPressImpl(key, control, shift, alt, base::Closure());
}
bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
@@ -341,35 +345,36 @@ bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
bool shift,
bool alt,
bool command,
- Task* task) {
+ const base::Closure& task) {
DCHECK(!command); // No command key on Windows
return SendKeyPressImpl(key, control, shift, alt, task);
}
bool SendMouseMove(long x, long y) {
- return SendMouseMoveImpl(x, y, NULL);
+ return SendMouseMoveImpl(x, y, base::Closure());
}
-bool SendMouseMoveNotifyWhenDone(long x, long y, Task* task) {
+bool SendMouseMoveNotifyWhenDone(long x, long y, const base::Closure& task) {
return SendMouseMoveImpl(x, y, task);
}
bool SendMouseEvents(MouseButton type, int state) {
- return SendMouseEventsImpl(type, state, NULL);
+ return SendMouseEventsImpl(type, state, base::Closure());
}
-bool SendMouseEventsNotifyWhenDone(MouseButton type, int state, Task* task) {
+bool SendMouseEventsNotifyWhenDone(MouseButton type, int state,
+ const base::Closure& task) {
return SendMouseEventsImpl(type, state, task);
}
bool SendMouseClick(MouseButton type) {
- return SendMouseEventsImpl(type, UP | DOWN, NULL);
+ return SendMouseEventsImpl(type, UP | DOWN, base::Closure());
}
void MoveMouseToCenterAndPress(views::View* view,
MouseButton button,
int state,
- Task* task) {
+ const base::Closure& task) {
DCHECK(view);
DCHECK(view->GetWidget());
gfx::Point view_center(view->width() / 2, view->height() / 2);
diff --git a/chrome/browser/automation/url_request_automation_job.cc b/chrome/browser/automation/url_request_automation_job.cc
index 231b259..16dca92 100644
--- a/chrome/browser/automation/url_request_automation_job.cc
+++ b/chrome/browser/automation/url_request_automation_job.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/automation/url_request_automation_job.h"
+#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/time.h"
@@ -61,7 +62,7 @@ URLRequestAutomationJob::URLRequestAutomationJob(
redirect_status_(0),
request_id_(request_id),
is_pending_(is_pending),
- ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
DVLOG(1) << "URLRequestAutomationJob create. Count: " << ++instance_count_;
DCHECK(message_filter_ != NULL);
@@ -76,7 +77,7 @@ URLRequestAutomationJob::~URLRequestAutomationJob() {
Cleanup();
}
-bool URLRequestAutomationJob::EnsureProtocolFactoryRegistered() {
+void URLRequestAutomationJob::EnsureProtocolFactoryRegistered() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!is_protocol_factory_registered_) {
@@ -88,8 +89,6 @@ bool URLRequestAutomationJob::EnsureProtocolFactoryRegistered() {
"https", &URLRequestAutomationJob::Factory);
is_protocol_factory_registered_ = true;
}
-
- return true;
}
net::URLRequestJob* URLRequestAutomationJob::Factory(
@@ -130,8 +129,8 @@ void URLRequestAutomationJob::Start() {
// callbacks happen as they would for network requests.
MessageLoop::current()->PostTask(
FROM_HERE,
- method_factory_.NewRunnableMethod(
- &URLRequestAutomationJob::StartAsync));
+ base::Bind(&URLRequestAutomationJob::StartAsync,
+ weak_factory_.GetWeakPtr()));
} else {
// If this is a pending job, then register it immediately with the message
// filter so it can be serviced later when we receive a request from the
@@ -169,8 +168,8 @@ bool URLRequestAutomationJob::ReadRawData(
} else {
MessageLoop::current()->PostTask(
FROM_HERE,
- method_factory_.NewRunnableMethod(
- &URLRequestAutomationJob::NotifyJobCompletionTask));
+ base::Bind(&URLRequestAutomationJob::NotifyJobCompletionTask,
+ weak_factory_.GetWeakPtr()));
}
return false;
}
diff --git a/chrome/browser/automation/url_request_automation_job.h b/chrome/browser/automation/url_request_automation_job.h
index e9dd127..096ce06 100644
--- a/chrome/browser/automation/url_request_automation_job.h
+++ b/chrome/browser/automation/url_request_automation_job.h
@@ -7,6 +7,7 @@
#define CHROME_BROWSER_AUTOMATION_URL_REQUEST_AUTOMATION_JOB_H_
#pragma once
+#include "base/memory/weak_ptr.h"
#include "base/task.h"
#include "chrome/common/ref_counted_util.h"
#include "net/url_request/url_request.h"
@@ -34,7 +35,7 @@ class URLRequestAutomationJob : public net::URLRequestJob {
bool is_pending);
// Register our factory for HTTP/HTTPs requests.
- static bool EnsureProtocolFactoryRegistered();
+ static void EnsureProtocolFactoryRegistered();
static net::URLRequest::ProtocolFactory Factory;
@@ -128,7 +129,7 @@ class URLRequestAutomationJob : public net::URLRequestJob {
// Contains the ip address and port of the destination host.
net::HostPortPair socket_address_;
- ScopedRunnableMethodFactory<URLRequestAutomationJob> method_factory_;
+ base::WeakPtrFactory<URLRequestAutomationJob> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequestAutomationJob);
};