[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef NET_COOKIES_COOKIE_STORE_TEST_HELPERS_H_ |
| 6 | #define NET_COOKIES_COOKIE_STORE_TEST_HELPERS_H_ |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 7 | |
| 8 | #include "net/cookies/cookie_monster.h" |
| 9 | |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/callback_forward.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | class DelayedCookieMonster : public CookieStore { |
| 19 | public: |
| 20 | DelayedCookieMonster(); |
| 21 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 22 | ~DelayedCookieMonster() override; |
| 23 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 24 | // Call the asynchronous CookieMonster function, expect it to immediately |
| 25 | // invoke the internal callback. |
| 26 | // Post a delayed task to invoke the original callback with the results. |
| 27 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 28 | void SetCookieWithOptionsAsync( |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 29 | const GURL& url, |
| 30 | const std::string& cookie_line, |
| 31 | const CookieOptions& options, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 32 | const CookieMonster::SetCookiesCallback& callback) override; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 33 | |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 34 | void SetCookieWithDetailsAsync(const GURL& url, |
| 35 | const std::string& name, |
| 36 | const std::string& value, |
| 37 | const std::string& domain, |
| 38 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 39 | base::Time creation_time, |
| 40 | base::Time expiration_time, |
| 41 | base::Time last_access_time, |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 42 | bool secure, |
| 43 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 44 | CookieSameSite same_site, |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 45 | CookiePriority priority, |
| 46 | const SetCookiesCallback& callback) override; |
| 47 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 48 | void GetCookiesWithOptionsAsync( |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 49 | const GURL& url, |
| 50 | const CookieOptions& options, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 51 | const CookieMonster::GetCookiesCallback& callback) override; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 52 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 53 | void GetCookieListWithOptionsAsync( |
| 54 | const GURL& url, |
| 55 | const CookieOptions& options, |
| 56 | const GetCookieListCallback& callback) override; |
[email protected] | dc8313a | 2014-03-24 21:38:14 | [diff] [blame] | 57 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 58 | void GetAllCookiesAsync(const GetCookieListCallback& callback) override; |
| 59 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 60 | virtual bool SetCookieWithOptions(const GURL& url, |
| 61 | const std::string& cookie_line, |
| 62 | const CookieOptions& options); |
| 63 | |
| 64 | virtual std::string GetCookiesWithOptions(const GURL& url, |
| 65 | const CookieOptions& options); |
| 66 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 67 | virtual void DeleteCookie(const GURL& url, |
| 68 | const std::string& cookie_name); |
| 69 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 70 | void DeleteCookieAsync(const GURL& url, |
| 71 | const std::string& cookie_name, |
| 72 | const base::Closure& callback) override; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 73 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 74 | void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
| 75 | const DeleteCallback& callback) override; |
| 76 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 77 | void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin, |
| 78 | const base::Time& delete_end, |
| 79 | const DeleteCallback& callback) override; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 80 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 81 | void DeleteAllCreatedBetweenWithPredicateAsync( |
| 82 | const base::Time& delete_begin, |
| 83 | const base::Time& delete_end, |
| 84 | const base::Callback<bool(const CanonicalCookie&)>& predicate, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 85 | const DeleteCallback& callback) override; |
[email protected] | a67a111 | 2013-12-19 19:04:02 | [diff] [blame] | 86 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 87 | void DeleteSessionCookiesAsync(const DeleteCallback&) override; |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 88 | |
mmenke | 96f3bab | 2016-01-22 17:34:02 | [diff] [blame] | 89 | void FlushStore(const base::Closure& callback) override; |
| 90 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 91 | std::unique_ptr<CookieStore::CookieChangedSubscription> AddCallbackForCookie( |
| 92 | const GURL& url, |
| 93 | const std::string& name, |
| 94 | const CookieChangedCallback& callback) override; |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 95 | |
nharper | 5babb5e6 | 2016-03-09 18:58:07 | [diff] [blame] | 96 | bool IsEphemeral() override; |
| 97 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 98 | private: |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 99 | // Be called immediately from CookieMonster. |
| 100 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 101 | void SetCookiesInternalCallback(bool result); |
| 102 | |
| 103 | void GetCookiesWithOptionsInternalCallback(const std::string& cookie); |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 104 | void GetCookieListWithOptionsInternalCallback(const CookieList& cookie); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 105 | |
| 106 | // Invoke the original callbacks. |
| 107 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 108 | void InvokeSetCookiesCallback( |
| 109 | const CookieMonster::SetCookiesCallback& callback); |
| 110 | |
| 111 | void InvokeGetCookieStringCallback( |
| 112 | const CookieMonster::GetCookiesCallback& callback); |
| 113 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 114 | void InvokeGetCookieListCallback( |
| 115 | const CookieMonster::GetCookieListCallback& callback); |
| 116 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 117 | friend class base::RefCountedThreadSafe<DelayedCookieMonster>; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 118 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 119 | std::unique_ptr<CookieMonster> cookie_monster_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 120 | |
| 121 | bool did_run_; |
| 122 | bool result_; |
| 123 | std::string cookie_; |
| 124 | std::string cookie_line_; |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 125 | CookieList cookie_list_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 126 | }; |
| 127 | |
cmumford | 8688a8f | 2015-10-15 18:08:06 | [diff] [blame] | 128 | class CookieURLHelper { |
| 129 | public: |
| 130 | explicit CookieURLHelper(const std::string& url_string); |
| 131 | |
| 132 | const std::string& domain() const { return domain_and_registry_; } |
| 133 | std::string host() const { return url_.host(); } |
| 134 | const GURL& url() const { return url_; } |
| 135 | const GURL AppendPath(const std::string& path) const; |
| 136 | |
| 137 | // Return a new string with the following substitutions: |
| 138 | // 1. "%R" -> Domain registry (i.e. "com") |
| 139 | // 2. "%D" -> Domain + registry (i.e. "google.com") |
| 140 | std::string Format(const std::string& format_string) const; |
| 141 | |
| 142 | private: |
| 143 | const GURL url_; |
| 144 | const std::string registry_; |
| 145 | const std::string domain_and_registry_; |
| 146 | }; |
| 147 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 148 | } // namespace net |
| 149 | |
| 150 | #endif // NET_COOKIES_COOKIE_STORE_TEST_HELPERS_H_ |