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

Unified Diff: net/android/network_library.cc

Issue 10171009: Update net/android/network_library.cc with fresher code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added comments, fixed caller Created 8 years, 8 months 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
Index: net/android/network_library.cc
diff --git a/net/android/network_library.cc b/net/android/network_library.cc
index 6fbcd30dcc85eb27acf45d4edc81412b8d9345a5..0d9b760df492e3a57ef8f3579d2a885bdb5e1f6f 100644
--- a/net/android/network_library.cc
+++ b/net/android/network_library.cc
@@ -4,19 +4,19 @@
#include "net/android/network_library.h"
-#include "base/android/auto_jobject.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
#include "base/logging.h"
#include "jni/android_network_library_jni.h"
using base::android::AttachCurrentThread;
-using base::android::AutoJObject;
-using base::android::CheckException;
+using base::android::ClearException;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
using base::android::GetApplicationContext;
+using base::android::ScopedJavaLocalRef;
using base::android::ToJavaArrayOfByteArray;
using base::android::ToJavaByteArray;
@@ -24,41 +24,28 @@ namespace net {
namespace android {
VerifyResult VerifyX509CertChain(const std::vector<std::string>& cert_chain,
- const std::string& hostname,
const std::string& auth_type) {
JNIEnv* env = AttachCurrentThread();
- if (!env) {
- // TODO(husky): Maybe initialize the JVM in unit tests?
+ if (!env || !g_AndroidNetworkLibrary_verifyServerCertificates) {
+ // TODO(bulach): Remove when we initialize the JVM in unit tests.
LOG(WARNING) << "JNI initialization failed";
return VERIFY_INVOCATION_ERROR;
}
- AutoJObject chain_byte_array = AutoJObject::FromLocalRef(
- env, ToJavaArrayOfByteArray(env, cert_chain));
- DCHECK(chain_byte_array.obj());
+ ScopedJavaLocalRef<jobjectArray> chain_byte_array =
+ ToJavaArrayOfByteArray(env, cert_chain);
+ DCHECK(!chain_byte_array.is_null());
- AutoJObject host_string = AutoJObject::FromLocalRef(
- env, ConvertUTF8ToJavaString(env, hostname));
- DCHECK(host_string.obj());
+ ScopedJavaLocalRef<jstring> auth_string =
+ ConvertUTF8ToJavaString(env, auth_type);
+ DCHECK(!auth_string.is_null());
- AutoJObject auth_string = AutoJObject::FromLocalRef(
- env, ConvertUTF8ToJavaString(env, auth_type));
- DCHECK(auth_string.obj());
-
- jint error = Java_AndroidNetworkLibrary_verifyServerCertificates(
- env, static_cast<jobjectArray>(chain_byte_array.obj()),
- static_cast<jstring>(host_string.obj()),
- static_cast<jstring>(auth_string.obj()));
+ jboolean trusted = Java_AndroidNetworkLibrary_verifyServerCertificates(
+ env, chain_byte_array.obj(), auth_string.obj());
+ if (ClearException(env))
+ return VERIFY_INVOCATION_ERROR;
- switch (error) {
- case 0:
- return VERIFY_OK;
- case 1:
- return VERIFY_BAD_HOSTNAME;
- case 2:
- return VERIFY_NO_TRUSTED_ROOT;
- }
- return VERIFY_INVOCATION_ERROR;
+ return trusted ? VERIFY_OK : VERIFY_NO_TRUSTED_ROOT;
}
bool StoreKeyPair(const uint8* public_key,
@@ -66,36 +53,32 @@ bool StoreKeyPair(const uint8* public_key,
const uint8* private_key,
size_t private_len) {
JNIEnv* env = AttachCurrentThread();
- AutoJObject public_array = AutoJObject::FromLocalRef(
- env, ToJavaByteArray(env, public_key, public_len));
- AutoJObject private_array = AutoJObject::FromLocalRef(
- env, ToJavaByteArray(env, private_key, private_len));
+ ScopedJavaLocalRef<jbyteArray> public_array =
+ ToJavaByteArray(env, public_key, public_len);
+ ScopedJavaLocalRef<jbyteArray> private_array =
+ ToJavaByteArray(env, private_key, private_len);
jboolean ret = Java_AndroidNetworkLibrary_storeKeyPair(env,
- GetApplicationContext(),
- static_cast<jbyteArray>(public_array.obj()),
- static_cast<jbyteArray>(private_array.obj()));
- if (CheckException(env) || !ret) {
- LOG(WARNING) << "Call to Java_AndroidNetworkLibrary_storeKeyPair failed";
- return false;
- }
- return true;
+ GetApplicationContext(), public_array.obj(), private_array.obj());
+ LOG_IF(WARNING, !ret) <<
+ "Call to Java_AndroidNetworkLibrary_storeKeyPair failed";
+ return ret;
}
bool GetMimeTypeFromExtension(const std::string& extension,
std::string* result) {
JNIEnv* env = AttachCurrentThread();
- AutoJObject extension_string = AutoJObject::FromLocalRef(
- env, ConvertUTF8ToJavaString(env, extension));
- AutoJObject ret = AutoJObject::FromLocalRef(
- env, Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
- env, static_cast<jstring>(extension_string.obj())));
+ ScopedJavaLocalRef<jstring> extension_string =
+ ConvertUTF8ToJavaString(env, extension);
+ ScopedJavaLocalRef<jstring> ret =
+ Java_AndroidNetworkLibrary_getMimeTypeFromExtension(
+ env, extension_string.obj());
- if (CheckException(env) || !ret.obj()) {
+ if (!ret.obj()) {
LOG(WARNING) << "Call to getMimeTypeFromExtension failed";
return false;
}
- *result = ConvertJavaStringToUTF8(env, static_cast<jstring>(ret.obj()));
+ *result = ConvertJavaStringToUTF8(ret);
return true;
}
@@ -105,5 +88,3 @@ bool RegisterNetworkLibrary(JNIEnv* env) {
} // namespace android
} // namespace net
-
-

Powered by Google App Engine
This is Rietveld 408576698