blob: 6f9305427ea7d8325275fbbbd6752c1a02585502 [file] [log] [blame]
[email protected]7dd2b872011-08-12 17:09:171// Copyright (c) 2011 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_BROWSING_DATA_COOKIE_HELPER_H_
6#define CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/ref_counted.h"
14#include "net/base/cookie_monster.h"
15
16class GURL;
17class Profile;
18
19namespace net {
20class URLRequestContextGetter;
21}
22
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// The client must call CancelNotification() if it's destroyed before the
29// callback is notified.
30class BrowsingDataCookieHelper
31 : public base::RefCountedThreadSafe<BrowsingDataCookieHelper> {
32 public:
33 explicit BrowsingDataCookieHelper(Profile* profile);
34
35 // Starts the fetching process, which will notify its completion via
36 // callback.
37 // This must be called only in the UI thread.
38 virtual void StartFetching(
39 const base::Callback<void(const net::CookieList& cookies)>& callback);
40
41 // Cancels the notification callback (i.e., the window that created it no
42 // longer exists).
43 // This must be called only in the UI thread.
44 virtual void CancelNotification();
45
46 // Requests a single cookie to be deleted in the IO thread. This must be
47 // called in the UI thread.
48 virtual void DeleteCookie(const net::CookieMonster::CanonicalCookie& cookie);
49
50 protected:
51 friend class base::RefCountedThreadSafe<BrowsingDataCookieHelper>;
52 virtual ~BrowsingDataCookieHelper();
53
54 private:
55 // Fetch the cookies. This must be called in the IO thread.
56 void FetchCookiesOnIOThread();
57
58 // Callback function for get cookie. This must be called in the IO thread.
59 void OnFetchComplete(const net::CookieList& cookies);
60
61 // Notifies the completion callback. This must be called in the UI thread.
62 void NotifyInUIThread(const net::CookieList& cookies);
63
64 // Delete a single cookie. This must be called in IO thread.
65 void DeleteCookieOnIOThread(
66 const net::CookieMonster::CanonicalCookie& cookie);
67
68 // Indicates whether or not we're currently fetching information:
69 // it's true when StartFetching() is called in the UI thread, and it's reset
70 // after we notify the callback in the UI thread.
71 // This only mutates on the UI thread.
72 bool is_fetching_;
73
74 Profile* profile_;
75
76 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
77
78 // This only mutates on the UI thread.
79 base::Callback<void(const net::CookieList& cookies)> completion_callback_;
80
81 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper);
82};
83
84// This class is a thin wrapper around BrowsingDataCookieHelper that does not
85// fetch its information from the persistent cookie store, but gets them passed
86// as a parameter during construction.
87class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper {
88 public:
89 explicit CannedBrowsingDataCookieHelper(Profile* profile);
90
91 // Return a copy of the cookie helper. Only one consumer can use the
92 // StartFetching method at a time, so we need to create a copy of the helper
93 // everytime we instantiate a cookies tree model for it.
94 CannedBrowsingDataCookieHelper* Clone();
95
96 // Adds cookies and delete the current cookies with the same Name, Domain,
97 // and Path as the newly created ones.
98 void AddReadCookies(const GURL& url,
99 const net::CookieList& cookie_list);
100
101 // Adds cookies that will be stored by the CookieMonster. Designed to mirror
102 // the logic of SetCookieWithOptions.
103 void AddChangedCookie(const GURL& url,
104 const std::string& cookie_line,
105 const net::CookieOptions& options);
106
107 // Clears the list of canned cookies.
108 void Reset();
109
110 // True if no cookie are currently stored.
111 bool empty() const;
112
113 // BrowsingDataCookieHelper methods.
114 virtual void StartFetching(
115 const net::CookieMonster::GetCookieListCallback& callback);
116 virtual void CancelNotification();
117
118 private:
119 // Check if the cookie list contains a cookie with the same name,
120 // domain, and path as the newly created cookie. Delete the old cookie
121 // if does.
122 bool DeleteMetchingCookie(
123 const net::CookieMonster::CanonicalCookie& add_cookie);
124
125 virtual ~CannedBrowsingDataCookieHelper();
126
127 net::CookieList cookie_list_;
128
129 Profile* profile_;
130
131 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper);
132};
133
134#endif // CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_