• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 using base::android::ConvertUTF8ToJavaString;
14 using base::android::ConvertJavaStringToUTF8;
15 using base::CommandLine;
16 
17 namespace {
18 
AppendJavaStringArrayToCommandLine(JNIEnv * env,jobjectArray array,bool includes_program)19 void AppendJavaStringArrayToCommandLine(JNIEnv* env,
20                                         jobjectArray array,
21                                         bool includes_program) {
22   std::vector<std::string> vec;
23   if (array)
24     base::android::AppendJavaStringArrayToStringVector(env, array, &vec);
25   if (!includes_program)
26     vec.insert(vec.begin(), "");
27   CommandLine extra_command_line(vec);
28   CommandLine::ForCurrentProcess()->AppendArguments(extra_command_line,
29                                                     includes_program);
30 }
31 
32 }  // namespace
33 
Reset(JNIEnv * env,const JavaParamRef<jclass> & clazz)34 static void Reset(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
35   CommandLine::Reset();
36 }
37 
HasSwitch(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)38 static jboolean HasSwitch(JNIEnv* env,
39                           const JavaParamRef<jclass>& clazz,
40                           const JavaParamRef<jstring>& jswitch) {
41   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
42   return CommandLine::ForCurrentProcess()->HasSwitch(switch_string);
43 }
44 
GetSwitchValue(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)45 static ScopedJavaLocalRef<jstring> GetSwitchValue(
46     JNIEnv* env,
47     const JavaParamRef<jclass>& clazz,
48     const JavaParamRef<jstring>& jswitch) {
49   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
50   std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueNative(
51       switch_string));
52   if (value.empty())
53     return ScopedJavaLocalRef<jstring>();
54   return ConvertUTF8ToJavaString(env, value);
55 }
56 
AppendSwitch(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch)57 static void AppendSwitch(JNIEnv* env,
58                          const JavaParamRef<jclass>& clazz,
59                          const JavaParamRef<jstring>& jswitch) {
60   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
61   CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
62 }
63 
AppendSwitchWithValue(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jstring> & jswitch,const JavaParamRef<jstring> & jvalue)64 static void AppendSwitchWithValue(JNIEnv* env,
65                                   const JavaParamRef<jclass>& clazz,
66                                   const JavaParamRef<jstring>& jswitch,
67                                   const JavaParamRef<jstring>& jvalue) {
68   std::string switch_string(ConvertJavaStringToUTF8(env, jswitch));
69   std::string value_string (ConvertJavaStringToUTF8(env, jvalue));
70   CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string,
71                                                       value_string);
72 }
73 
AppendSwitchesAndArguments(JNIEnv * env,const JavaParamRef<jclass> & clazz,const JavaParamRef<jobjectArray> & array)74 static void AppendSwitchesAndArguments(
75     JNIEnv* env,
76     const JavaParamRef<jclass>& clazz,
77     const JavaParamRef<jobjectArray>& array) {
78   AppendJavaStringArrayToCommandLine(env, array, false);
79 }
80 
81 namespace base {
82 namespace android {
83 
InitNativeCommandLineFromJavaArray(JNIEnv * env,jobjectArray array)84 void InitNativeCommandLineFromJavaArray(JNIEnv* env, jobjectArray array) {
85   // TODO(port): Make an overload of Init() that takes StringVector rather than
86   // have to round-trip via AppendArguments.
87   CommandLine::Init(0, NULL);
88   AppendJavaStringArrayToCommandLine(env, array, true);
89 }
90 
RegisterCommandLine(JNIEnv * env)91 bool RegisterCommandLine(JNIEnv* env) {
92   return RegisterNativesImpl(env);
93 }
94 
95 }  // namespace android
96 }  // namespace base
97