chrome_cookie_policy.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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#pragma once
8
9#include <map>
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  virtual ~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 Completion {
42   public:
43    static Completion ForSetCookie(net::CompletionCallback* callback) {
44      return Completion(true, callback);
45    }
46
47    static Completion ForGetCookies(net::CompletionCallback* callback) {
48      return Completion(false, callback);
49    }
50
51    bool is_set_cookie_request() const { return is_set_cookie_request_; }
52    net::CompletionCallback* callback() const { return callback_; }
53
54   private:
55    Completion(bool is_set_cookie_request, net::CompletionCallback* callback)
56        : is_set_cookie_request_(is_set_cookie_request),
57          callback_(callback) {
58    }
59
60    bool is_set_cookie_request_;
61    net::CompletionCallback* callback_;
62  };
63  typedef std::vector<Completion> Completions;
64  typedef std::map<std::string, Completions> HostCompletionsMap;
65
66  int CheckPolicy(const GURL& url) const;
67
68  // A map from hostname to callbacks awaiting a cookie policy response.
69  // This map is only accessed on the IO thread.
70  HostCompletionsMap host_completions_map_;
71
72  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
73
74  // True if blocking third-party cookies also applies to reading them.
75  bool strict_third_party_blocking_;
76
77  DISALLOW_COPY_AND_ASSIGN(ChromeCookiePolicy);
78};
79
80#endif  // CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_
81