Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_cookie_helper.h
blob7d9315b020fbb21d307346d12692640a77ed94b1
1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "net/cookies/cookie_monster.h"
16 class GURL;
18 namespace net {
19 class CanonicalCookie;
20 class URLRequestContextGetter;
23 // This class fetches cookie information on behalf of a caller
24 // on the UI thread.
25 // A client of this class need to call StartFetching from the UI thread to
26 // initiate the flow, and it'll be notified by the callback in its UI
27 // thread at some later point.
28 class BrowsingDataCookieHelper
29 : public base::RefCountedThreadSafe<BrowsingDataCookieHelper> {
30 public:
31 explicit BrowsingDataCookieHelper(
32 net::URLRequestContextGetter* request_context_getter);
34 // Starts the fetching process, which will notify its completion via
35 // callback.
36 // This must be called only in the UI thread.
37 virtual void StartFetching(
38 const base::Callback<void(const net::CookieList& cookies)>& callback);
40 // Requests a single cookie to be deleted in the IO thread. This must be
41 // called in the UI thread.
42 virtual void DeleteCookie(const net::CanonicalCookie& cookie);
44 protected:
45 friend class base::RefCountedThreadSafe<BrowsingDataCookieHelper>;
46 virtual ~BrowsingDataCookieHelper();
48 net::URLRequestContextGetter* request_context_getter() {
49 return request_context_getter_.get();
52 private:
53 // Fetch the cookies. This must be called in the IO thread.
54 void FetchCookiesOnIOThread();
56 // Callback function for get cookie. This must be called in the IO thread.
57 void OnFetchComplete(const net::CookieList& cookies);
59 // Notifies the completion callback. This must be called in the UI thread.
60 void NotifyInUIThread(const net::CookieList& cookies);
62 // Delete a single cookie. This must be called in IO thread.
63 void DeleteCookieOnIOThread(const net::CanonicalCookie& cookie);
65 // Indicates whether or not we're currently fetching information:
66 // it's true when StartFetching() is called in the UI thread, and it's reset
67 // after we notify the callback in the UI thread.
68 // This only mutates on the UI thread.
69 bool is_fetching_;
71 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
73 // This only mutates on the UI thread.
74 base::Callback<void(const net::CookieList& cookies)> completion_callback_;
76 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper);
79 // This class is a thin wrapper around BrowsingDataCookieHelper that does not
80 // fetch its information from the persistent cookie store. It is a simple
81 // container for CanonicalCookies. Clients that use this container can add
82 // cookies that are sent to a server via the AddReadCookies method and cookies
83 // that are received from a server or set via JavaScript using the method
84 // AddChangedCookie.
85 // Cookies are distinguished by the tuple cookie name (called cookie-name in
86 // RFC 6265), cookie domain (called cookie-domain in RFC 6265), cookie path
87 // (called cookie-path in RFC 6265) and host-only-flag (see RFC 6265 section
88 // 5.3). Cookies with same tuple (cookie-name, cookie-domain, cookie-path,
89 // host-only-flag) as cookie that are already stored, will replace the stored
90 // cookies.
91 class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper {
92 public:
93 typedef std::map<GURL, net::CookieList*> OriginCookieListMap;
95 explicit CannedBrowsingDataCookieHelper(
96 net::URLRequestContextGetter* request_context);
98 // Return a copy of the cookie helper. Only one consumer can use the
99 // StartFetching method at a time, so we need to create a copy of the helper
100 // everytime we instantiate a cookies tree model for it.
101 CannedBrowsingDataCookieHelper* Clone();
103 // Adds the cookies from |cookie_list|. Current cookies that have the same
104 // cookie name, cookie domain, cookie path, host-only-flag tuple as passed
105 // cookies are replaced by the passed cookies.
106 void AddReadCookies(const GURL& frame_url,
107 const GURL& request_url,
108 const net::CookieList& cookie_list);
110 // Adds a CanonicalCookie that is created from the passed |cookie_line|
111 // (called set-cookie-string in RFC 6225). The |cookie_line| is parsed,
112 // normalized and validated. Invalid |cookie_line|s are ignored. The logic
113 // for parsing, normalizing an validating the |cookie_line| mirrors the logic
114 // of CookieMonster's method SetCookieWithOptions. If the |cookie_line| does
115 // not include a cookie domain attribute (called domain-av in RFC 6265) or a
116 // cookie path (called path-av in RFC 6265), then the host and the
117 // default-path of the request-uri are used as domain-value and path-value
118 // for the cookie. CanonicalCookies created from a |cookie_line| with no
119 // cookie domain attribute are host only cookies.
120 // TODO(markusheintz): Remove the dublicated logic.
121 void AddChangedCookie(const GURL& frame_url,
122 const GURL& request_url,
123 const std::string& cookie_line,
124 const net::CookieOptions& options);
126 // Clears the list of canned cookies.
127 void Reset();
129 // True if no cookie are currently stored.
130 bool empty() const;
132 // BrowsingDataCookieHelper methods.
133 virtual void StartFetching(
134 const net::CookieMonster::GetCookieListCallback& callback) OVERRIDE;
135 virtual void DeleteCookie(const net::CanonicalCookie& cookie) OVERRIDE;
137 // Returns the number of stored cookies.
138 size_t GetCookieCount() const;
140 // Returns the map that contains the cookie lists for all frame urls.
141 const OriginCookieListMap& origin_cookie_list_map() {
142 return origin_cookie_list_map_;
145 private:
146 // Check if the cookie list contains a cookie with the same name,
147 // domain, and path as the newly created cookie. Delete the old cookie
148 // if does.
149 bool DeleteMatchingCookie(const net::CanonicalCookie& add_cookie,
150 net::CookieList* cookie_list);
152 virtual ~CannedBrowsingDataCookieHelper();
154 // Returns the |CookieList| for the given |origin|.
155 net::CookieList* GetCookiesFor(const GURL& origin);
157 // Adds the |cookie| to the cookie list for the given |frame_url|.
158 void AddCookie(const GURL& frame_url,
159 const net::CanonicalCookie& cookie);
161 // Map that contains the cookie lists for all frame origins.
162 OriginCookieListMap origin_cookie_list_map_;
164 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper);
167 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_