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/http_fetcher.cc b/http_fetcher.cc
index 3caadf0..b355fdb 100644
--- a/http_fetcher.cc
+++ b/http_fetcher.cc
@@ -4,16 +4,19 @@
 
 #include "update_engine/http_fetcher.h"
 
+#include <base/bind.h>
+
 using base::Closure;
+using chromeos::MessageLoop;
 using std::deque;
 using std::string;
 
 namespace chromeos_update_engine {
 
 HttpFetcher::~HttpFetcher() {
-  if (no_resolver_idle_id_) {
-    g_source_remove(no_resolver_idle_id_);
-    no_resolver_idle_id_ = 0;
+  if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
+    MessageLoop::current()->CancelTask(no_resolver_idle_id_);
+    no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
   }
 }
 
@@ -21,7 +24,7 @@
                               HttpContentType type) {
   post_data_set_ = true;
   post_data_.clear();
-  const char *char_data = reinterpret_cast<const char*>(data);
+  const char* char_data = reinterpret_cast<const char*>(data);
   post_data_.insert(post_data_.end(), char_data, char_data + size);
   post_content_type_ = type;
 }
@@ -31,30 +34,34 @@
 }
 
 // Proxy methods to set the proxies, then to pop them off.
-bool HttpFetcher::ResolveProxiesForUrl(const string& url, Closure* callback) {
+bool HttpFetcher::ResolveProxiesForUrl(const string& url,
+                                       const Closure& callback) {
+  CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
+  callback_.reset(new Closure(callback));
+
   if (!proxy_resolver_) {
     LOG(INFO) << "Not resolving proxies (no proxy resolver).";
-    no_resolver_idle_id_ = g_idle_add_full(
-        G_PRIORITY_DEFAULT,
-        utils::GlibRunClosure,
-        callback,
-        utils::GlibDestroyClosure);
+    no_resolver_idle_id_ = MessageLoop::current()->PostTask(
+        FROM_HERE,
+        base::Bind(&HttpFetcher::NoProxyResolverCallback,
+                   base::Unretained(this)));
     return true;
   }
-  CHECK_EQ(static_cast<Closure*>(nullptr), callback_);
-  callback_ = callback;
   return proxy_resolver_->GetProxiesForUrl(url,
                                            &HttpFetcher::StaticProxiesResolved,
                                            this);
 }
 
+void HttpFetcher::NoProxyResolverCallback() {
+  ProxiesResolved(deque<string>());
+}
+
 void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
-  no_resolver_idle_id_ = 0;
+  no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
   if (!proxies.empty())
     SetProxies(proxies);
-  CHECK_NE(static_cast<Closure*>(nullptr), callback_);
-  Closure* callback = callback_;
-  callback_ = nullptr;
+  CHECK_NE(static_cast<Closure*>(nullptr), callback_.get());
+  Closure* callback = callback_.release();
   // This may indirectly call back into ResolveProxiesForUrl():
   callback->Run();
   delete callback;