OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "net/android/content_uri_utils.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/location.h" |
| 10 #include "base/task_runner.h" |
| 11 #include "base/task_runner_util.h" |
| 12 #include "jni/ContentUriUtils_jni.h" |
| 13 #include "net/base/net_errors.h" |
| 14 |
| 15 using base::android::ConvertUTF8ToJavaString; |
| 16 |
| 17 namespace net { |
| 18 |
| 19 namespace { |
| 20 |
| 21 int64 GetLengthInternal(const base::FilePath& content_url) { |
| 22 JNIEnv* env = base::android::AttachCurrentThread(); |
| 23 ScopedJavaLocalRef<jstring> j_uri = |
| 24 ConvertUTF8ToJavaString(env, content_url.value()); |
| 25 jlong length = Java_ContentUriUtils_getContentLength( |
| 26 env, base::android::GetApplicationContext(), j_uri.obj()); |
| 27 return length; |
| 28 } |
| 29 |
| 30 void OnGetLengthCompleted( |
| 31 const net::Int64CompletionCallback& callback, int64 length) { |
| 32 callback.Run(length); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 bool RegisterContentUriUtils(JNIEnv* env) { |
| 38 return RegisterNativesImpl(env); |
| 39 } |
| 40 |
| 41 int64 GetContentUrlLength(base::TaskRunner* task_runner, |
| 42 const base::FilePath& content_url, |
| 43 const net::Int64CompletionCallback& callback) { |
| 44 bool posted = base::PostTaskAndReplyWithResult( |
| 45 task_runner, |
| 46 FROM_HERE, |
| 47 base::Bind(&GetLengthInternal, content_url), |
| 48 base::Bind(&OnGetLengthCompleted, callback)); |
| 49 DCHECK(posted); |
| 50 return net::ERR_IO_PENDING; |
| 51 } |
| 52 |
| 53 int64 GetContentUrlLengthSync(const base::FilePath& content_url) { |
| 54 return GetLengthInternal(content_url); |
| 55 } |
| 56 |
| 57 int OpenContentUrlForRead(const base::FilePath& content_url) { |
| 58 JNIEnv* env = base::android::AttachCurrentThread(); |
| 59 ScopedJavaLocalRef<jstring> j_uri = |
| 60 ConvertUTF8ToJavaString(env, content_url.value()); |
| 61 jint fd = Java_ContentUriUtils_openContentUriForRead( |
| 62 env, base::android::GetApplicationContext(), j_uri.obj()); |
| 63 if (fd < 0) |
| 64 return base::kInvalidPlatformFileValue; |
| 65 return fd; |
| 66 } |
| 67 |
| 68 } // namespace net |
OLD | NEW |