update_engine: Migrate time-based glib main loop calls to MessageLoop.

This patch replaces most calls to g_idle_add* and g_timeout_add* with
the equivalent MessageLoop::Post*Task(). To maintain compatibility with
unittests running the main loop and doing I/O we instantiate a
GlibMessageLoop for those tests.

BUG=chromium:499886
TEST=unittests still pass.

Change-Id: Ic87ba69bc47391ac3c36d1bfc3ca28d069666af1
Reviewed-on: https://chromium-review.googlesource.com/281197
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
diff --git a/proxy_resolver.cc b/proxy_resolver.cc
index c6ba03b..8b8a9c8 100644
--- a/proxy_resolver.cc
+++ b/proxy_resolver.cc
@@ -5,7 +5,9 @@
 #include "update_engine/proxy_resolver.h"
 
 #include <base/bind.h>
+#include <base/location.h>
 
+using chromeos::MessageLoop;
 using std::deque;
 using std::string;
 
@@ -14,31 +16,33 @@
 const char kNoProxy[] = "direct://";
 
 DirectProxyResolver::~DirectProxyResolver() {
-  if (idle_callback_id_) {
-    g_source_remove(idle_callback_id_);
-    idle_callback_id_ = 0;
+  if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
+    // The DirectProxyResolver is instantiated as part of the UpdateAttempter
+    // which is also instantiated by default by the FakeSystemState, even when
+    // it is not used. We check the manage_shares_id_ before calling the
+    // MessageLoop::current() since the unit test using a FakeSystemState may
+    // have not define a MessageLoop for the current thread.
+    MessageLoop::current()->CancelTask(idle_callback_id_);
+    idle_callback_id_ = MessageLoop::kTaskIdNull;
   }
 }
 
 bool DirectProxyResolver::GetProxiesForUrl(const string& url,
                                            ProxiesResolvedFn callback,
                                            void* data) {
-  base::Closure* closure = new base::Closure(base::Bind(
-      &DirectProxyResolver::ReturnCallback,
-      base::Unretained(this),
-      callback,
-      data));
-  idle_callback_id_ = g_idle_add_full(
-      G_PRIORITY_DEFAULT,
-      utils::GlibRunClosure,
-      closure,
-      utils::GlibDestroyClosure);
+  idle_callback_id_ = MessageLoop::current()->PostTask(
+      FROM_HERE,
+      base::Bind(
+            &DirectProxyResolver::ReturnCallback,
+            base::Unretained(this),
+            callback,
+            data));
   return true;
 }
 
 void DirectProxyResolver::ReturnCallback(ProxiesResolvedFn callback,
                                          void* data) {
-  idle_callback_id_ = 0;
+  idle_callback_id_ = MessageLoop::kTaskIdNull;
 
   // Initialize proxy pool with as many proxies as indicated (all identical).
   deque<string> proxies(num_proxies_, kNoProxy);