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

Unified Diff: base/android/java/src/org/chromium/base/ContentUriUtils.java

Issue 46303005: Fix chrome upload with content uri (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/android/content_uri_utils.cc ('k') | base/base.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/ContentUriUtils.java
diff --git a/base/android/java/src/org/chromium/base/ContentUriUtils.java b/base/android/java/src/org/chromium/base/ContentUriUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..c639396a19a26c1125484d6df8d27354a5f71bc7
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/ContentUriUtils.java
@@ -0,0 +1,76 @@
+// Copyright (c) 2013 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.
+
+package org.chromium.base;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.net.Uri;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import org.chromium.base.CalledByNative;
+
+/**
+ * This class provides methods to access content URI schemes.
+ */
+abstract class ContentUriUtils {
+ private static final String TAG = "ContentUriUtils";
+
+ // Prevent instantiation.
+ private ContentUriUtils() {}
+
+ /**
+ * Opens the content URI for reading, and returns the file descriptor to
+ * the caller. The caller is responsible for closing the file desciptor.
+ *
+ * @param context {@link Context} in interest
+ * @param uriString the content URI to open
+ * @returns file desciptor upon sucess, or -1 otherwise.
+ */
+ @CalledByNative
+ public static int openContentUriForRead(Context context, String uriString) {
+ ParcelFileDescriptor pfd = getParcelFileDescriptor(context, uriString);
+ if (pfd != null) {
+ return pfd.detachFd();
+ }
+ return -1;
+ }
+
+ /**
+ * Check whether a content URI exists.
+ *
+ * @param context {@link Context} in interest.
+ * @param uriString the content URI to query.
+ * @returns 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;
+ }
+
+ /**
+ * Helper method to open a content URI and return 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.
+ */
+ private static ParcelFileDescriptor getParcelFileDescriptor(Context context, String uriString) {
+ ContentResolver resolver = context.getContentResolver();
+ Uri uri = Uri.parse(uriString);
+
+ ParcelFileDescriptor pfd = null;
+ try {
+ pfd = resolver.openFileDescriptor(uri, "r");
+ } catch (java.io.FileNotFoundException e) {
+ Log.w(TAG, "Cannot find content uri: " + uriString, e);
+ }
+ return pfd;
+ }
+}
« no previous file with comments | « base/android/content_uri_utils.cc ('k') | base/base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698