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

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: Created 7 years 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 namespace content { 29 namespace content {
26 30
27 static void ReturnResultOnUIThread( 31 static void ReturnResultOnUIThread(
28 const base::Callback<void(const std::string&)>& callback, 32 const base::Callback<void(const std::string&)>& callback,
29 const std::string& result) { 33 const std::string& result) {
30 BrowserThread::PostTask( 34 BrowserThread::PostTask(
31 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); 35 BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
32 } 36 }
33 37
38 static void RequestPlatformPathFromBlobURL(
39 const GURL& url,
40 BrowserContext* browser_context,
41 const base::Callback<void(const std::string&)>& callback) {
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
43 ChromeBlobStorageContext* context =
44 ChromeBlobStorageContext::GetFor(browser_context);
45 scoped_ptr<webkit_blob::BlobDataHandle> handle =
46 context->context()->GetBlobDataFromPublicURL(url);
47 const std::vector<webkit_blob::BlobData::Item> items =
48 handle->data()->items();
49
50 // TODO(qinmin): handle the case when the blob data is not a single file.
51 DCHECK_EQ(1u, items.size());
xhwang 2014/01/14 17:57:40 The blob data comes from user. So user can easily
qinmin 2014/01/14 21:48:33 Done.
52 items[0].path().value();
xhwang 2014/01/14 17:57:40 This doesn't seem to do anything.. remove?
qinmin 2014/01/14 21:48:33 Done.
53 ReturnResultOnUIThread(callback, items[0].path().value());
54 }
55
56 static void RequestPlaformPathFromFileSystemURL(
57 const GURL& url,
58 int renderer_id,
59 scoped_refptr<fileapi::FileSystemContext> file_system_context,
60 const base::Callback<void(const std::string&)>& callback) {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
62 base::FilePath platform_path;
63 SyncGetPlatformPath(file_system_context.get(),
64 renderer_id,
65 url,
66 &platform_path);
67 base::FilePath data_storage_path;
68 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
69 if (data_storage_path.IsParent(platform_path))
70 ReturnResultOnUIThread(callback, platform_path.value());
71 else
72 ReturnResultOnUIThread(callback, std::string());
73 }
74
34 // Get the metadata from a media URL. When finished, a task is posted to the UI 75 // Get the metadata from a media URL. When finished, a task is posted to the UI
35 // thread to run the callback function. 76 // thread to run the callback function.
36 static void GetMediaMetadata( 77 static void GetMediaMetadata(
37 const std::string& url, const std::string& cookies, 78 const std::string& url, const std::string& cookies,
38 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) { 79 const media::MediaResourceGetter::ExtractMediaMetadataCB& callback) {
39 JNIEnv* env = base::android::AttachCurrentThread(); 80 JNIEnv* env = base::android::AttachCurrentThread();
40 81
41 base::android::ScopedJavaLocalRef<jstring> j_url_string = 82 base::android::ScopedJavaLocalRef<jstring> j_url_string =
42 base::android::ConvertUTF8ToJavaString(env, url); 83 base::android::ConvertUTF8ToJavaString(env, url);
43 base::android::ScopedJavaLocalRef<jstring> j_cookies = 84 base::android::ScopedJavaLocalRef<jstring> j_cookies =
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 resource_context_, renderer_id_, routing_id_)) { 183 resource_context_, renderer_id_, routing_id_)) {
143 net::CookieStore* cookie_store = 184 net::CookieStore* cookie_store =
144 context_getter_->GetURLRequestContext()->cookie_store(); 185 context_getter_->GetURLRequestContext()->cookie_store();
145 cookie_store->GetCookiesWithOptionsAsync( 186 cookie_store->GetCookiesWithOptionsAsync(
146 url, net::CookieOptions(), callback); 187 url, net::CookieOptions(), callback);
147 } else { 188 } else {
148 callback.Run(std::string()); 189 callback.Run(std::string());
149 } 190 }
150 } 191 }
151 192
152 // The task object that retrieves platform path on the FILE thread.
153 class PlatformPathGetterTask
154 : public base::RefCountedThreadSafe<PlatformPathGetterTask> {
155 public:
156 PlatformPathGetterTask(fileapi::FileSystemContext* file_system_context,
157 int renderer_id);
158
159 // Called by MediaResourceGetterImpl to get the platform path from a file
160 // system URL.
161 void RequestPlaformPath(
162 const GURL& url,
163 const media::MediaResourceGetter::GetPlatformPathCB& callback);
164
165 private:
166 friend class base::RefCountedThreadSafe<PlatformPathGetterTask>;
167 virtual ~PlatformPathGetterTask();
168
169 // File system context for getting the platform path.
170 fileapi::FileSystemContext* file_system_context_;
171
172 // Render process id, used to check whether the process can access the URL.
173 int renderer_id_;
174
175 DISALLOW_COPY_AND_ASSIGN(PlatformPathGetterTask);
176 };
177
178 PlatformPathGetterTask::PlatformPathGetterTask(
179 fileapi::FileSystemContext* file_system_context, int renderer_id)
180 : file_system_context_(file_system_context),
181 renderer_id_(renderer_id) {
182 }
183
184 PlatformPathGetterTask::~PlatformPathGetterTask() {}
185
186 void PlatformPathGetterTask::RequestPlaformPath(
187 const GURL& url,
188 const media::MediaResourceGetter::GetPlatformPathCB& callback) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
190 base::FilePath platform_path;
191 SyncGetPlatformPath(file_system_context_,
192 renderer_id_,
193 url,
194 &platform_path);
195 base::FilePath data_storage_path;
196 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_storage_path);
197 if (data_storage_path.IsParent(platform_path))
198 callback.Run(platform_path.value());
199 else
200 callback.Run(std::string());
201 }
202
203 MediaResourceGetterImpl::MediaResourceGetterImpl( 193 MediaResourceGetterImpl::MediaResourceGetterImpl(
204 BrowserContext* browser_context, 194 BrowserContext* browser_context,
205 fileapi::FileSystemContext* file_system_context, 195 fileapi::FileSystemContext* file_system_context,
206 int renderer_id, int routing_id) 196 int renderer_id, int routing_id)
207 : browser_context_(browser_context), 197 : browser_context_(browser_context),
208 file_system_context_(file_system_context), 198 file_system_context_(file_system_context),
209 weak_this_(this), 199 weak_this_(this),
210 renderer_id_(renderer_id), 200 renderer_id_(renderer_id),
211 routing_id_(routing_id) { 201 routing_id_(routing_id) {
212 } 202 }
(...skipping 16 matching lines...) Expand all
229 task, url, first_party_for_cookies, 219 task, url, first_party_for_cookies,
230 base::Bind(&ReturnResultOnUIThread, cb))); 220 base::Bind(&ReturnResultOnUIThread, cb)));
231 } 221 }
232 222
233 void MediaResourceGetterImpl::GetCookiesCallback( 223 void MediaResourceGetterImpl::GetCookiesCallback(
234 const GetCookieCB& callback, const std::string& cookies) { 224 const GetCookieCB& callback, const std::string& cookies) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
236 callback.Run(cookies); 226 callback.Run(cookies);
237 } 227 }
238 228
239 void MediaResourceGetterImpl::GetPlatformPathFromFileSystemURL( 229 void MediaResourceGetterImpl::GetPlatformPathFromURL(
240 const GURL& url, const GetPlatformPathCB& callback) { 230 const GURL& url, const GetPlatformPathCB& callback) {
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
242 scoped_refptr<PlatformPathGetterTask> task = new PlatformPathGetterTask( 232 DCHECK(url.SchemeIsFileSystem() || url.SchemeIs(chrome::kBlobScheme));
243 file_system_context_, renderer_id_);
244 233
245 GetPlatformPathCB cb = base::Bind( 234 GetPlatformPathCB cb = base::Bind(
246 &MediaResourceGetterImpl::GetPlatformPathCallback, 235 &MediaResourceGetterImpl::GetPlatformPathCallback,
247 weak_this_.GetWeakPtr(), callback); 236 weak_this_.GetWeakPtr(), callback);
237
238 if (url.SchemeIs(chrome::kBlobScheme)) {
239 BrowserThread::PostTask(
240 BrowserThread::IO,
241 FROM_HERE,
242 base::Bind(&RequestPlatformPathFromBlobURL, url, browser_context_, cb));
243 return;
244 }
245
246 scoped_refptr<fileapi::FileSystemContext> context(file_system_context_);
248 BrowserThread::PostTask( 247 BrowserThread::PostTask(
249 BrowserThread::FILE, 248 BrowserThread::FILE,
250 FROM_HERE, 249 FROM_HERE,
251 base::Bind(&PlatformPathGetterTask::RequestPlaformPath, 250 base::Bind(&RequestPlaformPathFromFileSystemURL, url, renderer_id_,
252 task, url, 251 context, cb));
253 base::Bind(&ReturnResultOnUIThread, cb)));
254 } 252 }
255 253
256 void MediaResourceGetterImpl::GetPlatformPathCallback( 254 void MediaResourceGetterImpl::GetPlatformPathCallback(
257 const GetPlatformPathCB& callback, const std::string& platform_path) { 255 const GetPlatformPathCB& callback, const std::string& platform_path) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
259 callback.Run(platform_path); 257 callback.Run(platform_path);
260 } 258 }
261 259
262 void MediaResourceGetterImpl::ExtractMediaMetadata( 260 void MediaResourceGetterImpl::ExtractMediaMetadata(
263 const std::string& url, const std::string& cookies, 261 const std::string& url, const std::string& cookies,
264 const ExtractMediaMetadataCB& callback) { 262 const ExtractMediaMetadataCB& callback) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); 264 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
267 pool->PostWorkerTask( 265 pool->PostWorkerTask(
268 FROM_HERE, base::Bind(&GetMediaMetadata, url, cookies, callback)); 266 FROM_HERE, base::Bind(&GetMediaMetadata, url, cookies, callback));
269 } 267 }
270 268
271 // static 269 // static
272 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) { 270 bool MediaResourceGetterImpl::RegisterMediaResourceGetter(JNIEnv* env) {
273 return RegisterNativesImpl(env); 271 return RegisterNativesImpl(env);
274 } 272 }
275 273
276 } // namespace content 274 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698