Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_plugin_registrant.cc
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
5#include "flutter/runtime/dart_plugin_registrant.h"
6
7#include <string>
8
9#include "flutter/fml/logging.h"
10#include "flutter/fml/trace_event.h"
13
14namespace flutter {
15
17
19 TRACE_EVENT0("flutter", "InvokeDartPluginRegistrantIfAvailable");
20
21 // The Dart plugin registrant is a static method with signature `void
22 // register()` within the class `_PluginRegistrant` generated by the Flutter
23 // tool.
24 //
25 // This method binds a plugin implementation to their platform
26 // interface based on the configuration of the app's pubpec.yaml, and the
27 // plugin's pubspec.yaml.
28 //
29 // Since this method may or may not be defined, check if the class is defined
30 // in the default library before calling the method.
31 Dart_Handle plugin_registrant =
32 ::Dart_GetClass(library_handle, tonic::ToDart("_PluginRegistrant"));
33
34 if (Dart_IsError(plugin_registrant)) {
35 return false;
36 }
38 tonic::DartInvokeField(plugin_registrant, "register", {}));
39 return true;
40}
41
43 std::string library_name =
45 ? "package:flutter/src/dart_plugin_registrant.dart"
47 Dart_Handle library = Dart_LookupLibrary(tonic::ToDart(library_name));
48 if (Dart_IsError(library)) {
49 return false;
50 }
51 Dart_Handle registrant_file_uri =
52 Dart_GetField(library, tonic::ToDart("dartPluginRegistrantLibrary"));
53 if (Dart_IsError(registrant_file_uri)) {
54 // TODO(gaaclarke): Find a way to remove this branch so the field is
55 // required. I couldn't get it working with unit tests.
57 }
58
59 std::string registrant_file_uri_string =
61 if (registrant_file_uri_string.empty()) {
62 return false;
63 }
64
65 Dart_Handle registrant_library = Dart_LookupLibrary(registrant_file_uri);
66 return InvokeDartPluginRegistrantIfAvailable(registrant_library);
67}
68} // namespace flutter
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle class_name)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
const char * dart_plugin_registrant_library_override
bool InvokeDartPluginRegistrantIfAvailable(Dart_Handle library_handle)
bool FindAndInvokeDartPluginRegistrant()
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33
Dart_Handle DartInvokeField(Dart_Handle target, const char *name, std::initializer_list< Dart_Handle > args)
#define TRACE_EVENT0(category_group, name)