Flutter Engine
 
Loading...
Searching...
No Matches
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 void handle_unknown_method(uint64_t ordinal,
52 bool method_has_response) override;
53
54 private:
55 /// Helper for actually running the Dart main. Returns true if successful,
56 /// false otherwise.
57 bool RunDartMain();
58
59 /// Creates and binds the namespace for this component. Returns true if
60 /// successful, false otherwise.
61 bool CreateAndBindNamespace();
62
63 bool SetUpFromKernel();
64 bool SetUpFromAppSnapshot();
65
66 bool CreateIsolate(const uint8_t* isolate_snapshot_data,
67 const uint8_t* isolate_snapshot_instructions);
68
69 // |Echo|
70 void EchoString(fidl::StringPtr value, EchoStringCallback callback) override;
71
72 // |ComponentController|
73 void Kill() override;
74 void Stop() override;
75
76 // Idle notification.
77 void MessageEpilogue(Dart_Handle result);
78 void OnIdleTimer(async_dispatcher_t* dispatcher,
79 async::WaitBase* wait,
80 zx_status_t status,
81 const zx_packet_signal* signal);
82
83 // The loop must be the first declared member so that it gets destroyed after
84 // binding_ which expects the existence of a loop.
85 std::unique_ptr<async::Loop> loop_;
86
87 std::string label_;
88 std::string url_;
89 std::string data_path_;
90 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
91 std::unique_ptr<sys::ComponentContext> context_;
92 std::unique_ptr<vfs::PseudoDir> dart_outgoing_dir_;
93 fuchsia::io::DirectoryPtr dart_outgoing_dir_ptr_;
94 fidl::InterfaceRequest<fuchsia::io::Directory> dart_outgoing_dir_request_;
95 fuchsia::io::NodePtr dart_outgoing_dir_ptr_to_check_on_open_;
96
97 fuchsia::component::runner::ComponentStartInfo start_info_;
98 fidl::Binding<fuchsia::component::runner::ComponentController> binding_;
99 fidl::BindingSet<dart::test::Echo> echo_binding_;
100
101 fdio_ns_t* namespace_ = nullptr;
102 int stdout_fd_ = -1;
103 int stderr_fd_ = -1;
104
105 dart_utils::ElfSnapshot elf_snapshot_; // AOT snapshot
106 dart_utils::MappedResource isolate_snapshot_data_; // JIT snapshot
107 dart_utils::MappedResource isolate_snapshot_instructions_; // JIT snapshot
108 std::vector<dart_utils::MappedResource> kernel_peices_;
109
110 Dart_Isolate isolate_;
111 int32_t return_code_ = 0;
112
113 zx::time idle_start_{0};
114 zx::timer idle_timer_;
115 async::WaitMethod<DartComponentController,
116 &DartComponentController::OnIdleTimer>
117 idle_wait_{this};
118
119 // Disallow copy and assignment.
121 DartComponentController& operator=(const DartComponentController&) = delete;
122};
123
124} // namespace dart_runner
125
126#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_
Starts a Dart component written in CFv2.
void handle_unknown_method(uint64_t ordinal, bool method_has_response) override
int32_t value
FlutterDesktopBinaryReply callback