OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/cookies/cookies_api.h" | 7 #include "chrome/browser/extensions/api/cookies/cookies_api.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 196 } |
197 | 197 |
198 bool CookiesGetFunction::RunImpl() { | 198 bool CookiesGetFunction::RunImpl() { |
199 parsed_args_ = Get::Params::Create(*args_); | 199 parsed_args_ = Get::Params::Create(*args_); |
200 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 200 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
201 | 201 |
202 // Read/validate input parameters. | 202 // Read/validate input parameters. |
203 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 203 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
204 return false; | 204 return false; |
205 | 205 |
206 std::string store_id = parsed_args_->details.store_id.get() ? | 206 std::string store_id = |
207 *parsed_args_->details.store_id : ""; | 207 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 208 : std::string(); |
208 net::URLRequestContextGetter* store_context = NULL; | 209 net::URLRequestContextGetter* store_context = NULL; |
209 if (!ParseStoreContext(&store_id, &store_context)) | 210 if (!ParseStoreContext(&store_id, &store_context)) |
210 return false; | 211 return false; |
211 store_context_ = store_context; | 212 store_context_ = store_context; |
212 if (!parsed_args_->details.store_id.get()) | 213 if (!parsed_args_->details.store_id.get()) |
213 parsed_args_->details.store_id.reset(new std::string(store_id)); | 214 parsed_args_->details.store_id.reset(new std::string(store_id)); |
214 | 215 |
215 store_context_ = store_context; | 216 store_context_ = store_context; |
216 | 217 |
217 bool rv = BrowserThread::PostTask( | 218 bool rv = BrowserThread::PostTask( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 270 |
270 bool CookiesGetAllFunction::RunImpl() { | 271 bool CookiesGetAllFunction::RunImpl() { |
271 parsed_args_ = GetAll::Params::Create(*args_); | 272 parsed_args_ = GetAll::Params::Create(*args_); |
272 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 273 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
273 | 274 |
274 if (parsed_args_->details.url.get() && | 275 if (parsed_args_->details.url.get() && |
275 !ParseUrl(*parsed_args_->details.url, &url_, false)) { | 276 !ParseUrl(*parsed_args_->details.url, &url_, false)) { |
276 return false; | 277 return false; |
277 } | 278 } |
278 | 279 |
279 std::string store_id = parsed_args_->details.store_id.get() ? | 280 std::string store_id = |
280 *parsed_args_->details.store_id : ""; | 281 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 282 : std::string(); |
281 net::URLRequestContextGetter* store_context = NULL; | 283 net::URLRequestContextGetter* store_context = NULL; |
282 if (!ParseStoreContext(&store_id, &store_context)) | 284 if (!ParseStoreContext(&store_id, &store_context)) |
283 return false; | 285 return false; |
284 store_context_ = store_context; | 286 store_context_ = store_context; |
285 if (!parsed_args_->details.store_id.get()) | 287 if (!parsed_args_->details.store_id.get()) |
286 parsed_args_->details.store_id.reset(new std::string(store_id)); | 288 parsed_args_->details.store_id.reset(new std::string(store_id)); |
287 | 289 |
288 bool rv = BrowserThread::PostTask( | 290 bool rv = BrowserThread::PostTask( |
289 BrowserThread::IO, FROM_HERE, | 291 BrowserThread::IO, FROM_HERE, |
290 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); | 292 base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 334 } |
333 | 335 |
334 bool CookiesSetFunction::RunImpl() { | 336 bool CookiesSetFunction::RunImpl() { |
335 parsed_args_ = Set::Params::Create(*args_); | 337 parsed_args_ = Set::Params::Create(*args_); |
336 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 338 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
337 | 339 |
338 // Read/validate input parameters. | 340 // Read/validate input parameters. |
339 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 341 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
340 return false; | 342 return false; |
341 | 343 |
342 std::string store_id = parsed_args_->details.store_id.get() ? | 344 std::string store_id = |
343 *parsed_args_->details.store_id : ""; | 345 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 346 : std::string(); |
344 net::URLRequestContextGetter* store_context = NULL; | 347 net::URLRequestContextGetter* store_context = NULL; |
345 if (!ParseStoreContext(&store_id, &store_context)) | 348 if (!ParseStoreContext(&store_id, &store_context)) |
346 return false; | 349 return false; |
347 store_context_ = store_context; | 350 store_context_ = store_context; |
348 if (!parsed_args_->details.store_id.get()) | 351 if (!parsed_args_->details.store_id.get()) |
349 parsed_args_->details.store_id.reset(new std::string(store_id)); | 352 parsed_args_->details.store_id.reset(new std::string(store_id)); |
350 | 353 |
351 bool rv = BrowserThread::PostTask( | 354 bool rv = BrowserThread::PostTask( |
352 BrowserThread::IO, FROM_HERE, | 355 BrowserThread::IO, FROM_HERE, |
353 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); | 356 base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this)); |
(...skipping 13 matching lines...) Expand all Loading... |
367 if (parsed_args_->details.expiration_date.get()) { | 370 if (parsed_args_->details.expiration_date.get()) { |
368 // Time::FromDoubleT converts double time 0 to empty Time object. So we need | 371 // Time::FromDoubleT converts double time 0 to empty Time object. So we need |
369 // to do special handling here. | 372 // to do special handling here. |
370 expiration_time = (*parsed_args_->details.expiration_date == 0) ? | 373 expiration_time = (*parsed_args_->details.expiration_date == 0) ? |
371 base::Time::UnixEpoch() : | 374 base::Time::UnixEpoch() : |
372 base::Time::FromDoubleT(*parsed_args_->details.expiration_date); | 375 base::Time::FromDoubleT(*parsed_args_->details.expiration_date); |
373 } | 376 } |
374 | 377 |
375 cookie_monster->SetCookieWithDetailsAsync( | 378 cookie_monster->SetCookieWithDetailsAsync( |
376 url_, | 379 url_, |
377 parsed_args_->details.name.get() ? *parsed_args_->details.name : "", | 380 parsed_args_->details.name.get() ? *parsed_args_->details.name |
378 parsed_args_->details.value.get() ? *parsed_args_->details.value : "", | 381 : std::string(), |
379 parsed_args_->details.domain.get() ? *parsed_args_->details.domain : "", | 382 parsed_args_->details.value.get() ? *parsed_args_->details.value |
380 parsed_args_->details.path.get() ? *parsed_args_->details.path : "", | 383 : std::string(), |
| 384 parsed_args_->details.domain.get() ? *parsed_args_->details.domain |
| 385 : std::string(), |
| 386 parsed_args_->details.path.get() ? *parsed_args_->details.path |
| 387 : std::string(), |
381 expiration_time, | 388 expiration_time, |
382 parsed_args_->details.secure.get() ? | 389 parsed_args_->details.secure.get() ? *parsed_args_->details.secure.get() |
383 *parsed_args_->details.secure.get() : | 390 : false, |
384 false, | 391 parsed_args_->details.http_only.get() ? *parsed_args_->details.http_only |
385 parsed_args_->details.http_only.get() ? | 392 : false, |
386 *parsed_args_->details.http_only : | |
387 false, | |
388 base::Bind(&CookiesSetFunction::PullCookie, this)); | 393 base::Bind(&CookiesSetFunction::PullCookie, this)); |
389 } | 394 } |
390 | 395 |
391 void CookiesSetFunction::PullCookie(bool set_cookie_result) { | 396 void CookiesSetFunction::PullCookie(bool set_cookie_result) { |
392 // Pull the newly set cookie. | 397 // Pull the newly set cookie. |
393 net::CookieMonster* cookie_monster = | 398 net::CookieMonster* cookie_monster = |
394 store_context_->GetURLRequestContext()->cookie_store()-> | 399 store_context_->GetURLRequestContext()->cookie_store()-> |
395 GetCookieMonster(); | 400 GetCookieMonster(); |
396 success_ = set_cookie_result; | 401 success_ = set_cookie_result; |
397 cookies_helpers::GetCookieListFromStore( | 402 cookies_helpers::GetCookieListFromStore( |
398 cookie_monster, url_, | 403 cookie_monster, url_, |
399 base::Bind(&CookiesSetFunction::PullCookieCallback, this)); | 404 base::Bind(&CookiesSetFunction::PullCookieCallback, this)); |
400 } | 405 } |
401 | 406 |
402 void CookiesSetFunction::PullCookieCallback( | 407 void CookiesSetFunction::PullCookieCallback( |
403 const net::CookieList& cookie_list) { | 408 const net::CookieList& cookie_list) { |
404 net::CookieList::const_iterator it; | 409 net::CookieList::const_iterator it; |
405 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { | 410 for (it = cookie_list.begin(); it != cookie_list.end(); ++it) { |
406 // Return the first matching cookie. Relies on the fact that the | 411 // Return the first matching cookie. Relies on the fact that the |
407 // CookieMonster returns them in canonical order (longest path, then | 412 // CookieMonster returns them in canonical order (longest path, then |
408 // earliest creation time). | 413 // earliest creation time). |
409 std::string name = parsed_args_->details.name.get() ? | 414 std::string name = |
410 *parsed_args_->details.name : ""; | 415 parsed_args_->details.name.get() ? *parsed_args_->details.name |
| 416 : std::string(); |
411 if (it->Name() == name) { | 417 if (it->Name() == name) { |
412 scoped_ptr<Cookie> cookie( | 418 scoped_ptr<Cookie> cookie( |
413 cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); | 419 cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id)); |
414 results_ = Set::Results::Create(*cookie); | 420 results_ = Set::Results::Create(*cookie); |
415 break; | 421 break; |
416 } | 422 } |
417 } | 423 } |
418 | 424 |
419 bool rv = BrowserThread::PostTask( | 425 bool rv = BrowserThread::PostTask( |
420 BrowserThread::UI, FROM_HERE, | 426 BrowserThread::UI, FROM_HERE, |
421 base::Bind(&CookiesSetFunction::RespondOnUIThread, this)); | 427 base::Bind(&CookiesSetFunction::RespondOnUIThread, this)); |
422 DCHECK(rv); | 428 DCHECK(rv); |
423 } | 429 } |
424 | 430 |
425 void CookiesSetFunction::RespondOnUIThread() { | 431 void CookiesSetFunction::RespondOnUIThread() { |
426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
427 if (!success_) { | 433 if (!success_) { |
428 std::string name = parsed_args_->details.name.get() ? | 434 std::string name = |
429 *parsed_args_->details.name : ""; | 435 parsed_args_->details.name.get() ? *parsed_args_->details.name |
430 error_ = ErrorUtils::FormatErrorMessage( | 436 : std::string(); |
431 keys::kCookieSetFailedError, name); | 437 error_ = ErrorUtils::FormatErrorMessage(keys::kCookieSetFailedError, name); |
432 } | 438 } |
433 SendResponse(success_); | 439 SendResponse(success_); |
434 } | 440 } |
435 | 441 |
436 CookiesRemoveFunction::CookiesRemoveFunction() { | 442 CookiesRemoveFunction::CookiesRemoveFunction() { |
437 } | 443 } |
438 | 444 |
439 CookiesRemoveFunction::~CookiesRemoveFunction() { | 445 CookiesRemoveFunction::~CookiesRemoveFunction() { |
440 } | 446 } |
441 | 447 |
442 bool CookiesRemoveFunction::RunImpl() { | 448 bool CookiesRemoveFunction::RunImpl() { |
443 parsed_args_ = Remove::Params::Create(*args_); | 449 parsed_args_ = Remove::Params::Create(*args_); |
444 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); | 450 EXTENSION_FUNCTION_VALIDATE(parsed_args_.get()); |
445 | 451 |
446 // Read/validate input parameters. | 452 // Read/validate input parameters. |
447 if (!ParseUrl(parsed_args_->details.url, &url_, true)) | 453 if (!ParseUrl(parsed_args_->details.url, &url_, true)) |
448 return false; | 454 return false; |
449 | 455 |
450 std::string store_id = parsed_args_->details.store_id.get() ? | 456 std::string store_id = |
451 *parsed_args_->details.store_id : ""; | 457 parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id |
| 458 : std::string(); |
452 net::URLRequestContextGetter* store_context = NULL; | 459 net::URLRequestContextGetter* store_context = NULL; |
453 if (!ParseStoreContext(&store_id, &store_context)) | 460 if (!ParseStoreContext(&store_id, &store_context)) |
454 return false; | 461 return false; |
455 store_context_ = store_context; | 462 store_context_ = store_context; |
456 if (!parsed_args_->details.store_id.get()) | 463 if (!parsed_args_->details.store_id.get()) |
457 parsed_args_->details.store_id.reset(new std::string(store_id)); | 464 parsed_args_->details.store_id.reset(new std::string(store_id)); |
458 | 465 |
459 // Pass the work off to the IO thread. | 466 // Pass the work off to the IO thread. |
460 bool rv = BrowserThread::PostTask( | 467 bool rv = BrowserThread::PostTask( |
461 BrowserThread::IO, FROM_HERE, | 468 BrowserThread::IO, FROM_HERE, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 return &g_factory.Get(); | 571 return &g_factory.Get(); |
565 } | 572 } |
566 | 573 |
567 void CookiesAPI::OnListenerAdded( | 574 void CookiesAPI::OnListenerAdded( |
568 const extensions::EventListenerInfo& details) { | 575 const extensions::EventListenerInfo& details) { |
569 cookies_event_router_.reset(new CookiesEventRouter(profile_)); | 576 cookies_event_router_.reset(new CookiesEventRouter(profile_)); |
570 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 577 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
571 } | 578 } |
572 | 579 |
573 } // namespace extensions | 580 } // namespace extensions |
OLD | NEW |