Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / media / android / media_resource_getter_impl.cc
blob4de64a68d1c185ae5db6ce95bd71a98131ec1081
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/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/common/content_client.h"
19 #include "content/public/common/url_constants.h"
20 #include "jni/MediaResourceGetter_jni.h"
21 #include "media/base/android/media_url_interceptor.h"
22 #include "net/base/auth.h"
23 #include "net/cookies/cookie_monster.h"
24 #include "net/cookies/cookie_store.h"
25 #include "net/http/http_auth.h"
26 #include "net/http/http_transaction_factory.h"
27 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_context_getter.h"
29 #include "url/gurl.h"
30 #include "webkit/browser/blob/blob_data_handle.h"
31 #include "webkit/browser/blob/blob_storage_context.h"
33 using base::android::ConvertUTF8ToJavaString;
34 using base::android::ScopedJavaLocalRef;
36 namespace content {
38 static void ReturnResultOnUIThread(
39 const base::Callback<void(const std::string&)>& callback,
40 const std::string& result) {
41 BrowserThread::PostTask(
42 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
45 static void RequestPlatformPathFromBlobURL(
46 const GURL& url,
47 BrowserContext* browser_context,
48 const base::Callback<void(const std::string&)>& callback) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
50 ChromeBlobStorageContext* context =
51 ChromeBlobStorageContext::GetFor(browser_context);
52 scoped_ptr<storage::BlobDataHandle> handle =
53 context->context()->GetBlobDataFromPublicURL(url);
54 const std::vector<storage::BlobData::Item> items = handle->data()->items();
56 // TODO(qinmin): handle the case when the blob data is not a single file.
57 DLOG_IF(WARNING, items.size() != 1u)
58 << "More than one blob data are present: " << items.size();
59 ReturnResultOnUIThread(callback, items[0].path().value());
62 static void RequestPlaformPathFromFileSystemURL(
63 const GURL& url,
64 int render_process_id,
65 scoped_refptr<storage::FileSystemContext> file_system_context,
66 const base::Callback<void(const std::string&)>& callback) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
68 base::FilePath platform_path;
69 SyncGetPlatformPath(file_system_context.get(),
70 render_process_id,
71 url,
72 &platform_path);
73 base::FilePath data_storage_path;
74 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
75 if (data_storage_path.IsParent(platform_path))
76 ReturnResultOnUIThread(callback, platform_path.value());
77 else
78 ReturnResultOnUIThread(callback, std::string());
81 // Posts a task to the UI thread to run the callback function.
82 static void PostMediaMetadataCallbackTask(
83 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback,
84 JNIEnv* env, ScopedJavaLocalRef<jobject>& j_metadata) {
85 BrowserThread::PostTask(
86 BrowserThread::UI, FROM_HERE,
87 base::Bind(callback, base::TimeDelta::FromMilliseconds(
88 Java_MediaMetadata_getDurationInMilliseconds(
89 env, j_metadata.obj())),
90 Java_MediaMetadata_getWidth(env, j_metadata.obj()),
91 Java_MediaMetadata_getHeight(env, j_metadata.obj()),
92 Java_MediaMetadata_isSuccess(env, j_metadata.obj())));
95 // Gets the metadata from a media URL. When finished, a task is posted to the UI
96 // thread to run the callback function.
97 static void GetMediaMetadata(
98 const std::string& url, const std::string& cookies,
99 const std::string& user_agent,
100 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
101 JNIEnv* env = base::android::AttachCurrentThread();
103 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url);
104 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies);
105 jobject j_context = base::android::GetApplicationContext();
106 ScopedJavaLocalRef<jstring> j_user_agent = ConvertUTF8ToJavaString(
107 env, user_agent);
108 ScopedJavaLocalRef<jobject> j_metadata =
109 Java_MediaResourceGetter_extractMediaMetadata(env,
110 j_context,
111 j_url_string.obj(),
112 j_cookies.obj(),
113 j_user_agent.obj());
115 PostMediaMetadataCallbackTask(callback, env, j_metadata);
118 // Gets the metadata from a file descriptor. When finished, a task is posted to
119 // the UI thread to run the callback function.
120 static void GetMediaMetadataFromFd(
121 const int fd, const int64 offset, const int64 size,
122 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
123 JNIEnv* env = base::android::AttachCurrentThread();
125 ScopedJavaLocalRef<jobject> j_metadata =
126 Java_MediaResourceGetter_extractMediaMetadataFromFd(
127 env, fd, offset, size);
129 PostMediaMetadataCallbackTask(callback, env, j_metadata);
132 // The task object that retrieves media resources on the IO thread.
133 // TODO(qinmin): refactor this class to make the code reusable by others as
134 // there are lots of duplicated functionalities elsewhere.
135 // http://crbug.com/395762.
136 class MediaResourceGetterTask
137 : public base::RefCountedThreadSafe<MediaResourceGetterTask> {
138 public:
139 MediaResourceGetterTask(BrowserContext* browser_context,
140 int render_process_id, int render_frame_id);
142 // Called by MediaResourceGetterImpl to start getting auth credentials.
143 net::AuthCredentials RequestAuthCredentials(const GURL& url) const;
145 // Called by MediaResourceGetterImpl to start getting cookies for a URL.
146 void RequestCookies(
147 const GURL& url, const GURL& first_party_for_cookies,
148 const media::MediaResourceGetter::GetCookieCB& callback);
150 private:
151 friend class base::RefCountedThreadSafe<MediaResourceGetterTask>;
152 virtual ~MediaResourceGetterTask();
154 void CheckPolicyForCookies(
155 const GURL& url, const GURL& first_party_for_cookies,
156 const media::MediaResourceGetter::GetCookieCB& callback,
157 const net::CookieList& cookie_list);
159 // Context getter used to get the CookieStore and auth cache.
160 net::URLRequestContextGetter* context_getter_;
162 // Resource context for checking cookie policies.
163 ResourceContext* resource_context_;
165 // Render process id, used to check whether the process can access cookies.
166 int render_process_id_;
168 // Render frame id, used to check tab specific cookie policy.
169 int render_frame_id_;
171 DISALLOW_COPY_AND_ASSIGN(MediaResourceGetterTask);
174 MediaResourceGetterTask::MediaResourceGetterTask(
175 BrowserContext* browser_context, int render_process_id, int render_frame_id)
176 : context_getter_(browser_context->GetRequestContext()),
177 resource_context_(browser_context->GetResourceContext()),
178 render_process_id_(render_process_id),
179 render_frame_id_(render_frame_id) {
182 MediaResourceGetterTask::~MediaResourceGetterTask() {}
184 net::AuthCredentials MediaResourceGetterTask::RequestAuthCredentials(
185 const GURL& url) const {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
187 net::HttpTransactionFactory* factory =
188 context_getter_->GetURLRequestContext()->http_transaction_factory();
189 if (!factory)
190 return net::AuthCredentials();
192 net::HttpAuthCache* auth_cache =
193 factory->GetSession()->http_auth_cache();
194 if (!auth_cache)
195 return net::AuthCredentials();
197 net::HttpAuthCache::Entry* entry =
198 auth_cache->LookupByPath(url.GetOrigin(), url.path());
200 // TODO(qinmin): handle other auth schemes. See http://crbug.com/395219.
201 if (entry && entry->scheme() == net::HttpAuth::AUTH_SCHEME_BASIC)
202 return entry->credentials();
203 else
204 return net::AuthCredentials();
207 void MediaResourceGetterTask::RequestCookies(
208 const GURL& url, const GURL& first_party_for_cookies,
209 const media::MediaResourceGetter::GetCookieCB& callback) {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
211 ChildProcessSecurityPolicyImpl* policy =
212 ChildProcessSecurityPolicyImpl::GetInstance();
213 if (!policy->CanAccessCookiesForOrigin(render_process_id_, url)) {
214 callback.Run(std::string());
215 return;
218 net::CookieStore* cookie_store =
219 context_getter_->GetURLRequestContext()->cookie_store();
220 if (!cookie_store) {
221 callback.Run(std::string());
222 return;
225 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
226 if (cookie_monster) {
227 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind(
228 &MediaResourceGetterTask::CheckPolicyForCookies, this,
229 url, first_party_for_cookies, callback));
230 } else {
231 callback.Run(std::string());
235 void MediaResourceGetterTask::CheckPolicyForCookies(
236 const GURL& url, const GURL& first_party_for_cookies,
237 const media::MediaResourceGetter::GetCookieCB& callback,
238 const net::CookieList& cookie_list) {
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
240 if (GetContentClient()->browser()->AllowGetCookie(
241 url, first_party_for_cookies, cookie_list,
242 resource_context_, render_process_id_, render_frame_id_)) {
243 net::CookieStore* cookie_store =
244 context_getter_->GetURLRequestContext()->cookie_store();
245 net::CookieOptions options;
246 options.set_include_httponly();
247 cookie_store->GetCookiesWithOptionsAsync(url, options, callback);
248 } else {
249 callback.Run(std::string());
253 MediaResourceGetterImpl::MediaResourceGetterImpl(
254 BrowserContext* browser_context,
255 storage::FileSystemContext* file_system_context,
256 int render_process_id,
257 int render_frame_id)
258 : browser_context_(browser_context),
259 file_system_context_(file_system_context),
260 render_process_id_(render_process_id),
261 render_frame_id_(render_frame_id),
262 weak_factory_(this) {
265 MediaResourceGetterImpl::~MediaResourceGetterImpl() {}
267 void MediaResourceGetterImpl::GetAuthCredentials(
268 const GURL& url, const GetAuthCredentialsCB& callback) {
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
270 scoped_refptr<MediaResourceGetterTask> task = new MediaResourceGetterTask(
271 browser_context_, 0, 0);
273 BrowserThread::PostTaskAndReplyWithResult(
274 BrowserThread::IO,
275 FROM_HERE,
276 base::Bind(&MediaResourceGetterTask::RequestAuthCredentials, task, url),
277 base::Bind(&MediaResourceGetterImpl::GetAuthCredentialsCallback,
278 weak_factory_.GetWeakPtr(), callback));
281 void MediaResourceGetterImpl::GetCookies(
282 const GURL& url, const GURL& first_party_for_cookies,
283 const GetCookieCB& callback) {
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
285 scoped_refptr<MediaResourceGetterTask> task = new MediaResourceGetterTask(
286 browser_context_, render_process_id_, render_frame_id_);
288 GetCookieCB cb = base::Bind(&MediaResourceGetterImpl::GetCookiesCallback,
289 weak_factory_.GetWeakPtr(),
290 callback);
291 BrowserThread::PostTask(
292 BrowserThread::IO,
293 FROM_HERE,
294 base::Bind(&MediaResourceGetterTask::RequestCookies,
295 task, url, first_party_for_cookies,
296 base::Bind(&ReturnResultOnUIThread, cb)));
299 void MediaResourceGetterImpl::GetAuthCredentialsCallback(
300 const GetAuthCredentialsCB& callback,
301 const net::AuthCredentials& credentials) {
302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
303 callback.Run(credentials.username(), credentials.password());
306 void MediaResourceGetterImpl::GetCookiesCallback(
307 const GetCookieCB& callback, const std::string& cookies) {
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
309 callback.Run(cookies);
312 void MediaResourceGetterImpl::GetPlatformPathFromURL(
313 const GURL& url, const GetPlatformPathCB& callback) {
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
315 DCHECK(url.SchemeIsFileSystem() || url.SchemeIs(url::kBlobScheme));
317 GetPlatformPathCB cb =
318 base::Bind(&MediaResourceGetterImpl::GetPlatformPathCallback,
319 weak_factory_.GetWeakPtr(),
320 callback);
322 if (url.SchemeIs(url::kBlobScheme)) {
323 BrowserThread::PostTask(
324 BrowserThread::IO,
325 FROM_HERE,
326 base::Bind(&RequestPlatformPathFromBlobURL, url, browser_context_, cb));
327 return;
330 scoped_refptr<storage::FileSystemContext> context(file_system_context_);
331 BrowserThread::PostTask(
332 BrowserThread::FILE,
333 FROM_HERE,
334 base::Bind(&RequestPlaformPathFromFileSystemURL, url, render_process_id_,
335 context, cb));
338 void MediaResourceGetterImpl::GetPlatformPathCallback(
339 const GetPlatformPathCB& callback, const std::string& platform_path) {
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
341 callback.Run(platform_path);
344 void MediaResourceGetterImpl::ExtractMediaMetadata(
345 const std::string& url, const std::string& cookies,
346 const std::string& user_agent, const ExtractMediaMetadataCB& callback) {
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
349 pool->PostWorkerTask(
350 FROM_HERE,
351 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback));
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));
364 // static
365 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) {
366 return RegisterNativesImpl(env);
369 } // namespace content