Chromium Code Reviews| 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 // The task object that retrieves cookie on the IO thread. | |
| 22 // TODO(qinmin): refactor this class to make the code reusable by others as | |
| 23 // there are lots of duplicated functionalities elsewhere. | |
| 24 class CookieGetterTask | |
| 25 : public base::RefCountedThreadSafe<CookieGetterTask> { | |
| 26 public: | |
| 27 CookieGetterTask(BrowserContext* browser_context, | |
| 28 int renderer_id, int routing_id, | |
| 29 base::WeakPtr<CookieGetterImpl> cookie_getter); | |
| 30 virtual ~CookieGetterTask(); | |
| 31 | |
| 32 // Called by CookieGetterImpl to start getting cookies for a URL. | |
| 33 void RequestCookies( | |
| 34 const GURL& url, const GURL& first_party_for_cookies, | |
| 35 const media::CookieGetter::GetCookieCB& callback); | |
| 36 | |
| 37 private: | |
| 38 void CheckPolicyForCookies( | |
| 39 const GURL& url, const GURL& first_party_for_cookies, | |
| 40 const media::CookieGetter::GetCookieCB& callback, | |
| 41 const net::CookieList& cookie_list); | |
| 42 | |
| 43 void ReturnCookies(const media::CookieGetter::GetCookieCB& callback, | |
| 44 const std::string& cookies); | |
| 45 | |
| 46 // Context getter used to get the CookieStore. | |
| 47 net::URLRequestContextGetter* context_getter_; | |
| 48 | |
| 49 // Resource context for checking cookie policies. | |
| 50 ResourceContext* resource_context_; | |
| 51 | |
| 52 // Render process id, used to check whether the process can access cookies. | |
| 53 int renderer_id_; | |
| 54 | |
| 55 // Routing id for the render view, used to check tab specific cookie policy. | |
| 56 int routing_id_; | |
| 57 | |
| 58 // The CookieGetterImpl object for callback. | |
| 59 base::WeakPtr<CookieGetterImpl> cookie_getter_; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask); | |
| 62 }; | |
| 63 | |
| 64 CookieGetterTask::CookieGetterTask( | |
| 65 BrowserContext* browser_context, int renderer_id, int routing_id, | |
| 66 base::WeakPtr<CookieGetterImpl> cookie_getter) | |
| 67 : context_getter_(browser_context->GetRequestContext()), | |
| 68 resource_context_(browser_context->GetResourceContext()), | |
| 69 renderer_id_(renderer_id), | |
| 70 routing_id_(routing_id), | |
| 71 cookie_getter_(cookie_getter) { | |
| 72 } | |
| 73 | |
| 74 CookieGetterTask::~CookieGetterTask() {} | |
| 75 | |
| 76 void CookieGetterTask::RequestCookies( | |
| 77 const GURL& url, const GURL& first_party_for_cookies, | |
| 78 const media::CookieGetter::GetCookieCB& callback) { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 80 ChildProcessSecurityPolicyImpl* policy = | |
| 81 ChildProcessSecurityPolicyImpl::GetInstance(); | |
| 82 if (!policy->CanUseCookiesForOrigin(renderer_id_, url)) { | |
| 83 ReturnCookies(callback, std::string()); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 net::CookieStore* cookie_store = | |
| 88 context_getter_->GetURLRequestContext()->cookie_store(); | |
| 89 if (!cookie_store) { | |
| 90 ReturnCookies(callback, std::string()); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); | |
| 95 if (cookie_monster) { | |
| 96 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( | |
| 97 &CookieGetterTask::CheckPolicyForCookies, this, | |
| 98 url, first_party_for_cookies, callback)); | |
| 99 } else { | |
| 100 ReturnCookies(callback, std::string()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void CookieGetterTask::CheckPolicyForCookies( | |
| 105 const GURL& url, const GURL& first_party_for_cookies, | |
| 106 const media::CookieGetter::GetCookieCB& callback, | |
| 107 const net::CookieList& cookie_list) { | |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 109 if (GetContentClient()->browser()->AllowGetCookie( | |
| 110 url, first_party_for_cookies, cookie_list, | |
| 111 resource_context_, renderer_id_, routing_id_)) { | |
| 112 net::CookieStore* cookie_store = | |
| 113 context_getter_->GetURLRequestContext()->cookie_store(); | |
| 114 cookie_store->GetCookiesWithOptionsAsync( | |
| 115 url, net::CookieOptions(), | |
| 116 base::Bind(&CookieGetterTask::ReturnCookies, this, callback)); | |
| 117 } else { | |
| 118 ReturnCookies(callback, std::string()); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void CookieGetterTask::ReturnCookies( | |
| 123 const media::CookieGetter::GetCookieCB& callback, | |
| 124 const std::string& cookies) { | |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 126 BrowserThread::PostTask( | |
| 127 BrowserThread::UI, | |
| 128 FROM_HERE, | |
| 129 base::Bind(&CookieGetterImpl::GetCookiesCallback, | |
| 130 cookie_getter_, cookies, callback)); | |
| 131 } | |
| 132 | |
| 133 CookieGetterImpl::CookieGetterImpl( | |
| 134 BrowserContext* browser_context, int renderer_id, int routing_id) | |
| 135 : browser_context_(browser_context), | |
| 136 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), | |
| 137 renderer_id_(renderer_id), | |
| 138 routing_id_(routing_id) { | |
| 139 } | |
| 140 | |
| 141 CookieGetterImpl::~CookieGetterImpl() {} | |
| 142 | |
| 143 void CookieGetterImpl::GetCookies(const std::string& url, | |
| 144 const std::string& first_party_for_cookies, | |
| 145 const GetCookieCB& callback) { | |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 147 scoped_refptr<CookieGetterTask> task = new CookieGetterTask( | |
| 148 browser_context_, renderer_id_, routing_id_, weak_this_.GetWeakPtr()); | |
| 149 BrowserThread::PostTask( | |
| 150 BrowserThread::IO, | |
| 151 FROM_HERE, | |
| 152 base::Bind(&CookieGetterTask::RequestCookies, | |
| 153 task, GURL(url), GURL(first_party_for_cookies), callback)); | |
|
scherkus (not reviewing)
2012/09/14 08:09:41
I think you can simplify this even more :)
* De
qinmin
2012/09/14 16:50:47
Yes, I was thinking of something similar last nigh
| |
| 154 } | |
| 155 | |
| 156 void CookieGetterImpl::GetCookiesCallback( | |
| 157 const std::string& cookies, const GetCookieCB& callback) { | |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 159 callback.Run(cookies); | |
| 160 } | |
| 161 | |
| 162 } // namespace content | |
| OLD | NEW |