Flutter Engine
The Flutter Engine
dart_component_controller.h
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#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_
7
8#include <dart/test/cpp/fidl.h>
9#include <fuchsia/component/runner/cpp/fidl.h>
10#include <lib/async-loop/cpp/loop.h>
11#include <lib/async/cpp/wait.h>
12#include <lib/fdio/namespace.h>
13#include <lib/fidl/cpp/binding.h>
14#include <lib/fidl/cpp/binding_set.h>
15#include <lib/fidl/cpp/string.h>
16#include <lib/sys/cpp/component_context.h>
17#include <lib/sys/cpp/service_directory.h>
18#include <lib/vfs/cpp/pseudo_dir.h>
19#include <lib/zx/timer.h>
20
21#include <memory>
22
24#include "third_party/dart/runtime/include/dart_api.h"
25
26namespace dart_runner {
27
28/// Starts a Dart component written in CFv2.
30 : public dart::test::Echo,
31 public fuchsia::component::runner::ComponentController {
32 public:
34 fuchsia::component::runner::ComponentStartInfo start_info,
35 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
36 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
37 controller);
38 ~DartComponentController() override;
39
40 /// Sets up the controller.
41 ///
42 /// This should be called before |Run|.
43 bool SetUp();
44
45 /// Runs the Dart component in a task, sending the return code back to
46 /// the Fuchsia component controller.
47 ///
48 /// This should be called after |SetUp|.
49 void Run();
50
51 private:
52 /// Helper for actually running the Dart main. Returns true if successful,
53 /// false otherwise.
54 bool RunDartMain();
55
56 /// Creates and binds the namespace for this component. Returns true if
57 /// successful, false otherwise.
58 bool CreateAndBindNamespace();
59
60 bool SetUpFromKernel();
61 bool SetUpFromAppSnapshot();
62
63 bool CreateIsolate(const uint8_t* isolate_snapshot_data,
64 const uint8_t* isolate_snapshot_instructions);
65
66 // |Echo|
67 void EchoString(fidl::StringPtr value, EchoStringCallback callback) override;
68
69 // |ComponentController|
70 void Kill() override;
71 void Stop() override;
72
73 // Idle notification.
74 void MessageEpilogue(Dart_Handle result);
75 void OnIdleTimer(async_dispatcher_t* dispatcher,
76 async::WaitBase* wait,
77 zx_status_t status,
78 const zx_packet_signal* signal);
79
80 // The loop must be the first declared member so that it gets destroyed after
81 // binding_ which expects the existence of a loop.
82 std::unique_ptr<async::Loop> loop_;
83
84 std::string label_;
85 std::string url_;
86 std::string data_path_;
87 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
88 std::unique_ptr<sys::ComponentContext> context_;
89 std::unique_ptr<vfs::PseudoDir> dart_outgoing_dir_;
90 fuchsia::io::DirectoryPtr dart_outgoing_dir_ptr_;
91 fidl::InterfaceRequest<fuchsia::io::Directory> dart_outgoing_dir_request_;
92 fuchsia::io::NodePtr dart_outgoing_dir_ptr_to_check_on_open_;
93
94 fuchsia::component::runner::ComponentStartInfo start_info_;
95 fidl::Binding<fuchsia::component::runner::ComponentController> binding_;
96 fidl::BindingSet<dart::test::Echo> echo_binding_;
97
98 fdio_ns_t* namespace_ = nullptr;
99 int stdout_fd_ = -1;
100 int stderr_fd_ = -1;
101
102 dart_utils::ElfSnapshot elf_snapshot_; // AOT snapshot
103 dart_utils::MappedResource isolate_snapshot_data_; // JIT snapshot
104 dart_utils::MappedResource isolate_snapshot_instructions_; // JIT snapshot
105 std::vector<dart_utils::MappedResource> kernel_peices_;
106
107 Dart_Isolate isolate_;
108 int32_t return_code_ = 0;
109
110 zx::time idle_start_{0};
111 zx::timer idle_timer_;
112 async::WaitMethod<DartComponentController,
113 &DartComponentController::OnIdleTimer>
114 idle_wait_{this};
115
116 // Disallow copy and assignment.
118 DartComponentController& operator=(const DartComponentController&) = delete;
119};
120
121} // namespace dart_runner
122
123#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_
Starts a Dart component written in CFv2.
DartComponentController(fuchsia::component::runner::ComponentStartInfo start_info, std::shared_ptr< sys::ServiceDirectory > runner_incoming_services, fidl::InterfaceRequest< fuchsia::component::runner::ComponentController > controller)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
uint8_t value
GAsyncResult * result
const uint8_t * isolate_snapshot_data
Definition: gen_snapshot.cc:69
const uint8_t * isolate_snapshot_instructions
Definition: gen_snapshot.cc:70
static void Echo(Thread *thread, JSONStream *js)
Definition: service.cc:1785
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394