AU: Manual proxy support

Utilize the ChromeProxyResolver to resolve proxies in our network
requests. This means the following changes:

- HttpFetcher classes take a ProxyResolver* in their ctor. Also, a few
  useful functions in HttpFetcher to allow subclasses to iterate
  through the proxies.

- LibcurlHttpFetcher support for using the ProxyResolver. It will
  attempt to use each proxy in the order specified. If any data comes
  in from any proxy, it won't continue down the list and will continue
  to use that proxy for its lifetime.

- UpdateAttempter can choose, for a given update session, whether or
  not to use the ChromeProxyResolver or DirectProxyResolver. For now,
  the logic is: for automatic checks, 80% of the time use
  ChromeProxyResolver, 20% DirectProxyResolver. For manual checks, the
  first 19 manual checks in a row use Chrome, then once it uses
  Direct, then starts over again. The idea is that the updater doesn't
  necessarily trust Chrome, so some requests should skip it. If a
  manual check is performed, the user likely wants her proxy settings
  honored, so use them, but don't allow frequent manual checks to
  starve out usage of the DirectProxyResolver.

- Updates to tests

BUG=3167
TEST=unittests, tested on device

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

Change-Id: Iee0f589e5b28d4b804afe1f5b6729ba066d48d62
diff --git a/http_fetcher.h b/http_fetcher.h
index 6d8608b..a50c760 100644
--- a/http_fetcher.h
+++ b/http_fetcher.h
@@ -5,10 +5,15 @@
 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__
 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__
 
+#include <deque>
 #include <string>
 #include <vector>
+
+#include <base/basictypes.h>
+#include <base/logging.h>
 #include <glib.h>
-#include "base/basictypes.h"
+
+#include "update_engine/proxy_resolver.h"
 
 // This class is a simple wrapper around an HTTP library (libcurl). We can
 // easily mock out this interface for testing.
@@ -23,10 +28,15 @@
 
 class HttpFetcher {
  public:
-  HttpFetcher()
+  // |proxy_resolver| is the resolver that will be consulted for proxy
+  // settings. It may be null, in which case direct connections will
+  // be used. Does not take ownership of the resolver.
+  explicit HttpFetcher(ProxyResolver* proxy_resolver)
       : post_data_set_(false),
         http_response_code_(0),
-        delegate_(NULL) {}
+        delegate_(NULL),
+        proxies_(1, kNoProxy),
+        proxy_resolver_(proxy_resolver) {}
   virtual ~HttpFetcher() {}
 
   void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; }
@@ -35,12 +45,19 @@
 
   // Optional: Post data to the server. The HttpFetcher should make a copy
   // of this data and upload it via HTTP POST during the transfer.
-  void SetPostData(const void* data, size_t size) {
-    post_data_set_ = true;
-    post_data_.clear();
-    const char *char_data = reinterpret_cast<const char*>(data);
-    post_data_.insert(post_data_.end(), char_data, char_data + size);
+  void SetPostData(const void* data, size_t size);
+
+  // Proxy methods to set the proxies, then to pop them off.
+  void ResolveProxiesForUrl(const std::string& url);
+  
+  void SetProxies(const std::deque<std::string>& proxies) {
+    proxies_ = proxies;
   }
+  const std::string& GetCurrentProxy() const {
+    return proxies_.front();
+  }
+  bool HasProxy() const { return !proxies_.empty(); }
+  void PopProxy() { proxies_.pop_front(); }
 
   // Downloading should resume from this offset
   virtual void SetOffset(off_t offset) = 0;
@@ -84,6 +101,12 @@
 
   // The delegate; may be NULL.
   HttpFetcherDelegate* delegate_;
+
+  // Proxy servers
+  std::deque<std::string> proxies_;
+  
+  ProxyResolver* const proxy_resolver_;
+
  private:
   DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
 };