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 3 // found in the LICENSE file. 4 5 #include "base/android/command_line_android.h" 6 7 #include "base/android/jni_array.h" 8 #include "base/android/jni_string.h" 9 #include "base/command_line.h" 10 #include "base/logging.h" 11 #include "jni/CommandLine_jni.h" 12 13 using base::android::ConvertUTF8ToJavaString; 14 using base::android::ConvertJavaStringToUTF8; 15 using base::android::JavaParamRef; 16 using base::android::ScopedJavaLocalRef; 17 using base::CommandLine; 18 19 namespace { 20 21 void AppendJavaStringArrayToCommandLine(JNIEnv* env, 22 jobjectArray array, 23 bool includes_program) { 24 std::vector<std::string> vec; 25 if (array) 26 base::android::AppendJavaStringArrayToStringVector(env, array, &vec); 27 if (!includes_program) 28 vec.insert(vec.begin(), ""); 29 CommandLine extra_command_line(vec); 30 CommandLine::ForCurrentProcess()->AppendArguments(extra_command_line, 31 includes_program); 32 } 33 34 } // namespace 35 36 static void Reset(JNIEnv* env, const JavaParamRef<jclass>& clazz) { 37 CommandLine::Reset(); 38 } 39 40 static jboolean HasSwitch(JNIEnv* env, 41 const JavaParamRef<jclass>& clazz, 42 const JavaParamRef<jstring>& jswitch) { 43 std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); 44 return CommandLine::ForCurrentProcess()->HasSwitch(switch_string); 45 } 46 47 static ScopedJavaLocalRef<jstring> GetSwitchValue( 48 JNIEnv* env, 49 const JavaParamRef<jclass>& clazz, 50 const JavaParamRef<jstring>& jswitch) { 51 std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); 52 std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueNative( 53 switch_string)); 54 if (value.empty()) 55 return ScopedJavaLocalRef<jstring>(); 56 return ConvertUTF8ToJavaString(env, value); 57 } 58 59 static void AppendSwitch(JNIEnv* env, 60 const JavaParamRef<jclass>& clazz, 61 const JavaParamRef<jstring>& jswitch) { 62 std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); 63 CommandLine::ForCurrentProcess()->AppendSwitch(switch_string); 64 } 65 66 static void AppendSwitchWithValue(JNIEnv* env, 67 const JavaParamRef<jclass>& clazz, 68 const JavaParamRef<jstring>& jswitch, 69 const JavaParamRef<jstring>& jvalue) { 70 std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); 71 std::string value_string (ConvertJavaStringToUTF8(env, jvalue)); 72 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string, 73 value_string); 74 } 75 76 static void AppendSwitchesAndArguments( 77 JNIEnv* env, 78 const JavaParamRef<jclass>& clazz, 79 const JavaParamRef<jobjectArray>& array) { 80 AppendJavaStringArrayToCommandLine(env, array, false); 81 } 82 83 namespace base { 84 namespace android { 85 86 void InitNativeCommandLineFromJavaArray(JNIEnv* env, jobjectArray array) { 87 // TODO(port): Make an overload of Init() that takes StringVector rather than 88 // have to round-trip via AppendArguments. 89 CommandLine::Init(0, NULL); 90 AppendJavaStringArrayToCommandLine(env, array, true); 91 } 92 93 bool RegisterCommandLine(JNIEnv* env) { 94 return RegisterNativesImpl(env); 95 } 96 97 } // namespace android 98 } // namespace base 99