OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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.base; | 5 package org.chromium.base; |
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; |
11 import java.io.FileInputStream; | 11 import java.io.FileInputStream; |
12 import java.io.FileNotFoundException; | 12 import java.io.FileNotFoundException; |
13 import java.io.IOException; | 13 import java.io.IOException; |
14 import java.io.InputStreamReader; | 14 import java.io.InputStreamReader; |
15 import java.io.Reader; | 15 import java.io.Reader; |
16 import java.util.ArrayList; | 16 import java.util.ArrayList; |
17 import java.util.Arrays; | 17 import java.util.Arrays; |
18 import java.util.HashMap; | 18 import java.util.HashMap; |
19 import java.util.List; | |
19 import java.util.concurrent.atomic.AtomicReference; | 20 import java.util.concurrent.atomic.AtomicReference; |
20 | 21 |
21 /** | 22 /** |
22 * Java mirror of base/command_line.h. | 23 * Java mirror of base/command_line.h. |
23 * Android applications don't have command line arguments. Instead, they're "sim ulated" by reading a | 24 * Android applications don't have command line arguments. Instead, they're "sim ulated" by reading a |
24 * file at a specific location early during startup. Applications each define th eir own files, e.g., | 25 * file at a specific location early during startup. Applications each define th eir own files, e.g., |
25 * ContentShellApplication.COMMAND_LINE_FILE or ChromeShellApplication.COMMAND_L INE_FILE. | 26 * ContentShellApplication.COMMAND_LINE_FILE or ChromeShellApplication.COMMAND_L INE_FILE. |
26 **/ | 27 **/ |
27 public abstract class CommandLine { | 28 public abstract class CommandLine { |
29 /** | |
30 * Allows classes who cache command line flags to be notified when those arg uments are updated | |
31 * at runtime. This happens in tests. | |
32 */ | |
33 public interface ResetListener { | |
34 /** Called when the command line arguments are reset. */ | |
35 void onCommandLineReset(); | |
36 } | |
37 | |
28 // Public abstract interface, implemented in derived classes. | 38 // Public abstract interface, implemented in derived classes. |
29 // All these methods reflect their native-side counterparts. | 39 // All these methods reflect their native-side counterparts. |
30 /** | 40 /** |
31 * Returns true if this command line contains the given switch. | 41 * Returns true if this command line contains the given switch. |
32 * (Switch names ARE case-sensitive). | 42 * (Switch names ARE case-sensitive). |
33 */ | 43 */ |
34 @VisibleForTesting | 44 @VisibleForTesting |
35 public abstract boolean hasSwitch(String switchString); | 45 public abstract boolean hasSwitch(String switchString); |
36 | 46 |
37 /** | 47 /** |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 public abstract void appendSwitchesAndArguments(String[] array); | 90 public abstract void appendSwitchesAndArguments(String[] array); |
81 | 91 |
82 /** | 92 /** |
83 * Determine if the command line is bound to the native (JNI) implementation . | 93 * Determine if the command line is bound to the native (JNI) implementation . |
84 * @return true if the underlying implementation is delegating to the native command line. | 94 * @return true if the underlying implementation is delegating to the native command line. |
85 */ | 95 */ |
86 public boolean isNativeImplementation() { | 96 public boolean isNativeImplementation() { |
87 return false; | 97 return false; |
88 } | 98 } |
89 | 99 |
100 private static final List<ResetListener> sResetListeners = new ArrayList<>() ; | |
90 private static final AtomicReference<CommandLine> sCommandLine = | 101 private static final AtomicReference<CommandLine> sCommandLine = |
91 new AtomicReference<CommandLine>(); | 102 new AtomicReference<CommandLine>(); |
92 | 103 |
93 /** | 104 /** |
94 * @returns true if the command line has already been initialized. | 105 * @returns true if the command line has already been initialized. |
95 */ | 106 */ |
96 public static boolean isInitialized() { | 107 public static boolean isInitialized() { |
97 return sCommandLine.get() != null; | 108 return sCommandLine.get() != null; |
98 } | 109 } |
99 | 110 |
(...skipping 25 matching lines...) Expand all Loading... | |
125 init(buffer == null ? null : tokenizeQuotedAruments(buffer)); | 136 init(buffer == null ? null : tokenizeQuotedAruments(buffer)); |
126 } | 137 } |
127 | 138 |
128 /** | 139 /** |
129 * Resets both the java proxy and the native command lines. This allows the entire | 140 * Resets both the java proxy and the native command lines. This allows the entire |
130 * command line initialization to be re-run including the call to onJniLoade d. | 141 * command line initialization to be re-run including the call to onJniLoade d. |
131 */ | 142 */ |
132 @VisibleForTesting | 143 @VisibleForTesting |
133 public static void reset() { | 144 public static void reset() { |
134 setInstance(null); | 145 setInstance(null); |
146 ThreadUtils.postOnUiThread(new Runnable() { | |
147 @Override | |
148 public void run() { | |
149 for (ResetListener listener : sResetListeners) listener.onComman dLineReset(); | |
150 } | |
151 }); | |
152 } | |
153 | |
154 public static void addResetListener(ResetListener listener) { | |
jdduke (slow)
2015/07/27 15:05:23
Hmm, I'd really like to understand use-cases that
dgn
2015/07/27 15:12:29
It should not be used out of tests, yes. An annota
| |
155 sResetListeners.add(listener); | |
156 } | |
157 | |
158 public static void removeResetListener(ResetListener listener) { | |
159 sResetListeners.remove(listener); | |
135 } | 160 } |
136 | 161 |
137 /** | 162 /** |
138 * Public for testing (TODO: why are the tests in a different package?) | 163 * Public for testing (TODO: why are the tests in a different package?) |
139 * Parse command line flags from a flat buffer, supporting double-quote encl osed strings | 164 * Parse command line flags from a flat buffer, supporting double-quote encl osed strings |
140 * containing whitespace. argv elements are derived by splitting the buffer on whitepace; | 165 * containing whitespace. argv elements are derived by splitting the buffer on whitepace; |
141 * double quote characters may enclose tokens containing whitespace; a doubl e-quote literal | 166 * double quote characters may enclose tokens containing whitespace; a doubl e-quote literal |
142 * may be escaped with back-slash. (Otherwise backslash is taken as a litera l). | 167 * may be escaped with back-slash. (Otherwise backslash is taken as a litera l). |
143 * @param buffer A command line in command line file format as described abo ve. | 168 * @param buffer A command line in command line file format as described abo ve. |
144 * @return the tokenized arguments, suitable for passing to init(). | 169 * @return the tokenized arguments, suitable for passing to init(). |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 } | 399 } |
375 } | 400 } |
376 | 401 |
377 private static native void nativeReset(); | 402 private static native void nativeReset(); |
378 private static native boolean nativeHasSwitch(String switchString); | 403 private static native boolean nativeHasSwitch(String switchString); |
379 private static native String nativeGetSwitchValue(String switchString); | 404 private static native String nativeGetSwitchValue(String switchString); |
380 private static native void nativeAppendSwitch(String switchString); | 405 private static native void nativeAppendSwitch(String switchString); |
381 private static native void nativeAppendSwitchWithValue(String switchString, String value); | 406 private static native void nativeAppendSwitchWithValue(String switchString, String value); |
382 private static native void nativeAppendSwitchesAndArguments(String[] array); | 407 private static native void nativeAppendSwitchesAndArguments(String[] array); |
383 } | 408 } |
OLD | NEW |