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

Commit 5d9cf53

Browse files
committed
Started loading the plugin registrant specified by the framework instead of looking for it.
1 parent 42a834a commit 5d9cf53

File tree

8 files changed

+64
-33
lines changed

8 files changed

+64
-33
lines changed

runtime/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ if (enable_unittests) {
187187
public_deps = [
188188
":plugin_registrant",
189189
"//flutter/fml",
190+
"//flutter/runtime:dart_plugin_registrant",
190191
"//flutter/testing",
191192
"//flutter/testing:fixture_test",
192193
]

runtime/dart_isolate.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@ bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
707707
: tonic::ToDart("main");
708708

709709
if (!FindAndInvokeDartPluginRegistrant()) {
710+
// TODO(gaaclarke): Remove once the framework PR lands that uses `--source`
711+
// to compile the Dart Plugin Registrant
712+
// (https://github.com/flutter/flutter/pull/100572).
710713
InvokeDartPluginRegistrantIfAvailable(library_handle);
711714
}
712715

runtime/dart_plugin_registrant.cc

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,14 @@
66

77
#include <string>
88

9+
#include "flutter/fml/logging.h"
910
#include "flutter/fml/trace_event.h"
1011
#include "third_party/tonic/converter/dart_converter.h"
1112
#include "third_party/tonic/logging/dart_invoke.h"
1213

1314
namespace flutter {
1415

15-
namespace {
16-
bool EndsWith(const std::string& str, const std::string& ending) {
17-
if (str.size() >= ending.size()) {
18-
return (0 ==
19-
str.compare(str.size() - ending.size(), ending.size(), ending));
20-
} else {
21-
return false;
22-
}
23-
}
24-
25-
Dart_Handle FindDartPluginRegistrantLibrary() {
26-
// TODO(99308): Instead of finding this, it could be passed down from the
27-
// tool.
28-
Dart_Handle libraries = Dart_GetLoadedLibraries();
29-
intptr_t length = 0;
30-
Dart_ListLength(libraries, &length);
31-
for (intptr_t i = 0; i < length; ++i) {
32-
Dart_Handle library = Dart_ListGetAt(libraries, i);
33-
std::string library_name =
34-
tonic::DartConverter<std::string>::FromDart(Dart_ToString(library));
35-
if (EndsWith(library_name,
36-
"dart_tool/flutter_build/dart_plugin_registrant.dart'")) {
37-
return library;
38-
}
39-
}
40-
return Dart_Null();
41-
}
42-
} // namespace
16+
const char* dart_plugin_registrant_library_override = nullptr;
4317

4418
bool InvokeDartPluginRegistrantIfAvailable(Dart_Handle library_handle) {
4519
TRACE_EVENT0("flutter", "InvokeDartPluginRegistrantIfAvailable");
@@ -65,12 +39,30 @@ bool InvokeDartPluginRegistrantIfAvailable(Dart_Handle library_handle) {
6539
}
6640

6741
bool FindAndInvokeDartPluginRegistrant() {
68-
auto dart_plugin_registrant_library = FindDartPluginRegistrantLibrary();
69-
if (!Dart_IsNull(dart_plugin_registrant_library)) {
70-
return InvokeDartPluginRegistrantIfAvailable(
71-
dart_plugin_registrant_library);
72-
} else {
42+
std::string library_name =
43+
dart_plugin_registrant_library_override == nullptr
44+
? "package:flutter/src/dart_plugin_registrant.dart"
45+
: dart_plugin_registrant_library_override;
46+
Dart_Handle library = Dart_LookupLibrary(tonic::ToDart(library_name));
47+
if (Dart_IsError(library)) {
7348
return false;
7449
}
50+
Dart_Handle registrant_file_uri =
51+
Dart_GetField(library, tonic::ToDart("dartPluginRegistrantLibrary"));
52+
if (Dart_IsError(registrant_file_uri)) {
53+
// TODO(gaaclarke): Find a way to remove this branch so the field is
54+
// required. I couldn't get it working with unit tests.
55+
return InvokeDartPluginRegistrantIfAvailable(library);
56+
} else {
57+
std::string registrant_file_uri_string =
58+
tonic::DartConverter<std::string>::FromDart(registrant_file_uri);
59+
if (registrant_file_uri_string.empty()) {
60+
FML_LOG(ERROR) << "Unexpected empty dartPluginRegistrantLibrary.";
61+
return false;
62+
} else {
63+
Dart_Handle registrant_library = Dart_LookupLibrary(registrant_file_uri);
64+
return InvokeDartPluginRegistrantIfAvailable(registrant_library);
65+
}
66+
}
7567
}
7668
} // namespace flutter

runtime/dart_plugin_registrant.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
namespace flutter {
1111

12+
/// The name of the library where the Dart Plugin Registrant will be looked for.
13+
/// This is available for testing.
14+
extern const char* dart_plugin_registrant_library_override;
15+
1216
/// Looks for the Dart Plugin Registrant in `library_handle` and invokes it if
1317
/// it is found.
1418
/// @return `true` when the registrant has been invoked.

runtime/dart_plugin_registrant_unittests.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include "flutter/runtime/dart_isolate.h"
66

7+
#include <cstdlib>
78
#include "flutter/fml/paths.h"
9+
#include "flutter/runtime/dart_plugin_registrant.h"
810
#include "flutter/runtime/dart_vm.h"
911
#include "flutter/runtime/dart_vm_lifecycle.h"
1012
#include "flutter/testing/dart_isolate_runner.h"
@@ -23,6 +25,30 @@ const std::string elf_file_name = "plugin_registrant_app_elf_snapshot.so";
2325
class DartIsolateTest : public FixtureTest {
2426
public:
2527
DartIsolateTest() : FixtureTest(kernel_file_name, elf_file_name, "") {}
28+
29+
void OverrideDartPluginRegistrant(const std::string& override_value) {
30+
dart_plugin_registrant_library_ = override_value;
31+
dart_plugin_registrant_library_override =
32+
dart_plugin_registrant_library_.c_str();
33+
}
34+
35+
void SetUp() override {
36+
std::string source_path = GetSourcePath();
37+
if (source_path[0] != '/') {
38+
// On windows we need an extra '/' prefix.
39+
source_path = "/" + source_path;
40+
}
41+
std::string registrant_uri = std::string("file://") + source_path +
42+
"flutter/runtime/fixtures/dart_tool/"
43+
"flutter_build/dart_plugin_registrant.dart";
44+
OverrideDartPluginRegistrant(registrant_uri);
45+
}
46+
47+
void TearDown() override {
48+
dart_plugin_registrant_library_override = nullptr;
49+
}
50+
51+
std::string dart_plugin_registrant_library_;
2652
};
2753

2854
TEST_F(DartIsolateTest, DartPluginRegistrantIsPresent) {

runtime/fixtures/dart_tool/flutter_build/dart_plugin_registrant.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:isolate';
66
import 'dart:ui';
7+
import 'dart:io' show Platform;
78

89
void passMessage(String message) native 'PassMessage';
910

testing/testing.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ template("fixtures_location") {
2727

2828
location_path = rebase_path(invoker.assets_dir)
2929
testing_assets_path = rebase_path("$root_out_dir/gen/flutter/testing/assets")
30+
source_path = rebase_path("//")
3031

3132
# Array of source lines. We use a list to ensure a trailing newline is
3233
# emitted by write_file() to comply with -Wnewline-eof.
3334
location_source = [
3435
"namespace flutter { namespace testing { ",
36+
"const char* GetSourcePath() {return \"$source_path\";} ",
3537
"const char* GetFixturesPath() {return \"$location_path\";} ",
3638
"const char* GetTestingAssetsPath() {return \"$testing_assets_path\";} ",
3739
"}}",

testing/testing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace flutter {
1717
namespace testing {
1818

19+
const char* GetSourcePath();
20+
1921
//------------------------------------------------------------------------------
2022
/// @brief Returns the directory containing the test fixture for the target
2123
/// if this target has fixtures configured. If there are no

0 commit comments

Comments
 (0)