Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: content/browser/media/android/media_resource_getter_impl.cc

Issue 98823003: Supporting blob urls for html5 media (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressing comments Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/content_browser_client.h" 17 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h" 18 #include "content/public/common/content_client.h"
19 #include "content/public/common/url_constants.h"
18 #include "jni/MediaResourceGetter_jni.h" 20 #include "jni/MediaResourceGetter_jni.h"
19 #include "net/cookies/cookie_monster.h" 21 #include "net/cookies/cookie_monster.h"
20 #include "net/cookies/cookie_store.h" 22 #include "net/cookies/cookie_store.h"
21 #include "net/url_request/url_request_context.h" 23 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h" 24 #include "net/url_request/url_request_context_getter.h"
23 #include "url/gurl.h" 25 #include "url/gurl.h"
26 #include "webkit/browser/blob/blob_data_handle.h"
27 #include "webkit/browser/blob/blob_storage_context.h"
24 28
25 using base::android::ConvertUTF8ToJavaString; 29 using base::android::ConvertUTF8ToJavaString;
26 using base::android::ScopedJavaLocalRef; 30 using base::android::ScopedJavaLocalRef;
27 31
28 namespace content { 32 namespace content {
29 33
30 static void ReturnResultOnUIThread( 34 static void ReturnResultOnUIThread(
31 const base::Callback<void(const std::string&)>& callback, 35 const base::Callback<void(const std::string&)>& callback,
32 const std::string& result) { 36 const std::string& result) {
33 BrowserThread::PostTask( 37 BrowserThread::PostTask(
34 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); 38 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
35 } 39 }
36 40
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();
52
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());
57 }
58
59 static void RequestPlaformPathFromFileSystemURL(
60 const GURL& url,
61 int renderer_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 renderer_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());
76 }
77
37 // Get the metadata from a media URL. When finished, a task is posted to the UI 78 // Get the metadata from a media URL. When finished, a task is posted to the UI
38 // thread to run the callback function. 79 // thread to run the callback function.
39 static void GetMediaMetadata( 80 static void GetMediaMetadata(
40 const std::string& url, const std::string& cookies, 81 const std::string& url, const std::string& cookies,
41 const std::string& user_agent, 82 const std::string& user_agent,
42 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) { 83 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
43 JNIEnv* env = base::android::AttachCurrentThread(); 84 JNIEnv* env = base::android::AttachCurrentThread();
44 85
45 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url); 86 ScopedJavaLocalRef<jstring> j_url_string = ConvertUTF8ToJavaString(env, url);
46 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies); 87 ScopedJavaLocalRef<jstring> j_cookies = ConvertUTF8ToJavaString(env, cookies);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 resource_context_, renderer_id_, routing_id_)) { 190 resource_context_, renderer_id_, routing_id_)) {
150 net::CookieStore* cookie_store = 191 net::CookieStore* cookie_store =
151 context_getter_->GetURLRequestContext()->cookie_store(); 192 context_getter_->GetURLRequestContext()->cookie_store();
152 cookie_store->GetCookiesWithOptionsAsync( 193 cookie_store->GetCookiesWithOptionsAsync(
153 url, net::CookieOptions(), callback); 194 url, net::CookieOptions(), callback);
154 } else { 195 } else {
155 callback.Run(std::string()); 196 callback.Run(std::string());
156 } 197 }
157 } 198 }
158 199
159 // The task object that retrieves platform path on the FILE thread.
160 class PlatformPathGetterTask
161 : public base::RefCountedThreadSafe<PlatformPathGetterTask> {
162 public:
163 PlatformPathGetterTask(fileapi::FileSystemContext* file_system_context,
164 int renderer_id);
165
166 // Called by MediaResourceGetterImpl to get the platform path from a file
167 // system URL.
168 void RequestPlaformPath(
169 const GURL& url,
170 const media::MediaResourceGetter::GetPlatformPathCB& callback);
171
172 private:
173 friend class base::RefCountedThreadSafe<PlatformPathGetterTask>;
174 virtual ~PlatformPathGetterTask();
175
176 // File system context for getting the platform path.
177 fileapi::FileSystemContext* file_system_context_;
178
179 // Render process id, used to check whether the process can access the URL.
180 int renderer_id_;
181
182 DISALLOW_COPY_AND_ASSIGN(PlatformPathGetterTask);
183 };
184
185 PlatformPathGetterTask::PlatformPathGetterTask(
186 fileapi::FileSystemContext* file_system_context, int renderer_id)
187 : file_system_context_(file_system_context),
188 renderer_id_(renderer_id) {
189 }
190
191 PlatformPathGetterTask::~PlatformPathGetterTask() {}
192
193 void PlatformPathGetterTask::RequestPlaformPath(
194 const GURL& url,
195 const media::MediaResourceGetter::GetPlatformPathCB& callback) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
197 base::FilePath platform_path;
198 SyncGetPlatformPath(file_system_context_,
199 renderer_id_,
200 url,
201 &platform_path);
202 base::FilePath data_storage_path;
203 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
204 if (data_storage_path.IsParent(platform_path))
205 callback.Run(platform_path.value());
206 else
207 callback.Run(std::string());
208 }
209
210 MediaResourceGetterImpl::MediaResourceGetterImpl( 200 MediaResourceGetterImpl::MediaResourceGetterImpl(
211 BrowserContext* browser_context, 201 BrowserContext* browser_context,
212 fileapi::FileSystemContext* file_system_context, 202 fileapi::FileSystemContext* file_system_context,
213 int renderer_id, int routing_id) 203 int renderer_id, int routing_id)
214 : browser_context_(browser_context), 204 : browser_context_(browser_context),
215 file_system_context_(file_system_context), 205 file_system_context_(file_system_context),
216 weak_this_(this), 206 weak_this_(this),
217 renderer_id_(renderer_id), 207 renderer_id_(renderer_id),
218 routing_id_(routing_id) { 208 routing_id_(routing_id) {
219 } 209 }
(...skipping 16 matching lines...) Expand all
236 task, url, first_party_for_cookies, 226 task, url, first_party_for_cookies,
237 base::Bind(&ReturnResultOnUIThread, cb))); 227 base::Bind(&ReturnResultOnUIThread, cb)));
238 } 228 }
239 229
240 void MediaResourceGetterImpl::GetCookiesCallback( 230 void MediaResourceGetterImpl::GetCookiesCallback(
241 const GetCookieCB& callback, const std::string& cookies) { 231 const GetCookieCB& callback, const std::string& cookies) {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
243 callback.Run(cookies); 233 callback.Run(cookies);
244 } 234 }
245 235
246 void MediaResourceGetterImpl::GetPlatformPathFromFileSystemURL( 236 void MediaResourceGetterImpl::GetPlatformPathFromURL(
247 const GURL& url, const GetPlatformPathCB& callback) { 237 const GURL& url, const GetPlatformPathCB& callback) {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 scoped_refptr<PlatformPathGetterTask> task = new PlatformPathGetterTask( 239 DCHECK(url.SchemeIsFileSystem() || url.SchemeIs(chrome::kBlobScheme));
250 file_system_context_, renderer_id_);
251 240
252 GetPlatformPathCB cb = base::Bind( 241 GetPlatformPathCB cb = base::Bind(
253 &MediaResourceGetterImpl::GetPlatformPathCallback, 242 &MediaResourceGetterImpl::GetPlatformPathCallback,
254 weak_this_.GetWeakPtr(), callback); 243 weak_this_.GetWeakPtr(), callback);
244
245 if (url.SchemeIs(chrome::kBlobScheme)) {
246 BrowserThread::PostTask(
247 BrowserThread::IO,
248 FROM_HERE,
249 base::Bind(&RequestPlatformPathFromBlobURL, url, browser_context_, cb));
250 return;
251 }
252
253 scoped_refptr<fileapi::FileSystemContext> context(file_system_context_);
255 BrowserThread::PostTask( 254 BrowserThread::PostTask(
256 BrowserThread::FILE, 255 BrowserThread::FILE,
257 FROM_HERE, 256 FROM_HERE,
258 base::Bind(&PlatformPathGetterTask::RequestPlaformPath, 257 base::Bind(&RequestPlaformPathFromFileSystemURL, url, renderer_id_,
259 task, url, 258 context, cb));
260 base::Bind(&ReturnResultOnUIThread, cb)));
261 } 259 }
262 260
263 void MediaResourceGetterImpl::GetPlatformPathCallback( 261 void MediaResourceGetterImpl::GetPlatformPathCallback(
264 const GetPlatformPathCB& callback, const std::string& platform_path) { 262 const GetPlatformPathCB& callback, const std::string& platform_path) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 callback.Run(platform_path); 264 callback.Run(platform_path);
267 } 265 }
268 266
269 void MediaResourceGetterImpl::ExtractMediaMetadata( 267 void MediaResourceGetterImpl::ExtractMediaMetadata(
270 const std::string& url, const std::string& cookies, 268 const std::string& url, const std::string& cookies,
271 const std::string& user_agent, const ExtractMediaMetadataCB& callback) { 269 const std::string& user_agent, const ExtractMediaMetadataCB& callback) {
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
273 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); 271 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
274 pool->PostWorkerTask( 272 pool->PostWorkerTask(
275 FROM_HERE, 273 FROM_HERE,
276 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback)); 274 base::Bind(&GetMediaMetadata, url, cookies, user_agent, callback));
277 } 275 }
278 276
279 // static 277 // static
280 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) { 278 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) {
281 return RegisterNativesImpl(env); 279 return RegisterNativesImpl(env);
282 } 280 }
283 281
284 } // namespace content 282 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698