update_engine: Use org.chromium.NetworkProxyService.

Make update_engine call Chrome's new
org.chromium.NetworkProxyService D-Bus service to resolve
network proxies instead of using
org.chromium.LibCrosService. The new service supports
asynchronous replies instead of responding via D-Bus
signals.

BUG=chromium:446115,chromium:703217
TEST=unit tests pass; also added debug logging and verified
     that chrome's proxy settings are used

Change-Id: Iebd268ea3e551c0760416d955828b9d7ebf851fb
Reviewed-on: https://chromium-review.googlesource.com/497491
Commit-Ready: Dan Erat <derat@chromium.org>
Tested-by: Dan Erat <derat@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
diff --git a/chrome_browser_proxy_resolver.h b/chrome_browser_proxy_resolver.h
index eb92bac..03dbdad 100644
--- a/chrome_browser_proxy_resolver.h
+++ b/chrome_browser_proxy_resolver.h
@@ -18,78 +18,66 @@
 #define UPDATE_ENGINE_CHROME_BROWSER_PROXY_RESOLVER_H_
 
 #include <deque>
+#include <map>
 #include <string>
 
-#include <gtest/gtest_prod.h>  // for FRIEND_TEST
+#include <base/memory/weak_ptr.h>
 
-#include <brillo/message_loops/message_loop.h>
-
-#include "update_engine/libcros_proxy.h"
 #include "update_engine/proxy_resolver.h"
 
+namespace brillo {
+class Error;
+}  // namespace brillo
+
+namespace org {
+namespace chromium {
+class NetworkProxyServiceInterfaceProxyInterface;
+}  // namespace chromium
+}  // namespace org
+
 namespace chromeos_update_engine {
 
-extern const char kLibCrosServiceName[];
-extern const char kLibCrosProxyResolveName[];
-extern const char kLibCrosProxyResolveSignalInterface[];
-
 class ChromeBrowserProxyResolver : public ProxyResolver {
  public:
-  explicit ChromeBrowserProxyResolver(LibCrosProxy* libcros_proxy);
+  explicit ChromeBrowserProxyResolver(
+      org::chromium::NetworkProxyServiceInterfaceProxyInterface* dbus_proxy);
   ~ChromeBrowserProxyResolver() override;
 
-  // Initialize the ProxyResolver using the provided DBus proxies.
-  bool Init();
-
-  ProxyRequestId GetProxiesForUrl(const std::string& url,
-                                  const ProxiesResolvedFn& callback) override;
-  bool CancelProxyRequest(ProxyRequestId request) override;
-
- private:
-  FRIEND_TEST(ChromeBrowserProxyResolverTest, ParseTest);
-  FRIEND_TEST(ChromeBrowserProxyResolverTest, SuccessTest);
-  struct ProxyRequestData {
-    brillo::MessageLoop::TaskId timeout_id;
-    ProxiesResolvedFn callback;
-  };
-  typedef std::multimap<std::string, std::unique_ptr<ProxyRequestData>>
-      CallbacksMap;
-
-  // Called when the signal in UpdateEngineLibcrosProxyResolvedInterface is
-  // connected.
-  void OnSignalConnected(const std::string& interface_name,
-                         const std::string& signal_name,
-                         bool successful);
-
-  // Handle a reply from Chrome:
-  void OnProxyResolvedSignal(const std::string& source_url,
-                             const std::string& proxy_info,
-                             const std::string& error_message);
-
-  // Handle no reply. The |request| pointer points to the ProxyRequestData in
-  // the |callbacks_| map that triggered this timeout.
-  void HandleTimeout(std::string source_url, ProxyRequestData* request);
-
   // Parses a string-encoded list of proxies and returns a deque
   // of individual proxies. The last one will always be kNoProxy.
   static std::deque<std::string> ParseProxyString(const std::string& input);
 
-  // Process a proxy response by calling all the callbacks associated with the
-  // passed |source_url|. All the timeouts associated with these callbacks will
-  // be removed.
-  void ProcessUrlResponse(const std::string& source_url,
-                          const std::deque<std::string>& proxies);
+  // ProxyResolver:
+  ProxyRequestId GetProxiesForUrl(const std::string& url,
+                                  const ProxiesResolvedFn& callback) override;
+  bool CancelProxyRequest(ProxyRequestId request) override;
 
-  // Shutdown the dbus proxy object.
-  void Shutdown();
+private:
+  // Callback for successful D-Bus calls made by GetProxiesForUrl().
+  void OnResolveProxyResponse(ProxyRequestId request_id,
+                              const std::string& proxy_info,
+                              const std::string& error_message);
 
-  // DBus proxies to request a HTTP proxy resolution. The request is done in the
-  // service_interface_proxy() interface and the response is received as a
-  // signal in the ue_proxy_resolved_interface().
-  LibCrosProxy* libcros_proxy_;
+  // Callback for failed D-Bus calls made by GetProxiesForUrl().
+  void OnResolveProxyError(ProxyRequestId request_id, brillo::Error* error);
 
-  int timeout_;
-  CallbacksMap callbacks_;
+  // Finds the callback identified by |request_id| in |pending_callbacks_|,
+  // passes |proxies| to it, and deletes it. Does nothing if the request has
+  // been cancelled.
+  void RunCallback(ProxyRequestId request_id,
+                   const std::deque<std::string>& proxies);
+
+  // D-Bus proxy for resolving network proxies.
+  org::chromium::NetworkProxyServiceInterfaceProxyInterface* dbus_proxy_;
+
+  // Next ID to return from GetProxiesForUrl().
+  ProxyRequestId next_request_id_;
+
+  // Callbacks that were passed to GetProxiesForUrl() but haven't yet been run.
+  std::map<ProxyRequestId, ProxiesResolvedFn> pending_callbacks_;
+
+  base::WeakPtrFactory<ChromeBrowserProxyResolver> weak_ptr_factory_;
+
   DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProxyResolver);
 };