forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisolate_configurator.cc
114 lines (97 loc) · 4.13 KB
/
isolate_configurator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "isolate_configurator.h"
#include "dart-pkg/fuchsia/sdk_ext/fuchsia.h"
#include "dart-pkg/zircon/sdk_ext/handle.h"
#include "lib/tonic/converter/dart_converter.h"
#include "lib/tonic/dart_state.h"
#include "lib/tonic/logging/dart_error.h"
#include "lib/ui/flutter/sdk_ext/src/natives.h"
#include "third_party/dart/runtime/include/dart_api.h"
namespace flutter {
IsolateConfigurator::IsolateConfigurator(
UniqueFDIONS fdio_ns,
fidl::InterfaceHandle<views_v1::ViewContainer> view_container,
fidl::InterfaceHandle<component::ApplicationEnvironment>
application_environment,
fidl::InterfaceRequest<component::ServiceProvider>
outgoing_services_request)
: fdio_ns_(std::move(fdio_ns)),
view_container_(std::move(view_container)),
application_environment_(std::move(application_environment)),
outgoing_services_request_(std::move(outgoing_services_request)) {}
IsolateConfigurator::~IsolateConfigurator() = default;
bool IsolateConfigurator::ConfigureCurrentIsolate(
mozart::NativesDelegate* natives_delegate) {
if (used_) {
return false;
}
used_ = true;
BindFuchsia();
BindZircon();
BindDartIO();
BindScenic(natives_delegate);
// This is now owned by the Dart bindings. So relinquish our ownership of the
// handle.
(void)fdio_ns_.release();
return true;
}
void IsolateConfigurator::BindFuchsia() {
fuchsia::dart::Initialize(std::move(application_environment_),
std::move(outgoing_services_request_));
}
void IsolateConfigurator::BindZircon() {
// Tell dart:zircon about the FDIO namespace configured for this instance.
Dart_Handle zircon_lib = Dart_LookupLibrary(tonic::ToDart("dart:zircon"));
DART_CHECK_VALID(zircon_lib);
Dart_Handle namespace_type =
Dart_GetType(zircon_lib, tonic::ToDart("_Namespace"), 0, nullptr);
DART_CHECK_VALID(namespace_type);
DART_CHECK_VALID(
Dart_SetField(namespace_type, //
tonic::ToDart("_namespace"), //
tonic::ToDart(reinterpret_cast<intptr_t>(fdio_ns_.get()))));
}
void IsolateConfigurator::BindDartIO() {
// Grab the dart:io lib.
Dart_Handle io_lib = Dart_LookupLibrary(tonic::ToDart("dart:io"));
DART_CHECK_VALID(io_lib);
// Disable dart:io exit()
Dart_Handle embedder_config_type =
Dart_GetType(io_lib, tonic::ToDart("_EmbedderConfig"), 0, nullptr);
DART_CHECK_VALID(embedder_config_type);
DART_CHECK_VALID(Dart_SetField(embedder_config_type,
tonic::ToDart("_mayExit"), Dart_False()));
// Tell dart:io about the FDIO namespace configured for this instance.
Dart_Handle namespace_type =
Dart_GetType(io_lib, tonic::ToDart("_Namespace"), 0, nullptr);
DART_CHECK_VALID(namespace_type);
Dart_Handle namespace_args[] = {
Dart_NewInteger(reinterpret_cast<intptr_t>(fdio_ns_.get())), //
};
DART_CHECK_VALID(namespace_args[0]);
DART_CHECK_VALID(Dart_Invoke(namespace_type, tonic::ToDart("_setupNamespace"),
1, namespace_args));
}
void IsolateConfigurator::BindScenic(
mozart::NativesDelegate* natives_delegate) {
Dart_Handle mozart_internal =
Dart_LookupLibrary(tonic::ToDart("dart:mozart.internal"));
DART_CHECK_VALID(mozart_internal);
DART_CHECK_VALID(Dart_SetNativeResolver(mozart_internal, //
mozart::NativeLookup, //
mozart::NativeSymbol) //
);
DART_CHECK_VALID(Dart_SetField(
mozart_internal, //
tonic::ToDart("_context"), //
tonic::DartConverter<uint64_t>::ToDart(reinterpret_cast<intptr_t>(
static_cast<mozart::NativesDelegate*>(natives_delegate)))));
DART_CHECK_VALID(
Dart_SetField(mozart_internal, //
tonic::ToDart("_viewContainer"), //
tonic::ToDart(zircon::dart::Handle::Create(
view_container_.TakeChannel().release()))));
}
} // namespace flutter