AU: Make proxy resolution asynchronous.

This doesn't change proxy resolution overall (we still use settings
stored in the session manager), but it changes the implementation in
the updater to be asynchronous. The clients of the proxy resolver now
give a callback to be called when the proxies are known.

This is anticipation of a switch to using Chrome to resolve proxies,
which will need to be asynchronous.

BUG=chromium-os:12079
TEST=unittests; tested update on device w/ and w/o proxy settings

Review URL: http://codereview.chromium.org/6516026

Change-Id: Icc5c08e3abf4381be55d8d555020d4c630a07fd6
diff --git a/proxy_resolver.h b/proxy_resolver.h
index af228d2..53fd920 100644
--- a/proxy_resolver.h
+++ b/proxy_resolver.h
@@ -5,28 +5,39 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PROXY_RESOLVER_H__
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_PROXY_RESOLVER_H__
 
-#include <base/logging.h>
 
 #include <deque>
 #include <string>
 
+#include <base/logging.h>
+#include <google/protobuf/stubs/common.h>
+
+#include "update_engine/utils.h"
+
 namespace chromeos_update_engine {
 
 extern const char kNoProxy[];
 
+// Callback for a call to GetProxiesForUrl().
+// Resultant proxies are in |out_proxy|. Each will be in one of the
+// following forms:
+// http://<host>[:<port>] - HTTP proxy
+// socks{4,5}://<host>[:<port>] - SOCKS4/5 proxy
+// kNoProxy - no proxy
+typedef void (*ProxiesResolvedFn)(const std::deque<std::string>& proxies,
+                                  void* data);
+
 class ProxyResolver {
  public:
   ProxyResolver() {}
   virtual ~ProxyResolver() {}
 
-  // Stores a list of proxies for a given |url| in |out_proxy|.
-  // Returns true on success. The resultant proxy will be in one of the
-  // following forms:
-  // http://<host>[:<port>] - HTTP proxy
-  // socks{4,5}://<host>[:<port>] - SOCKS4/5 proxy
-  // kNoProxy - no proxy
+  // Finds proxies for the given URL and returns them via the callback.
+  // |data| will be passed to the callback.
+  // Returns true on success.
   virtual bool GetProxiesForUrl(const std::string& url,
-                                std::deque<std::string>* out_proxies) = 0;
+                                ProxiesResolvedFn callback,
+                                void* data) = 0;
 
  private:
   DISALLOW_COPY_AND_ASSIGN(ProxyResolver);
@@ -35,11 +46,18 @@
 // Always says to not use a proxy
 class DirectProxyResolver : public ProxyResolver {
  public:
-  DirectProxyResolver() {}
+  DirectProxyResolver() : idle_callback_id_(0) {}
+  virtual ~DirectProxyResolver();
   virtual bool GetProxiesForUrl(const std::string& url,
-                                std::deque<std::string>* out_proxies);
+                                ProxiesResolvedFn callback,
+                                void* data);
 
  private:
+  // The ID of the idle main loop callback
+  guint idle_callback_id_;
+
+  // The MainLoop callback, from here we return to the client.
+  void ReturnCallback(ProxiesResolvedFn callback, void* data);
   DISALLOW_COPY_AND_ASSIGN(DirectProxyResolver);
 };