Deduplicate some cookie manager code

The two cookie managers in restricted_cookie_manager.cc and
cookie_manager.cc need some shared functionality and rather
than having a duplicate set of helper functions (of which one
clashed in some jumbo builds and cause build breakage), move
the shared helper functions to a new file cookie_managers_shared.cc.

Bug: 729800
Change-Id: I1839bd04e96471569b1b8a53bf1921f182b8bff4
Reviewed-on: https://chromium-review.googlesource.com/c/1482974
Reviewed-by: Maks Orlovich <morlovich@chromium.org>
Commit-Queue: Daniel Bratell <bratell@opera.com>
Cr-Commit-Position: refs/heads/master@{#635522}
diff --git a/services/network/BUILD.gn b/services/network/BUILD.gn
index 13b8b829..dd858f0 100644
--- a/services/network/BUILD.gn
+++ b/services/network/BUILD.gn
@@ -17,6 +17,8 @@
     "conditional_cache_deletion_helper.h",
     "cookie_manager.cc",
     "cookie_manager.h",
+    "cookie_managers_shared.cc",
+    "cookie_managers_shared.h",
     "cookie_settings.cc",
     "cookie_settings.h",
     "cors/cors_url_loader.cc",
diff --git a/services/network/cookie_manager.cc b/services/network/cookie_manager.cc
index cbc745a..0c7ce77a 100644
--- a/services/network/cookie_manager.cc
+++ b/services/network/cookie_manager.cc
@@ -14,6 +14,7 @@
 #include "net/cookies/cookie_options.h"
 #include "net/cookies/cookie_store.h"
 #include "net/cookies/cookie_util.h"
+#include "services/network/cookie_managers_shared.h"
 #include "services/network/session_cleanup_channel_id_store.h"
 #include "services/network/session_cleanup_cookie_store.h"
 #include "url/gurl.h"
@@ -25,28 +26,6 @@
 
 namespace {
 
-mojom::CookieChangeCause ChangeCauseTranslation(
-    net::CookieChangeCause net_cause) {
-  switch (net_cause) {
-    case net::CookieChangeCause::INSERTED:
-      return mojom::CookieChangeCause::INSERTED;
-    case net::CookieChangeCause::EXPLICIT:
-      return mojom::CookieChangeCause::EXPLICIT;
-    case net::CookieChangeCause::UNKNOWN_DELETION:
-      return mojom::CookieChangeCause::UNKNOWN_DELETION;
-    case net::CookieChangeCause::OVERWRITE:
-      return mojom::CookieChangeCause::OVERWRITE;
-    case net::CookieChangeCause::EXPIRED:
-      return mojom::CookieChangeCause::EXPIRED;
-    case net::CookieChangeCause::EVICTED:
-      return mojom::CookieChangeCause::EVICTED;
-    case net::CookieChangeCause::EXPIRED_OVERWRITE:
-      return mojom::CookieChangeCause::EXPIRED_OVERWRITE;
-  }
-  NOTREACHED();
-  return mojom::CookieChangeCause::EXPLICIT;
-}
-
 // Converts the one-argument callbacks to two-argument callback that ignores
 // the second arument for the cookie_store
 net::CookieStore::GetCookieListCallback IgnoreSecondArg(
@@ -60,18 +39,6 @@
       std::move(callback));
 }
 
-net::CookieStore::SetCookiesCallback StatusToBool(
-    base::OnceCallback<void(bool)> callback) {
-  return base::BindOnce(
-      [](base::OnceCallback<void(bool)> callback,
-         const net::CanonicalCookie::CookieInclusionStatus status) {
-        bool success =
-            (status == net::CanonicalCookie::CookieInclusionStatus::INCLUDE);
-        std::move(callback).Run(success);
-      },
-      std::move(callback));
-}
-
 }  // namespace
 
 CookieManager::ListenerRegistration::ListenerRegistration() {}
@@ -81,7 +48,7 @@
 void CookieManager::ListenerRegistration::DispatchCookieStoreChange(
     const net::CanonicalCookie& cookie,
     net::CookieChangeCause cause) {
-  listener->OnCookieChange(cookie, ChangeCauseTranslation(cause));
+  listener->OnCookieChange(cookie, ToCookieChangeCause(cause));
 }
 
 CookieManager::CookieManager(
@@ -140,7 +107,7 @@
                                        SetCanonicalCookieCallback callback) {
   cookie_store_->SetCanonicalCookieAsync(
       std::make_unique<net::CanonicalCookie>(cookie), source_scheme,
-      modify_http_only, StatusToBool(std::move(callback)));
+      modify_http_only, AdaptCookieInclusionStatusToBool(std::move(callback)));
 }
 
 void CookieManager::DeleteCanonicalCookie(
diff --git a/services/network/cookie_managers_shared.cc b/services/network/cookie_managers_shared.cc
new file mode 100644
index 0000000..04ea94d
--- /dev/null
+++ b/services/network/cookie_managers_shared.cc
@@ -0,0 +1,47 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "services/network/cookie_managers_shared.h"
+
+#include <utility>
+
+#include "base/bind.h"
+#include "net/cookies/cookie_change_dispatcher.h"
+
+namespace network {
+
+mojom::CookieChangeCause ToCookieChangeCause(net::CookieChangeCause net_cause) {
+  switch (net_cause) {
+    case net::CookieChangeCause::INSERTED:
+      return mojom::CookieChangeCause::INSERTED;
+    case net::CookieChangeCause::EXPLICIT:
+      return mojom::CookieChangeCause::EXPLICIT;
+    case net::CookieChangeCause::UNKNOWN_DELETION:
+      return mojom::CookieChangeCause::UNKNOWN_DELETION;
+    case net::CookieChangeCause::OVERWRITE:
+      return mojom::CookieChangeCause::OVERWRITE;
+    case net::CookieChangeCause::EXPIRED:
+      return mojom::CookieChangeCause::EXPIRED;
+    case net::CookieChangeCause::EVICTED:
+      return mojom::CookieChangeCause::EVICTED;
+    case net::CookieChangeCause::EXPIRED_OVERWRITE:
+      return mojom::CookieChangeCause::EXPIRED_OVERWRITE;
+  }
+  NOTREACHED();
+  return mojom::CookieChangeCause::EXPLICIT;
+}
+
+net::CookieStore::SetCookiesCallback AdaptCookieInclusionStatusToBool(
+    base::OnceCallback<void(bool)> callback) {
+  return base::BindOnce(
+      [](base::OnceCallback<void(bool)> callback,
+         const net::CanonicalCookie::CookieInclusionStatus status) {
+        bool success =
+            (status == net::CanonicalCookie::CookieInclusionStatus::INCLUDE);
+        std::move(callback).Run(success);
+      },
+      std::move(callback));
+}
+
+}  // namespace network
diff --git a/services/network/cookie_managers_shared.h b/services/network/cookie_managers_shared.h
new file mode 100644
index 0000000..09d5c8f
--- /dev/null
+++ b/services/network/cookie_managers_shared.h
@@ -0,0 +1,24 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SERVICES_NETWORK_COOKIE_MANAGERS_SHARED_H_
+#define SERVICES_NETWORK_COOKIE_MANAGERS_SHARED_H_
+
+#include "net/cookies/cookie_store.h"
+#include "services/network/public/mojom/cookie_manager.mojom.h"
+
+namespace net {
+enum class CookieChangeCause;
+}
+
+namespace network {
+
+mojom::CookieChangeCause ToCookieChangeCause(net::CookieChangeCause net_cause);
+
+net::CookieStore::SetCookiesCallback AdaptCookieInclusionStatusToBool(
+    base::OnceCallback<void(bool)> callback);
+
+}  // namespace network
+
+#endif  // SERVICES_NETWORK_COOKIE_MANAGERS_SHARED_H_
diff --git a/services/network/restricted_cookie_manager.cc b/services/network/restricted_cookie_manager.cc
index a6e5a1e..75acb6e 100644
--- a/services/network/restricted_cookie_manager.cc
+++ b/services/network/restricted_cookie_manager.cc
@@ -18,47 +18,10 @@
 #include "net/cookies/cookie_constants.h"
 #include "net/cookies/cookie_options.h"
 #include "net/cookies/cookie_store.h"
+#include "services/network/cookie_managers_shared.h"
 
 namespace network {
 
-namespace {
-
-// TODO(pwnall): De-duplicate from cookie_manager.cc
-mojom::CookieChangeCause ToCookieChangeCause(net::CookieChangeCause net_cause) {
-  switch (net_cause) {
-    case net::CookieChangeCause::INSERTED:
-      return mojom::CookieChangeCause::INSERTED;
-    case net::CookieChangeCause::EXPLICIT:
-      return mojom::CookieChangeCause::EXPLICIT;
-    case net::CookieChangeCause::UNKNOWN_DELETION:
-      return mojom::CookieChangeCause::UNKNOWN_DELETION;
-    case net::CookieChangeCause::OVERWRITE:
-      return mojom::CookieChangeCause::OVERWRITE;
-    case net::CookieChangeCause::EXPIRED:
-      return mojom::CookieChangeCause::EXPIRED;
-    case net::CookieChangeCause::EVICTED:
-      return mojom::CookieChangeCause::EVICTED;
-    case net::CookieChangeCause::EXPIRED_OVERWRITE:
-      return mojom::CookieChangeCause::EXPIRED_OVERWRITE;
-  }
-  NOTREACHED();
-  return mojom::CookieChangeCause::EXPLICIT;
-}
-
-net::CookieStore::SetCookiesCallback StatusToBool(
-    base::OnceCallback<void(bool)> callback) {
-  return base::BindOnce(
-      [](base::OnceCallback<void(bool)> callback,
-         const net::CanonicalCookie::CookieInclusionStatus status) {
-        bool success =
-            (status == net::CanonicalCookie::CookieInclusionStatus::INCLUDE);
-        std::move(callback).Run(success);
-      },
-      std::move(callback));
-}
-
-}  // anonymous namespace
-
 class RestrictedCookieManager::Listener : public base::LinkNode<Listener> {
  public:
   Listener(net::CookieStore* cookie_store,
@@ -221,9 +184,9 @@
 
   // TODO(pwnall): source_scheme might depend on the renderer.
   bool modify_http_only = false;
-  cookie_store_->SetCanonicalCookieAsync(std::move(sanitized_cookie),
-                                         origin_.scheme(), modify_http_only,
-                                         StatusToBool(std::move(callback)));
+  cookie_store_->SetCanonicalCookieAsync(
+      std::move(sanitized_cookie), origin_.scheme(), modify_http_only,
+      AdaptCookieInclusionStatusToBool(std::move(callback)));
 }
 
 void RestrictedCookieManager::AddChangeListener(