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/jni_array.h"
6 #include "base/android/jni_string.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "jni/CommandLine_jni.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 
JNI_CommandLine_AppendJavaStringArrayToCommandLine(JNIEnv * env,const JavaParamRef<jobjectArray> & array,bool includes_program)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 
JNI_CommandLine_HasSwitch(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)35 static jboolean JNI_CommandLine_HasSwitch(
36     JNIEnv* env,
37     const JavaParamRef<jclass>& clazz,
38     const JavaParamRef<jstring>& jswitch) {
39   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
40   return CommandLine::ForCurrentProcess()->HasSwitch(switch_string);
41 }
42 
JNI_CommandLine_GetSwitchValue(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)43 static ScopedJavaLocalRef<jstring> JNI_CommandLine_GetSwitchValue(
44     JNIEnv* env,
45     const JavaParamRef<jclass>& clazz,
46     const JavaParamRef<jstring>& jswitch) {
47   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
48   std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueNative(
49       switch_string));
50   if (value.empty())
51     return ScopedJavaLocalRef<jstring>();
52   return ConvertUTF8ToJavaString(env, value);
53 }
54 
JNI_CommandLine_AppendSwitch(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)55 static void JNI_CommandLine_AppendSwitch(JNIEnv* env,
56                                          const JavaParamRef<jclass>& clazz,
57                                          const JavaParamRef<jstring>& jswitch) {
58   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
59   CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
60 }
61 
JNI_CommandLine_AppendSwitchWithValue(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch,const JavaParamRef<jstring> & jvalue)62 static void JNI_CommandLine_AppendSwitchWithValue(
63     JNIEnv* env,
64     const JavaParamRef<jclass>& clazz,
65     const JavaParamRef<jstring>& jswitch,
66     const JavaParamRef<jstring>& jvalue) {
67   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
68   std::string value_string (ConvertJavaStringToUTF8(env, jvalue));
69   CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string,
70                                                       value_string);
71 }
72 
JNI_CommandLine_AppendSwitchesAndArguments(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jobjectArray> & array)73 static void JNI_CommandLine_AppendSwitchesAndArguments(
74     JNIEnv* env,
75     const JavaParamRef<jclass>& clazz,
76     const JavaParamRef<jobjectArray>& array) {
77   JNI_CommandLine_AppendJavaStringArrayToCommandLine(env, array, false);
78 }
79 
JNI_CommandLine_Init(JNIEnv * env,const JavaParamRef<jclass> & jclazz,const JavaParamRef<jobjectArray> & init_command_line)80 static void JNI_CommandLine_Init(
81     JNIEnv* env,
82     const JavaParamRef<jclass>& jclazz,
83     const JavaParamRef<jobjectArray>& init_command_line) {
84   // TODO(port): Make an overload of Init() that takes StringVector rather than
85   // have to round-trip via AppendArguments.
86   CommandLine::Init(0, nullptr);
87   JNI_CommandLine_AppendJavaStringArrayToCommandLine(env, init_command_line,
88                                                      true);
89 }
90