1 // Copyright 2013 The Chromium Authors
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 "base/android/content_uri_utils.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/base_jni_headers/ContentUriUtils_jni.h"
10
11 using base::android::ConvertUTF8ToJavaString;
12 using base::android::ScopedJavaLocalRef;
13
14 namespace base {
15
ContentUriExists(const FilePath & content_uri)16 bool ContentUriExists(const FilePath& content_uri) {
17 JNIEnv* env = base::android::AttachCurrentThread();
18 ScopedJavaLocalRef<jstring> j_uri =
19 ConvertUTF8ToJavaString(env, content_uri.value());
20 return Java_ContentUriUtils_contentUriExists(env, j_uri);
21 }
22
OpenContentUriForRead(const FilePath & content_uri)23 File OpenContentUriForRead(const FilePath& content_uri) {
24 JNIEnv* env = base::android::AttachCurrentThread();
25 ScopedJavaLocalRef<jstring> j_uri =
26 ConvertUTF8ToJavaString(env, content_uri.value());
27 jint fd = Java_ContentUriUtils_openContentUriForRead(env, j_uri);
28 if (fd < 0)
29 return File();
30 return File(fd);
31 }
32
GetContentUriMimeType(const FilePath & content_uri)33 std::string GetContentUriMimeType(const FilePath& content_uri) {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 ScopedJavaLocalRef<jstring> j_uri =
36 ConvertUTF8ToJavaString(env, content_uri.value());
37 ScopedJavaLocalRef<jstring> j_mime =
38 Java_ContentUriUtils_getMimeType(env, j_uri);
39 if (j_mime.is_null())
40 return std::string();
41
42 return base::android::ConvertJavaStringToUTF8(env, j_mime.obj());
43 }
44
MaybeGetFileDisplayName(const FilePath & content_uri,std::u16string * file_display_name)45 bool MaybeGetFileDisplayName(const FilePath& content_uri,
46 std::u16string* file_display_name) {
47 if (!content_uri.IsContentUri())
48 return false;
49
50 DCHECK(file_display_name);
51
52 JNIEnv* env = base::android::AttachCurrentThread();
53 ScopedJavaLocalRef<jstring> j_uri =
54 ConvertUTF8ToJavaString(env, content_uri.value());
55 ScopedJavaLocalRef<jstring> j_display_name =
56 Java_ContentUriUtils_maybeGetDisplayName(env, j_uri);
57
58 if (j_display_name.is_null())
59 return false;
60
61 *file_display_name = base::android::ConvertJavaStringToUTF16(j_display_name);
62 return true;
63 }
64
DeleteContentUri(const FilePath & content_uri)65 bool DeleteContentUri(const FilePath& content_uri) {
66 DCHECK(content_uri.IsContentUri());
67 JNIEnv* env = base::android::AttachCurrentThread();
68 ScopedJavaLocalRef<jstring> j_uri =
69 ConvertUTF8ToJavaString(env, content_uri.value());
70
71 return Java_ContentUriUtils_delete(env, j_uri);
72 }
73
GetContentUriFromFilePath(const FilePath & file_path)74 FilePath GetContentUriFromFilePath(const FilePath& file_path) {
75 JNIEnv* env = base::android::AttachCurrentThread();
76 ScopedJavaLocalRef<jstring> j_file_path =
77 ConvertUTF8ToJavaString(env, file_path.value());
78 ScopedJavaLocalRef<jstring> j_content_uri =
79 Java_ContentUriUtils_getContentUriFromFilePath(env, j_file_path);
80 if (j_content_uri.is_null())
81 return FilePath();
82
83 return FilePath(base::android::ConvertJavaStringToUTF8(env, j_content_uri));
84 }
85
86 } // namespace base
87