Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
please no backticks. I think double quotes are the
Mike West
2011/02/15 14:26:58
Hasn't everyone internalized Markdown yet? :)
Ch
| |
| 278 // this syncronous bit of workflow. | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
of the workflow?
Mike West
2011/02/15 14:26:58
Done.
| |
| 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 Loading... | |
| 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 | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
nit. dot at end of comments
Mike West
2011/02/15 14:26:58
Done.
| |
| 351 cookie_list_ = | |
| 352 extension_cookies_helpers::GetCookieListFromStore(cookie_store, url_); | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
why don't you set result_ already here? you wouldn
Mike West
2011/02/15 14:26:58
This is the workflow used in `GetCookieFunction::G
Aaron Boodman
2011/02/15 22:26:04
It seems OK to set the result on the IO thread. Do
Mike West
2011/02/16 09:52:40
Alright. I've made changes to `GetCookieFunction`
| |
| 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 Loading... | |
| 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 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 if (!ParseStoreContext(details, &store_context, NULL)) |
| 404 return false; | 429 return false; |
| 405 DCHECK(store_context); | 430 DCHECK(store_context); |
| 406 | 431 |
| 407 // We don't bother to synchronously wait for the result here, because | 432 // 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 | 433 // CookieMonster is only ever accessed on the IO thread, so any other accesses |
| 409 // should happen after this. | 434 // should happen after this. |
| 410 bool rv = BrowserThread::PostTask( | 435 bool rv = BrowserThread::PostTask( |
| 411 BrowserThread::IO, FROM_HERE, | 436 BrowserThread::IO, FROM_HERE, |
| 412 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); | 437 new RemoveCookieTask(url, name, make_scoped_refptr(store_context))); |
| 413 DCHECK(rv); | 438 DCHECK(rv); |
| 414 | 439 |
| 440 DictionaryValue* test = new DictionaryValue(); | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
better variable name please
Mike West
2011/02/15 14:26:58
*ahem* Yeah. That was not smart.
| |
| 441 std::string url_string; | |
| 442 details->GetString(keys::kUrlKey, &url_string); | |
| 443 test->SetString(keys::kNameKey, name); | |
| 444 test->SetString(keys::kUrlKey, url_string); | |
| 445 result_.reset(test); | |
|
jochen (gone - plz use gerrit)
2011/02/15 13:44:10
hum, so you return name/url even if no cookie was
Mike West
2011/02/15 14:26:58
I apparently do. I'd somehow convinced myself tha
Aaron Boodman
2011/02/15 22:26:04
My vague preference is for the way you have it. Si
Mike West
2011/02/16 09:52:40
I think that makes sense. I'll leave it this way.
| |
| 446 | |
| 415 return true; | 447 return true; |
| 416 } | 448 } |
| 417 | 449 |
| 418 void RemoveCookieFunction::Run() { | 450 void RemoveCookieFunction::Run() { |
| 419 SendResponse(RunImpl()); | 451 SendResponse(RunImpl()); |
| 420 } | 452 } |
| 421 | 453 |
| 422 bool GetAllCookieStoresFunction::RunImpl() { | 454 bool GetAllCookieStoresFunction::RunImpl() { |
| 423 Profile* original_profile = profile(); | 455 Profile* original_profile = profile(); |
| 424 DCHECK(original_profile); | 456 DCHECK(original_profile); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 extension_cookies_helpers::CreateCookieStoreValue( | 491 extension_cookies_helpers::CreateCookieStoreValue( |
| 460 incognito_profile, incognito_tab_ids.release())); | 492 incognito_profile, incognito_tab_ids.release())); |
| 461 } | 493 } |
| 462 result_.reset(cookie_store_list); | 494 result_.reset(cookie_store_list); |
| 463 return true; | 495 return true; |
| 464 } | 496 } |
| 465 | 497 |
| 466 void GetAllCookieStoresFunction::Run() { | 498 void GetAllCookieStoresFunction::Run() { |
| 467 SendResponse(RunImpl()); | 499 SendResponse(RunImpl()); |
| 468 } | 500 } |
| OLD | NEW |