Add a FrameHostMsg_BeginNavigation IPC
[chromium-blink-merge.git] / content / browser / media / android / media_resource_getter_impl.cc
blobf33c26d0bea3952997f94a46713f7a34c2a8172c
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 "net/cookies/cookie_monster.h"
22 #include "net/cookies/cookie_store.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "url/gurl.h"
26 #include "webkit/browser/blob/blob_data_handle.h"
27 #include "webkit/browser/blob/blob_storage_context.h"
29 using base::android::ConvertUTF8ToJavaString;
30 using base::android::ScopedJavaLocalRef;
32 namespace content {
34 static void ReturnResultOnUIThread(
35 const base::Callback<void(const std::string&)>& callback,
36 const std::string& result) {
37 BrowserThread::PostTask(
38 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
41 static void RequestPlatformPathFromBlobURL(
42 const GURL& url,
43 BrowserContext* browser_context,
44 const base::Callback<void(const std::string&)>& callback) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
46 ChromeBlobStorageContext* context =
47 ChromeBlobStorageContext::GetFor(browser_context);
48 scoped_ptr<webkit_blob::BlobDataHandle> handle =
49 context->context()->GetBlobDataFromPublicURL(url);
50 const std::vector<webkit_blob::BlobData::Item> items =
51 handle->data()->items();
53 // TODO(qinmin): handle the case when the blob data is not a single file.
54 DLOG_IF(WARNING, items.size() != 1u)
55 << "More than one blob data are present: " << items.size();
56 ReturnResultOnUIThread(callback, items[0].path().value());
59 static void RequestPlaformPathFromFileSystemURL(
60 const GURL& url,
61 int render_process_id,
62 scoped_refptr<fileapi::FileSystemContext> file_system_context,
63 const base::Callback<void(const std::string&)>& callback) {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
65 base::FilePath platform_path;
66 SyncGetPlatformPath(file_system_context.get(),
67 render_process_id,
68 url,
69 &platform_path);
70 base::FilePath data_storage_path;
71 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
72 if (data_storage_path.IsParent(platform_path))
73 ReturnResultOnUIThread(callback, platform_path.value());
74 else
75 ReturnResultOnUIThread(callback, std::string());
78 // Get the metadata from a media URL. When finished, a task is posted to the UI
79 // thread to run the callback function.
80 static void GetMediaMetadata(
81 const std::string& url, const std::string& cookies,
82 const std::string& user_agent,
83 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
84 JNIEnv* env = base::android::AttachCurrentThread();
86 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url);
87 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies);
88 jobject j_context = base::android::GetApplicationContext();
89 ScopedJavaLocalRef<jstring> j_user_agent = ConvertUTF8ToJavaString(
90 env, user_agent);
91 ScopedJavaLocalRef<jobject> j_metadata =
92 Java_MediaResourceGetter_extractMediaMetadata(env,
93 j_context,
94 j_url_string.obj(),
95 j_cookies.obj(),
96 j_user_agent.obj());
97 BrowserThread::PostTask(
98 BrowserThread::UI, FROM_HERE,
99 base::Bind(callback, base::TimeDelta::FromMilliseconds(
100 Java_MediaMetadata_getDurationInMilliseconds(
101 env, j_metadata.obj())),
102 Java_MediaMetadata_getWidth(env, j_metadata.obj()),
103 Java_MediaMetadata_getHeight(env, j_metadata.obj()),
104 Java_MediaMetadata_isSuccess(env, j_metadata.obj())));
107 // The task object that retrieves cookie on the IO thread.
108 // TODO(qinmin): refactor this class to make the code reusable by others as
109 // there are lots of duplicated functionalities elsewhere.
110 class CookieGetterTask
111 : public base::RefCountedThreadSafe<CookieGetterTask> {
112 public:
113 CookieGetterTask(BrowserContext* browser_context,
114 int render_process_id, int render_frame_id);
116 // Called by CookieGetterImpl to start getting cookies for a URL.
117 void RequestCookies(
118 const GURL& url, const GURL& first_party_for_cookies,
119 const media::MediaResourceGetter::GetCookieCB& callback);
121 private:
122 friend class base::RefCountedThreadSafe<CookieGetterTask>;
123 virtual ~CookieGetterTask();
125 void CheckPolicyForCookies(
126 const GURL& url, const GURL& first_party_for_cookies,
127 const media::MediaResourceGetter::GetCookieCB& callback,
128 const net::CookieList& cookie_list);
130 // Context getter used to get the CookieStore.
131 net::URLRequestContextGetter* context_getter_;
133 // Resource context for checking cookie policies.
134 ResourceContext* resource_context_;
136 // Render process id, used to check whether the process can access cookies.
137 int render_process_id_;
139 // Render frame id, used to check tab specific cookie policy.
140 int render_frame_id_;
142 DISALLOW_COPY_AND_ASSIGN(CookieGetterTask);
145 CookieGetterTask::CookieGetterTask(
146 BrowserContext* browser_context, int render_process_id, int render_frame_id)
147 : context_getter_(browser_context->GetRequestContext()),
148 resource_context_(browser_context->GetResourceContext()),
149 render_process_id_(render_process_id),
150 render_frame_id_(render_frame_id) {
153 CookieGetterTask::~CookieGetterTask() {}
155 void CookieGetterTask::RequestCookies(
156 const GURL& url, const GURL& first_party_for_cookies,
157 const media::MediaResourceGetter::GetCookieCB& callback) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
159 ChildProcessSecurityPolicyImpl* policy =
160 ChildProcessSecurityPolicyImpl::GetInstance();
161 if (!policy->CanAccessCookiesForOrigin(render_process_id_, url)) {
162 callback.Run(std::string());
163 return;
166 net::CookieStore* cookie_store =
167 context_getter_->GetURLRequestContext()->cookie_store();
168 if (!cookie_store) {
169 callback.Run(std::string());
170 return;
173 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
174 if (cookie_monster) {
175 cookie_monster->GetAllCookiesForURLAsync(url, base::Bind(
176 &CookieGetterTask::CheckPolicyForCookies, this,
177 url, first_party_for_cookies, callback));
178 } else {
179 callback.Run(std::string());
183 void CookieGetterTask::CheckPolicyForCookies(
184 const GURL& url, const GURL& first_party_for_cookies,
185 const media::MediaResourceGetter::GetCookieCB& callback,
186 const net::CookieList& cookie_list) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
188 if (GetContentClient()->browser()->AllowGetCookie(
189 url, first_party_for_cookies, cookie_list,
190 resource_context_, render_process_id_, render_frame_id_)) {
191 net::CookieStore* cookie_store =
192 context_getter_->GetURLRequestContext()->cookie_store();
193 net::CookieOptions options;
194 options.set_include_httponly();
195 cookie_store->GetCookiesWithOptionsAsync(url, options, callback);
196 } else {
197 callback.Run(std::string());
201 MediaResourceGetterImpl::MediaResourceGetterImpl(
202 BrowserContext* browser_context,
203 fileapi::FileSystemContext* file_system_context,
204 int render_process_id,
205 int render_frame_id)
206 : browser_context_(browser_context),
207 file_system_context_(file_system_context),
208 render_process_id_(render_process_id),
209 render_frame_id_(render_frame_id),
210 weak_factory_(this) {}
212 MediaResourceGetterImpl::~MediaResourceGetterImpl() {}
214 void MediaResourceGetterImpl::GetCookies(
215 const GURL& url, const GURL& first_party_for_cookies,
216 const GetCookieCB& callback) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
218 scoped_refptr<CookieGetterTask> task = new CookieGetterTask(
219 browser_context_, render_process_id_, render_frame_id_);
221 GetCookieCB cb = base::Bind(&MediaResourceGetterImpl::GetCookiesCallback,
222 weak_factory_.GetWeakPtr(),
223 callback);
224 BrowserThread::PostTask(
225 BrowserThread::IO,
226 FROM_HERE,
227 base::Bind(&CookieGetterTask::RequestCookies,
228 task, url, first_party_for_cookies,
229 base::Bind(&ReturnResultOnUIThread, cb)));
232 void MediaResourceGetterImpl::GetCookiesCallback(
233 const GetCookieCB& callback, const std::string& cookies) {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235 callback.Run(cookies);
238 void MediaResourceGetterImpl::GetPlatformPathFromURL(
239 const GURL& url, const GetPlatformPathCB& callback) {
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
241 DCHECK(url.SchemeIsFileSystem() || url.SchemeIs(url::kBlobScheme));
243 GetPlatformPathCB cb =
244 base::Bind(&MediaResourceGetterImpl::GetPlatformPathCallback,
245 weak_factory_.GetWeakPtr(),
246 callback);
248 if (url.SchemeIs(url::kBlobScheme)) {
249 BrowserThread::PostTask(
250 BrowserThread::IO,
251 FROM_HERE,
252 base::Bind(&RequestPlatformPathFromBlobURL, url, browser_context_, cb));
253 return;
256 scoped_refptr<fileapi::FileSystemContext> context(file_system_context_);
257 BrowserThread::PostTask(
258 BrowserThread::FILE,
259 FROM_HERE,
260 base::Bind(&RequestPlaformPathFromFileSystemURL, url, render_process_id_,
261 context, cb));
264 void MediaResourceGetterImpl::GetPlatformPathCallback(
265 const GetPlatformPathCB& callback, const std::string& platform_path) {
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
267 callback.Run(platform_path);
270 void MediaResourceGetterImpl::ExtractMediaMetadata(
271 const std::string& url, const std::string& cookies,
272 const std::string& user_agent, const ExtractMediaMetadataCB& callback) {
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
274 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
275 pool->PostWorkerTask(
276 FROM_HERE,
277 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback));
280 // static
281 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) {
282 return RegisterNativesImpl(env);
285 } // namespace content