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
1314namespace 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
4418bool InvokeDartPluginRegistrantIfAvailable (Dart_Handle library_handle) {
4519 TRACE_EVENT0 (" flutter" , " InvokeDartPluginRegistrantIfAvailable" );
@@ -65,12 +39,30 @@ bool InvokeDartPluginRegistrantIfAvailable(Dart_Handle library_handle) {
6539}
6640
6741bool 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
0 commit comments