OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.common; | 5 package org.chromium.content.common; |
6 | 6 |
7 import android.text.TextUtils; | 7 import android.text.TextUtils; |
8 import android.util.Log; | 8 import android.util.Log; |
9 | 9 |
10 import java.io.File; | 10 import java.io.File; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 /** | 119 /** |
120 * @returns true if the command line has already been initialized. | 120 * @returns true if the command line has already been initialized. |
121 */ | 121 */ |
122 public static boolean isInitialized() { | 122 public static boolean isInitialized() { |
123 return sCommandLine.get() != null; | 123 return sCommandLine.get() != null; |
124 } | 124 } |
125 | 125 |
126 // Equivalent to CommandLine::ForCurrentProcess in C++. | 126 // Equivalent to CommandLine::ForCurrentProcess in C++. |
127 public static CommandLine getInstance() { | 127 public static CommandLine getInstance() { |
128 assert sCommandLine.get() != null; | 128 CommandLine commandLine = sCommandLine.get(); |
129 return sCommandLine.get(); | 129 assert commandLine != null; |
| 130 return commandLine; |
130 } | 131 } |
131 | 132 |
132 /** | 133 /** |
133 * Initialize the singleton instance, must be called exactly once (either di
rectly or | 134 * Initialize the singleton instance, must be called exactly once (either di
rectly or |
134 * via one of the convenience wrappers below) before using the static single
ton instance. | 135 * via one of the convenience wrappers below) before using the static single
ton instance. |
135 * @param args command line flags in 'argv' format: args[0] is the program n
ame. | 136 * @param args command line flags in 'argv' format: args[0] is the program n
ame. |
136 */ | 137 */ |
137 public static void init(String[] args) { | 138 public static void init(String[] args) { |
138 assert sCommandLine.get() == null; | 139 setInstance(new JavaCommandLine(args)); |
139 sCommandLine.compareAndSet(null, new JavaCommandLine(args)); | |
140 } | 140 } |
141 | 141 |
142 /** | 142 /** |
143 * Initialize the command line from the command-line file. | 143 * Initialize the command line from the command-line file. |
144 * | 144 * |
145 * @param file The fully qualified command line file. | 145 * @param file The fully qualified command line file. |
146 */ | 146 */ |
147 public static void initFromFile(String file) { | 147 public static void initFromFile(String file) { |
148 char[] buffer = new char[0]; | 148 char[] buffer = new char[0]; |
149 try { | 149 try { |
150 // Arbitrary clamp of 8k on the amount of file we read in. | 150 // Arbitrary clamp of 8k on the amount of file we read in. |
151 buffer = readUtf8FileFully(file, 8 * 1024); | 151 buffer = readUtf8FileFully(file, 8 * 1024); |
152 } catch (FileNotFoundException e) { | 152 } catch (FileNotFoundException e) { |
153 // Ignore: having a command line file is optional. | 153 // Ignore: having a command line file is optional. |
154 } catch (IOException e) { | 154 } catch (IOException e) { |
155 Log.w(TAG, "error reading command line file " + file + e); | 155 Log.w(TAG, "error reading command line file " + file + e); |
156 } | 156 } |
157 init(tokenizeQuotedAruments(buffer)); | 157 init(tokenizeQuotedAruments(buffer)); |
158 } | 158 } |
159 | 159 |
160 /** | 160 /** |
161 * Resets both the java proxy and the native command lines. This allows the
entire | 161 * Resets both the java proxy and the native command lines. This allows the
entire |
162 * command line initialization to be re-run including the call to onJniLoade
d. | 162 * command line initialization to be re-run including the call to onJniLoade
d. |
163 */ | 163 */ |
164 public static void reset() { | 164 public static void reset() { |
165 if (sCommandLine.get() != null && sCommandLine.get().isNativeImplementat
ion()) { | 165 setInstance(null); |
166 nativeReset(); | |
167 } | |
168 sCommandLine.set(null); | |
169 } | 166 } |
170 | 167 |
171 /** | 168 /** |
172 * Public for testing (TODO: why are the tests in a different package?) | 169 * Public for testing (TODO: why are the tests in a different package?) |
173 * Parse command line flags from a flat buffer, supporting double-quote encl
osed strings | 170 * Parse command line flags from a flat buffer, supporting double-quote encl
osed strings |
174 * containing whitespace. argv elements are derived by splitting the buffer
on whitepace; | 171 * containing whitespace. argv elements are derived by splitting the buffer
on whitepace; |
175 * double quote characters may enclose tokens containing whitespace; a doubl
e-quote literal | 172 * double quote characters may enclose tokens containing whitespace; a doubl
e-quote literal |
176 * may be escaped with back-slash. (Otherwise backslash is taken as a litera
l). | 173 * may be escaped with back-slash. (Otherwise backslash is taken as a litera
l). |
177 * @param buffer A command line in command line file format as described abo
ve. | 174 * @param buffer A command line in command line file format as described abo
ve. |
178 * @return the tokenized arguments, suitable for passing to init(). | 175 * @return the tokenized arguments, suitable for passing to init(). |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 219 |
223 public static String[] getJavaSwitchesOrNull() { | 220 public static String[] getJavaSwitchesOrNull() { |
224 CommandLine commandLine = sCommandLine.get(); | 221 CommandLine commandLine = sCommandLine.get(); |
225 if (commandLine != null) { | 222 if (commandLine != null) { |
226 assert !commandLine.isNativeImplementation(); | 223 assert !commandLine.isNativeImplementation(); |
227 return ((JavaCommandLine) commandLine).getCommandLineArguments(); | 224 return ((JavaCommandLine) commandLine).getCommandLineArguments(); |
228 } | 225 } |
229 return null; | 226 return null; |
230 } | 227 } |
231 | 228 |
| 229 private static void setInstance(CommandLine commandLine) { |
| 230 CommandLine oldCommandLine = sCommandLine.getAndSet(commandLine); |
| 231 if (oldCommandLine != null && oldCommandLine.isNativeImplementation()) { |
| 232 nativeReset(); |
| 233 } |
| 234 } |
| 235 |
232 /** | 236 /** |
233 * @param fileName the file to read in. | 237 * @param fileName the file to read in. |
234 * @param sizeLimit cap on the file size: will throw an exception if exceede
d | 238 * @param sizeLimit cap on the file size: will throw an exception if exceede
d |
235 * @return Array of chars read from the file | 239 * @return Array of chars read from the file |
236 * @throws FileNotFoundException file does not exceed | 240 * @throws FileNotFoundException file does not exceed |
237 * @throws IOException error encountered accessing the file | 241 * @throws IOException error encountered accessing the file |
238 */ | 242 */ |
239 private static char[] readUtf8FileFully(String fileName, int sizeLimit) thro
ws | 243 private static char[] readUtf8FileFully(String fileName, int sizeLimit) thro
ws |
240 FileNotFoundException, IOException { | 244 FileNotFoundException, IOException { |
241 Reader reader = null; | 245 Reader reader = null; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 385 } |
382 } | 386 } |
383 | 387 |
384 private static native void nativeReset(); | 388 private static native void nativeReset(); |
385 private static native boolean nativeHasSwitch(String switchString); | 389 private static native boolean nativeHasSwitch(String switchString); |
386 private static native String nativeGetSwitchValue(String switchString); | 390 private static native String nativeGetSwitchValue(String switchString); |
387 private static native void nativeAppendSwitch(String switchString); | 391 private static native void nativeAppendSwitch(String switchString); |
388 private static native void nativeAppendSwitchWithValue(String switchString,
String value); | 392 private static native void nativeAppendSwitchWithValue(String switchString,
String value); |
389 private static native void nativeAppendSwitchesAndArguments(String[] array); | 393 private static native void nativeAppendSwitchesAndArguments(String[] array); |
390 }; | 394 }; |
OLD | NEW |