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/chrome_browser_proxy_resolver.cc b/chrome_browser_proxy_resolver.cc
index 7696d4b..116c67a 100644
--- a/chrome_browser_proxy_resolver.cc
+++ b/chrome_browser_proxy_resolver.cc
@@ -22,6 +22,8 @@
 namespace chromeos_update_engine {
 
 using base::StringTokenizer;
+using base::TimeDelta;
+using chromeos::MessageLoop;
 using std::deque;
 using std::make_pair;
 using std::pair;
@@ -52,7 +54,7 @@
 
 ChromeBrowserProxyResolver::ChromeBrowserProxyResolver(
     DBusWrapperInterface* dbus)
-    : dbus_(dbus), proxy_(nullptr), timeout_(kTimeout) {}
+    : dbus_(dbus), timeout_(kTimeout) {}
 
 bool ChromeBrowserProxyResolver::Init() {
   if (proxy_)
@@ -105,8 +107,8 @@
 
   // Kill outstanding timers
   for (auto& timer : timers_) {
-    g_source_destroy(timer.second);
-    timer.second = nullptr;
+    MessageLoop::current()->CancelTask(timer.second);
+    timer.second = MessageLoop::kTaskIdNull;
   }
 }
 
@@ -137,14 +139,12 @@
   }
 
   callbacks_.insert(make_pair(url, make_pair(callback, data)));
-  base::Closure* closure = new base::Closure(base::Bind(
-      &ChromeBrowserProxyResolver::HandleTimeout,
-      base::Unretained(this),
-      url));
-  GSource* timer = g_timeout_source_new_seconds(timeout);
-  g_source_set_callback(
-      timer, utils::GlibRunClosure, closure, utils::GlibDestroyClosure);
-  g_source_attach(timer, nullptr);
+  MessageLoop::TaskId timer = MessageLoop::current()->PostDelayedTask(
+      FROM_HERE,
+      base::Bind(&ChromeBrowserProxyResolver::HandleTimeout,
+                 base::Unretained(this),
+                 url),
+      TimeDelta::FromSeconds(timeout));
   timers_.insert(make_pair(url, timer));
   return true;
 }
@@ -196,7 +196,7 @@
     TEST_AND_RETURN_FALSE(it != timers_.end());
     TEST_AND_RETURN_FALSE(it->first == source_url);
     if (delete_timer)
-      g_source_destroy(it->second);
+      MessageLoop::current()->CancelTask(it->second);
     timers_.erase(it);
   }
   return true;