Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: base/android/java/src/org/chromium/base/ContentUriUtils.java

Issue 739033003: Support content scheme uri for Chrome on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/android/content_uri_utils_unittest.cc ('k') | base/base.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base; 5 package org.chromium.base;
6 6
7 import android.content.ContentResolver; 7 import android.content.ContentResolver;
8 import android.content.Context; 8 import android.content.Context;
9 import android.database.Cursor; 9 import android.database.Cursor;
10 import android.net.Uri; 10 import android.net.Uri;
11 import android.os.ParcelFileDescriptor; 11 import android.os.ParcelFileDescriptor;
12 import android.util.Log; 12 import android.util.Log;
13 13
14 import java.io.File; 14 import java.io.File;
15 import java.io.FileNotFoundException;
15 16
16 /** 17 /**
17 * This class provides methods to access content URI schemes. 18 * This class provides methods to access content URI schemes.
18 */ 19 */
19 public abstract class ContentUriUtils { 20 public abstract class ContentUriUtils {
20 private static final String TAG = "ContentUriUtils"; 21 private static final String TAG = "ContentUriUtils";
21 private static FileProviderUtil sFileProviderUtil; 22 private static FileProviderUtil sFileProviderUtil;
22 23
23 /** 24 /**
24 * Provides functionality to translate a file into a content URI for use 25 * Provides functionality to translate a file into a content URI for use
25 * with a content provider. 26 * with a content provider.
26 */ 27 */
27 public interface FileProviderUtil { 28 public interface FileProviderUtil {
28 /** 29 /**
29 * Generate a content uri from the given file. 30 * Generate a content URI from the given file.
30 * @param context Application context. 31 * @param context Application context.
31 * @param file The file to be translated. 32 * @param file The file to be translated.
32 */ 33 */
33 public Uri getContentUriFromFile(Context context, File file); 34 Uri getContentUriFromFile(Context context, File file);
34 } 35 }
35 36
36 // Prevent instantiation. 37 // Prevent instantiation.
37 private ContentUriUtils() {} 38 private ContentUriUtils() {}
38 39
39 public static void setFileProviderUtil(FileProviderUtil util) { 40 public static void setFileProviderUtil(FileProviderUtil util) {
40 sFileProviderUtil = util; 41 sFileProviderUtil = util;
41 } 42 }
42 43
43 public static Uri getContentUriFromFile(Context context, File file) { 44 public static Uri getContentUriFromFile(Context context, File file) {
44 ThreadUtils.assertOnUiThread(); 45 ThreadUtils.assertOnUiThread();
45 if (sFileProviderUtil != null) { 46 if (sFileProviderUtil != null) {
46 return sFileProviderUtil.getContentUriFromFile(context, file); 47 return sFileProviderUtil.getContentUriFromFile(context, file);
47 } 48 }
48 return null; 49 return null;
49 } 50 }
50 51
51 /** 52 /**
52 * Opens the content URI for reading, and returns the file descriptor to 53 * Opens the content URI for reading, and returns the file descriptor to
53 * the caller. The caller is responsible for closing the file desciptor. 54 * the caller. The caller is responsible for closing the file desciptor.
54 * 55 *
55 * @param context {@link Context} in interest 56 * @param context {@link Context} in interest
56 * @param uriString the content URI to open 57 * @param uriString the content URI to open
57 * @returns file desciptor upon sucess, or -1 otherwise. 58 * @return file desciptor upon sucess, or -1 otherwise.
58 */ 59 */
59 @CalledByNative 60 @CalledByNative
60 public static int openContentUriForRead(Context context, String uriString) { 61 public static int openContentUriForRead(Context context, String uriString) {
61 ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); 62 ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString);
62 if (pfd != null) { 63 if (pfd != null) {
63 return pfd.detachFd(); 64 return pfd.detachFd();
64 } 65 }
65 return -1; 66 return -1;
66 } 67 }
67 68
68 /** 69 /**
69 * Check whether a content URI exists. 70 * Check whether a content URI exists.
70 * 71 *
71 * @param context {@link Context} in interest. 72 * @param context {@link Context} in interest.
72 * @param uriString the content URI to query. 73 * @param uriString the content URI to query.
73 * @returns true if the uri exists, or false otherwise. 74 * @return true if the URI exists, or false otherwise.
74 */ 75 */
75 @CalledByNative 76 @CalledByNative
76 public static boolean contentUriExists(Context context, String uriString) { 77 public static boolean contentUriExists(Context context, String uriString) {
77 ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString); 78 return getParcelFileDescriptor(context, uriString) != null;
78 if (pfd == null) {
79 return false;
80 }
81 return true;
82 } 79 }
83 80
84 /** 81 /**
85 * Helper method to open a content URI and return the ParcelFileDescriptor. 82 * Retrieve the MIME type for the content URI.
83 *
84 * @param context {@link Context} in interest.
85 * @param uriString the content URI to look up.
86 * @return MIME type or null if the input params are empty or invalid.
87 */
88 @CalledByNative
89 public static String getMimeType(Context context, String uriString) {
90 ContentResolver resolver = context.getContentResolver();
91 if (resolver == null) return null;
92 Uri uri = Uri.parse(uriString);
93 return resolver.getType(uri);
94 }
95
96 /**
97 * Helper method to open a content URI and returns the ParcelFileDescriptor.
86 * 98 *
87 * @param context {@link Context} in interest. 99 * @param context {@link Context} in interest.
88 * @param uriString the content URI to open. 100 * @param uriString the content URI to open.
89 * @returns ParcelFileDescriptor of the content URI, or NULL if the file doe s not exist. 101 * @return ParcelFileDescriptor of the content URI, or NULL if the file does not exist.
90 */ 102 */
91 private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) { 103 private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) {
92 ContentResolver resolver = context.getContentResolver(); 104 ContentResolver resolver = context.getContentResolver();
93 Uri uri = Uri.parse(uriString); 105 Uri uri = Uri.parse(uriString);
94 106
95 ParcelFileDescriptor pfd = null; 107 ParcelFileDescriptor pfd = null;
96 try { 108 try {
97 pfd = resolver.openFileDescriptor(uri, "r"); 109 pfd = resolver.openFileDescriptor(uri, "r");
98 } catch (java.io.FileNotFoundException e) { 110 } catch (FileNotFoundException e) {
99 Log.w(TAG, "Cannot find content uri: " + uriString, e); 111 Log.w(TAG, "Cannot find content uri: " + uriString, e);
112 } catch (SecurityException e) {
113 Log.w(TAG, "Cannot open content uri: " + uriString, e);
114 } catch (IllegalArgumentException e) {
115 Log.w(TAG, "Unknown content uri: " + uriString, e);
100 } 116 }
101 return pfd; 117 return pfd;
102 } 118 }
103 119
104 /** 120 /**
105 * Method to resolve the display name of a content URI. 121 * Method to resolve the display name of a content URI.
106 * 122 *
107 * @param uri the content URI to be resolved. 123 * @param uri the content URI to be resolved.
108 * @param contentResolver the content resolver to query. 124 * @param contentResolver the content resolver to query.
109 * @param columnField the column field to query. 125 * @param columnField the column field to query.
110 * @returns the display name of the @code uri if present in the database 126 * @return the display name of the @code uri if present in the database
111 * or an empty string otherwise. 127 * or an empty string otherwise.
112 */ 128 */
113 public static String getDisplayName( 129 public static String getDisplayName(
114 Uri uri, ContentResolver contentResolver, String columnField) { 130 Uri uri, ContentResolver contentResolver, String columnField) {
115 if (contentResolver == null || uri == null) return ""; 131 if (contentResolver == null || uri == null) return "";
116 Cursor cursor = null; 132 Cursor cursor = null;
117 try { 133 try {
118 cursor = contentResolver.query(uri, null, null, null, null); 134 cursor = contentResolver.query(uri, null, null, null, null);
119 135
120 if (cursor != null && cursor.getCount() >= 1) { 136 if (cursor != null && cursor.getCount() >= 1) {
121 cursor.moveToFirst(); 137 cursor.moveToFirst();
122 int index = cursor.getColumnIndex(columnField); 138 int index = cursor.getColumnIndex(columnField);
123 if (index > -1) return cursor.getString(index); 139 if (index > -1) return cursor.getString(index);
124 } 140 }
125 } catch (NullPointerException e) { 141 } catch (NullPointerException e) {
126 // Some android models don't handle the provider call correctly. 142 // Some android models don't handle the provider call correctly.
127 // see crbug.com/345393 143 // see crbug.com/345393
128 return ""; 144 return "";
129 } finally { 145 } finally {
130 if (cursor != null) cursor.close(); 146 if (cursor != null) cursor.close();
131 } 147 }
132 return ""; 148 return "";
133 } 149 }
134 } 150 }
OLDNEW
« no previous file with comments | « base/android/content_uri_utils_unittest.cc ('k') | base/base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698