More progress towards removing content settings code from the content layer.  We can't use CookiePolicy anymore since, being in the network layer, we can't give it render_process_id/render_view_id, which are needed to put up the content settings UI.  Instead of the networking code calling CookiePolicy then calling the delegate (ResourceDispatcherHost) to inform it if something is blocked, we directly ask the delegate if something is allowed.  ResourceDispatcherHost then calls the embedder, and passes along the IDs to identify the tab.  In the next change, I'll use this mechansim in RenderMessageFilter and remove the CookiePolicy class.

BUG=76793
Review URL: http://codereview.chromium.org/6995013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84881 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc
index be93cc2..b7ce694 100644
--- a/chrome/browser/chrome_content_browser_client.cc
+++ b/chrome/browser/chrome_content_browser_client.cc
@@ -29,9 +29,13 @@
 #include "chrome/common/pref_names.h"
 #include "content/browser/renderer_host/browser_render_process_host.h"
 #include "content/browser/renderer_host/render_view_host.h"
+#include "content/browser/renderer_host/render_view_host_notification_task.h"
 #include "content/browser/resource_context.h"
 #include "content/browser/tab_contents/tab_contents.h"
 #include "content/browser/worker_host/worker_process_host.h"
+#include "net/base/cookie_monster.h"
+#include "net/base/cookie_options.h"
+#include "net/base/static_cookie_policy.h"
 
 #if defined(OS_LINUX)
 #include "base/linux_util.h"
@@ -183,13 +187,87 @@
 }
 
 bool ChromeContentBrowserClient::AllowAppCache(
-    const GURL& manifest_url, const content::ResourceContext* context) {
-  ContentSetting setting = context->host_content_settings_map()->
+    const GURL& manifest_url, const content::ResourceContext& context) {
+  ContentSetting setting = context.host_content_settings_map()->
       GetContentSetting(manifest_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
   DCHECK(setting != CONTENT_SETTING_DEFAULT);
   return setting != CONTENT_SETTING_BLOCK;
 }
 
+bool ChromeContentBrowserClient::AllowGetCookie(
+    const GURL& url,
+    const GURL& first_party,
+    const net::CookieList& cookie_list,
+    const content::ResourceContext& context,
+    int render_process_id,
+    int render_view_id) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  bool allow = true;
+  if (context.host_content_settings_map()->BlockThirdPartyCookies()) {
+    bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
+        switches::kBlockReadingThirdPartyCookies);
+    net::StaticCookiePolicy policy(strict ?
+        net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
+        net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
+    int rv = policy.CanGetCookies(url, first_party);
+    DCHECK_NE(net::ERR_IO_PENDING, rv);
+    if (rv != net::OK)
+      allow = false;
+  }
+
+  if (allow) {
+    ContentSetting setting = context.host_content_settings_map()->
+        GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
+    allow = setting == CONTENT_SETTING_ALLOW ||
+        setting == CONTENT_SETTING_SESSION_ONLY;
+  }
+
+  CallRenderViewHostContentSettingsDelegate(
+      render_process_id, render_view_id,
+      &RenderViewHostDelegate::ContentSettings::OnCookiesRead,
+      url, cookie_list, !allow);
+  return allow;
+}
+
+bool ChromeContentBrowserClient::AllowSetCookie(
+    const GURL& url,
+    const GURL& first_party,
+    const std::string& cookie_line,
+    const content::ResourceContext& context,
+    int render_process_id,
+    int render_view_id,
+    net::CookieOptions* options) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  bool allow = true;
+  if (context.host_content_settings_map()->BlockThirdPartyCookies()) {
+    bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
+        switches::kBlockReadingThirdPartyCookies);
+    net::StaticCookiePolicy policy(strict ?
+        net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
+        net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
+    int rv = policy.CanSetCookie(url, first_party, cookie_line);
+    if (rv != net::OK)
+      allow = false;
+  }
+
+  if (allow) {
+    ContentSetting setting = context.host_content_settings_map()->
+        GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
+
+    if (setting == CONTENT_SETTING_SESSION_ONLY)
+      options->set_force_session();
+
+    allow = setting == CONTENT_SETTING_ALLOW ||
+        setting == CONTENT_SETTING_SESSION_ONLY;
+  }
+
+  CallRenderViewHostContentSettingsDelegate(
+      render_process_id, render_view_id,
+      &RenderViewHostDelegate::ContentSettings::OnCookieChanged,
+      url, cookie_line, *options, !allow);
+  return allow;
+}
+
 #if defined(OS_LINUX)
 int ChromeContentBrowserClient::GetCrashSignalFD(
     const std::string& process_type) {