[shill] Switch from home-grown EventQueue to Chrome's MessageLoop

Many other chrome os daemons have borrowed chrome's message loop abstraction,
which supports posting Task and DelayedTask objects to the loop for later
processing.  There are also Callback* objects in there that we can use for
other things, as I've done with the GlibIOInputHandler.  Unit tests contain
some trivial examples.

More documentation in:
base/message_loop.h
base/task.h
base/callback.h

BUG=chromium-os:15105
TEST=build, unit tests

Change-Id: Ia53dcbdf70da45e4ceda7eed1debbc4bb507446e
Reviewed-on: http://gerrit.chromium.org/gerrit/713
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
diff --git a/shill_event.h b/shill_event.h
index cda8872..9ae4304 100644
--- a/shill_event.h
+++ b/shill_event.h
@@ -7,96 +7,19 @@
 
 #include <vector>
 
+#include <base/basictypes.h>
+#include <base/callback.h>
+#include <base/message_loop.h>
+#include <base/ref_counted.h>
+#include <base/scoped_ptr.h>
+
+namespace base {
+class MessageLoopProxy;
+}  // namespace base
+class Task;
+
 namespace shill {
 
-// This is a pure-virtual base class for callback objects which can be
-// queued up and called later.  The callback virtual method takes a single
-// argument, which will be handed to the dispatcher to call on all listeners.
-template <typename Arg>
-class Callback {
- public:
-  virtual ~Callback() {}
-  virtual void Run(Arg arg) = 0;
-};
-
-// This is a callback subclass that contains an object and method to call.
-// These methods take a passed-in argument specific to the callback type.
-template <typename Class, typename Arg>
-class ClassCallback : public Callback<Arg> {
- public:
-  typedef void (Class::*MethodType)(Arg arg);
-
-  ClassCallback(Class* object, MethodType method)
-    : object_(object), method_(method) {}
-  ~ClassCallback() {}
-
-  void Run(Arg arg) {
-    (object_->*method_)(arg);
-  }
-
- private:
-  Class* object_;
-  MethodType method_;
-};
-
-// This is the event queue superclass, which contains a function for
-// dispatching all events in the queue to their respective listeners.
-// A common "AlertDispatcher()" function is used by subclasses to alert
-// the central dispatcher that events have been queued and a dispatch
-// should be performed soon.
-class EventDispatcher;
-class EventQueueItem {
- public:
-  EventQueueItem(EventDispatcher *dispatcher);
-  ~EventQueueItem();
-  virtual void Dispatch() = 0;
-  void AlertDispatcher();
- private:
-  EventDispatcher *dispatcher_;
-};
-
-// This is a template subclass of EventQueueItem which is specific to
-// a particular argument type.  This object contains a queue of events
-// waiting for delivery to liesteners, and a list of even callbacks --
-// the listeners for this event.
-template <typename Arg>
-class EventQueue : public EventQueueItem {
-  typedef Callback<Arg>CallbackType;
-
- public:
-  explicit EventQueue(EventDispatcher *dispatcher)
-    : EventQueueItem(dispatcher) {}
-
-  inline void AddCallback(CallbackType *cb) {
-    callback_list_.push_back(cb);
-  }
-
-  void RemoveCallback(CallbackType *cb) {
-    for (size_t event_idx = 0; event_idx < callback_list_.size(); ++event_idx) {
-      if (callback_list_[event_idx] == cb) {
-        callback_list_.erase(callback_list_.begin() + event_idx);
-        return;
-      }
-    }
-  }
-
-  void Dispatch() {
-    for (size_t event_idx = 0; event_idx < event_queue_.size(); ++event_idx)
-      for (size_t call_idx = 0; call_idx < callback_list_.size(); ++call_idx)
-        callback_list_[call_idx]->Run(event_queue_[event_idx]);
-    event_queue_.clear();
-  }
-
-  void AddEvent(Arg arg) {
-    event_queue_.push_back(arg);
-    AlertDispatcher();
-  }
-
- private:
-  std::vector<CallbackType *> callback_list_;
-  std::vector<Arg> event_queue_;
-};
-
 struct InputData {
   unsigned char *buf;
   size_t len;
@@ -113,13 +36,20 @@
 // their listeners during the idle loop.
 class EventDispatcher {
  public:
-  void DispatchEvents();
-  void ExecuteOnIdle();
-  void RegisterCallbackQueue(EventQueueItem *queue);
-  void UnregisterCallbackQueue(EventQueueItem *queue);
-  IOInputHandler *CreateInputHandler(int fd, Callback<InputData *> *callback);
+  EventDispatcher();
+  virtual ~EventDispatcher();
+  void DispatchForever();
+
+  // These are thin wrappers around calls of the same name in
+  // <base/message_loop_proxy.h>
+  bool PostTask(Task* task);
+  bool PostDelayedTask(Task* task, int64 delay_ms);
+
+  IOInputHandler *CreateInputHandler(int fd,
+                                     Callback1<InputData*>::Type *callback);
  private:
-  std::vector<EventQueueItem*> queue_list_;
+  scoped_ptr<MessageLoop> dont_use_directly_;
+  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
 };
 
 }  // namespace shill