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/command_line_android.h"
6
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"
12
13using base::android::ConvertUTF8ToJavaString;
14using base::android::ConvertJavaStringToUTF8;
15
16namespace {
17
18void AppendJavaStringArrayToCommandLine(JNIEnv* env,
19                                        jobjectArray array,
20                                        bool includes_program) {
21  std::vector<std::string> vec;
22  if (array)
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,
28                                                    includes_program);
29}
30
31}  // namespace
32
33static void Reset(JNIEnv* env, jclass clazz) {
34  CommandLine::Reset();
35}
36
37static jboolean HasSwitch(JNIEnv* env, jclass clazz, jstring jswitch) {
38  std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
39  return CommandLine::ForCurrentProcess()->HasSwitch(switch_string);
40}
41
42static jstring GetSwitchValue(JNIEnv* env, jclass clazz, jstring jswitch) {
43  std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
44  std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueNative(
45      switch_string));
46  if (value.empty())
47    return 0;
48  // OK to release, JNI binding.
49  return ConvertUTF8ToJavaString(env, value).Release();
50}
51
52static void AppendSwitch(JNIEnv* env, jclass clazz, jstring jswitch) {
53  std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
54  CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
55}
56
57static 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,
62                                                      value_string);
63}
64
65static void AppendSwitchesAndArguments(JNIEnv* env, jclass clazz,
66                                       jobjectArray array) {
67  AppendJavaStringArrayToCommandLine(env, array, false);
68}
69
70namespace base {
71namespace android {
72
73void 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);
78}
79
80bool RegisterCommandLine(JNIEnv* env) {
81  return RegisterNativesImpl(env);
82}
83
84}  // namespace android
85}  // namespace base
86