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