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/net/base/layered_network_delegate.cc b/net/base/layered_network_delegate.cc
index b5a37da..5b4430a 100644
--- a/net/base/layered_network_delegate.cc
+++ b/net/base/layered_network_delegate.cc
@@ -182,26 +182,36 @@
     AuthCredentials* credentials) {}
 
 bool LayeredNetworkDelegate::OnCanGetCookies(const URLRequest& request,
-                                             const CookieList& cookie_list) {
-  OnCanGetCookiesInternal(request, cookie_list);
-  return nested_network_delegate_->CanGetCookies(request, cookie_list);
+                                             const CookieList& cookie_list,
+                                             bool allowed_from_caller) {
+  return nested_network_delegate_->CanGetCookies(
+      request, cookie_list,
+      OnCanGetCookiesInternal(request, cookie_list, allowed_from_caller));
 }
 
-void LayeredNetworkDelegate::OnCanGetCookiesInternal(
+bool LayeredNetworkDelegate::OnCanGetCookiesInternal(
     const URLRequest& request,
-    const CookieList& cookie_list) {}
+    const CookieList& cookie_list,
+    bool allowed_from_caller) {
+  return allowed_from_caller;
+}
 
 bool LayeredNetworkDelegate::OnCanSetCookie(const URLRequest& request,
                                             const net::CanonicalCookie& cookie,
-                                            CookieOptions* options) {
-  OnCanSetCookieInternal(request, cookie, options);
-  return nested_network_delegate_->CanSetCookie(request, cookie, options);
+                                            CookieOptions* options,
+                                            bool allowed_from_caller) {
+  return nested_network_delegate_->CanSetCookie(
+      request, cookie, options,
+      OnCanSetCookieInternal(request, cookie, options, allowed_from_caller));
 }
 
-void LayeredNetworkDelegate::OnCanSetCookieInternal(
+bool LayeredNetworkDelegate::OnCanSetCookieInternal(
     const URLRequest& request,
     const net::CanonicalCookie& cookie,
-    CookieOptions* options) {}
+    CookieOptions* options,
+    bool allowed_from_caller) {
+  return allowed_from_caller;
+}
 
 bool LayeredNetworkDelegate::OnCanAccessFile(
     const URLRequest& request,
@@ -220,13 +230,15 @@
 bool LayeredNetworkDelegate::OnCanEnablePrivacyMode(
     const GURL& url,
     const GURL& site_for_cookies) const {
-  OnCanEnablePrivacyModeInternal(url, site_for_cookies);
-  return nested_network_delegate_->CanEnablePrivacyMode(url, site_for_cookies);
+  return OnCanEnablePrivacyModeInternal(url, site_for_cookies) ||
+         nested_network_delegate_->CanEnablePrivacyMode(url, site_for_cookies);
 }
 
-void LayeredNetworkDelegate::OnCanEnablePrivacyModeInternal(
+bool LayeredNetworkDelegate::OnCanEnablePrivacyModeInternal(
     const GURL& url,
-    const GURL& site_for_cookies) const {}
+    const GURL& site_for_cookies) const {
+  return false;
+}
 
 bool LayeredNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
   OnAreExperimentalCookieFeaturesEnabledInternal();