@@ -53,6 +53,82 @@ void GetCookieListFromStore(
53
53
}
54
54
}
55
55
56
+ bool MatchesDomain (base::DictionaryValue* details, const std::string& domain) {
57
+ std::string val;
58
+ if (!details->GetString (" domain" , &val))
59
+ return true ;
60
+
61
+ // Add a leading '.' character to the filter domain if it doesn't exist.
62
+ if (net::cookie_util::DomainIsHostOnly (val))
63
+ val.insert (0 , " ." );
64
+
65
+ std::string sub_domain (domain);
66
+ // Strip any leading '.' character from the input cookie domain.
67
+ if (!net::cookie_util::DomainIsHostOnly (sub_domain))
68
+ sub_domain = sub_domain.substr (1 );
69
+
70
+ // Now check whether the domain argument is a subdomain of the filter domain.
71
+ for (sub_domain.insert (0 , " ." );
72
+ sub_domain.length () >= val.length ();) {
73
+ if (sub_domain == val)
74
+ return true ;
75
+ const size_t next_dot = sub_domain.find (' .' , 1 ); // Skip over leading dot.
76
+ sub_domain.erase (0 , next_dot);
77
+ }
78
+ return false ;
79
+ }
80
+
81
+ bool MatchesCookie (base::DictionaryValue* details,
82
+ const net::CanonicalCookie& cookie) {
83
+ std::string val;
84
+
85
+ bool flag;
86
+ if (details->GetString (" name" , &val))
87
+ if (val != cookie.Name ())
88
+ return false ;
89
+
90
+ if (!MatchesDomain (details, cookie.Domain ()))
91
+ return false ;
92
+
93
+ if (details->GetString (" path" , &val))
94
+ if (val != cookie.Path ())
95
+ return false ;
96
+
97
+ if (details->GetBoolean (" secure" , &flag))
98
+ if (flag != cookie.IsSecure ())
99
+ return false ;
100
+
101
+ if (details->GetBoolean (" session" , &flag))
102
+ if (flag != cookie.IsPersistent ())
103
+ return false ;
104
+
105
+ return true ;
106
+ }
107
+
108
+ base::DictionaryValue*
109
+ PopulateCookieObject (const net::CanonicalCookie& canonical_cookie) {
110
+
111
+ base::DictionaryValue* result = new base::DictionaryValue ();
112
+ // A cookie is a raw byte sequence. By explicitly parsing it as UTF-8, we
113
+ // apply error correction, so the string can be safely passed to the renderer.
114
+ result->SetString (" name" , UTF16ToUTF8 (UTF8ToUTF16 (canonical_cookie.Name ())));
115
+ result->SetString (" value" , UTF16ToUTF8 (UTF8ToUTF16 (canonical_cookie.Value ())));
116
+ result->SetString (" domain" , canonical_cookie.Domain ());
117
+ result->SetBoolean (" host_only" , net::cookie_util::DomainIsHostOnly (
118
+ canonical_cookie.Domain ()));
119
+ // A non-UTF8 path is invalid, so we just replace it with an empty string.
120
+ result->SetString (" path" , IsStringUTF8 (canonical_cookie.Path ()) ? canonical_cookie.Path ()
121
+ : std::string ());
122
+ result->SetBoolean (" secure" , canonical_cookie.IsSecure ());
123
+ result->SetBoolean (" http_only" , canonical_cookie.IsHttpOnly ());
124
+ result->SetBoolean (" session" , !canonical_cookie.IsPersistent ());
125
+ if (canonical_cookie.IsPersistent ()) {
126
+ result->SetDouble (" expiration_date" ,
127
+ canonical_cookie.ExpiryDate ().ToDoubleT ());
128
+ }
129
+ return result;
130
+ }
131
+
56
132
} // namespace
57
133
58
134
namespace api {
@@ -66,6 +142,8 @@ Window::Window(int id,
66
142
DVLOG (1 ) << " Window::Window(" << id << " )" ;
67
143
// Set ID for Shell
68
144
shell_->set_id (id);
145
+
146
+ result_.reset (new base::ListValue);
69
147
}
70
148
71
149
Window::~Window () {
@@ -161,6 +239,8 @@ void Window::Call(const std::string& method,
161
239
shell_->window ()->CapturePage (image_format_str);
162
240
} else if (method == " CookieGet" ) {
163
241
CookieGet (arguments);
242
+ } else if (method == " CookieGetAll" ) {
243
+ CookieGet (arguments, true );
164
244
} else {
165
245
NOTREACHED () << " Invalid call to Window method:" << method
166
246
<< " arguments:" << arguments;
@@ -191,7 +271,7 @@ void Window::CallSync(const std::string& method,
191
271
}
192
272
193
273
194
- void Window::CookieGet (const base::ListValue& arguments) {
274
+ void Window::CookieGet (const base::ListValue& arguments, bool get_all ) {
195
275
content::RenderProcessHost* render_process_host =
196
276
dispatcher_host ()->render_view_host ()->GetProcess ();
197
277
net::URLRequestContextGetter* context_getter =
@@ -202,16 +282,33 @@ void Window::CookieGet(const base::ListValue& arguments) {
202
282
std::string url;
203
283
204
284
store_context_ = context_getter;
285
+ arguments.GetDictionary (0 , &details);
205
286
if (details) {
206
287
details_.reset (details->DeepCopyWithoutEmptyChildren ());
207
288
details->GetString (" url" , &url);
208
289
}
209
290
url_ = GURL (url);
210
291
211
- bool rv = BrowserThread::PostTask (
212
- BrowserThread::IO, FROM_HERE,
213
- base::Bind (&Window::GetCookieOnIOThread, base::Unretained (this )));
214
- DCHECK (rv);
292
+ if (get_all) {
293
+ bool rv = BrowserThread::PostTask (
294
+ BrowserThread::IO, FROM_HERE,
295
+ base::Bind (&Window::GetAllCookieOnIOThread, base::Unretained (this )));
296
+ DCHECK (rv);
297
+ }else {
298
+ bool rv = BrowserThread::PostTask (
299
+ BrowserThread::IO, FROM_HERE,
300
+ base::Bind (&Window::GetCookieOnIOThread, base::Unretained (this )));
301
+ DCHECK (rv);
302
+ }
303
+ }
304
+
305
+ void Window::GetAllCookieOnIOThread () {
306
+ DCHECK (BrowserThread::CurrentlyOn (BrowserThread::IO));
307
+ net::CookieStore* cookie_store =
308
+ store_context_->GetURLRequestContext ()->cookie_store ();
309
+ GetCookieListFromStore (
310
+ cookie_store, url_,
311
+ base::Bind (&Window::GetAllCookieCallback, base::Unretained (this )));
215
312
}
216
313
217
314
void Window::GetCookieOnIOThread () {
@@ -223,13 +320,26 @@ void Window::GetCookieOnIOThread() {
223
320
base::Bind (&Window::GetCookieCallback, base::Unretained (this )));
224
321
}
225
322
323
+ void Window::GetAllCookieCallback (const net::CookieList& cookie_list) {
324
+ net::CookieList::const_iterator it;
325
+ result_->Clear ();
326
+ for (it = cookie_list.begin (); it != cookie_list.end (); ++it) {
327
+ if (MatchesCookie (details_.get (), *it)) {
328
+ result_->Append (PopulateCookieObject (*it));
329
+ }
330
+ }
331
+
332
+ bool rv = BrowserThread::PostTask (
333
+ BrowserThread::UI, FROM_HERE,
334
+ base::Bind (&Window::RespondOnUIThread, base::Unretained (this )));
335
+ DCHECK (rv);
336
+ }
337
+
226
338
void Window::GetCookieCallback (const net::CookieList& cookie_list) {
227
339
net::CookieList::const_iterator it;
228
340
std::string name;
229
341
details_->GetString (" name" , &name);
230
342
231
- if (!result_)
232
- result_.reset (new base::DictionaryValue);
233
343
result_->Clear ();
234
344
235
345
for (it = cookie_list.begin (); it != cookie_list.end (); ++it) {
@@ -238,24 +348,7 @@ void Window::GetCookieCallback(const net::CookieList& cookie_list) {
238
348
// earliest creation time).
239
349
240
350
if (it->Name () == name) {
241
- const net::CanonicalCookie& canonical_cookie = *it;
242
- // A cookie is a raw byte sequence. By explicitly parsing it as UTF-8, we
243
- // apply error correction, so the string can be safely passed to the renderer.
244
- result_->SetString (" name" , UTF16ToUTF8 (UTF8ToUTF16 (canonical_cookie.Name ())));
245
- result_->SetString (" value" , UTF16ToUTF8 (UTF8ToUTF16 (canonical_cookie.Value ())));
246
- result_->SetString (" domain" , canonical_cookie.Domain ());
247
- result_->SetBoolean (" host_only" , net::cookie_util::DomainIsHostOnly (
248
- canonical_cookie.Domain ()));
249
- // A non-UTF8 path is invalid, so we just replace it with an empty string.
250
- result_->SetString (" path" , IsStringUTF8 (canonical_cookie.Path ()) ? canonical_cookie.Path ()
251
- : std::string ());
252
- result_->SetBoolean (" secure" , canonical_cookie.IsSecure ());
253
- result_->SetBoolean (" http_only" , canonical_cookie.IsHttpOnly ());
254
- result_->SetBoolean (" session" , !canonical_cookie.IsPersistent ());
255
- if (canonical_cookie.IsPersistent ()) {
256
- result_->SetDouble (" expiration_date" ,
257
- canonical_cookie.ExpiryDate ().ToDoubleT ());
258
- }
351
+ result_->Append (PopulateCookieObject (*it));
259
352
break ;
260
353
}
261
354
}
@@ -268,9 +361,9 @@ void Window::GetCookieCallback(const net::CookieList& cookie_list) {
268
361
269
362
void Window::RespondOnUIThread () {
270
363
DCHECK (BrowserThread::CurrentlyOn (BrowserThread::UI));
271
- base::ListValue args ;
272
- args .Append (result_.release ());
273
- dispatcher_host ()->SendEvent (this , " __nw_gotcookie" , args );
364
+ base::ListValue ret ;
365
+ ret .Append (result_.release ());
366
+ dispatcher_host ()->SendEvent (this , " __nw_gotcookie" , ret );
274
367
}
275
368
276
369
} // namespace api
0 commit comments