Chromium Code Reviews| Index: content/browser/android/cookie_getter_impl.cc |
| diff --git a/content/browser/android/cookie_getter_impl.cc b/content/browser/android/cookie_getter_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0984c34c2d5101d377b34579549de501a55b2e9e |
| --- /dev/null |
| +++ b/content/browser/android/cookie_getter_impl.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/android/cookie_getter_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "content/browser/child_process_security_policy_impl.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/content_browser_client.h" |
| +#include "content/public/common/content_client.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/cookies/cookie_monster.h" |
| +#include "net/cookies/cookie_store.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +namespace content { |
| + |
| +CookieGetterTask::CookieGetterTask( |
| + BrowserContext* browser_context, int renderer_id, int routing_id) |
| + : context_getter_(browser_context->GetRequestContext()), |
| + resource_context_(browser_context->GetResourceContext()), |
| + renderer_id_(renderer_id), |
| + routing_id_(routing_id), |
| + finish_event_(true, false) { |
| +} |
| + |
| +CookieGetterTask::~CookieGetterTask() {} |
| + |
| +void CookieGetterTask::RequestCookies( |
| + const GURL& url, const GURL& first_party_for_cookies) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + ChildProcessSecurityPolicyImpl* policy = |
| + ChildProcessSecurityPolicyImpl::GetInstance(); |
| + if (!policy->CanUseCookiesForOrigin(renderer_id_, url)) |
| + return; |
| + |
| + net::CookieStore* cookie_store = |
| + context_getter_->GetURLRequestContext()->cookie_store(); |
| + if (!cookie_store) |
| + return; |
| + |
| + net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); |
| + if (cookie_monster) { |
| + cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( |
| + &CookieGetterTask::CheckPolicyForCookies, this, |
| + url, first_party_for_cookies)); |
| + finish_event_.Wait(); |
|
scherkus (not reviewing)
2012/09/13 10:40:14
why the blocking wait on the IO thread?
is there
qinmin
2012/09/13 18:51:16
If this function finishes here, CookieGetterImpl::
scherkus (not reviewing)
2012/09/14 08:09:40
PostTaskAndReply() is handy but it only works for
|
| + } |
| +} |
| + |
| +void CookieGetterTask::CheckPolicyForCookies( |
| + const GURL& url, const GURL& first_party_for_cookies, |
| + const net::CookieList& cookie_list) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + if (GetContentClient()->browser()->AllowGetCookie( |
| + url, first_party_for_cookies, cookie_list, |
| + resource_context_, renderer_id_, routing_id_)) { |
| + net::CookieStore* cookie_store = |
| + context_getter_->GetURLRequestContext()->cookie_store(); |
| + cookie_store->GetCookiesWithOptionsAsync( |
| + url, net::CookieOptions(), |
| + base::Bind(&CookieGetterTask::ReturnCookies, this)); |
| + } else { |
| + finish_event_.Signal(); |
| + } |
| +} |
| + |
| +void CookieGetterTask::ReturnCookies(const std::string& cookies) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + cookies_ = cookies; |
| + finish_event_.Signal(); |
| +} |
| + |
| +CookieGetterImpl::CookieGetterImpl( |
| + BrowserContext* browser_context, int renderer_id, int routing_id) |
| + : browser_context_(browser_context), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), |
| + renderer_id_(renderer_id), |
| + routing_id_(routing_id) { |
| +} |
| + |
| +CookieGetterImpl::~CookieGetterImpl() {} |
| + |
| +void CookieGetterImpl::GetCookies(const std::string& url, |
| + const std::string& first_party_for_cookies, |
| + const GetCookieCB& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + scoped_refptr<CookieGetterTask> task = new CookieGetterTask( |
| + browser_context_, renderer_id_, routing_id_); |
| + BrowserThread::PostTaskAndReply( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&CookieGetterTask::RequestCookies, |
| + task, GURL(url), GURL(first_party_for_cookies)), |
| + base::Bind(&CookieGetterImpl::GetCookiesCallback, |
| + weak_this_.GetWeakPtr(), task, callback)); |
| +} |
| + |
| +void CookieGetterImpl::GetCookiesCallback( |
| + scoped_refptr<CookieGetterTask> task, const GetCookieCB& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + callback.Run(task->cookies()); |
| +} |
| + |
| +} // namespace content |