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 | |
38 // Called by CookieGetterImpl to start getting cookies for a URL. | |
39 void RequestCookies( | |
40 const GURL& url, const GURL& first_party_for_cookies, | |
41 const media::CookieGetter::GetCookieCB& callback); | |
42 | |
43 private: | |
44 friend class base::RefCountedThreadSafe<CookieGetterTask>; | |
45 virtual ~CookieGetterTask(); | |
46 | |
47 void CheckPolicyForCookies( | |
48 const GURL& url, const GURL& first_party_for_cookies, | |
49 const media::CookieGetter::GetCookieCB& callback, | |
50 const net::CookieList& cookie_list); | |
51 | |
52 // Context getter used to get the CookieStore. | |
53 net::URLRequestContextGetter* context_getter_; | |
54 | |
55 // Resource context for checking cookie policies. | |
56 ResourceContext* resource_context_; | |
57 | |
58 // Render process id, used to check whether the process can access cookies. | |
59 int renderer_id_; | |
60 | |
61 // Routing id for the render view, used to check tab specific cookie policy. | |
62 int routing_id_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask); | |
65 }; | |
66 | |
67 CookieGetterTask::CookieGetterTask( | |
68 BrowserContext* browser_context, int renderer_id, int routing_id) | |
69 : context_getter_(browser_context->GetRequestContext()), | |
70 resource_context_(browser_context->GetResourceContext()), | |
71 renderer_id_(renderer_id), | |
72 routing_id_(routing_id) { | |
73 } | |
74 | |
75 CookieGetterTask::~CookieGetterTask() {} | |
76 | |
77 void CookieGetterTask::RequestCookies( | |
78 const GURL& url, const GURL& first_party_for_cookies, | |
79 const media::CookieGetter::GetCookieCB& callback) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
81 ChildProcessSecurityPolicyImpl* policy = | |
82 ChildProcessSecurityPolicyImpl::GetInstance(); | |
83 if (!policy->CanAccessCookiesForOrigin(renderer_id_, url)) { | |
84 callback.Run(std::string()); | |
85 return; | |
86 } | |
87 | |
88 net::CookieStore* cookie_store = | |
89 context_getter_->GetURLRequestContext()->cookie_store(); | |
90 if (!cookie_store) { | |
91 callback.Run(std::string()); | |
92 return; | |
93 } | |
94 | |
95 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); | |
96 if (cookie_monster) { | |
97 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( | |
98 &CookieGetterTask::CheckPolicyForCookies, this, | |
99 url, first_party_for_cookies, callback)); | |
100 } else { | |
101 callback.Run(std::string()); | |
102 } | |
103 } | |
104 | |
105 void CookieGetterTask::CheckPolicyForCookies( | |
106 const GURL& url, const GURL& first_party_for_cookies, | |
107 const media::CookieGetter::GetCookieCB& callback, | |
108 const net::CookieList& cookie_list) { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
110 if (GetContentClient()->browser()->AllowGetCookie( | |
111 url, first_party_for_cookies, cookie_list, | |
112 resource_context_, renderer_id_, routing_id_)) { | |
113 net::CookieStore* cookie_store = | |
114 context_getter_->GetURLRequestContext()->cookie_store(); | |
115 cookie_store->GetCookiesWithOptionsAsync( | |
116 url, net::CookieOptions(), callback); | |
117 } else { | |
118 callback.Run(std::string()); | |
119 } | |
120 } | |
121 | |
122 CookieGetterImpl::CookieGetterImpl( | |
123 BrowserContext* browser_context, int renderer_id, int routing_id) | |
124 : browser_context_(browser_context), | |
125 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), | |
126 renderer_id_(renderer_id), | |
127 routing_id_(routing_id) { | |
128 } | |
129 | |
130 CookieGetterImpl::~CookieGetterImpl() {} | |
131 | |
132 void CookieGetterImpl::GetCookies(const std::string& url, | |
133 const std::string& first_party_for_cookies, | |
134 const GetCookieCB& callback) { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
136 scoped_refptr<CookieGetterTask> task = new CookieGetterTask( | |
137 browser_context_, renderer_id_, routing_id_); | |
138 | |
139 GetCookieCB cb = base::Bind( | |
140 &CookieGetterImpl::GetCookiesCallback, weak_this_.GetWeakPtr(), callback); | |
141 BrowserThread::PostTask( | |
142 BrowserThread::IO, | |
143 FROM_HERE, | |
144 base::Bind(&CookieGetterTask::RequestCookies, | |
145 task, GURL(url), GURL(first_party_for_cookies), | |
146 base::Bind(&ReturnCookieOnUIThread, cb))); | |
147 } | |
148 | |
149 void CookieGetterImpl::GetCookiesCallback( | |
150 const GetCookieCB& callback, const std::string& cookies) { | |
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
152 callback.Run(cookies); | |
153 } | |
154 | |
155 } // namespace content | |
OLD | NEW |