chrome_cookie_policy.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_ 6#define CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_ 7 8#include <map> 9#include <queue> 10#include <string> 11#include <vector> 12 13#include "base/ref_counted.h" 14#include "googleurl/src/gurl.h" 15#include "net/base/cookie_policy.h" 16 17class HostContentSettingsMap; 18 19// Implements CookiePolicy that may delay queries to ask the user to decide. 20// 21// We will only prompt the user before setting a cookie. We do not prompt the 22// user before getting a cookie. However, if we are in the process of 23// prompting the user, then any requests to get cookies will be deferred. 24// This is done so that cookie requests remain FIFO. 25// 26class ChromeCookiePolicy 27 : public base::RefCountedThreadSafe<ChromeCookiePolicy>, 28 public net::CookiePolicy { 29 public: 30 explicit ChromeCookiePolicy(HostContentSettingsMap* map); 31 ~ChromeCookiePolicy(); 32 33 // CookiePolicy methods: 34 virtual int CanGetCookies(const GURL& url, const GURL& first_party, 35 net::CompletionCallback* callback); 36 virtual int CanSetCookie(const GURL& url, const GURL& first_party, 37 const std::string& cookie_line, 38 net::CompletionCallback* callback); 39 40 private: 41 class PromptDelegate; 42 friend class PromptDelegate; 43 44 class Completion { 45 public: 46 static Completion ForSetCookie(net::CompletionCallback* callback) { 47 return Completion(true, callback); 48 } 49 50 static Completion ForGetCookies(net::CompletionCallback* callback) { 51 return Completion(false, callback); 52 } 53 54 bool is_set_cookie_request() const { return is_set_cookie_request_; } 55 net::CompletionCallback* callback() const { return callback_; } 56 57 private: 58 Completion(bool is_set_cookie_request, net::CompletionCallback* callback) 59 : is_set_cookie_request_(is_set_cookie_request), 60 callback_(callback) { 61 } 62 63 bool is_set_cookie_request_; 64 net::CompletionCallback* callback_; 65 }; 66 typedef std::vector<Completion> Completions; 67 typedef std::map<std::string, Completions> HostCompletionsMap; 68 69 int CheckPolicy(const GURL& url) const; 70 void PromptForSetCookie(const GURL& url, const std::string& cookie_line); 71 void DidPromptForSetCookie(const std::string& host, int result); 72 73 // A map from hostname to callbacks awaiting a cookie policy response. 74 // This map is only accessed on the IO thread. 75 HostCompletionsMap host_completions_map_; 76 77 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 78}; 79 80#endif // CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_ 81