Reland "Unify ChromeNetworkDelegate and NetworkServiceNetworkDelegate cookie logic"

This reverts commit 150c7353da386029eac1513f22257a3717ebf991.

Added some more tests, and support for the special casing of chrome:// URLs.
The chrome-extension:// case is only needed when network service is disabled,
since chrome-extension:// requests only use the URLRequestContext in that case.

See diff after Patchset 1 for changes since the original.

Original change's description:
> Revert "Unify ChromeNetworkDelegate and NetworkServiceNetworkDelegate cookie logic"
>
> This reverts commit 8420d58c08a2b1dd5a0a414e44284491a5518331.
>
> Reason for revert: This breaks the sign-in on the print dialog when third party
> cookies are blocked: http://crbug.com/878051.
> This is because the network service does not have the custom logic to whitelist
> chrome:// or chrome-extensions:// schemes, which used to be here:
> https://cs.chromium.org/chromium/src/components/content_settings/core/browser/cookie_settings.cc?l=98&rcl=5edbbd319ff4010b66e07ec404a3188716a9cda2
>
> The print dialog issues requests from chrome://print, which was now blocked from
> using third party cookies.
>
> Original change's description:
> > Unify ChromeNetworkDelegate and NetworkServiceNetworkDelegate cookie logic
> >
> > This was suggested in crbug.com/789636 so this logic can run in
> > production before the launch of the network service.
> >
> > Had to keep the tab notification logic in each delegate, since the
> > way NetworkServiceNetworkDelegate maps the request back to the
> > original tab (using the URLLoader set as user data on the request)
> > does not work if network service is disabled.
> >
> > Bug: 789636, 789632
> > Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:linux_mojo;master.tryserver.chromium.android:android_cronet_tester;master.tryserver.chromium.mac:ios-simulator-cronet
> > Change-Id: I22395f914639d8ce83457a63062927e876caeaa9
> > Reviewed-on: https://chromium-review.googlesource.com/1113903
> > Reviewed-by: Matt Menke <[email protected]>
> > Reviewed-by: John Abd-El-Malek <[email protected]>
> > Commit-Queue: Clark DuVall <[email protected]>
> > Cr-Commit-Position: refs/heads/master@{#570877}
>
> Bug: 878051, 789636, 789632
> Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:linux_mojo
> Change-Id: Ib023acff943f992c8b6ec8b15bbdbc1524ed3720
> Reviewed-on: https://chromium-review.googlesource.com/1194329
> Commit-Queue: Clark DuVall <[email protected]>
> Reviewed-by: John Abd-El-Malek <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#586944}

Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:linux_mojo;master.tryserver.chromium.android:android_cronet_tester
Change-Id: Ia2b596baeacd1d25d99104db88403846e251f806
Bug: 878051, 789636, 789632
Reviewed-on: https://chromium-review.googlesource.com/1195147
Commit-Queue: Clark DuVall <[email protected]>
Reviewed-by: Matt Menke <[email protected]>
Reviewed-by: Will Harris <[email protected]>
Reviewed-by: John Abd-El-Malek <[email protected]>
Cr-Commit-Position: refs/heads/master@{#588072}
diff --git a/chrome/browser/net/chrome_network_delegate.cc b/chrome/browser/net/chrome_network_delegate.cc
index 78c216c..a301416 100644
--- a/chrome/browser/net/chrome_network_delegate.cc
+++ b/chrome/browser/net/chrome_network_delegate.cc
@@ -267,48 +267,35 @@
       request, auth_info, std::move(callback), credentials);
 }
 
-bool ChromeNetworkDelegate::OnCanGetCookies(
-    const net::URLRequest& request,
-    const net::CookieList& cookie_list) {
-  // nullptr during tests, or when we're running in the system context.
-  if (!cookie_settings_.get())
-    return true;
-
-  bool allow = cookie_settings_->IsCookieAccessAllowed(
-      request.url(), request.site_for_cookies());
-
+bool ChromeNetworkDelegate::OnCanGetCookies(const net::URLRequest& request,
+                                            const net::CookieList& cookie_list,
+                                            bool allowed_from_caller) {
   const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
   if (info) {
     BrowserThread::PostTask(
         BrowserThread::UI, FROM_HERE,
         base::BindOnce(&TabSpecificContentSettings::CookiesRead,
                        info->GetWebContentsGetterForRequest(), request.url(),
-                       request.site_for_cookies(), cookie_list, !allow));
+                       request.site_for_cookies(), cookie_list,
+                       !allowed_from_caller));
   }
-
-  return allow;
+  return allowed_from_caller;
 }
 
 bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
                                            const net::CanonicalCookie& cookie,
-                                           net::CookieOptions* options) {
-  // nullptr during tests, or when we're running in the system context.
-  if (!cookie_settings_.get())
-    return true;
-
-  bool allow = cookie_settings_->IsCookieAccessAllowed(
-      request.url(), request.site_for_cookies());
-
+                                           net::CookieOptions* options,
+                                           bool allowed_from_caller) {
   const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
   if (info) {
     BrowserThread::PostTask(
         BrowserThread::UI, FROM_HERE,
         base::BindOnce(&TabSpecificContentSettings::CookieChanged,
                        info->GetWebContentsGetterForRequest(), request.url(),
-                       request.site_for_cookies(), cookie, !allow));
+                       request.site_for_cookies(), cookie,
+                       !allowed_from_caller));
   }
-
-  return allow;
+  return allowed_from_caller;
 }
 
 bool ChromeNetworkDelegate::OnCanAccessFile(
@@ -347,16 +334,6 @@
   g_access_to_all_files_enabled = enabled;
 }
 
-bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
-    const GURL& url,
-    const GURL& site_for_cookies) const {
-  // nullptr during tests, or when we're running in the system context.
-  if (!cookie_settings_.get())
-    return false;
-
-  return !cookie_settings_->IsCookieAccessAllowed(url, site_for_cookies);
-}
-
 bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
   return experimental_web_platform_features_enabled_;
 }