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

Side by Side Diff: base/android/jni_string.cc

Issue 9584014: Remove env() getter from JavaRef<> (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: re-applied downstream patch after it was re-worked Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/android/scoped_java_ref.h » ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "base/android/jni_string.h" 5 #include "base/android/jni_string.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 10
(...skipping 11 matching lines...) Expand all
22 namespace base { 22 namespace base {
23 namespace android { 23 namespace android {
24 24
25 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) { 25 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) {
26 // JNI's GetStringUTFChars() returns strings in Java-modified UTF8, so we 26 // JNI's GetStringUTFChars() returns strings in Java-modified UTF8, so we
27 // instead get the String in UTF16 and convert using our own utility function. 27 // instead get the String in UTF16 and convert using our own utility function.
28 return UTF16ToUTF8(ConvertJavaStringToUTF16(env, str)); 28 return UTF16ToUTF8(ConvertJavaStringToUTF16(env, str));
29 } 29 }
30 30
31 std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) { 31 std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) {
32 return ConvertJavaStringToUTF8(str.env(), str.obj()); 32 return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj());
33 } 33 }
34 34
35 ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString( 35 ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(
36 JNIEnv* env, const base::StringPiece& str) { 36 JNIEnv* env, const base::StringPiece& str) {
37 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string 37 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string
38 // via our own UTF16 conversion utility. 38 // via our own UTF16 conversion utility.
39 // Further, Dalvik requires the string passed into NewStringUTF() to come from 39 // Further, Dalvik requires the string passed into NewStringUTF() to come from
40 // a trusted source. We can't guarantee that all UTF8 will be sanitized before 40 // a trusted source. We can't guarantee that all UTF8 will be sanitized before
41 // it gets here, so constructing via UTF16 side-steps this issue. 41 // it gets here, so constructing via UTF16 side-steps this issue.
42 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be 42 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be
43 // a significant performance hit by doing it this way). 43 // a significant performance hit by doing it this way).
44 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( 44 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
45 env, UTF8ToUTF16(str))); 45 env, UTF8ToUTF16(str)));
46 } 46 }
47 47
48 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { 48 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) {
49 const jchar* chars = env->GetStringChars(str, NULL); 49 const jchar* chars = env->GetStringChars(str, NULL);
50 DCHECK(chars); 50 DCHECK(chars);
51 // GetStringChars isn't required to NULL-terminate the strings 51 // GetStringChars isn't required to NULL-terminate the strings
52 // it returns, so the length must be explicitly checked. 52 // it returns, so the length must be explicitly checked.
53 string16 result(chars, env->GetStringLength(str)); 53 string16 result(chars, env->GetStringLength(str));
54 env->ReleaseStringChars(str, chars); 54 env->ReleaseStringChars(str, chars);
55 CheckException(env); 55 CheckException(env);
56 return result; 56 return result;
57 } 57 }
58 58
59 string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) { 59 string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) {
60 return ConvertJavaStringToUTF16(str.env(), str.obj()); 60 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj());
61 } 61 }
62 62
63 // TODO(joth): change this to accept const StringPiece16&. 63 // TODO(joth): change this to accept const StringPiece16&.
64 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(JNIEnv* env, 64 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(JNIEnv* env,
65 const string16& str) { 65 const string16& str) {
66 return ScopedJavaLocalRef<jstring>(env, 66 return ScopedJavaLocalRef<jstring>(env,
67 ConvertUTF16ToJavaStringImpl(env, str)); 67 ConvertUTF16ToJavaStringImpl(env, str));
68 } 68 }
69 69
70 } // namespace android 70 } // namespace android
71 } // namespace base 71 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/android/scoped_java_ref.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698