OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "content/browser/android/cookie_getter_impl.h" | 5 #include "content/browser/android/media_resource_getter_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/child_process_security_policy_impl.h" | 8 #include "content/browser/child_process_security_policy_impl.h" |
9 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
12 #include "content/public/common/content_client.h" | 12 #include "content/public/common/content_client.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "net/cookies/cookie_monster.h" | 14 #include "net/cookies/cookie_monster.h" |
15 #include "net/cookies/cookie_store.h" | 15 #include "net/cookies/cookie_store.h" |
16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 static void ReturnCookieOnUIThread( | 21 static void ReturnResultOnUIThread( |
22 const media::CookieGetter::GetCookieCB& callback, | 22 const base::Callback<void(const std::string&)>& callback, |
23 const std::string& cookies) { | 23 const std::string& result) { |
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
25 BrowserThread::PostTask( | 24 BrowserThread::PostTask( |
26 BrowserThread::UI, FROM_HERE, base::Bind(callback, cookies)); | 25 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); |
27 } | 26 } |
28 | 27 |
29 // The task object that retrieves cookie on the IO thread. | 28 // The task object that retrieves cookie on the IO thread. |
30 // TODO(qinmin): refactor this class to make the code reusable by others as | 29 // TODO(qinmin): refactor this class to make the code reusable by others as |
31 // there are lots of duplicated functionalities elsewhere. | 30 // there are lots of duplicated functionalities elsewhere. |
32 class CookieGetterTask | 31 class CookieGetterTask |
33 : public base::RefCountedThreadSafe<CookieGetterTask> { | 32 : public base::RefCountedThreadSafe<CookieGetterTask> { |
34 public: | 33 public: |
35 CookieGetterTask(BrowserContext* browser_context, | 34 CookieGetterTask(BrowserContext* browser_context, |
36 int renderer_id, int routing_id); | 35 int renderer_id, int routing_id); |
37 | 36 |
38 // Called by CookieGetterImpl to start getting cookies for a URL. | 37 // Called by CookieGetterImpl to start getting cookies for a URL. |
39 void RequestCookies( | 38 void RequestCookies( |
40 const GURL& url, const GURL& first_party_for_cookies, | 39 const GURL& url, const GURL& first_party_for_cookies, |
41 const media::CookieGetter::GetCookieCB& callback); | 40 const media::MediaResourceGetter::GetCookieCB& callback); |
42 | 41 |
43 private: | 42 private: |
44 friend class base::RefCountedThreadSafe<CookieGetterTask>; | 43 friend class base::RefCountedThreadSafe<CookieGetterTask>; |
45 virtual ~CookieGetterTask(); | 44 virtual ~CookieGetterTask(); |
46 | 45 |
47 void CheckPolicyForCookies( | 46 void CheckPolicyForCookies( |
48 const GURL& url, const GURL& first_party_for_cookies, | 47 const GURL& url, const GURL& first_party_for_cookies, |
49 const media::CookieGetter::GetCookieCB& callback, | 48 const media::MediaResourceGetter::GetCookieCB& callback, |
50 const net::CookieList& cookie_list); | 49 const net::CookieList& cookie_list); |
51 | 50 |
52 // Context getter used to get the CookieStore. | 51 // Context getter used to get the CookieStore. |
53 net::URLRequestContextGetter* context_getter_; | 52 net::URLRequestContextGetter* context_getter_; |
54 | 53 |
55 // Resource context for checking cookie policies. | 54 // Resource context for checking cookie policies. |
56 ResourceContext* resource_context_; | 55 ResourceContext* resource_context_; |
57 | 56 |
58 // Render process id, used to check whether the process can access cookies. | 57 // Render process id, used to check whether the process can access cookies. |
59 int renderer_id_; | 58 int renderer_id_; |
60 | 59 |
61 // Routing id for the render view, used to check tab specific cookie policy. | 60 // Routing id for the render view, used to check tab specific cookie policy. |
62 int routing_id_; | 61 int routing_id_; |
63 | 62 |
64 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask); | 63 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask); |
65 }; | 64 }; |
66 | 65 |
67 CookieGetterTask::CookieGetterTask( | 66 CookieGetterTask::CookieGetterTask( |
68 BrowserContext* browser_context, int renderer_id, int routing_id) | 67 BrowserContext* browser_context, int renderer_id, int routing_id) |
69 : context_getter_(browser_context->GetRequestContext()), | 68 : context_getter_(browser_context->GetRequestContext()), |
70 resource_context_(browser_context->GetResourceContext()), | 69 resource_context_(browser_context->GetResourceContext()), |
71 renderer_id_(renderer_id), | 70 renderer_id_(renderer_id), |
72 routing_id_(routing_id) { | 71 routing_id_(routing_id) { |
73 } | 72 } |
74 | 73 |
75 CookieGetterTask::~CookieGetterTask() {} | 74 CookieGetterTask::~CookieGetterTask() {} |
76 | 75 |
77 void CookieGetterTask::RequestCookies( | 76 void CookieGetterTask::RequestCookies( |
78 const GURL& url, const GURL& first_party_for_cookies, | 77 const GURL& url, const GURL& first_party_for_cookies, |
79 const media::CookieGetter::GetCookieCB& callback) { | 78 const media::MediaResourceGetter::GetCookieCB& callback) { |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
81 ChildProcessSecurityPolicyImpl* policy = | 80 ChildProcessSecurityPolicyImpl* policy = |
82 ChildProcessSecurityPolicyImpl::GetInstance(); | 81 ChildProcessSecurityPolicyImpl::GetInstance(); |
83 if (!policy->CanAccessCookiesForOrigin(renderer_id_, url)) { | 82 if (!policy->CanAccessCookiesForOrigin(renderer_id_, url)) { |
84 callback.Run(std::string()); | 83 callback.Run(std::string()); |
85 return; | 84 return; |
86 } | 85 } |
87 | 86 |
88 net::CookieStore* cookie_store = | 87 net::CookieStore* cookie_store = |
89 context_getter_->GetURLRequestContext()->cookie_store(); | 88 context_getter_->GetURLRequestContext()->cookie_store(); |
90 if (!cookie_store) { | 89 if (!cookie_store) { |
91 callback.Run(std::string()); | 90 callback.Run(std::string()); |
92 return; | 91 return; |
93 } | 92 } |
94 | 93 |
95 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); | 94 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); |
96 if (cookie_monster) { | 95 if (cookie_monster) { |
97 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( | 96 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind( |
98 &CookieGetterTask::CheckPolicyForCookies, this, | 97 &CookieGetterTask::CheckPolicyForCookies, this, |
99 url, first_party_for_cookies, callback)); | 98 url, first_party_for_cookies, callback)); |
100 } else { | 99 } else { |
101 callback.Run(std::string()); | 100 callback.Run(std::string()); |
102 } | 101 } |
103 } | 102 } |
104 | 103 |
105 void CookieGetterTask::CheckPolicyForCookies( | 104 void CookieGetterTask::CheckPolicyForCookies( |
106 const GURL& url, const GURL& first_party_for_cookies, | 105 const GURL& url, const GURL& first_party_for_cookies, |
107 const media::CookieGetter::GetCookieCB& callback, | 106 const media::MediaResourceGetter::GetCookieCB& callback, |
108 const net::CookieList& cookie_list) { | 107 const net::CookieList& cookie_list) { |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
110 if (GetContentClient()->browser()->AllowGetCookie( | 109 if (GetContentClient()->browser()->AllowGetCookie( |
111 url, first_party_for_cookies, cookie_list, | 110 url, first_party_for_cookies, cookie_list, |
112 resource_context_, renderer_id_, routing_id_)) { | 111 resource_context_, renderer_id_, routing_id_)) { |
113 net::CookieStore* cookie_store = | 112 net::CookieStore* cookie_store = |
114 context_getter_->GetURLRequestContext()->cookie_store(); | 113 context_getter_->GetURLRequestContext()->cookie_store(); |
115 cookie_store->GetCookiesWithOptionsAsync( | 114 cookie_store->GetCookiesWithOptionsAsync( |
116 url, net::CookieOptions(), callback); | 115 url, net::CookieOptions(), callback); |
117 } else { | 116 } else { |
118 callback.Run(std::string()); | 117 callback.Run(std::string()); |
119 } | 118 } |
120 } | 119 } |
121 | 120 |
122 CookieGetterImpl::CookieGetterImpl( | 121 // The task object that retrieves platform path on the FILE thread. |
123 BrowserContext* browser_context, int renderer_id, int routing_id) | 122 class PlatformPathGetterTask |
123 : public base::RefCountedThreadSafe<PlatformPathGetterTask> { | |
scherkus (not reviewing)
2013/03/08 21:26:27
should be 4 space indent
qinmin
2013/03/08 22:36:12
Done.
| |
124 public: | |
125 PlatformPathGetterTask(fileapi::FileSystemContext* file_system_context, | |
126 int renderer_id); | |
127 | |
128 // Called by MediaResourceGetterImpl to get the platform path from a file | |
129 // system URL. | |
130 void RequestPlaformPath( | |
131 const GURL& url, | |
132 const media::MediaResourceGetter::GetPlatformPathCB& callback); | |
133 | |
134 private: | |
135 friend class base::RefCountedThreadSafe<PlatformPathGetterTask>; | |
136 virtual ~PlatformPathGetterTask(); | |
137 | |
138 // File system context for getting the platform path. | |
139 fileapi::FileSystemContext* file_system_context_; | |
140 | |
141 // Render process id, used to check whether the process can access the URL. | |
142 int renderer_id_; | |
143 | |
144 DISALLOW_COPY_AND_ASSIGN(PlatformPathGetterTask); | |
145 }; | |
146 | |
147 PlatformPathGetterTask::PlatformPathGetterTask( | |
148 fileapi::FileSystemContext* file_system_context, int renderer_id) | |
149 : file_system_context_(file_system_context), | |
150 renderer_id_(renderer_id) { | |
151 } | |
152 | |
153 PlatformPathGetterTask::~PlatformPathGetterTask() {} | |
154 | |
155 void PlatformPathGetterTask::RequestPlaformPath( | |
156 const GURL& url, | |
157 const media::MediaResourceGetter::GetPlatformPathCB& callback) { | |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
159 base::FilePath platform_path; | |
160 FileAPIMessageFilter::SyncGetPlatformPath(file_system_context_, | |
161 renderer_id_, | |
162 url, | |
163 &platform_path); | |
kinuko
2013/03/08 02:43:21
If you can implement this with async API, can you
qinmin
2013/03/08 03:09:28
For GetMetadata(), does it has to be on the IO thr
kinuko
2013/03/08 03:14:32
Ah- I see you're already on File thread, sorry I m
| |
164 callback.Run(platform_path.value()); | |
165 } | |
166 | |
167 MediaResourceGetterImpl::MediaResourceGetterImpl( | |
168 BrowserContext* browser_context, | |
169 fileapi::FileSystemContext* file_system_context, | |
170 int renderer_id, int routing_id) | |
124 : browser_context_(browser_context), | 171 : browser_context_(browser_context), |
172 file_system_context_(file_system_context), | |
125 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), | 173 ALLOW_THIS_IN_INITIALIZER_LIST(weak_this_(this)), |
126 renderer_id_(renderer_id), | 174 renderer_id_(renderer_id), |
127 routing_id_(routing_id) { | 175 routing_id_(routing_id) { |
128 } | 176 } |
129 | 177 |
130 CookieGetterImpl::~CookieGetterImpl() {} | 178 MediaResourceGetterImpl::~MediaResourceGetterImpl() {} |
131 | 179 |
132 void CookieGetterImpl::GetCookies(const std::string& url, | 180 void MediaResourceGetterImpl::GetCookies(const std::string& url, |
133 const std::string& first_party_for_cookies, | 181 const std::string& first_party_for_cookies, |
134 const GetCookieCB& callback) { | 182 const GetCookieCB& callback) { |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
136 scoped_refptr<CookieGetterTask> task = new CookieGetterTask( | 184 scoped_refptr<CookieGetterTask> task = new CookieGetterTask( |
137 browser_context_, renderer_id_, routing_id_); | 185 browser_context_, renderer_id_, routing_id_); |
138 | 186 |
139 GetCookieCB cb = base::Bind( | 187 GetCookieCB cb = base::Bind(&MediaResourceGetterImpl::GetCookiesCallback, |
140 &CookieGetterImpl::GetCookiesCallback, weak_this_.GetWeakPtr(), callback); | 188 weak_this_.GetWeakPtr(), callback); |
141 BrowserThread::PostTask( | 189 BrowserThread::PostTask( |
142 BrowserThread::IO, | 190 BrowserThread::IO, |
143 FROM_HERE, | 191 FROM_HERE, |
144 base::Bind(&CookieGetterTask::RequestCookies, | 192 base::Bind(&CookieGetterTask::RequestCookies, |
145 task, GURL(url), GURL(first_party_for_cookies), | 193 task, GURL(url), GURL(first_party_for_cookies), |
146 base::Bind(&ReturnCookieOnUIThread, cb))); | 194 base::Bind(&ReturnResultOnUIThread, cb))); |
147 } | 195 } |
148 | 196 |
149 void CookieGetterImpl::GetCookiesCallback( | 197 void MediaResourceGetterImpl::GetCookiesCallback( |
150 const GetCookieCB& callback, const std::string& cookies) { | 198 const GetCookieCB& callback, const std::string& cookies) { |
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
152 callback.Run(cookies); | 200 callback.Run(cookies); |
153 } | 201 } |
154 | 202 |
203 void MediaResourceGetterImpl::GetPlatformPathFromFileSystemURL( | |
204 const std::string& url, const GetPlatformPathCB& callback) { | |
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
206 scoped_refptr<PlatformPathGetterTask> task = new PlatformPathGetterTask( | |
207 file_system_context_, renderer_id_); | |
208 | |
209 GetPlatformPathCB cb = base::Bind( | |
210 &MediaResourceGetterImpl::GetPlatformPathCallback, | |
211 weak_this_.GetWeakPtr(), callback); | |
212 BrowserThread::PostTask( | |
213 BrowserThread::FILE, | |
214 FROM_HERE, | |
215 base::Bind(&PlatformPathGetterTask::RequestPlaformPath, | |
216 task, GURL(url), | |
217 base::Bind(&ReturnResultOnUIThread, cb))); | |
218 } | |
219 | |
220 void MediaResourceGetterImpl::GetPlatformPathCallback( | |
221 const GetPlatformPathCB& callback, const std::string& platform_path) { | |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
223 callback.Run(platform_path); | |
224 } | |
225 | |
155 } // namespace content | 226 } // namespace content |
OLD | NEW |