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