| 6 | #include "base/android/jni_string.h" |
| 7 | #include "base/base_jni_headers/CommandLine_jni.h" |
| 8 | #include "base/command_line.h" |
| 9 | #include "base/logging.h" |
| 10 |
| 11 | using base::android::ConvertUTF8ToJavaString; |
| 12 | using base::android::ConvertJavaStringToUTF8; |
| 13 | using base::android::JavaParamRef; |
| 14 | using base::android::ScopedJavaLocalRef; |
| 15 | using base::CommandLine; |
| 16 |
| 17 | namespace { |
| 18 |
| 19 | void JNI_CommandLine_AppendJavaStringArrayToCommandLine( |
| 20 | JNIEnv* env, |
| 21 | const JavaParamRef<jobjectArray>& array, |
| 22 | bool includes_program) { |
| 23 | std::vector<std::string> vec; |
| 24 | if (array) |
| 25 | base::android::AppendJavaStringArrayToStringVector(env, array, &vec); |
| 26 | if (!includes_program) |
| 27 | vec.insert(vec.begin(), std::string()); |
| 28 | CommandLine extra_command_line(vec); |
| 29 | CommandLine::ForCurrentProcess()->AppendArguments(extra_command_line, |
| 30 | includes_program); |
| 31 | } |
| 32 |
| 33 | } // namespace |
| 34 |
| 35 | static jboolean JNI_CommandLine_HasSwitch( |
| 36 | JNIEnv* env, |
| 37 | const JavaParamRef<jstring>& jswitch) { |
| 38 | std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); |
| 39 | return CommandLine::ForCurrentProcess()->HasSwitch(switch_string); |
| 40 | } |
| 41 |
| 42 | static ScopedJavaLocalRef<jstring> JNI_CommandLine_GetSwitchValue( |
| 43 | JNIEnv* env, |
| 44 | const JavaParamRef<jstring>& jswitch) { |
| 45 | std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); |
| 46 | std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueNative( |
| 47 | switch_string)); |
| 48 | if (value.empty()) |
| 49 | return ScopedJavaLocalRef<jstring>(); |
| 50 | return ConvertUTF8ToJavaString(env, value); |
| 51 | } |
| 52 |
| 53 | static ScopedJavaLocalRef<jobjectArray> JNI_CommandLine_GetSwitchesFlattened( |
| 54 | JNIEnv* env) { |
| 55 | // JNI doesn't support returning Maps. Instead, express this map as a 1 |
| 56 | // dimensional array: [ key1, value1, key2, value2, ... ] |
| 57 | std::vector<std::string> keys_and_values; |
| 58 | for (const auto& entry : CommandLine::ForCurrentProcess()->GetSwitches()) { |
| 59 | keys_and_values.push_back(entry.first); |
| 60 | keys_and_values.push_back(entry.second); |
| 61 | } |
| 62 | return base::android::ToJavaArrayOfStrings(env, keys_and_values); |
| 63 | } |
| 64 |
| 65 | static void JNI_CommandLine_AppendSwitch(JNIEnv* env, |
| 66 | const JavaParamRef<jstring>& jswitch) { |
| 67 | std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); |
| 68 | CommandLine::ForCurrentProcess()->AppendSwitch(switch_string); |
| 69 | } |
| 70 |
| 71 | static void JNI_CommandLine_AppendSwitchWithValue( |
| 72 | JNIEnv* env, |
| 73 | const JavaParamRef<jstring>& jswitch, |
| 74 | const JavaParamRef<jstring>& jvalue) { |
| 75 | std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); |
| 76 | std::string value_string(ConvertJavaStringToUTF8(env, jvalue)); |
| 77 | CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string, |
| 78 | value_string); |
| 79 | } |
| 80 |
| 81 | static void JNI_CommandLine_AppendSwitchesAndArguments( |
| 82 | JNIEnv* env, |
| 83 | const JavaParamRef<jobjectArray>& array) { |
| 84 | JNI_CommandLine_AppendJavaStringArrayToCommandLine(env, array, false); |
| 85 | } |
| 86 |
| 87 | static void JNI_CommandLine_RemoveSwitch(JNIEnv* env, |
| 88 | const JavaParamRef<jstring>& jswitch) { |
| 89 | std::string switch_string(ConvertJavaStringToUTF8(env, jswitch)); |
| 90 | CommandLine::ForCurrentProcess()->RemoveSwitch(switch_string); |
| 91 | } |
| 92 |
| 93 | static void JNI_CommandLine_Init( |
| 94 | JNIEnv* env, |
| 95 | const JavaParamRef<jobjectArray>& init_command_line) { |
| 96 | // TODO(port): Make an overload of Init() that takes StringVector rather than |
| 97 | // have to round-trip via AppendArguments. |
| 98 | CommandLine::Init(0, nullptr); |
| 99 | JNI_CommandLine_AppendJavaStringArrayToCommandLine(env, init_command_line, |
| 100 | true); |
| 101 | } |
| 102 |