Flutter Engine
isolate_configurator.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 "isolate_configurator.h"
6 
9 #include "third_party/dart/runtime/include/dart_api.h"
13 
14 namespace flutter_runner {
15 
17  UniqueFDIONS fdio_ns,
18  fidl::InterfaceHandle<fuchsia::sys::Environment> environment,
19  zx::channel directory_request,
20  zx::eventpair view_ref)
21  : fdio_ns_(std::move(fdio_ns)),
22  environment_(std::move(environment)),
23  directory_request_(std::move(directory_request)),
24  view_ref_(std::move(view_ref)) {}
25 
27 
29  if (used_) {
30  return false;
31  }
32  used_ = true;
33 
34  BindFuchsia();
35  BindZircon();
36  BindDartIO();
37 
38  // This is now owned by the Dart bindings. So relinquish our ownership of the
39  // handle.
40  (void)fdio_ns_.release();
41 
42  return true;
43 }
44 
45 void IsolateConfigurator::BindFuchsia() {
46  fuchsia::dart::Initialize(std::move(environment_),
47  std::move(directory_request_),
48  std::move(view_ref_));
49 }
50 
51 void IsolateConfigurator::BindZircon() {
52  // Tell dart:zircon about the FDIO namespace configured for this instance.
53  Dart_Handle zircon_lib = Dart_LookupLibrary(tonic::ToDart("dart:zircon"));
54  FML_CHECK(!tonic::LogIfError(zircon_lib));
55 
56  Dart_Handle namespace_type =
57  Dart_GetType(zircon_lib, tonic::ToDart("_Namespace"), 0, nullptr);
58  FML_CHECK(!tonic::LogIfError(namespace_type));
59 
60  Dart_Handle result =
61  Dart_SetField(namespace_type, //
62  tonic::ToDart("_namespace"), //
63  tonic::ToDart(reinterpret_cast<intptr_t>(fdio_ns_.get())));
64  FML_CHECK(!tonic::LogIfError(result));
65 }
66 
67 void IsolateConfigurator::BindDartIO() {
68  // Grab the dart:io lib.
69  Dart_Handle io_lib = Dart_LookupLibrary(tonic::ToDart("dart:io"));
70  FML_CHECK(!tonic::LogIfError(io_lib));
71 
72  // Disable dart:io exit()
73  Dart_Handle embedder_config_type =
74  Dart_GetType(io_lib, tonic::ToDart("_EmbedderConfig"), 0, nullptr);
75  FML_CHECK(!tonic::LogIfError(embedder_config_type));
76 
77  Dart_Handle result = Dart_SetField(embedder_config_type,
78  tonic::ToDart("_mayExit"), Dart_False());
79  FML_CHECK(!tonic::LogIfError(result));
80 
81  // Tell dart:io about the FDIO namespace configured for this instance.
82  Dart_Handle namespace_type =
83  Dart_GetType(io_lib, tonic::ToDart("_Namespace"), 0, nullptr);
84  FML_CHECK(!tonic::LogIfError(namespace_type));
85 
86  Dart_Handle namespace_args[] = {
87  Dart_NewInteger(reinterpret_cast<intptr_t>(fdio_ns_.get())), //
88  };
89  result = Dart_Invoke(namespace_type, tonic::ToDart("_setupNamespace"), 1,
90  namespace_args);
91  FML_CHECK(!tonic::LogIfError(result));
92 }
93 
94 } // namespace flutter_runner
Definition: ref_ptr.h:252
const T & get() const
Definition: unique_object.h:87
void Initialize(fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, std::optional< zx::eventpair > view_ref)
Definition: fuchsia.cc:103
#define FML_CHECK(condition)
Definition: logging.h:68
IsolateConfigurator(UniqueFDIONS fdio_ns, fidl::InterfaceHandle< fuchsia::sys::Environment > environment, zx::channel directory_request, zx::eventpair view_ref)
Dart_Handle ToDart(const T &object)
bool LogIfError(Dart_Handle handle)
Definition: dart_error.cc:15