Move ProxyResolver to common.

HttpFetcher depends on it and HttpFetcher is in common.

Bug: 28171891
Test: mma
Change-Id: I1783821c4a7d6785ee23df1cde648a3e1ea8fadf
diff --git a/common/http_fetcher.h b/common/http_fetcher.h
index b2fba1c..1f5c945 100644
--- a/common/http_fetcher.h
+++ b/common/http_fetcher.h
@@ -28,7 +28,7 @@
 #include <brillo/message_loops/message_loop.h>
 
 #include "update_engine/common/http_common.h"
-#include "update_engine/proxy_resolver.h"
+#include "update_engine/common/proxy_resolver.h"
 
 // This class is a simple wrapper around an HTTP library (libcurl). We can
 // easily mock out this interface for testing.
diff --git a/common/http_fetcher_unittest.cc b/common/http_fetcher_unittest.cc
index 23df67a..66767fb 100644
--- a/common/http_fetcher_unittest.cc
+++ b/common/http_fetcher_unittest.cc
@@ -44,12 +44,12 @@
 #include "update_engine/common/file_fetcher.h"
 #include "update_engine/common/http_common.h"
 #include "update_engine/common/mock_http_fetcher.h"
+#include "update_engine/common/mock_proxy_resolver.h"
 #include "update_engine/common/multi_range_http_fetcher.h"
+#include "update_engine/common/proxy_resolver.h"
 #include "update_engine/common/test_utils.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/libcurl_http_fetcher.h"
-#include "update_engine/mock_proxy_resolver.h"
-#include "update_engine/proxy_resolver.h"
 
 using brillo::MessageLoop;
 using std::make_pair;
diff --git a/common/mock_proxy_resolver.h b/common/mock_proxy_resolver.h
new file mode 100644
index 0000000..67de68f
--- /dev/null
+++ b/common/mock_proxy_resolver.h
@@ -0,0 +1,38 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_COMMON_MOCK_PROXY_RESOLVER_H_
+#define UPDATE_ENGINE_COMMON_MOCK_PROXY_RESOLVER_H_
+
+#include <string>
+
+#include <gmock/gmock.h>
+
+#include "update_engine/common/proxy_resolver.h"
+
+namespace chromeos_update_engine {
+
+class MockProxyResolver : public ProxyResolver {
+ public:
+  MOCK_METHOD2(GetProxiesForUrl,
+               ProxyRequestId(const std::string& url,
+                              const ProxiesResolvedFn& callback));
+  MOCK_METHOD1(CancelProxyRequest, bool(ProxyRequestId request));
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_COMMON_MOCK_PROXY_RESOLVER_H_
diff --git a/common/proxy_resolver.cc b/common/proxy_resolver.cc
new file mode 100644
index 0000000..0591c3e
--- /dev/null
+++ b/common/proxy_resolver.cc
@@ -0,0 +1,66 @@
+//
+// Copyright (C) 2010 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/common/proxy_resolver.h"
+
+#include <base/bind.h>
+#include <base/location.h>
+
+using brillo::MessageLoop;
+using std::deque;
+using std::string;
+
+namespace chromeos_update_engine {
+
+const char kNoProxy[] = "direct://";
+const ProxyRequestId kProxyRequestIdNull = brillo::MessageLoop::kTaskIdNull;
+
+DirectProxyResolver::~DirectProxyResolver() {
+  if (idle_callback_id_ != MessageLoop::kTaskIdNull) {
+    // The DirectProxyResolver is instantiated as part of the UpdateAttempter
+    // which is also instantiated by default by the FakeSystemState, even when
+    // it is not used. We check the manage_shares_id_ before calling the
+    // MessageLoop::current() since the unit test using a FakeSystemState may
+    // have not define a MessageLoop for the current thread.
+    MessageLoop::current()->CancelTask(idle_callback_id_);
+    idle_callback_id_ = MessageLoop::kTaskIdNull;
+  }
+}
+
+ProxyRequestId DirectProxyResolver::GetProxiesForUrl(
+    const string& url, const ProxiesResolvedFn& callback) {
+  idle_callback_id_ = MessageLoop::current()->PostTask(
+      FROM_HERE,
+      base::Bind(&DirectProxyResolver::ReturnCallback,
+                 base::Unretained(this),
+                 callback));
+  return idle_callback_id_;
+}
+
+bool DirectProxyResolver::CancelProxyRequest(ProxyRequestId request) {
+  return MessageLoop::current()->CancelTask(request);
+}
+
+void DirectProxyResolver::ReturnCallback(const ProxiesResolvedFn& callback) {
+  idle_callback_id_ = MessageLoop::kTaskIdNull;
+
+  // Initialize proxy pool with as many proxies as indicated (all identical).
+  deque<string> proxies(num_proxies_, kNoProxy);
+
+  callback.Run(proxies);
+}
+
+}  // namespace chromeos_update_engine
diff --git a/common/proxy_resolver.h b/common/proxy_resolver.h
new file mode 100644
index 0000000..9bd51fc
--- /dev/null
+++ b/common/proxy_resolver.h
@@ -0,0 +1,98 @@
+//
+// Copyright (C) 2010 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_COMMON_PROXY_RESOLVER_H_
+#define UPDATE_ENGINE_COMMON_PROXY_RESOLVER_H_
+
+#include <deque>
+#include <string>
+
+#include <base/logging.h>
+#include <brillo/message_loops/message_loop.h>
+
+#include "update_engine/common/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 base::Callback<void(const std::deque<std::string>& proxies)>
+    ProxiesResolvedFn;
+
+// An id that identifies a proxy request. Used to cancel an ongoing request
+// before the callback is called.
+typedef brillo::MessageLoop::TaskId ProxyRequestId;
+
+// A constant identifying an invalid ProxyRequestId.
+extern const ProxyRequestId kProxyRequestIdNull;
+
+class ProxyResolver {
+ public:
+  ProxyResolver() {}
+  virtual ~ProxyResolver() {}
+
+  // Finds proxies for the given URL and returns them via the callback.
+  // Returns the id of the pending request on success or kProxyRequestIdNull
+  // otherwise.
+  virtual ProxyRequestId GetProxiesForUrl(
+      const std::string& url, const ProxiesResolvedFn& callback) = 0;
+
+  // Cancel the proxy resolution request initiated by GetProxiesForUrl(). The
+  // |request| value must be the one provided by GetProxiesForUrl().
+  virtual bool CancelProxyRequest(ProxyRequestId request) = 0;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(ProxyResolver);
+};
+
+// Always says to not use a proxy
+class DirectProxyResolver : public ProxyResolver {
+ public:
+  DirectProxyResolver() = default;
+  ~DirectProxyResolver() override;
+  ProxyRequestId GetProxiesForUrl(const std::string& url,
+                                  const ProxiesResolvedFn& callback) override;
+  bool CancelProxyRequest(ProxyRequestId request) override;
+
+  // Set the number of direct (non-) proxies to be returned by resolver.
+  // The default value is 1; higher numbers are currently used in testing.
+  inline void set_num_proxies(size_t num_proxies) {
+    num_proxies_ = num_proxies;
+  }
+
+ private:
+  // The ID of the main loop callback.
+  brillo::MessageLoop::TaskId idle_callback_id_{
+      brillo::MessageLoop::kTaskIdNull};
+
+  // Number of direct proxies to return on resolved list; currently used for
+  // testing.
+  size_t num_proxies_{1};
+
+  // The MainLoop callback, from here we return to the client.
+  void ReturnCallback(const ProxiesResolvedFn& callback);
+  DISALLOW_COPY_AND_ASSIGN(DirectProxyResolver);
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_COMMON_PROXY_RESOLVER_H_
diff --git a/common/proxy_resolver_unittest.cc b/common/proxy_resolver_unittest.cc
new file mode 100644
index 0000000..101bf6b
--- /dev/null
+++ b/common/proxy_resolver_unittest.cc
@@ -0,0 +1,91 @@
+//
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/common/proxy_resolver.h"
+
+#include <deque>
+#include <string>
+
+#include <gtest/gtest.h>
+
+#include <base/bind.h>
+#include <brillo/message_loops/fake_message_loop.h>
+
+using std::deque;
+using std::string;
+
+namespace chromeos_update_engine {
+
+class ProxyResolverTest : public ::testing::Test {
+ protected:
+  virtual ~ProxyResolverTest() = default;
+
+  void SetUp() override { loop_.SetAsCurrent(); }
+
+  void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
+
+  brillo::FakeMessageLoop loop_{nullptr};
+  DirectProxyResolver resolver_;
+};
+
+TEST_F(ProxyResolverTest, DirectProxyResolverCallbackTest) {
+  bool called = false;
+  deque<string> callback_proxies;
+  auto callback = base::Bind(
+      [](bool* called,
+         deque<string>* callback_proxies,
+         const deque<string>& proxies) {
+        *called = true;
+        *callback_proxies = proxies;
+      },
+      &called,
+      &callback_proxies);
+
+  EXPECT_NE(kProxyRequestIdNull,
+            resolver_.GetProxiesForUrl("http://foo", callback));
+  // Check the callback is not called until the message loop runs.
+  EXPECT_FALSE(called);
+  loop_.Run();
+  EXPECT_TRUE(called);
+  EXPECT_EQ(kNoProxy, callback_proxies.front());
+}
+
+TEST_F(ProxyResolverTest, DirectProxyResolverCancelCallbackTest) {
+  bool called = false;
+  auto callback = base::Bind(
+      [](bool* called, const deque<string>& proxies) { *called = true; },
+      &called);
+
+  ProxyRequestId request = resolver_.GetProxiesForUrl("http://foo", callback);
+  EXPECT_FALSE(called);
+  EXPECT_TRUE(resolver_.CancelProxyRequest(request));
+  loop_.Run();
+  EXPECT_FALSE(called);
+}
+
+TEST_F(ProxyResolverTest, DirectProxyResolverSimultaneousCallbacksTest) {
+  int called = 0;
+  auto callback = base::Bind(
+      [](int* called, const deque<string>& proxies) { (*called)++; }, &called);
+
+  resolver_.GetProxiesForUrl("http://foo", callback);
+  resolver_.GetProxiesForUrl("http://bar", callback);
+  EXPECT_EQ(0, called);
+  loop_.Run();
+  EXPECT_EQ(2, called);
+}
+
+}  // namespace chromeos_update_engine