1 // Copyright 2012 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/path_utils.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_array.h" 9 #include "base/android/jni_string.h" 10 #include "base/android/scoped_java_ref.h" 11 #include "base/files/file_path.h" 12 13 #include "base/base_jni_headers/PathUtils_jni.h" 14 15 namespace base { 16 namespace android { 17 GetDataDirectory(FilePath * result)18bool GetDataDirectory(FilePath* result) { 19 JNIEnv* env = AttachCurrentThread(); 20 ScopedJavaLocalRef<jstring> path = Java_PathUtils_getDataDirectory(env); 21 FilePath data_path(ConvertJavaStringToUTF8(path)); 22 *result = data_path; 23 return true; 24 } 25 GetCacheDirectory(FilePath * result)26bool GetCacheDirectory(FilePath* result) { 27 JNIEnv* env = AttachCurrentThread(); 28 ScopedJavaLocalRef<jstring> path = Java_PathUtils_getCacheDirectory(env); 29 FilePath cache_path(ConvertJavaStringToUTF8(path)); 30 *result = cache_path; 31 return true; 32 } 33 GetThumbnailCacheDirectory(FilePath * result)34bool GetThumbnailCacheDirectory(FilePath* result) { 35 JNIEnv* env = AttachCurrentThread(); 36 ScopedJavaLocalRef<jstring> path = 37 Java_PathUtils_getThumbnailCacheDirectory(env); 38 FilePath thumbnail_cache_path(ConvertJavaStringToUTF8(path)); 39 *result = thumbnail_cache_path; 40 return true; 41 } 42 GetDownloadsDirectory(FilePath * result)43bool GetDownloadsDirectory(FilePath* result) { 44 JNIEnv* env = AttachCurrentThread(); 45 ScopedJavaLocalRef<jstring> path = Java_PathUtils_getDownloadsDirectory(env); 46 FilePath downloads_path(ConvertJavaStringToUTF8(path)); 47 *result = downloads_path; 48 return true; 49 } 50 GetAllPrivateDownloadsDirectories()51std::vector<FilePath> GetAllPrivateDownloadsDirectories() { 52 std::vector<std::string> dirs; 53 JNIEnv* env = AttachCurrentThread(); 54 auto jarray = Java_PathUtils_getAllPrivateDownloadsDirectories(env); 55 base::android::AppendJavaStringArrayToStringVector(env, jarray, &dirs); 56 57 std::vector<base::FilePath> file_paths; 58 for (const auto& dir : dirs) 59 file_paths.emplace_back(dir); 60 return file_paths; 61 } 62 GetSecondaryStorageDownloadDirectories()63std::vector<FilePath> GetSecondaryStorageDownloadDirectories() { 64 std::vector<std::string> dirs; 65 JNIEnv* env = AttachCurrentThread(); 66 auto jarray = Java_PathUtils_getExternalDownloadVolumesNames(env); 67 base::android::AppendJavaStringArrayToStringVector(env, jarray, &dirs); 68 69 std::vector<base::FilePath> file_paths; 70 for (const auto& dir : dirs) 71 file_paths.emplace_back(dir); 72 return file_paths; 73 } 74 GetNativeLibraryDirectory(FilePath * result)75bool GetNativeLibraryDirectory(FilePath* result) { 76 JNIEnv* env = AttachCurrentThread(); 77 ScopedJavaLocalRef<jstring> path = 78 Java_PathUtils_getNativeLibraryDirectory(env); 79 FilePath library_path(ConvertJavaStringToUTF8(path)); 80 *result = library_path; 81 return true; 82 } 83 GetExternalStorageDirectory(FilePath * result)84bool GetExternalStorageDirectory(FilePath* result) { 85 JNIEnv* env = AttachCurrentThread(); 86 ScopedJavaLocalRef<jstring> path = 87 Java_PathUtils_getExternalStorageDirectory(env); 88 FilePath storage_path(ConvertJavaStringToUTF8(path)); 89 *result = storage_path; 90 return true; 91 } 92 93 } // namespace android 94 } // namespace base 95