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

Side by Side Diff: chrome/browser/extensions/extension_cookies_api.cc

Issue 6525016: Adding callbacks to `chrome.cookies.{set,remove}`. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixing variable names, clarifying a test, and restoring accidentally deleted comments. Created 9 years, 10 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) 2010 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 // Implements the Chrome Extensions Cookies API. 5 // Implements the Chrome Extensions Cookies API.
6 6
7 #include "chrome/browser/extensions/extension_cookies_api.h" 7 #include "chrome/browser/extensions/extension_cookies_api.h"
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/task.h" 10 #include "base/task.h"
11 #include "base/values.h" 11 #include "base/values.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 bool rv = BrowserThread::PostTask( 170 bool rv = BrowserThread::PostTask(
171 BrowserThread::IO, FROM_HERE, 171 BrowserThread::IO, FROM_HERE,
172 NewRunnableMethod(this, &GetCookieFunction::GetCookieOnIOThread)); 172 NewRunnableMethod(this, &GetCookieFunction::GetCookieOnIOThread));
173 DCHECK(rv); 173 DCHECK(rv);
174 174
175 // Will finish asynchronously. 175 // Will finish asynchronously.
176 return true; 176 return true;
177 } 177 }
178 178
179 void GetCookieFunction::GetCookieOnIOThread() { 179 void GetCookieFunction::GetCookieOnIOThread() {
180 // Start with "null" as return value
181 result_.reset(Value::CreateNullValue());
jochen (gone - plz use gerrit) 2011/02/17 08:18:26 based on your comments, shouldn't get also return
Mike West 2011/02/17 09:11:00 You're right. Though I still don't like it. :)
182
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 184 net::CookieStore* cookie_store = store_context_->GetCookieStore();
182 cookie_list_ = 185
186 net::CookieList cookie_list =
183 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 187 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
184
185 bool rv = BrowserThread::PostTask(
186 BrowserThread::UI, FROM_HERE,
187 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread));
188 DCHECK(rv);
189 }
190
191 void GetCookieFunction::RespondOnUIThread() {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
193
194 net::CookieList::iterator it; 188 net::CookieList::iterator it;
195 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) { 189 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
196 // Return the first matching cookie. Relies on the fact that the 190 // Return the first matching cookie. Relies on the fact that the
197 // CookieMonster returns them in canonical order (longest path, then 191 // CookieMonster returns them in canonical order (longest path, then
198 // earliest creation time). 192 // earliest creation time).
199 if (it->Name() == name_) { 193 if (it->Name() == name_) {
200 result_.reset( 194 result_.reset(
201 extension_cookies_helpers::CreateCookieValue(*it, store_id_)); 195 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
202 break; 196 break;
203 } 197 }
204 } 198 }
205 199
206 // The cookie doesn't exist; return null. 200 bool rv = BrowserThread::PostTask(
207 if (it == cookie_list_.end()) 201 BrowserThread::UI, FROM_HERE,
208 result_.reset(Value::CreateNullValue()); 202 NewRunnableMethod(this, &GetCookieFunction::RespondOnUIThread));
203 DCHECK(rv);
204 }
209 205
206 void GetCookieFunction::RespondOnUIThread() {
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 SendResponse(true); 208 SendResponse(true);
211 } 209 }
212 210
213 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {} 211 GetAllCookiesFunction::GetAllCookiesFunction() : details_(NULL) {}
214 212
215 GetAllCookiesFunction::~GetAllCookiesFunction() {} 213 GetAllCookiesFunction::~GetAllCookiesFunction() {}
216 214
217 bool GetAllCookiesFunction::RunImpl() { 215 bool GetAllCookiesFunction::RunImpl() {
218 // Return false if the arguments are malformed. 216 // Return false if the arguments are malformed.
219 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_)); 217 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details_));
(...skipping 14 matching lines...) Expand all
234 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread)); 232 NewRunnableMethod(this, &GetAllCookiesFunction::GetAllCookiesOnIOThread));
235 DCHECK(rv); 233 DCHECK(rv);
236 234
237 // Will finish asynchronously. 235 // Will finish asynchronously.
238 return true; 236 return true;
239 } 237 }
240 238
241 void GetAllCookiesFunction::GetAllCookiesOnIOThread() { 239 void GetAllCookiesFunction::GetAllCookiesOnIOThread() {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
243 net::CookieStore* cookie_store = store_context_->GetCookieStore(); 241 net::CookieStore* cookie_store = store_context_->GetCookieStore();
244 cookie_list_ = 242 net::CookieList cookie_list =
245 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); 243 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
246 244
245 const Extension* extension = GetExtension();
246 if (extension) {
247 ListValue* matching_list = new ListValue();
248 extension_cookies_helpers::AppendMatchingCookiesToList(
249 cookie_list, store_id_, url_, details_,
250 GetExtension(), matching_list);
251 result_.reset(matching_list);
252 }
247 bool rv = BrowserThread::PostTask( 253 bool rv = BrowserThread::PostTask(
248 BrowserThread::UI, FROM_HERE, 254 BrowserThread::UI, FROM_HERE,
249 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread)); 255 NewRunnableMethod(this, &GetAllCookiesFunction::RespondOnUIThread));
250 DCHECK(rv); 256 DCHECK(rv);
251 } 257 }
252 258
253 void GetAllCookiesFunction::RespondOnUIThread() { 259 void GetAllCookiesFunction::RespondOnUIThread() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255
256 const Extension* extension = GetExtension();
257 if (extension) {
258 ListValue* matching_list = new ListValue();
259 extension_cookies_helpers::AppendMatchingCookiesToList(
260 cookie_list_, store_id_, url_, details_,
261 GetExtension(), matching_list);
262 result_.reset(matching_list);
263 }
264 SendResponse(true); 261 SendResponse(true);
265 } 262 }
266 263
267 SetCookieFunction::SetCookieFunction() 264 SetCookieFunction::SetCookieFunction()
268 : secure_(false), 265 : secure_(false),
269 http_only_(false), 266 http_only_(false),
270 success_(false) { 267 success_(false) {
271 } 268 }
272 269
273 SetCookieFunction::~SetCookieFunction() { 270 SetCookieFunction::~SetCookieFunction() {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 BrowserThread::IO, FROM_HERE, 327 BrowserThread::IO, FROM_HERE,
331 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread)); 328 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread));
332 DCHECK(rv); 329 DCHECK(rv);
333 330
334 // Will finish asynchronously. 331 // Will finish asynchronously.
335 return true; 332 return true;
336 } 333 }
337 334
338 void SetCookieFunction::SetCookieOnIOThread() { 335 void SetCookieFunction::SetCookieOnIOThread() {
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
340 net::CookieMonster* cookie_monster = 337 net::CookieStore* cookie_store = store_context_->GetCookieStore();
341 store_context_->GetCookieStore()->GetCookieMonster(); 338 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
jochen (gone - plz use gerrit) 2011/02/17 08:18:26 cookie monster implements cookie store, so you can
Mike West 2011/02/17 09:11:00 Done.
342 success_ = cookie_monster->SetCookieWithDetails( 339 success_ = cookie_monster->SetCookieWithDetails(
343 url_, name_, value_, domain_, path_, expiration_time_, 340 url_, name_, value_, domain_, path_, expiration_time_,
344 secure_, http_only_); 341 secure_, http_only_);
345 342
343 // Pull the newly set cookie.
344 net::CookieList cookie_list =
345 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
346 net::CookieList::iterator it;
347 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
348 // Return the first matching cookie. Relies on the fact that the
349 // CookieMonster returns them in canonical order (longest path, then
350 // earliest creation time).
351 if (it->Name() == name_) {
352 result_.reset(
353 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
354 break;
355 }
356 }
357
346 bool rv = BrowserThread::PostTask( 358 bool rv = BrowserThread::PostTask(
347 BrowserThread::UI, FROM_HERE, 359 BrowserThread::UI, FROM_HERE,
348 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); 360 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread));
349 DCHECK(rv); 361 DCHECK(rv);
350 } 362 }
351 363
352 void SetCookieFunction::RespondOnUIThread() { 364 void SetCookieFunction::RespondOnUIThread() {
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 if (!success_) { 366 if (!success_) {
355 error_ = ExtensionErrorUtils::FormatErrorMessage( 367 error_ = ExtensionErrorUtils::FormatErrorMessage(
356 keys::kCookieSetFailedError, name_); 368 keys::kCookieSetFailedError, name_);
357 } 369 }
370
jochen (gone - plz use gerrit) 2011/02/17 08:18:26 no empty line
Mike West 2011/02/17 09:11:00 Done.
358 SendResponse(success_); 371 SendResponse(success_);
359 } 372 }
360 373
361 namespace { 374 namespace {
362 375
363 class RemoveCookieTask : public Task { 376 class RemoveCookieTask : public Task {
364 public: 377 public:
365 RemoveCookieTask(const GURL& url, 378 RemoveCookieTask(const GURL& url,
366 const std::string& name, 379 const std::string& name,
367 const scoped_refptr<URLRequestContextGetter>& context_getter) 380 const scoped_refptr<URLRequestContextGetter>& context_getter)
(...skipping 25 matching lines...) Expand all
393 // Read/validate input parameters. 406 // Read/validate input parameters.
394 GURL url; 407 GURL url;
395 if (!ParseUrl(details, &url, true)) 408 if (!ParseUrl(details, &url, true))
396 return false; 409 return false;
397 410
398 std::string name; 411 std::string name;
399 // Get the cookie name string or return false. 412 // Get the cookie name string or return false.
400 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); 413 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name));
401 414
402 URLRequestContextGetter* store_context = NULL; 415 URLRequestContextGetter* store_context = NULL;
403 if (!ParseStoreContext(details, &store_context, NULL)) 416 std::string store_id;
417 if (!ParseStoreContext(details, &store_context, &store_id))
404 return false; 418 return false;
405 DCHECK(store_context); 419 DCHECK(store_context);
406 420
407 // We don't bother to synchronously wait for the result here, because 421 // We don't bother to synchronously wait for the result here, because
408 // CookieMonster is only ever accessed on the IO thread, so any other accesses 422 // CookieMonster is only ever accessed on the IO thread, so any other accesses
409 // should happen after this. 423 // should happen after this.
410 bool rv = BrowserThread::PostTask( 424 bool rv = BrowserThread::PostTask(
411 BrowserThread::IO, FROM_HERE, 425 BrowserThread::IO, FROM_HERE,
412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); 426 new RemoveCookieTask(url, name, make_scoped_refptr(store_context)));
413 DCHECK(rv); 427 DCHECK(rv);
414 428
429 DictionaryValue* resultDictionary = new DictionaryValue();
430 std::string url_string;
jochen (gone - plz use gerrit) 2011/02/17 08:18:26 Then use url.spec() instead
Mike West 2011/02/17 09:11:00 Done.
431 details->GetString(keys::kUrlKey, &url_string);
432 resultDictionary->SetString(keys::kNameKey, name);
433 resultDictionary->SetString(keys::kUrlKey, url_string);
434 resultDictionary->SetString(keys::kStoreIdKey, store_id);
435 result_.reset(resultDictionary);
436
415 return true; 437 return true;
416 } 438 }
417 439
418 void RemoveCookieFunction::Run() { 440 void RemoveCookieFunction::Run() {
419 SendResponse(RunImpl()); 441 SendResponse(RunImpl());
420 } 442 }
421 443
422 bool GetAllCookieStoresFunction::RunImpl() { 444 bool GetAllCookieStoresFunction::RunImpl() {
423 Profile* original_profile = profile(); 445 Profile* original_profile = profile();
424 DCHECK(original_profile); 446 DCHECK(original_profile);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 extension_cookies_helpers::CreateCookieStoreValue( 481 extension_cookies_helpers::CreateCookieStoreValue(
460 incognito_profile, incognito_tab_ids.release())); 482 incognito_profile, incognito_tab_ids.release()));
461 } 483 }
462 result_.reset(cookie_store_list); 484 result_.reset(cookie_store_list);
463 return true; 485 return true;
464 } 486 }
465 487
466 void GetAllCookieStoresFunction::Run() { 488 void GetAllCookieStoresFunction::Run() {
467 SendResponse(RunImpl()); 489 SendResponse(RunImpl());
468 } 490 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698