Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: net/base/cookie_monster_store_test.cc

Issue 7864008: Split initial load of cookies by domains (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/cookie_monster_store_test.h" 5 #include "net/base/cookie_monster_store_test.h"
6 6
7 #include "base/bind.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
9 #include "base/time.h" 10 #include "base/time.h"
10 #include "googleurl/src/gurl.h" 11 #include "googleurl/src/gurl.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 namespace net { 14 namespace net {
15 // Wrapper class for posting a loaded callback. Since the Callback class is not
16 // reference counted, we cannot post a callback to the message loop directly,
17 // instead we post a LoadCallbackTask.
Randy Smith (Not in Mondays) 2011/10/11 15:58:58 nit: LoadCallbackTask -> LoadedCallbackTask?
Randy Smith (Not in Mondays) 2011/10/11 20:03:15 Apologies, but I'm confused by this comment and a
erikwright (departed) 2011/10/11 20:11:12 The instance of LoadedCallback that you would bind
guohui 2011/10/11 20:15:37 Done.
Randy Smith (Not in Mondays) 2011/10/13 21:36:28 Got it. Pity; a make_scoped_ptr() wrapper (doesn'
18 class LoadedCallbackTask
19 : public base::RefCountedThreadSafe<LoadedCallbackTask> {
20 public:
21 typedef CookieMonster::PersistentCookieStore::LoadedCallback LoadedCallback;
22 LoadedCallbackTask(LoadedCallback loaded_callback,
23 std::vector<CookieMonster::CanonicalCookie*> cookies)
24 : loaded_callback_(loaded_callback), cookies_(cookies) {}
25
26 void Run() {
27 loaded_callback_.Run(cookies_);
28 }
29
30 private:
31 LoadedCallback loaded_callback_;
32 std::vector<CookieMonster::CanonicalCookie*> cookies_;
33
34 DISALLOW_COPY_AND_ASSIGN(LoadedCallbackTask);
35 }; // Wrapper class LoadedCallbackTask
14 36
15 MockPersistentCookieStore::MockPersistentCookieStore() 37 MockPersistentCookieStore::MockPersistentCookieStore()
16 : load_return_value_(true) { 38 : load_return_value_(true),
39 loaded_(false) {
17 } 40 }
18 41
19 MockPersistentCookieStore::~MockPersistentCookieStore() {} 42 MockPersistentCookieStore::~MockPersistentCookieStore() {}
20 43
21 void MockPersistentCookieStore::SetLoadExpectation( 44 void MockPersistentCookieStore::SetLoadExpectation(
22 bool return_value, 45 bool return_value,
23 const std::vector<CookieMonster::CanonicalCookie*>& result) { 46 const std::vector<CookieMonster::CanonicalCookie*>& result) {
24 load_return_value_ = return_value; 47 load_return_value_ = return_value;
25 load_result_ = result; 48 load_result_ = result;
26 } 49 }
27 50
28 bool MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) { 51 void MockPersistentCookieStore::Load(const LoadedCallback& loaded_callback) {
29 bool ok = load_return_value_;
30 std::vector<CookieMonster::CanonicalCookie*> out_cookies; 52 std::vector<CookieMonster::CanonicalCookie*> out_cookies;
31 if (ok) { 53 if (load_return_value_) {
32 out_cookies = load_result_; 54 out_cookies = load_result_;
55 loaded_ = true;
33 } 56 }
34 loaded_callback.Run(out_cookies); 57 MessageLoop::current()->PostTask(FROM_HERE,
35 return ok; 58 base::Bind(&LoadedCallbackTask::Run,
59 new LoadedCallbackTask(loaded_callback, out_cookies)));
60 }
61
62 void MockPersistentCookieStore::LoadCookiesForKey(const std::string& key,
63 const LoadedCallback& loaded_callback) {
64 if (!loaded_) {
65 Load(loaded_callback);
66 } else {
67 MessageLoop::current()->PostTask(FROM_HERE,
68 base::Bind(&LoadedCallbackTask::Run,
69 new LoadedCallbackTask(loaded_callback,
70 std::vector<CookieMonster::CanonicalCookie*>())));
71 }
36 } 72 }
37 73
38 void MockPersistentCookieStore::AddCookie( 74 void MockPersistentCookieStore::AddCookie(
39 const CookieMonster::CanonicalCookie& cookie) { 75 const CookieMonster::CanonicalCookie& cookie) {
40 commands_.push_back( 76 commands_.push_back(
41 CookieStoreCommand(CookieStoreCommand::ADD, cookie)); 77 CookieStoreCommand(CookieStoreCommand::ADD, cookie));
42 } 78 }
43 79
44 void MockPersistentCookieStore::UpdateCookieAccessTime( 80 void MockPersistentCookieStore::UpdateCookieAccessTime(
45 const CookieMonster::CanonicalCookie& cookie) { 81 const CookieMonster::CanonicalCookie& cookie) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 std::string cookie_path = pc.Path(); 142 std::string cookie_path = pc.Path();
107 143
108 return CookieMonster::CanonicalCookie( 144 return CookieMonster::CanonicalCookie(
109 GURL(), pc.Name(), pc.Value(), key, cookie_path, 145 GURL(), pc.Name(), pc.Value(), key, cookie_path,
110 pc.MACKey(), pc.MACAlgorithm(), 146 pc.MACKey(), pc.MACAlgorithm(),
111 creation_time, creation_time, cookie_expires, 147 creation_time, creation_time, cookie_expires,
112 pc.IsSecure(), pc.IsHttpOnly(), 148 pc.IsSecure(), pc.IsHttpOnly(),
113 !cookie_expires.is_null()); 149 !cookie_expires.is_null());
114 } 150 }
115 151
116 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore() {} 152 MockSimplePersistentCookieStore::MockSimplePersistentCookieStore()
153 : loaded_(false) {}
117 154
118 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {} 155 MockSimplePersistentCookieStore::~MockSimplePersistentCookieStore() {}
119 156
120 bool MockSimplePersistentCookieStore::Load( 157 void MockSimplePersistentCookieStore::Load(
121 const LoadedCallback& loaded_callback) { 158 const LoadedCallback& loaded_callback) {
122 std::vector<CookieMonster::CanonicalCookie*> out_cookies; 159 std::vector<CookieMonster::CanonicalCookie*> out_cookies;
160
123 for (CanonicalCookieMap::const_iterator it = cookies_.begin(); 161 for (CanonicalCookieMap::const_iterator it = cookies_.begin();
124 it != cookies_.end(); it++) 162 it != cookies_.end(); it++)
125 out_cookies.push_back( 163 out_cookies.push_back(
126 new CookieMonster::CanonicalCookie(it->second)); 164 new CookieMonster::CanonicalCookie(it->second));
127 loaded_callback.Run(out_cookies); 165
128 return true; 166 MessageLoop::current()->PostTask(FROM_HERE,
167 base::Bind(&LoadedCallbackTask::Run,
168 new LoadedCallbackTask(loaded_callback, out_cookies)));
169 loaded_ = true;
170 }
171
172 void MockSimplePersistentCookieStore::LoadCookiesForKey(const std::string& key,
173 const LoadedCallback& loaded_callback) {
174 if (!loaded_) {
175 Load(loaded_callback);
176 } else {
177 MessageLoop::current()->PostTask(FROM_HERE,
178 base::Bind(&LoadedCallbackTask::Run,
179 new LoadedCallbackTask(loaded_callback,
180 std::vector<CookieMonster::CanonicalCookie*>())));
181 }
129 } 182 }
130 183
131 void MockSimplePersistentCookieStore::AddCookie( 184 void MockSimplePersistentCookieStore::AddCookie(
132 const CookieMonster::CanonicalCookie& cookie) { 185 const CookieMonster::CanonicalCookie& cookie) {
133 int64 creation_time = cookie.CreationDate().ToInternalValue(); 186 int64 creation_time = cookie.CreationDate().ToInternalValue();
134 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end()); 187 EXPECT_TRUE(cookies_.find(creation_time) == cookies_.end());
135 cookies_[creation_time] = cookie; 188 cookies_[creation_time] = cookie;
136 } 189 }
137 190
138 void MockSimplePersistentCookieStore::UpdateCookieAccessTime( 191 void MockSimplePersistentCookieStore::UpdateCookieAccessTime(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", 236 GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path",
184 mac_key, mac_algorithm, creation_time, expiration_time, 237 mac_key, mac_algorithm, creation_time, expiration_time,
185 last_access_time, false, false, true); 238 last_access_time, false, false, true);
186 store->AddCookie(cc); 239 store->AddCookie(cc);
187 } 240 }
188 241
189 return new CookieMonster(store, NULL); 242 return new CookieMonster(store, NULL);
190 } 243 }
191 244
192 } // namespace net 245 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698