[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / content / browser / media / android / media_resource_getter_impl.cc
blob99142e11398befa8f6a61586a50911e53c88ded0
1 // Copyright 2013 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.
5 #include "content/browser/media/android/media_resource_getter_impl.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/bind.h"
10 #include "base/path_service.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/fileapi/browser_file_system_helper.h"
14 #include "content/browser/fileapi/chrome_blob_storage_context.h"
15 #include "content/browser/resource_context_impl.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/content_browser_client.h"
19 #include "content/public/common/content_client.h"
20 #include "content/public/common/url_constants.h"
21 #include "jni/MediaResourceGetter_jni.h"
22 #include "media/base/android/media_url_interceptor.h"
23 #include "net/base/auth.h"
24 #include "net/cookies/cookie_monster.h"
25 #include "net/cookies/cookie_store.h"
26 #include "net/http/http_auth.h"
27 #include "net/http/http_transaction_factory.h"
28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_context_getter.h"
30 #include "storage/browser/blob/blob_data_handle.h"
31 #include "storage/browser/blob/blob_data_item.h"
32 #include "storage/browser/blob/blob_data_snapshot.h"
33 #include "storage/browser/blob/blob_storage_context.h"
34 #include "url/gurl.h"
36 using base::android::ConvertUTF8ToJavaString;
37 using base::android::ScopedJavaLocalRef;
39 namespace content {
41 static void ReturnResultOnUIThread(
42 const base::Callback<void(const std::string&)>& callback,
43 const std::string& result) {
44 BrowserThread::PostTask(
45 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
48 static void RequestPlatformPathFromBlobURL(
49 const GURL& url,
50 ResourceContext* resource_context,
51 const base::Callback<void(const std::string&)>& callback) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
53 ChromeBlobStorageContext* blob_storage_context =
54 GetChromeBlobStorageContextForResourceContext(resource_context);
56 scoped_ptr<storage::BlobDataHandle> handle =
57 blob_storage_context->context()->GetBlobDataFromPublicURL(url);
58 if (!handle) {
59 // There are plenty of cases where handle can be empty. The most trivial is
60 // when JS has aready revoked the given blob URL via URL.revokeObjectURL
61 ReturnResultOnUIThread(callback, std::string());
62 return;
64 scoped_ptr<storage::BlobDataSnapshot> data = handle->CreateSnapshot();
65 if (!data) {
66 ReturnResultOnUIThread(callback, std::string());
67 NOTREACHED();
68 return;
70 const std::vector<scoped_refptr<storage::BlobDataItem>>& items =
71 data->items();
73 // TODO(qinmin): handle the case when the blob data is not a single file.
74 DLOG_IF(WARNING, items.size() != 1u)
75 << "More than one blob item is present: " << items.size();
76 ReturnResultOnUIThread(callback, items[0]->path().value());
79 static void RequestPlaformPathFromFileSystemURL(
80 const GURL& url,
81 int render_process_id,
82 scoped_refptr<storage::FileSystemContext> file_system_context,
83 const base::Callback<void(const std::string&)>& callback) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
85 base::FilePath platform_path;
86 SyncGetPlatformPath(file_system_context.get(),
87 render_process_id,
88 url,
89 &platform_path);
90 base::FilePath data_storage_path;
91 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
92 if (data_storage_path.IsParent(platform_path))
93 ReturnResultOnUIThread(callback, platform_path.value());
94 else
95 ReturnResultOnUIThread(callback, std::string());
98 // Posts a task to the UI thread to run the callback function.
99 static void PostMediaMetadataCallbackTask(
100 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback,
101 JNIEnv* env, ScopedJavaLocalRef<jobject>& j_metadata) {
102 BrowserThread::PostTask(
103 BrowserThread::UI, FROM_HERE,
104 base::Bind(callback, base::TimeDelta::FromMilliseconds(
105 Java_MediaMetadata_getDurationInMilliseconds(
106 env, j_metadata.obj())),
107 Java_MediaMetadata_getWidth(env, j_metadata.obj()),
108 Java_MediaMetadata_getHeight(env, j_metadata.obj()),
109 Java_MediaMetadata_isSuccess(env, j_metadata.obj())));
112 // Gets the metadata from a media URL. When finished, a task is posted to the UI
113 // thread to run the callback function.
114 static void GetMediaMetadata(
115 const std::string& url, const std::string& cookies,
116 const std::string& user_agent,
117 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
118 JNIEnv* env = base::android::AttachCurrentThread();
120 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url);
121 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies);
122 jobject j_context = base::android::GetApplicationContext();
123 ScopedJavaLocalRef<jstring> j_user_agent = ConvertUTF8ToJavaString(
124 env, user_agent);
125 ScopedJavaLocalRef<jobject> j_metadata =
126 Java_MediaResourceGetter_extractMediaMetadata(env,
127 j_context,
128 j_url_string.obj(),
129 j_cookies.obj(),
130 j_user_agent.obj());
132 PostMediaMetadataCallbackTask(callback, env, j_metadata);
135 // Gets the metadata from a file descriptor. When finished, a task is posted to
136 // the UI thread to run the callback function.
137 static void GetMediaMetadataFromFd(
138 const int fd, const int64 offset, const int64 size,
139 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
140 JNIEnv* env = base::android::AttachCurrentThread();
142 ScopedJavaLocalRef<jobject> j_metadata =
143 Java_MediaResourceGetter_extractMediaMetadataFromFd(
144 env, fd, offset, size);
146 PostMediaMetadataCallbackTask(callback, env, j_metadata);
149 // The task object that retrieves media resources on the IO thread.
150 // TODO(qinmin): refactor this class to make the code reusable by others as
151 // there are lots of duplicated functionalities elsewhere.
152 // http://crbug.com/395762.
153 class MediaResourceGetterTask
154 : public base::RefCountedThreadSafe<MediaResourceGetterTask> {
155 public:
156 MediaResourceGetterTask(BrowserContext* browser_context,
157 int render_process_id, int render_frame_id);
159 // Called by MediaResourceGetterImpl to start getting auth credentials.
160 net::AuthCredentials RequestAuthCredentials(const GURL& url) const;
162 // Called by MediaResourceGetterImpl to start getting cookies for a URL.
163 void RequestCookies(
164 const GURL& url, const GURL& first_party_for_cookies,
165 const media::MediaResourceGetter::GetCookieCB& callback);
167 private:
168 friend class base::RefCountedThreadSafe<MediaResourceGetterTask>;
169 virtual ~MediaResourceGetterTask();
171 void CheckPolicyForCookies(
172 const GURL& url, const GURL& first_party_for_cookies,
173 const media::MediaResourceGetter::GetCookieCB& callback,
174 const net::CookieList& cookie_list);
176 // Context getter used to get the CookieStore and auth cache.
177 net::URLRequestContextGetter* context_getter_;
179 // Resource context for checking cookie policies.
180 ResourceContext* resource_context_;
182 // Render process id, used to check whether the process can access cookies.
183 int render_process_id_;
185 // Render frame id, used to check tab specific cookie policy.
186 int render_frame_id_;
188 DISALLOW_COPY_AND_ASSIGN(MediaResourceGetterTask);
191 MediaResourceGetterTask::MediaResourceGetterTask(
192 BrowserContext* browser_context, int render_process_id, int render_frame_id)
193 : context_getter_(browser_context->GetRequestContext()),
194 resource_context_(browser_context->GetResourceContext()),
195 render_process_id_(render_process_id),
196 render_frame_id_(render_frame_id) {
199 MediaResourceGetterTask::~MediaResourceGetterTask() {}
201 net::AuthCredentials MediaResourceGetterTask::RequestAuthCredentials(
202 const GURL& url) const {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
204 net::HttpTransactionFactory* factory =
205 context_getter_->GetURLRequestContext()->http_transaction_factory();
206 if (!factory)
207 return net::AuthCredentials();
209 net::HttpAuthCache* auth_cache =
210 factory->GetSession()->http_auth_cache();
211 if (!auth_cache)
212 return net::AuthCredentials();
214 net::HttpAuthCache::Entry* entry =
215 auth_cache->LookupByPath(url.GetOrigin(), url.path());
217 // TODO(qinmin): handle other auth schemes. See http://crbug.com/395219.
218 if (entry && entry->scheme() == net::HttpAuth::AUTH_SCHEME_BASIC)
219 return entry->credentials();
220 else
221 return net::AuthCredentials();
224 void MediaResourceGetterTask::RequestCookies(
225 const GURL& url, const GURL& first_party_for_cookies,
226 const media::MediaResourceGetter::GetCookieCB& callback) {
227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
228 ChildProcessSecurityPolicyImpl* policy =
229 ChildProcessSecurityPolicyImpl::GetInstance();
230 if (!policy->CanAccessCookiesForOrigin(render_process_id_, url)) {
231 callback.Run(std::string());
232 return;
235 net::CookieStore* cookie_store =
236 context_getter_->GetURLRequestContext()->cookie_store();
237 if (!cookie_store) {
238 callback.Run(std::string());
239 return;
242 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
243 if (cookie_monster) {
244 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind(
245 &MediaResourceGetterTask::CheckPolicyForCookies, this,
246 url, first_party_for_cookies, callback));
247 } else {
248 callback.Run(std::string());
252 void MediaResourceGetterTask::CheckPolicyForCookies(
253 const GURL& url, const GURL& first_party_for_cookies,
254 const media::MediaResourceGetter::GetCookieCB& callback,
255 const net::CookieList& cookie_list) {
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
257 if (GetContentClient()->browser()->AllowGetCookie(
258 url, first_party_for_cookies, cookie_list,
259 resource_context_, render_process_id_, render_frame_id_)) {
260 net::CookieStore* cookie_store =
261 context_getter_->GetURLRequestContext()->cookie_store();
262 net::CookieOptions options;
263 options.set_include_httponly();
264 cookie_store->GetCookiesWithOptionsAsync(url, options, callback);
265 } else {
266 callback.Run(std::string());
270 MediaResourceGetterImpl::MediaResourceGetterImpl(
271 BrowserContext* browser_context,
272 storage::FileSystemContext* file_system_context,
273 int render_process_id,
274 int render_frame_id)
275 : browser_context_(browser_context),
276 file_system_context_(file_system_context),
277 render_process_id_(render_process_id),
278 render_frame_id_(render_frame_id),
279 weak_factory_(this) {
282 MediaResourceGetterImpl::~MediaResourceGetterImpl() {}
284 void MediaResourceGetterImpl::GetAuthCredentials(
285 const GURL& url, const GetAuthCredentialsCB& callback) {
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
287 scoped_refptr<MediaResourceGetterTask> task = new MediaResourceGetterTask(
288 browser_context_, 0, 0);
290 BrowserThread::PostTaskAndReplyWithResult(
291 BrowserThread::IO,
292 FROM_HERE,
293 base::Bind(&MediaResourceGetterTask::RequestAuthCredentials, task, url),
294 base::Bind(&MediaResourceGetterImpl::GetAuthCredentialsCallback,
295 weak_factory_.GetWeakPtr(), callback));
298 void MediaResourceGetterImpl::GetCookies(
299 const GURL& url, const GURL& first_party_for_cookies,
300 const GetCookieCB& callback) {
301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
302 scoped_refptr<MediaResourceGetterTask> task = new MediaResourceGetterTask(
303 browser_context_, render_process_id_, render_frame_id_);
305 GetCookieCB cb = base::Bind(&MediaResourceGetterImpl::GetCookiesCallback,
306 weak_factory_.GetWeakPtr(),
307 callback);
308 BrowserThread::PostTask(
309 BrowserThread::IO,
310 FROM_HERE,
311 base::Bind(&MediaResourceGetterTask::RequestCookies,
312 task, url, first_party_for_cookies,
313 base::Bind(&ReturnResultOnUIThread, cb)));
316 void MediaResourceGetterImpl::GetAuthCredentialsCallback(
317 const GetAuthCredentialsCB& callback,
318 const net::AuthCredentials& credentials) {
319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
320 callback.Run(credentials.username(), credentials.password());
323 void MediaResourceGetterImpl::GetCookiesCallback(
324 const GetCookieCB& callback, const std::string& cookies) {
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
326 callback.Run(cookies);
329 void MediaResourceGetterImpl::GetPlatformPathFromURL(
330 const GURL& url, const GetPlatformPathCB& callback) {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
332 DCHECK(url.SchemeIsFileSystem() || url.SchemeIs(url::kBlobScheme));
334 GetPlatformPathCB cb =
335 base::Bind(&MediaResourceGetterImpl::GetPlatformPathCallback,
336 weak_factory_.GetWeakPtr(),
337 callback);
339 if (url.SchemeIs(url::kBlobScheme)) {
340 BrowserThread::PostTask(
341 BrowserThread::IO,
342 FROM_HERE,
343 base::Bind(&RequestPlatformPathFromBlobURL, url,
344 browser_context_->GetResourceContext(), cb));
345 return;
348 scoped_refptr<storage::FileSystemContext> context(file_system_context_);
349 BrowserThread::PostTask(
350 BrowserThread::FILE,
351 FROM_HERE,
352 base::Bind(&RequestPlaformPathFromFileSystemURL, url, render_process_id_,
353 context, cb));
356 void MediaResourceGetterImpl::GetPlatformPathCallback(
357 const GetPlatformPathCB& callback, const std::string& platform_path) {
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
359 callback.Run(platform_path);
362 void MediaResourceGetterImpl::ExtractMediaMetadata(
363 const std::string& url, const std::string& cookies,
364 const std::string& user_agent, const ExtractMediaMetadataCB& callback) {
365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
366 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
367 pool->PostWorkerTask(
368 FROM_HERE,
369 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback));
372 void MediaResourceGetterImpl::ExtractMediaMetadata(
373 const int fd, const int64 offset, const int64 size,
374 const ExtractMediaMetadataCB& callback) {
375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
376 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
377 pool->PostWorkerTask(
378 FROM_HERE,
379 base::Bind(&GetMediaMetadataFromFd, fd, offset, size, callback));
382 // static
383 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) {
384 return RegisterNativesImpl(env);
387 } // namespace content