1 // Copyright 2013 The Chromium Authors
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 "build/robolectric_buildflags.h"
9
10 #if BUILDFLAG(IS_ROBOLECTRIC)
11 #include "base/base_robolectric_jni/CommandLine_jni.h" // nogncheck
12 #else
13 #include "base/base_jni/CommandLine_jni.h"
14 #endif
15
16 using base::android::ConvertUTF8ToJavaString;
17 using base::android::ConvertJavaStringToUTF8;
18 using base::android::JavaParamRef;
19 using base::android::ScopedJavaLocalRef;
20 using base::CommandLine;
21
22 namespace {
23
AppendToCommandLine(std::vector<std::string> & vec,bool includes_program)24 void AppendToCommandLine(std::vector<std::string>& vec, bool includes_program) {
25 if (!includes_program)
26 vec.insert(vec.begin(), std::string());
27 CommandLine extra_command_line(vec);
28 CommandLine::ForCurrentProcess()->AppendArguments(extra_command_line,
29 includes_program);
30 }
31
32 } // namespace
33
JNI_CommandLine_HasSwitch(JNIEnv * env,std::string & switch_string)34 static jboolean JNI_CommandLine_HasSwitch(JNIEnv* env,
35 std::string& switch_string) {
36 return CommandLine::ForCurrentProcess()->HasSwitch(switch_string);
37 }
38
JNI_CommandLine_GetSwitchValue(JNIEnv * env,std::string & switch_string)39 static std::string JNI_CommandLine_GetSwitchValue(JNIEnv* env,
40 std::string& switch_string) {
41 return CommandLine::ForCurrentProcess()->GetSwitchValueNative(switch_string);
42 }
43
JNI_CommandLine_GetSwitchesFlattened(JNIEnv * env)44 static std::vector<std::string> JNI_CommandLine_GetSwitchesFlattened(
45 JNIEnv* env) {
46 // JNI doesn't support returning Maps. Instead, express this map as a 1
47 // dimensional array: [ key1, value1, key2, value2, ... ]
48 std::vector<std::string> keys_and_values;
49 for (const auto& entry : CommandLine::ForCurrentProcess()->GetSwitches()) {
50 keys_and_values.push_back(entry.first);
51 keys_and_values.push_back(entry.second);
52 }
53 return keys_and_values;
54 }
55
JNI_CommandLine_AppendSwitch(JNIEnv * env,std::string & switch_string)56 static void JNI_CommandLine_AppendSwitch(JNIEnv* env,
57 std::string& switch_string) {
58 CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
59 }
60
JNI_CommandLine_AppendSwitchWithValue(JNIEnv * env,std::string & switch_string,std::string & value_string)61 static void JNI_CommandLine_AppendSwitchWithValue(JNIEnv* env,
62 std::string& switch_string,
63 std::string& value_string) {
64 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string,
65 value_string);
66 }
67
JNI_CommandLine_AppendSwitchesAndArguments(JNIEnv * env,std::vector<std::string> & vec)68 static void JNI_CommandLine_AppendSwitchesAndArguments(
69 JNIEnv* env,
70 std::vector<std::string>& vec) {
71 AppendToCommandLine(vec, false);
72 }
73
JNI_CommandLine_RemoveSwitch(JNIEnv * env,std::string & switch_string)74 static void JNI_CommandLine_RemoveSwitch(JNIEnv* env,
75 std::string& switch_string) {
76 CommandLine::ForCurrentProcess()->RemoveSwitch(switch_string);
77 }
78
JNI_CommandLine_Init(JNIEnv * env,std::vector<std::string> & init_command_line)79 static void JNI_CommandLine_Init(JNIEnv* env,
80 std::vector<std::string>& init_command_line) {
81 // TODO(port): Make an overload of Init() that takes StringVector rather than
82 // have to round-trip via AppendArguments.
83 CommandLine::Init(0, nullptr);
84 AppendToCommandLine(init_command_line, true);
85 }
86