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.
5 #include "base/android/command_line_android.h"
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"
13 using base::android::ConvertUTF8ToJavaString
;
14 using base::android::ConvertJavaStringToUTF8
;
18 void AppendJavaStringArrayToCommandLine(JNIEnv
* env
,
20 bool includes_program
) {
21 std::vector
<std::string
> vec
;
23 base::android::AppendJavaStringArrayToStringVector(env
, array
, &vec
);
24 if (!includes_program
)
25 vec
.insert(vec
.begin(), "");
26 CommandLine
extra_command_line(vec
);
27 CommandLine::ForCurrentProcess()->AppendArguments(extra_command_line
,
33 static void Reset(JNIEnv
* env
, jclass clazz
) {
37 static jboolean
HasSwitch(JNIEnv
* env
, jclass clazz
, jstring jswitch
) {
38 std::string
switch_string(ConvertJavaStringToUTF8(env
, jswitch
));
39 return CommandLine::ForCurrentProcess()->HasSwitch(switch_string
);
42 static jstring
GetSwitchValue(JNIEnv
* env
, jclass clazz
, jstring jswitch
) {
43 std::string
switch_string(ConvertJavaStringToUTF8(env
, jswitch
));
44 std::string
value(CommandLine::ForCurrentProcess()->GetSwitchValueNative(
48 // OK to release, JNI binding.
49 return ConvertUTF8ToJavaString(env
, value
).Release();
52 static void AppendSwitch(JNIEnv
* env
, jclass clazz
, jstring jswitch
) {
53 std::string
switch_string(ConvertJavaStringToUTF8(env
, jswitch
));
54 CommandLine::ForCurrentProcess()->AppendSwitch(switch_string
);
57 static void AppendSwitchWithValue(JNIEnv
* env
, jclass clazz
,
58 jstring jswitch
, jstring jvalue
) {
59 std::string
switch_string(ConvertJavaStringToUTF8(env
, jswitch
));
60 std::string
value_string (ConvertJavaStringToUTF8(env
, jvalue
));
61 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string
,
65 static void AppendSwitchesAndArguments(JNIEnv
* env
, jclass clazz
,
67 AppendJavaStringArrayToCommandLine(env
, array
, false);
73 void InitNativeCommandLineFromJavaArray(JNIEnv
* env
, jobjectArray array
) {
74 // TODO(port): Make an overload of Init() that takes StringVector rather than
75 // have to round-trip via AppendArguments.
76 CommandLine::Init(0, NULL
);
77 AppendJavaStringArrayToCommandLine(env
, array
, true);
80 bool RegisterCommandLine(JNIEnv
* env
) {
81 return RegisterNativesImpl(env
);
84 } // namespace android