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