summaryrefslogtreecommitdiffstats
path: root/base/android
diff options
context:
space:
mode:
Diffstat (limited to 'base/android')
-rw-r--r--base/android/content_uri_utils.cc10
-rw-r--r--base/android/content_uri_utils.h10
-rw-r--r--base/android/content_uri_utils_unittest.cc37
-rw-r--r--base/android/java/src/org/chromium/base/ContentUriUtils.java42
4 files changed, 83 insertions, 16 deletions
diff --git a/base/android/content_uri_utils.cc b/base/android/content_uri_utils.cc
index 0e0c0ea..0482fee 100644
--- a/base/android/content_uri_utils.cc
+++ b/base/android/content_uri_utils.cc
@@ -35,4 +35,14 @@ File OpenContentUriForRead(const FilePath& content_uri) {
return File(fd);
}
+std::string GetContentUriMimeType(const FilePath& content_uri) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ ScopedJavaLocalRef<jstring> j_uri =
+ ConvertUTF8ToJavaString(env, content_uri.value());
+ ScopedJavaLocalRef<jstring> j_mime =
+ Java_ContentUriUtils_getMimeType(
+ env, base::android::GetApplicationContext(), j_uri.obj());
+ return base::android::ConvertJavaStringToUTF8(env, j_mime.obj());
+}
+
} // namespace base
diff --git a/base/android/content_uri_utils.h b/base/android/content_uri_utils.h
index 827ec92..e66b770 100644
--- a/base/android/content_uri_utils.h
+++ b/base/android/content_uri_utils.h
@@ -16,13 +16,17 @@ namespace base {
bool RegisterContentUriUtils(JNIEnv* env);
-// Opens a content uri for read and returns the file descriptor to the caller.
-// Returns -1 if the uri is invalid.
+// Opens a content URI for read and returns the file descriptor to the caller.
+// Returns -1 if the URI is invalid.
BASE_EXPORT File OpenContentUriForRead(const FilePath& content_uri);
-// Check whether a content uri exists.
+// Check whether a content URI exists.
BASE_EXPORT bool ContentUriExists(const FilePath& content_uri);
+// Gets MIME type from a content URI. Returns an empty string if the URI is
+// invalid.
+BASE_EXPORT std::string GetContentUriMimeType(const FilePath& content_uri);
+
} // namespace base
#endif // BASE_ANDROID_CONTENT_URI_UTILS_H_
diff --git a/base/android/content_uri_utils_unittest.cc b/base/android/content_uri_utils_unittest.cc
new file mode 100644
index 0000000..c762035
--- /dev/null
+++ b/base/android/content_uri_utils_unittest.cc
@@ -0,0 +1,37 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/content_uri_utils.h"
+#include "base/files/file_util.h"
+#include "base/path_service.h"
+#include "base/test/test_file_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace android {
+
+TEST(ContentUriUtilsTest, ContentUriMimeTest) {
+ // Get the test image path.
+ FilePath data_dir;
+ ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &data_dir));
+ data_dir = data_dir.AppendASCII("file_util");
+ ASSERT_TRUE(PathExists(data_dir));
+ FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
+
+ // Insert the image into MediaStore. MediaStore will do some conversions, and
+ // return the content URI.
+ FilePath path = base::InsertImageIntoMediaStore(image_file);
+ EXPECT_TRUE(path.IsContentUri());
+ EXPECT_TRUE(PathExists(path));
+
+ std::string mime = GetContentUriMimeType(path);
+ EXPECT_EQ(mime, std::string("image/png"));
+
+ FilePath invalid_path("content://foo.bar");
+ mime = GetContentUriMimeType(invalid_path);
+ EXPECT_TRUE(mime.empty());
+}
+
+} // namespace android
+} // namespace base
diff --git a/base/android/java/src/org/chromium/base/ContentUriUtils.java b/base/android/java/src/org/chromium/base/ContentUriUtils.java
index 70c27ed..ef527e1 100644
--- a/base/android/java/src/org/chromium/base/ContentUriUtils.java
+++ b/base/android/java/src/org/chromium/base/ContentUriUtils.java
@@ -12,6 +12,7 @@ import android.os.ParcelFileDescriptor;
import android.util.Log;
import java.io.File;
+import java.io.FileNotFoundException;
/**
* This class provides methods to access content URI schemes.
@@ -26,11 +27,11 @@ public abstract class ContentUriUtils {
*/
public interface FileProviderUtil {
/**
- * Generate a content uri from the given file.
+ * Generate a content URI from the given file.
* @param context Application context.
* @param file The file to be translated.
*/
- public Uri getContentUriFromFile(Context context, File file);
+ Uri getContentUriFromFile(Context context, File file);
}
// Prevent instantiation.
@@ -54,7 +55,7 @@ public abstract class ContentUriUtils {
*
* @param context {@link Context} in interest
* @param uriString the content URI to open
- * @returns file desciptor upon sucess, or -1 otherwise.
+ * @return file desciptor upon sucess, or -1 otherwise.
*/
@CalledByNative
public static int openContentUriForRead(Context context, String uriString) {
@@ -70,23 +71,34 @@ public abstract class ContentUriUtils {
*
* @param context {@link Context} in interest.
* @param uriString the content URI to query.
- * @returns true if the uri exists, or false otherwise.
+ * @return true if the URI exists, or false otherwise.
*/
@CalledByNative
public static boolean contentUriExists(Context context, String uriString) {
- ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString);
- if (pfd == null) {
- return false;
- }
- return true;
+ return getParcelFileDescriptor(context, uriString) != null;
+ }
+
+ /**
+ * Retrieve the MIME type for the content URI.
+ *
+ * @param context {@link Context} in interest.
+ * @param uriString the content URI to look up.
+ * @return MIME type or null if the input params are empty or invalid.
+ */
+ @CalledByNative
+ public static String getMimeType(Context context, String uriString) {
+ ContentResolver resolver = context.getContentResolver();
+ if (resolver == null) return null;
+ Uri uri = Uri.parse(uriString);
+ return resolver.getType(uri);
}
/**
- * Helper method to open a content URI and return the ParcelFileDescriptor.
+ * Helper method to open a content URI and returns the ParcelFileDescriptor.
*
* @param context {@link Context} in interest.
* @param uriString the content URI to open.
- * @returns ParcelFileDescriptor of the content URI, or NULL if the file does not exist.
+ * @return ParcelFileDescriptor of the content URI, or NULL if the file does not exist.
*/
private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) {
ContentResolver resolver = context.getContentResolver();
@@ -95,8 +107,12 @@ public abstract class ContentUriUtils {
ParcelFileDescriptor pfd = null;
try {
pfd = resolver.openFileDescriptor(uri, "r");
- } catch (java.io.FileNotFoundException e) {
+ } catch (FileNotFoundException e) {
Log.w(TAG, "Cannot find content uri: " + uriString, e);
+ } catch (SecurityException e) {
+ Log.w(TAG, "Cannot open content uri: " + uriString, e);
+ } catch (IllegalArgumentException e) {
+ Log.w(TAG, "Unknown content uri: " + uriString, e);
}
return pfd;
}
@@ -107,7 +123,7 @@ public abstract class ContentUriUtils {
* @param uri the content URI to be resolved.
* @param contentResolver the content resolver to query.
* @param columnField the column field to query.
- * @returns the display name of the @code uri if present in the database
+ * @return the display name of the @code uri if present in the database
* or an empty string otherwise.
*/
public static String getDisplayName(