Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0c734d7

Browse files
committed
reviews
1 parent fb0aeb1 commit 0c734d7

File tree

11 files changed

+204
-163
lines changed

11 files changed

+204
-163
lines changed

shell/platform/android/android_shell_holder.cc

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ AndroidShellHolder::AndroidShellHolder(
7171
);
7272
}
7373
weak_platform_view = platform_view_android->GetWeakPtr();
74-
shell.OnDisplayUpdates(DisplayUpdateType::kStartup,
75-
{Display(jni_facade->GetDisplayRefreshRate())});
74+
auto display = Display(jni_facade->GetDisplayRefreshRate());
75+
shell.OnDisplayUpdates(DisplayUpdateType::kStartup, {display});
7676
return platform_view_android;
7777
};
7878

@@ -170,14 +170,17 @@ const flutter::Settings& AndroidShellHolder::GetSettings() const {
170170

171171
std::unique_ptr<AndroidShellHolder> AndroidShellHolder::Spawn(
172172
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
173-
std::string entrypoint,
174-
std::string libraryUrl) const {
173+
const std::string& entrypoint,
174+
const std::string& libraryUrl) const {
175175
FML_DCHECK(shell_ && shell_->IsSetup())
176176
<< "A new Shell can only be spawned "
177177
"if the current Shell is properly constructed";
178178

179179
// Pull out the new PlatformViewAndroid from the new Shell to feed to it to
180180
// the new AndroidShellHolder.
181+
//
182+
// It's a weak pointer because it's owned by the Shell (which we're also)
183+
// making below. And the AndroidShellHolder then owns the Shell.
181184
fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
182185

183186
// Take out the old AndroidContext to reuse inside the PlatformViewAndroid
@@ -202,8 +205,8 @@ std::unique_ptr<AndroidShellHolder> AndroidShellHolder::Spawn(
202205
android_context // Android context
203206
);
204207
weak_platform_view = platform_view_android->GetWeakPtr();
205-
shell.OnDisplayUpdates(DisplayUpdateType::kStartup,
206-
{Display(jni_facade->GetDisplayRefreshRate())});
208+
auto display = Display(jni_facade->GetDisplayRefreshRate());
209+
shell.OnDisplayUpdates(DisplayUpdateType::kStartup, {display});
207210
return platform_view_android;
208211
};
209212

@@ -223,14 +226,14 @@ std::unique_ptr<AndroidShellHolder> AndroidShellHolder::Spawn(
223226
std::unique_ptr<flutter::Shell> shell = shell_->Spawn(
224227
std::move(config.value()), on_create_platform_view, on_create_rasterizer);
225228

226-
return std::make_unique<AndroidShellHolder>(GetSettings(), jni_facade,
227-
thread_host_, std::move(shell),
228-
weak_platform_view);
229+
return std::unique_ptr<AndroidShellHolder>(
230+
new AndroidShellHolder(GetSettings(), jni_facade, thread_host_,
231+
std::move(shell), weak_platform_view));
229232
}
230233

231234
void AndroidShellHolder::Launch(std::shared_ptr<AssetManager> asset_manager,
232-
std::string entrypoint,
233-
std::string libraryUrl) {
235+
const std::string& entrypoint,
236+
const std::string& libraryUrl) {
234237
if (!IsValid()) {
235238
return;
236239
}
@@ -264,8 +267,8 @@ void AndroidShellHolder::NotifyLowMemoryWarning() {
264267

265268
std::optional<RunConfiguration> AndroidShellHolder::BuildRunConfiguration(
266269
std::shared_ptr<flutter::AssetManager> asset_manager,
267-
std::string entrypoint,
268-
std::string libraryUrl) const {
270+
const std::string& entrypoint,
271+
const std::string& libraryUrl) const {
269272
std::unique_ptr<IsolateConfiguration> isolate_configuration;
270273
if (flutter::DartVM::IsRunningPrecompiledCode()) {
271274
isolate_configuration = IsolateConfiguration::CreateForAppSnapshot();

shell/platform/android/android_shell_holder.h

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ namespace flutter {
2323
//----------------------------------------------------------------------------
2424
/// @brief This is the Android owner of the core engine Shell.
2525
///
26-
/// This is the top orchestrator class on the C++ side for the
26+
/// @details This is the top orchestrator class on the C++ side for the
2727
/// Android embedding. It corresponds to a FlutterEngine on the
28-
/// Java side. This class is in C++ because the core Shell is in
29-
/// C++ and an Android specific orchestrator needs to exist to
30-
/// compose it with other Android specific components such as
31-
/// the PlatformViewAndroid. This composition of many to one
28+
/// Java side. This class is in C++ because the Shell is in
29+
/// C++ and an Android orchestrator needs to exist to
30+
/// compose it with other Android specific C++ components such as
31+
/// the PlatformViewAndroid. This composition of many-to-one
3232
/// C++ components would be difficult to do through JNI whereas
3333
/// a FlutterEngine and AndroidShellHolder has a 1:1 relationship.
3434
///
@@ -43,23 +43,6 @@ class AndroidShellHolder {
4343
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
4444
bool is_background_view);
4545

46-
//----------------------------------------------------------------------------
47-
/// @brief Constructor with its components injected.
48-
///
49-
/// This is similar to the standard constructor, except its
50-
/// members were constructed elsewhere and injected.
51-
///
52-
/// All injected components must be non-null and valid.
53-
///
54-
/// Used when constructing the Shell from the inside out when
55-
/// spawning from an existing Shell.
56-
///
57-
AndroidShellHolder(flutter::Settings settings,
58-
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
59-
std::shared_ptr<ThreadHost> thread_host,
60-
std::unique_ptr<Shell> shell,
61-
fml::WeakPtr<PlatformViewAndroid> platform_view);
62-
6346
~AndroidShellHolder();
6447

6548
bool IsValid() const;
@@ -68,13 +51,15 @@ class AndroidShellHolder {
6851
/// @brief This is a factory for a derived AndroidShellHolder from an
6952
/// existing AndroidShellHolder.
7053
///
71-
/// The new and existing AndroidShellHolder and underlying
72-
/// Shells Creates one Shell from another Shell where the created
54+
/// @details Creates one Shell from another Shell where the created
7355
/// Shell takes the opportunity to share any internal components
7456
/// it can. This results is a Shell that has a smaller startup
7557
/// time cost and a smaller memory footprint than an Shell created
7658
/// with a Create function.
7759
///
60+
/// The new Shell is returned in a new AndroidShellHolder
61+
/// instance.
62+
///
7863
/// The new Shell's flutter::Settings cannot be changed from that
7964
/// of the initial Shell. The RunConfiguration subcomponent can
8065
/// be changed however in the spawned Shell to run a different
@@ -88,14 +73,17 @@ class AndroidShellHolder {
8873
/// @param[in] jni_facade this argument should be the JNI callback facade of
8974
/// a new JNI instance meant to hold this AndroidShellHolder.
9075
///
76+
/// @returns A new AndroidShellHolder containing a new Shell. Returns
77+
/// nullptr when a new Shell can't be created.
78+
///
9179
std::unique_ptr<AndroidShellHolder> Spawn(
9280
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
93-
std::string entrypoint,
94-
std::string libraryUrl) const;
81+
const std::string& entrypoint,
82+
const std::string& libraryUrl) const;
9583

9684
void Launch(std::shared_ptr<AssetManager> asset_manager,
97-
std::string entrypoint,
98-
std::string libraryUrl);
85+
const std::string& entrypoint,
86+
const std::string& libraryUrl);
9987

10088
const flutter::Settings& GetSettings() const;
10189

@@ -118,11 +106,27 @@ class AndroidShellHolder {
118106
uint64_t next_pointer_flow_id_ = 0;
119107
std::shared_ptr<AssetManager> asset_manager_;
120108

109+
//----------------------------------------------------------------------------
110+
/// @brief Constructor with its components injected.
111+
///
112+
/// @details This is similar to the standard constructor, except its
113+
/// members were constructed elsewhere and injected.
114+
///
115+
/// All injected components must be non-null and valid.
116+
///
117+
/// Used when constructing the Shell from the inside out when
118+
/// spawning from an existing Shell.
119+
///
120+
AndroidShellHolder(flutter::Settings settings,
121+
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
122+
std::shared_ptr<ThreadHost> thread_host,
123+
std::unique_ptr<Shell> shell,
124+
fml::WeakPtr<PlatformViewAndroid> platform_view);
121125
static void ThreadDestructCallback(void* value);
122126
std::optional<RunConfiguration> BuildRunConfiguration(
123127
std::shared_ptr<flutter::AssetManager> asset_manager,
124-
std::string entrypoint,
125-
std::string libraryUrl) const;
128+
const std::string& entrypoint,
129+
const std::string& libraryUrl) const;
126130

127131
FML_DISALLOW_COPY_AND_ASSIGN(AndroidShellHolder);
128132
};

shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
public class FlutterEngine {
7777
private static final String TAG = "FlutterEngine";
7878

79-
@NonNull private final Context context;
8079
@NonNull private final FlutterJNI flutterJNI;
8180
@NonNull private final FlutterRenderer renderer;
8281
@NonNull private final DartExecutor dartExecutor;
@@ -118,7 +117,7 @@ public void onPreEngineRestart() {
118117
}
119118

120119
@Override
121-
public void onEngineDestroy() {
120+
public void onEngineWillDestroy() {
122121
// This inner implementation doesn't do anything since FlutterEngine sent this
123122
// notification in the first place. It's meant for external listeners.
124123
}
@@ -277,7 +276,6 @@ public FlutterEngine(
277276
@Nullable String[] dartVmArgs,
278277
boolean automaticallyRegisterPlugins,
279278
boolean waitForRestorationData) {
280-
this.context = context;
281279
AssetManager assetManager;
282280
try {
283281
assetManager = context.createPackageContext(context.getPackageName(), 0).getAssets();
@@ -365,13 +363,17 @@ private boolean isAttachedToJni() {
365363
* Create a second {@link FlutterEngine} based on this current one by sharing as much resources
366364
* together as possible to minimize startup latency and memory cost.
367365
*
366+
* @param context is a Context used to create the {@link FlutterEngine}. Could be the same Context
367+
* as the current engine or a different one. Generally, only an application Context is needed
368+
* for the {@link FlutterEngine} and its dependencies.
368369
* @param dartEntrypoint specifies the {@link DartEntrypoint} the new engine should run. It
369370
* doesn't need to be the same entrypoint as the current engine but must be built in the same
370371
* AOT or snapshot.
371372
* @return a new {@link FlutterEngine}.
372373
*/
373374
@NonNull
374-
/*package*/ FlutterEngine spawn(@NonNull DartEntrypoint dartEntrypoint) {
375+
/*package*/ FlutterEngine spawn(
376+
@NonNull Context context, @NonNull DartEntrypoint dartEntrypoint) {
375377
if (!isAttachedToJni()) {
376378
throw new IllegalStateException(
377379
"Spawn can only be called on a fully constructed FlutterEngine");
@@ -380,7 +382,11 @@ private boolean isAttachedToJni() {
380382
FlutterJNI newFlutterJNI =
381383
flutterJNI.spawn(
382384
dartEntrypoint.dartEntrypointFunctionName, dartEntrypoint.dartEntrypointLibrary);
383-
return new FlutterEngine(context, null, newFlutterJNI);
385+
return new FlutterEngine(
386+
context, // Context.
387+
null, // FlutterLoader. A null value passed here causes the constructor to get it from the
388+
// FlutterInjector.
389+
newFlutterJNI); // FlutterJNI.
384390
}
385391

386392
/**
@@ -422,7 +428,7 @@ private void registerPlugins() {
422428
public void destroy() {
423429
Log.v(TAG, "Destroying.");
424430
for (EngineLifecycleListener listener : engineLifecycleListeners) {
425-
listener.onEngineDestroy();
431+
listener.onEngineWillDestroy();
426432
}
427433
// The order that these things are destroyed is important.
428434
pluginRegistry.destroy();
@@ -614,6 +620,6 @@ public interface EngineLifecycleListener {
614620
*
615621
* <p>For the duration of the call, the Flutter engine is still valid.
616622
*/
617-
void onEngineDestroy();
623+
void onEngineWillDestroy();
618624
}
619625
}

shell/platform/android/io/flutter/embedding/engine/FlutterEngineGroup.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* This class is experimental. Please do not ship production code using it.
1717
*
1818
* <p>Represents a collection of {@link io.flutter.embedding.engine.FlutterEngine}s who share
19-
* resources among each other to allow them to be created faster and with less memory than calling
20-
* the {@link io.flutter.embedding.engine.FlutterEngine}'s constructor multiple times.
19+
* resources to allow them to be created faster and with less memory than calling the {@link
20+
* io.flutter.embedding.engine.FlutterEngine}'s constructor multiple times.
2121
*
2222
* <p>When creating or recreating the first {@link io.flutter.embedding.engine.FlutterEngine} in the
2323
* FlutterEngineGroup, the behavior is the same as creating a {@link
@@ -31,19 +31,18 @@
3131
*/
3232
public class FlutterEngineGroup {
3333

34-
public FlutterEngineGroup(@NonNull Context context) {
35-
this.context = context;
36-
}
37-
38-
private final Context context;
3934
/* package */ @VisibleForTesting final List<FlutterEngine> activeEngines = new ArrayList<>();
4035

41-
public FlutterEngine createAndRunDefaultEngine() {
42-
return createAndRunEngine(null);
36+
public FlutterEngine createAndRunDefaultEngine(@NonNull Context context) {
37+
return createAndRunEngine(context, null);
4338
}
4439

45-
public FlutterEngine createAndRunEngine(@Nullable DartEntrypoint dartEntrypoint) {
40+
public FlutterEngine createAndRunEngine(
41+
@NonNull Context context, @Nullable DartEntrypoint dartEntrypoint) {
4642
FlutterEngine engine = null;
43+
// This is done up here because an engine needs to be created first in order to be able to use
44+
// DartEntrypoint.createDefault. The engine creation initializes the FlutterLoader so
45+
// DartEntrypoint known where to find the assets for the AOT or kernel code.
4746
if (activeEngines.size() == 0) {
4847
engine = createEngine(context);
4948
}
@@ -55,7 +54,7 @@ public FlutterEngine createAndRunEngine(@Nullable DartEntrypoint dartEntrypoint)
5554
if (activeEngines.size() == 0) {
5655
engine.getDartExecutor().executeDartEntrypoint(dartEntrypoint);
5756
} else {
58-
engine = activeEngines.get(0).spawn(dartEntrypoint);
57+
engine = activeEngines.get(0).spawn(context, dartEntrypoint);
5958
}
6059

6160
activeEngines.add(engine);
@@ -70,7 +69,7 @@ public void onPreEngineRestart() {
7069
}
7170

7271
@Override
73-
public void onEngineDestroy() {
72+
public void onEngineWillDestroy() {
7473
activeEngines.remove(engineToCleanUpOnDestroy);
7574
}
7675
});

0 commit comments

Comments
 (0)