Flutter Engine
The Flutter Engine
FlutterShellArgs.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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
5package io.flutter.embedding.engine;
6
7import android.content.Context;
8import android.content.Intent;
9import androidx.annotation.NonNull;
10import java.util.*;
11
12/**
13 * Arguments that can be delivered to the Flutter shell when it is created.
14 *
15 * <p>The term "shell" refers to the native code that adapts Flutter to different platforms.
16 * Flutter's Android Java code initializes a native "shell" and passes these arguments to that
17 * native shell when it is initialized. See {@link
18 * io.flutter.embedding.engine.loader.FlutterLoader#ensureInitializationComplete(Context, String[])}
19 * for more information.
20 */
21@SuppressWarnings({"WeakerAccess", "unused"})
22public class FlutterShellArgs {
23 public static final String ARG_KEY_TRACE_STARTUP = "trace-startup";
24 public static final String ARG_TRACE_STARTUP = "--trace-startup";
25 public static final String ARG_KEY_START_PAUSED = "start-paused";
26 public static final String ARG_START_PAUSED = "--start-paused";
27 public static final String ARG_KEY_DISABLE_SERVICE_AUTH_CODES = "disable-service-auth-codes";
28 public static final String ARG_DISABLE_SERVICE_AUTH_CODES = "--disable-service-auth-codes";
29 public static final String ARG_KEY_ENDLESS_TRACE_BUFFER = "endless-trace-buffer";
30 public static final String ARG_ENDLESS_TRACE_BUFFER = "--endless-trace-buffer";
31 public static final String ARG_KEY_USE_TEST_FONTS = "use-test-fonts";
32 public static final String ARG_USE_TEST_FONTS = "--use-test-fonts";
33 public static final String ARG_KEY_ENABLE_DART_PROFILING = "enable-dart-profiling";
34 public static final String ARG_ENABLE_DART_PROFILING = "--enable-dart-profiling";
35 public static final String ARG_KEY_ENABLE_SOFTWARE_RENDERING = "enable-software-rendering";
36 public static final String ARG_ENABLE_SOFTWARE_RENDERING = "--enable-software-rendering";
37 public static final String ARG_KEY_SKIA_DETERMINISTIC_RENDERING = "skia-deterministic-rendering";
38 public static final String ARG_SKIA_DETERMINISTIC_RENDERING = "--skia-deterministic-rendering";
39 public static final String ARG_KEY_TRACE_SKIA = "trace-skia";
40 public static final String ARG_TRACE_SKIA = "--trace-skia";
41 public static final String ARG_KEY_TRACE_SKIA_ALLOWLIST = "trace-skia-allowlist";
42 public static final String ARG_TRACE_SKIA_ALLOWLIST = "--trace-skia-allowlist=";
43 public static final String ARG_KEY_TRACE_SYSTRACE = "trace-systrace";
44 public static final String ARG_TRACE_SYSTRACE = "--trace-systrace";
45 public static final String ARG_KEY_TRACE_TO_FILE = "trace-to-file";
46 public static final String ARG_TRACE_TO_FILE = "--trace-to-file";
47 public static final String ARG_KEY_ENABLE_IMPELLER = "enable-impeller";
48 public static final String ARG_ENABLE_IMPELLER = "--enable-impeller";
49 public static final String ARG_KEY_ENABLE_VULKAN_VALIDATION = "enable-vulkan-validation";
50 public static final String ARG_ENABLE_VULKAN_VALIDATION = "--enable-vulkan-validation";
51 public static final String ARG_KEY_DUMP_SHADER_SKP_ON_SHADER_COMPILATION =
52 "dump-skp-on-shader-compilation";
53 public static final String ARG_DUMP_SHADER_SKP_ON_SHADER_COMPILATION =
54 "--dump-skp-on-shader-compilation";
55 public static final String ARG_KEY_CACHE_SKSL = "cache-sksl";
56 public static final String ARG_CACHE_SKSL = "--cache-sksl";
57 public static final String ARG_KEY_PURGE_PERSISTENT_CACHE = "purge-persistent-cache";
58 public static final String ARG_PURGE_PERSISTENT_CACHE = "--purge-persistent-cache";
59 public static final String ARG_KEY_VERBOSE_LOGGING = "verbose-logging";
60 public static final String ARG_VERBOSE_LOGGING = "--verbose-logging";
61 public static final String ARG_KEY_VM_SERVICE_PORT = "vm-service-port";
62 public static final String ARG_VM_SERVICE_PORT = "--vm-service-port=";
63 // TODO(bkonyi): remove once flutter_tools no longer uses this option.
64 // See https://github.com/dart-lang/sdk/issues/50233
65 public static final String ARG_KEY_OBSERVATORY_PORT = "observatory-port";
66 public static final String ARG_KEY_DART_FLAGS = "dart-flags";
67 public static final String ARG_DART_FLAGS = "--dart-flags";
68
69 @NonNull
70 public static FlutterShellArgs fromIntent(@NonNull Intent intent) {
71 // Before adding more entries to this list, consider that arbitrary
72 // Android applications can generate intents with extra data and that
73 // there are many security-sensitive args in the binary.
74 // TODO(mattcarroll): I left this warning as-is, but we should clarify what exactly this warning
75 // is warning against.
76 ArrayList<String> args = new ArrayList<>();
77
78 if (intent.getBooleanExtra(ARG_KEY_TRACE_STARTUP, false)) {
79 args.add(ARG_TRACE_STARTUP);
80 }
81 if (intent.getBooleanExtra(ARG_KEY_START_PAUSED, false)) {
82 args.add(ARG_START_PAUSED);
83 }
84 int vmServicePort = intent.getIntExtra(ARG_KEY_VM_SERVICE_PORT, 0);
85 if (vmServicePort > 0) {
86 args.add(ARG_VM_SERVICE_PORT + Integer.toString(vmServicePort));
87 } else {
88 // TODO(bkonyi): remove once flutter_tools no longer uses this option.
89 // See https://github.com/dart-lang/sdk/issues/50233
90 vmServicePort = intent.getIntExtra(ARG_KEY_OBSERVATORY_PORT, 0);
91 if (vmServicePort > 0) {
92 args.add(ARG_VM_SERVICE_PORT + Integer.toString(vmServicePort));
93 }
94 }
95 if (intent.getBooleanExtra(ARG_KEY_DISABLE_SERVICE_AUTH_CODES, false)) {
96 args.add(ARG_DISABLE_SERVICE_AUTH_CODES);
97 }
98 if (intent.getBooleanExtra(ARG_KEY_ENDLESS_TRACE_BUFFER, false)) {
99 args.add(ARG_ENDLESS_TRACE_BUFFER);
100 }
101 if (intent.getBooleanExtra(ARG_KEY_USE_TEST_FONTS, false)) {
102 args.add(ARG_USE_TEST_FONTS);
103 }
104 if (intent.getBooleanExtra(ARG_KEY_ENABLE_DART_PROFILING, false)) {
105 args.add(ARG_ENABLE_DART_PROFILING);
106 }
107 if (intent.getBooleanExtra(ARG_KEY_ENABLE_SOFTWARE_RENDERING, false)) {
108 args.add(ARG_ENABLE_SOFTWARE_RENDERING);
109 }
110 if (intent.getBooleanExtra(ARG_KEY_SKIA_DETERMINISTIC_RENDERING, false)) {
111 args.add(ARG_SKIA_DETERMINISTIC_RENDERING);
112 }
113 if (intent.getBooleanExtra(ARG_KEY_TRACE_SKIA, false)) {
114 args.add(ARG_TRACE_SKIA);
115 }
116 String traceSkiaAllowlist = intent.getStringExtra(ARG_KEY_TRACE_SKIA_ALLOWLIST);
117 if (traceSkiaAllowlist != null) {
118 args.add(ARG_TRACE_SKIA_ALLOWLIST + traceSkiaAllowlist);
119 }
120 if (intent.getBooleanExtra(ARG_KEY_TRACE_SYSTRACE, false)) {
121 args.add(ARG_TRACE_SYSTRACE);
122 }
123 if (intent.hasExtra(ARG_KEY_TRACE_TO_FILE)) {
124 args.add(ARG_TRACE_TO_FILE + "=" + intent.getStringExtra(ARG_KEY_TRACE_TO_FILE));
125 }
126 if (intent.getBooleanExtra(ARG_KEY_ENABLE_IMPELLER, false)) {
127 args.add(ARG_ENABLE_IMPELLER);
128 }
129 if (intent.getBooleanExtra(ARG_KEY_ENABLE_VULKAN_VALIDATION, false)) {
130 args.add(ARG_ENABLE_VULKAN_VALIDATION);
131 }
132 if (intent.getBooleanExtra(ARG_KEY_DUMP_SHADER_SKP_ON_SHADER_COMPILATION, false)) {
133 args.add(ARG_DUMP_SHADER_SKP_ON_SHADER_COMPILATION);
134 }
135 if (intent.getBooleanExtra(ARG_KEY_CACHE_SKSL, false)) {
136 args.add(ARG_CACHE_SKSL);
137 }
138 if (intent.getBooleanExtra(ARG_KEY_PURGE_PERSISTENT_CACHE, false)) {
139 args.add(ARG_PURGE_PERSISTENT_CACHE);
140 }
141 if (intent.getBooleanExtra(ARG_KEY_VERBOSE_LOGGING, false)) {
142 args.add(ARG_VERBOSE_LOGGING);
143 }
144
145 // NOTE: all flags provided with this argument are subject to filtering
146 // based on a list of allowed flags in shell/common/switches.cc. If any
147 // flag provided is not allowed, the process will immediately terminate.
148 if (intent.hasExtra(ARG_KEY_DART_FLAGS)) {
149 args.add(ARG_DART_FLAGS + "=" + intent.getStringExtra(ARG_KEY_DART_FLAGS));
150 }
151
152 return new FlutterShellArgs(args);
153 }
154
155 @NonNull private Set<String> args;
156
157 /**
158 * Creates a set of Flutter shell arguments from a given {@code String[]} array. The given
159 * arguments are automatically de-duplicated.
160 */
161 public FlutterShellArgs(@NonNull String[] args) {
162 this.args = new HashSet<>(Arrays.asList(args));
163 }
164
165 /**
166 * Creates a set of Flutter shell arguments from a given {@code List<String>}. The given arguments
167 * are automatically de-duplicated.
168 */
170 this.args = new HashSet<>(args);
171 }
172
173 /** Creates a set of Flutter shell arguments from a given {@code Set<String>}. */
174 public FlutterShellArgs(@NonNull Set<String> args) {
175 this.args = new HashSet<>(args);
176 }
177
178 /**
179 * Adds the given {@code arg} to this set of arguments.
180 *
181 * @param arg argument to add
182 */
183 public void add(@NonNull String arg) {
184 args.add(arg);
185 }
186
187 /**
188 * Removes the given {@code arg} from this set of arguments.
189 *
190 * @param arg argument to remove
191 */
192 public void remove(@NonNull String arg) {
193 args.remove(arg);
194 }
195
196 /**
197 * Returns a new {@code String[]} array which contains each of the arguments within this {@code
198 * FlutterShellArgs}.
199 *
200 * @return array of arguments
201 */
202 @NonNull
203 public String[] toArray() {
204 String[] argsArray = new String[args.size()];
205 return args.toArray(argsArray);
206 }
207}
FlutterShellArgs(@NonNull List< String > args)
static FlutterShellArgs fromIntent(@NonNull Intent intent)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args