OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/media/android/media_resource_getter_impl.h" | 5 #include "content/browser/media/android/media_resource_getter_impl.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
12 #include "content/browser/child_process_security_policy_impl.h" | 12 #include "content/browser/child_process_security_policy_impl.h" |
13 #include "content/browser/fileapi/browser_file_system_helper.h" | 13 #include "content/browser/fileapi/browser_file_system_helper.h" |
14 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 14 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
18 #include "content/public/common/content_client.h" | 18 #include "content/public/common/content_client.h" |
19 #include "content/public/common/url_constants.h" | 19 #include "content/public/common/url_constants.h" |
20 #include "jni/MediaResourceGetter_jni.h" | 20 #include "jni/MediaResourceGetter_jni.h" |
| 21 #include "media/base/android/media_url_interceptor.h" |
21 #include "net/base/auth.h" | 22 #include "net/base/auth.h" |
22 #include "net/cookies/cookie_monster.h" | 23 #include "net/cookies/cookie_monster.h" |
23 #include "net/cookies/cookie_store.h" | 24 #include "net/cookies/cookie_store.h" |
24 #include "net/http/http_auth.h" | 25 #include "net/http/http_auth.h" |
25 #include "net/http/http_transaction_factory.h" | 26 #include "net/http/http_transaction_factory.h" |
26 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
27 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
29 #include "webkit/browser/blob/blob_data_handle.h" | 30 #include "webkit/browser/blob/blob_data_handle.h" |
30 #include "webkit/browser/blob/blob_storage_context.h" | 31 #include "webkit/browser/blob/blob_storage_context.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 url, | 72 url, |
72 &platform_path); | 73 &platform_path); |
73 base::FilePath data_storage_path; | 74 base::FilePath data_storage_path; |
74 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path); | 75 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path); |
75 if (data_storage_path.IsParent(platform_path)) | 76 if (data_storage_path.IsParent(platform_path)) |
76 ReturnResultOnUIThread(callback, platform_path.value()); | 77 ReturnResultOnUIThread(callback, platform_path.value()); |
77 else | 78 else |
78 ReturnResultOnUIThread(callback, std::string()); | 79 ReturnResultOnUIThread(callback, std::string()); |
79 } | 80 } |
80 | 81 |
81 // Get the metadata from a media URL. When finished, a task is posted to the UI | 82 // Posts a task to the UI thread to run the callback function. |
| 83 static void PostMediaMetadataCallbackTask( |
| 84 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback, |
| 85 JNIEnv* env, ScopedJavaLocalRef<jobject>& j_metadata) { |
| 86 BrowserThread::PostTask( |
| 87 BrowserThread::UI, FROM_HERE, |
| 88 base::Bind(callback, base::TimeDelta::FromMilliseconds( |
| 89 Java_MediaMetadata_getDurationInMilliseconds( |
| 90 env, j_metadata.obj())), |
| 91 Java_MediaMetadata_getWidth(env, j_metadata.obj()), |
| 92 Java_MediaMetadata_getHeight(env, j_metadata.obj()), |
| 93 Java_MediaMetadata_isSuccess(env, j_metadata.obj()))); |
| 94 } |
| 95 |
| 96 // Gets the metadata from a media URL. When finished, a task is posted to the UI |
82 // thread to run the callback function. | 97 // thread to run the callback function. |
83 static void GetMediaMetadata( | 98 static void GetMediaMetadata( |
84 const std::string& url, const std::string& cookies, | 99 const std::string& url, const std::string& cookies, |
85 const std::string& user_agent, | 100 const std::string& user_agent, |
86 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) { | 101 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) { |
87 JNIEnv* env = base::android::AttachCurrentThread(); | 102 JNIEnv* env = base::android::AttachCurrentThread(); |
88 | 103 |
89 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url); | 104 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url); |
90 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies); | 105 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies); |
91 jobject j_context = base::android::GetApplicationContext(); | 106 jobject j_context = base::android::GetApplicationContext(); |
92 ScopedJavaLocalRef<jstring> j_user_agent = ConvertUTF8ToJavaString( | 107 ScopedJavaLocalRef<jstring> j_user_agent = ConvertUTF8ToJavaString( |
93 env, user_agent); | 108 env, user_agent); |
94 ScopedJavaLocalRef<jobject> j_metadata = | 109 ScopedJavaLocalRef<jobject> j_metadata = |
95 Java_MediaResourceGetter_extractMediaMetadata(env, | 110 Java_MediaResourceGetter_extractMediaMetadata(env, |
96 j_context, | 111 j_context, |
97 j_url_string.obj(), | 112 j_url_string.obj(), |
98 j_cookies.obj(), | 113 j_cookies.obj(), |
99 j_user_agent.obj()); | 114 j_user_agent.obj()); |
100 BrowserThread::PostTask( | 115 |
101 BrowserThread::UI, FROM_HERE, | 116 PostMediaMetadataCallbackTask(callback, env, j_metadata); |
102 base::Bind(callback, base::TimeDelta::FromMilliseconds( | 117 } |
103 Java_MediaMetadata_getDurationInMilliseconds( | 118 |
104 env, j_metadata.obj())), | 119 // Gets the metadata from a file descriptor. When finished, a task is posted to |
105 Java_MediaMetadata_getWidth(env, j_metadata.obj()), | 120 // the UI thread to run the callback function. |
106 Java_MediaMetadata_getHeight(env, j_metadata.obj()), | 121 static void GetMediaMetadataFromFd( |
107 Java_MediaMetadata_isSuccess(env, j_metadata.obj()))); | 122 const int fd, const int64 offset, const int64 size, |
| 123 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) { |
| 124 JNIEnv* env = base::android::AttachCurrentThread(); |
| 125 |
| 126 ScopedJavaLocalRef<jobject> j_metadata = |
| 127 Java_MediaResourceGetter_extractMediaMetadataFromFd( |
| 128 env, fd, offset, size); |
| 129 |
| 130 PostMediaMetadataCallbackTask(callback, env, j_metadata); |
108 } | 131 } |
109 | 132 |
110 // The task object that retrieves media resources on the IO thread. | 133 // The task object that retrieves media resources on the IO thread. |
111 // TODO(qinmin): refactor this class to make the code reusable by others as | 134 // TODO(qinmin): refactor this class to make the code reusable by others as |
112 // there are lots of duplicated functionalities elsewhere. | 135 // there are lots of duplicated functionalities elsewhere. |
113 // http://crbug.com/395762. | 136 // http://crbug.com/395762. |
114 class MediaResourceGetterTask | 137 class MediaResourceGetterTask |
115 : public base::RefCountedThreadSafe<MediaResourceGetterTask> { | 138 : public base::RefCountedThreadSafe<MediaResourceGetterTask> { |
116 public: | 139 public: |
117 MediaResourceGetterTask(BrowserContext* browser_context, | 140 MediaResourceGetterTask(BrowserContext* browser_context, |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 void MediaResourceGetterImpl::ExtractMediaMetadata( | 344 void MediaResourceGetterImpl::ExtractMediaMetadata( |
322 const std::string& url, const std::string& cookies, | 345 const std::string& url, const std::string& cookies, |
323 const std::string& user_agent, const ExtractMediaMetadataCB& callback) { | 346 const std::string& user_agent, const ExtractMediaMetadataCB& callback) { |
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
325 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 348 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
326 pool->PostWorkerTask( | 349 pool->PostWorkerTask( |
327 FROM_HERE, | 350 FROM_HERE, |
328 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback)); | 351 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback)); |
329 } | 352 } |
330 | 353 |
| 354 void MediaResourceGetterImpl::ExtractMediaMetadata( |
| 355 const int fd, const int64 offset, const int64 size, |
| 356 const ExtractMediaMetadataCB& callback) { |
| 357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 358 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 359 pool->PostWorkerTask( |
| 360 FROM_HERE, |
| 361 base::Bind(&GetMediaMetadataFromFd, fd, offset, size, callback)); |
| 362 } |
| 363 |
331 // static | 364 // static |
332 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) { | 365 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) { |
333 return RegisterNativesImpl(env); | 366 return RegisterNativesImpl(env); |
334 } | 367 } |
335 | 368 |
336 } // namespace content | 369 } // namespace content |
OLD | NEW |