| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/android/cookie_getter_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/browser/child_process_security_policy_impl.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "content/public/common/content_client.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 #include "net/cookies/cookie_monster.h" |
| 15 #include "net/cookies/cookie_store.h" |
| 16 #include "net/url_request/url_request_context.h" |
| 17 #include "net/url_request/url_request_context_getter.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 static void ReturnCookieOnUIThread( |
| 22 const media::CookieGetter::GetCookieCB& callback, |
| 23 const std::string& cookies) { |
| 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 25 BrowserThread::PostTask( |
| 26 BrowserThread::UI, FROM_HERE, base::Bind(callback, cookies)); |
| 27 } |
| 28 |
| 29 // The task object that retrieves cookie on the IO thread. |
| 30 // TODO(qinmin): refactor this class to make the code reusable by others as |
| 31 // there are lots of duplicated functionalities elsewhere. |
| 32 class CookieGetterTask |
| 33 : public base::RefCountedThreadSafe<CookieGetterTask> { |
| 34 public: |
| 35 CookieGetterTask(BrowserContext* browser_context, |
| 36 int renderer_id, int routing_id); |
| 37 virtual ~CookieGetterTask(); |
| 38 |
| 39 // Called by CookieGetterImpl to start getting cookies for a URL. |
| 40 void RequestCookies( |
| 41 const GURL& url, const GURL& first_party_for_cookies, |
| 42 const media::CookieGetter::GetCookieCB& callback); |
| 43 |
| 44 private: |
| 45 void CheckPolicyForCookies( |
| 46 const GURL& url, const GURL& first_party_for_cookies, |
| 47 const media::CookieGetter::GetCookieCB& callback, |
| 48 const net::CookieList& cookie_list); |
| 49 |
| 50 // Context getter used to get the CookieStore. |
| 51 net::URLRequestContextGetter* context_getter_; |
| 52 |
| 53 // Resource context for checking cookie policies. |
| 54 ResourceContext* resource_context_; |
| 55 |
| 56 // Render process id, used to check whether the process can access cookies. |
| 57 int renderer_id_; |
| 58 |
| 59 // Routing id for the render view, used to check tab specific cookie policy. |
| 60 int routing_id_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask); |
| 63 }; |
| 64 |
| 65 CookieGetterTask::CookieGetterTask( |
| 66 BrowserContext* browser_context, int renderer_id, int routing_id) |
| 67 : context_getter_(browser_context->GetRequestContext()), |
| 68 resource_context_(browser_context->GetResourceContext()), |
| 69 renderer_id_(renderer_id), |
| 70 routing_id_(routing_id) { |
| 71 } |
| 72 |
| 73 CookieGetterTask::~CookieGetterTask() {} |
| 74 |
| 75 void CookieGetterTask::RequestCookies( |
| 76 const GURL& url, const GURL& first_party_for_cookies, |
| 77 const media::CookieGetter::GetCookieCB& callback) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 79 ChildProcessSecurityPolicyImpl* policy = |
| 80 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 81 if (!policy->CanUseCookiesForOrigin(renderer_id_, url)) { |
| 82 callback.Run(std::string()); |
| 83 return; |
| 84 } |
| 85 |
| 86 net::CookieStore* cookie_store = |
| 87 context_getter_->GetURLRequestContext()->cookie_store(); |
| 88 if (!cookie_store) { |
| 89 callback.Run(std::string()); |
| 90 return; |
| 91 } |
| 92 |
| 93 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); |
| 94 if (cookie_monster) { |
| 95 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( |
| 96 &CookieGetterTask::CheckPolicyForCookies, this, |
| 97 url, first_party_for_cookies, callback)); |
| 98 } else { |
| 99 callback.Run(std::string()); |
| 100 } |
| 101 } |
| 102 |
| 103 void CookieGetterTask::CheckPolicyForCookies( |
| 104 const GURL& url, const GURL& first_party_for_cookies, |
| 105 const media::CookieGetter::GetCookieCB& callback, |
| 106 const net::CookieList& cookie_list) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 108 if (GetContentClient()->browser()->AllowGetCookie( |
| 109 url, first_party_for_cookies, cookie_list, |
| 110 resource_context_, renderer_id_, routing_id_)) { |
| 111 net::CookieStore* cookie_store = |
| 112 context_getter_->GetURLRequestContext()->cookie_store(); |
| 113 cookie_store->GetCookiesWithOptionsAsync( |
| 114 url, net::CookieOptions(), callback); |
| 115 } else { |
| 116 callback.Run(std::string()); |
| 117 } |
| 118 } |
| 119 |
| 120 CookieGetterImpl::CookieGetterImpl( |
| 121 BrowserContext* browser_context, int renderer_id, int routing_id) |
| 122 : browser_context_(browser_context), |
| 123 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), |
| 124 renderer_id_(renderer_id), |
| 125 routing_id_(routing_id) { |
| 126 } |
| 127 |
| 128 CookieGetterImpl::~CookieGetterImpl() {} |
| 129 |
| 130 void CookieGetterImpl::GetCookies(const std::string& url, |
| 131 const std::string& first_party_for_cookies, |
| 132 const GetCookieCB& callback) { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 134 scoped_refptr<CookieGetterTask> task = new CookieGetterTask( |
| 135 browser_context_, renderer_id_, routing_id_); |
| 136 |
| 137 GetCookieCB cb = base::Bind( |
| 138 &CookieGetterImpl::GetCookiesCallback, weak_this_.GetWeakPtr(), callback); |
| 139 BrowserThread::PostTask( |
| 140 BrowserThread::IO, |
| 141 FROM_HERE, |
| 142 base::Bind(&CookieGetterTask::RequestCookies, |
| 143 task, GURL(url), GURL(first_party_for_cookies), |
| 144 base::Bind(&ReturnCookieOnUIThread, cb))); |
| 145 } |
| 146 |
| 147 void CookieGetterImpl::GetCookiesCallback( |
| 148 const GetCookieCB& callback, const std::string& cookies) { |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 150 callback.Run(cookies); |
| 151 } |
| 152 |
| 153 } // namespace content |
| OLD | NEW |