Home | History | Annotate | Download | only in jni
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      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 "android/jni/mime_utils.h"
      6 
      7 #include "android/jni/jni_utils.h"
      8 
      9 // Accessors for the various JNI binding objects.
     10 namespace {
     11 
     12 static jclass get_class(JNIEnv* env) {
     13   static jclass clazz = env->FindClass("libcore/net/MimeUtils");
     14   return clazz;
     15 }
     16 
     17 static jmethodID guess_mime_type_from_extension(JNIEnv* env) {
     18   static jmethodID method = env->GetStaticMethodID(get_class(env),
     19       "guessMimeTypeFromExtension", "(Ljava/lang/String;)Ljava/lang/String;");
     20   return method;
     21 }
     22 
     23 static jmethodID guess_extension_from_mime_type(JNIEnv* env) {
     24   static jmethodID method = env->GetStaticMethodID(get_class(env),
     25       "guessExtensionFromMimeType", "(Ljava/lang/String;)Ljava/lang/String;");
     26   return method;
     27 }
     28 
     29 } // namespace
     30 
     31 namespace android {
     32 
     33 bool MimeUtils::GuessMimeTypeFromExtension(const std::string& extension,
     34     std::string* result) {
     35   JNIEnv* env = jni::GetJNIEnv();
     36   jstring jExtension = env->NewStringUTF(extension.c_str());
     37   jobject jResult = env->CallStaticObjectMethod(
     38       get_class(env), guess_mime_type_from_extension(env), jExtension);
     39   env->DeleteLocalRef(jExtension);
     40   if (jResult) {
     41     *result = jni::JstringToStdString(env, static_cast<jstring>(jResult));
     42     env->DeleteLocalRef(jResult);
     43   }
     44   return jResult;
     45 }
     46 
     47 bool MimeUtils::GuessExtensionFromMimeType(const std::string& mimeType,
     48     std::string* result) {
     49   JNIEnv* env = jni::GetJNIEnv();
     50   jstring jMimeType = env->NewStringUTF(mimeType.c_str());
     51   jobject jResult = env->CallStaticObjectMethod(
     52       get_class(env), guess_extension_from_mime_type(env), jMimeType);
     53   env->DeleteLocalRef(jMimeType);
     54   if (jResult) {
     55     *result = jni::JstringToStdString(env, static_cast<jstring>(jResult));
     56     env->DeleteLocalRef(jResult);
     57   }
     58   return jResult;
     59 }
     60 
     61 } // namespace android
     62 
     63