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

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: Fixes based on Jochen's 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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 SetCookieFunction::SetCookieFunction() 267 SetCookieFunction::SetCookieFunction()
268 : secure_(false), 268 : secure_(false),
269 http_only_(false), 269 http_only_(false),
270 success_(false) { 270 success_(false) {
271 } 271 }
272 272
273 SetCookieFunction::~SetCookieFunction() { 273 SetCookieFunction::~SetCookieFunction() {
274 } 274 }
275 275
276 bool SetCookieFunction::RunImpl() { 276 bool SetCookieFunction::RunImpl() {
277 // Start with "null" being returned to callback, in case things fail in
Aaron Boodman 2011/02/15 22:26:04 There is no need to explicitly set the result to n
278 // this synchronous bit of the workflow.
279 result_.reset(Value::CreateNullValue());
280
277 // Return false if the arguments are malformed. 281 // Return false if the arguments are malformed.
278 DictionaryValue* details; 282 DictionaryValue* details;
279 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 283 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
280 DCHECK(details); 284 DCHECK(details);
281 285
282 // Read/validate input parameters. 286 // Read/validate input parameters.
283 if (!ParseUrl(details, &url_, true)) 287 if (!ParseUrl(details, &url_, true))
284 return false; 288 return false;
285 // The macros below return false if argument types are not as expected. 289 // The macros below return false if argument types are not as expected.
286 if (details->HasKey(keys::kNameKey)) 290 if (details->HasKey(keys::kNameKey))
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 BrowserThread::IO, FROM_HERE, 334 BrowserThread::IO, FROM_HERE,
331 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread)); 335 NewRunnableMethod(this, &SetCookieFunction::SetCookieOnIOThread));
332 DCHECK(rv); 336 DCHECK(rv);
333 337
334 // Will finish asynchronously. 338 // Will finish asynchronously.
335 return true; 339 return true;
336 } 340 }
337 341
338 void SetCookieFunction::SetCookieOnIOThread() { 342 void SetCookieFunction::SetCookieOnIOThread() {
339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
340 net::CookieMonster* cookie_monster = 344 net::CookieStore* cookie_store = store_context_->GetCookieStore();
341 store_context_->GetCookieStore()->GetCookieMonster(); 345 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
342 success_ = cookie_monster->SetCookieWithDetails( 346 success_ = cookie_monster->SetCookieWithDetails(
343 url_, name_, value_, domain_, path_, expiration_time_, 347 url_, name_, value_, domain_, path_, expiration_time_,
344 secure_, http_only_); 348 secure_, http_only_);
345 349
350 // Pull the newly set cookie.
351 cookie_list_ =
352 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_);
353
346 bool rv = BrowserThread::PostTask( 354 bool rv = BrowserThread::PostTask(
347 BrowserThread::UI, FROM_HERE, 355 BrowserThread::UI, FROM_HERE,
348 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread)); 356 NewRunnableMethod(this, &SetCookieFunction::RespondOnUIThread));
349 DCHECK(rv); 357 DCHECK(rv);
350 } 358 }
351 359
352 void SetCookieFunction::RespondOnUIThread() { 360 void SetCookieFunction::RespondOnUIThread() {
353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 if (!success_) { 362 if (!success_) {
355 error_ = ExtensionErrorUtils::FormatErrorMessage( 363 error_ = ExtensionErrorUtils::FormatErrorMessage(
356 keys::kCookieSetFailedError, name_); 364 keys::kCookieSetFailedError, name_);
365 } else {
366 net::CookieList::iterator it;
367 for (it = cookie_list_.begin(); it != cookie_list_.end(); ++it) {
368 // Return the first matching cookie. Relies on the fact that the
369 // CookieMonster returns them in canonical order (longest path, then
370 // earliest creation time).
371 if (it->Name() == name_) {
372 result_.reset(
373 extension_cookies_helpers::CreateCookieValue(*it, store_id_));
374 break;
375 }
376 }
357 } 377 }
378
358 SendResponse(success_); 379 SendResponse(success_);
359 } 380 }
360 381
361 namespace { 382 namespace {
362 383
363 class RemoveCookieTask : public Task { 384 class RemoveCookieTask : public Task {
364 public: 385 public:
365 RemoveCookieTask(const GURL& url, 386 RemoveCookieTask(const GURL& url,
366 const std::string& name, 387 const std::string& name,
367 const scoped_refptr<URLRequestContextGetter>& context_getter) 388 const scoped_refptr<URLRequestContextGetter>& context_getter)
(...skipping 10 matching lines...) Expand all
378 const GURL url_; 399 const GURL url_;
379 const std::string name_; 400 const std::string name_;
380 const scoped_refptr<URLRequestContextGetter> context_getter_; 401 const scoped_refptr<URLRequestContextGetter> context_getter_;
381 402
382 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTask); 403 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTask);
383 }; 404 };
384 405
385 } // namespace 406 } // namespace
386 407
387 bool RemoveCookieFunction::RunImpl() { 408 bool RemoveCookieFunction::RunImpl() {
409 // Start with "null" being returned to callback, in case things fail in
410 // this syncronous bit of the workflow.
411 result_.reset(Value::CreateNullValue());
412
388 // Return false if the arguments are malformed. 413 // Return false if the arguments are malformed.
389 DictionaryValue* details; 414 DictionaryValue* details;
390 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); 415 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
391 DCHECK(details); 416 DCHECK(details);
392 417
393 // Read/validate input parameters. 418 // Read/validate input parameters.
394 GURL url; 419 GURL url;
395 if (!ParseUrl(details, &url, true)) 420 if (!ParseUrl(details, &url, true))
396 return false; 421 return false;
397 422
398 std::string name; 423 std::string name;
399 // Get the cookie name string or return false. 424 // Get the cookie name string or return false.
400 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name)); 425 EXTENSION_FUNCTION_VALIDATE(details->GetString(keys::kNameKey, &name));
401 426
402 URLRequestContextGetter* store_context = NULL; 427 URLRequestContextGetter* store_context = NULL;
403 if (!ParseStoreContext(details, &store_context, NULL)) 428 std::string store_id_string;
429 if (!ParseStoreContext(details, &store_context, &store_id_string))
404 return false; 430 return false;
405 DCHECK(store_context); 431 DCHECK(store_context);
406 432
407 // We don't bother to synchronously wait for the result here, because 433 // 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 434 // CookieMonster is only ever accessed on the IO thread, so any other accesses
409 // should happen after this. 435 // should happen after this.
410 bool rv = BrowserThread::PostTask( 436 bool rv = BrowserThread::PostTask(
411 BrowserThread::IO, FROM_HERE, 437 BrowserThread::IO, FROM_HERE,
412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); 438 new RemoveCookieTask(url, name, make_scoped_refptr(store_context)));
413 DCHECK(rv); 439 DCHECK(rv);
414 440
441 DictionaryValue* resultDictionary = new DictionaryValue();
442 std::string url_string;
443 details->GetString(keys::kUrlKey, &url_string);
444 resultDictionary->SetString(keys::kNameKey, name);
445 resultDictionary->SetString(keys::kUrlKey, url_string);
446 resultDictionary->SetString(keys::kStoreIdKey, store_id_string);
447 result_.reset(resultDictionary);
448
415 return true; 449 return true;
416 } 450 }
417 451
418 void RemoveCookieFunction::Run() { 452 void RemoveCookieFunction::Run() {
419 SendResponse(RunImpl()); 453 SendResponse(RunImpl());
420 } 454 }
421 455
422 bool GetAllCookieStoresFunction::RunImpl() { 456 bool GetAllCookieStoresFunction::RunImpl() {
423 Profile* original_profile = profile(); 457 Profile* original_profile = profile();
424 DCHECK(original_profile); 458 DCHECK(original_profile);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 extension_cookies_helpers::CreateCookieStoreValue( 493 extension_cookies_helpers::CreateCookieStoreValue(
460 incognito_profile, incognito_tab_ids.release())); 494 incognito_profile, incognito_tab_ids.release()));
461 } 495 }
462 result_.reset(cookie_store_list); 496 result_.reset(cookie_store_list);
463 return true; 497 return true;
464 } 498 }
465 499
466 void GetAllCookieStoresFunction::Run() { 500 void GetAllCookieStoresFunction::Run() {
467 SendResponse(RunImpl()); 501 SendResponse(RunImpl());
468 } 502 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_cookies_api.h ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698