1 // 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.
5 #ifndef CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "net/base/cookie_monster.h"
20 class URLRequestContextGetter
;
23 // This class fetches cookie information on behalf of a caller
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.
30 class BrowsingDataCookieHelper
31 : public base::RefCountedThreadSafe
<BrowsingDataCookieHelper
> {
33 explicit BrowsingDataCookieHelper(Profile
* profile
);
35 // Starts the fetching process, which will notify its completion via
37 // This must be called only in the UI thread.
38 virtual void StartFetching(
39 const base::Callback
<void(const net::CookieList
& cookies
)>& callback
);
41 // Cancels the notification callback (i.e., the window that created it no
43 // This must be called only in the UI thread.
44 virtual void CancelNotification();
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
);
51 friend class base::RefCountedThreadSafe
<BrowsingDataCookieHelper
>;
52 virtual ~BrowsingDataCookieHelper();
55 // Fetch the cookies. This must be called in the IO thread.
56 void FetchCookiesOnIOThread();
58 // Callback function for get cookie. This must be called in the IO thread.
59 void OnFetchComplete(const net::CookieList
& cookies
);
61 // Notifies the completion callback. This must be called in the UI thread.
62 void NotifyInUIThread(const net::CookieList
& cookies
);
64 // Delete a single cookie. This must be called in IO thread.
65 void DeleteCookieOnIOThread(
66 const net::CookieMonster::CanonicalCookie
& cookie
);
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.
76 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
78 // This only mutates on the UI thread.
79 base::Callback
<void(const net::CookieList
& cookies
)> completion_callback_
;
81 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper
);
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.
87 class CannedBrowsingDataCookieHelper
: public BrowsingDataCookieHelper
{
89 explicit CannedBrowsingDataCookieHelper(Profile
* profile
);
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();
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
);
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
);
107 // Clears the list of canned cookies.
110 // True if no cookie are currently stored.
113 // BrowsingDataCookieHelper methods.
114 virtual void StartFetching(
115 const net::CookieMonster::GetCookieListCallback
& callback
) OVERRIDE
;
116 virtual void CancelNotification() OVERRIDE
;
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
122 bool DeleteMetchingCookie(
123 const net::CookieMonster::CanonicalCookie
& add_cookie
);
125 virtual ~CannedBrowsingDataCookieHelper();
127 net::CookieList cookie_list_
;
131 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper
);
134 #endif // CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_