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 <memory>
9 
10 #include <fuchsia/sys/cpp/fidl.h>
11 #include <lib/async-loop/cpp/loop.h>
12 #include <lib/async/cpp/wait.h>
13 #include <lib/fdio/namespace.h>
14 #include <lib/sys/cpp/component_context.h>
15 #include <lib/sys/cpp/service_directory.h>
16 #include <lib/zx/timer.h>
17 
18 #include "lib/fidl/cpp/binding.h"
20 #include "third_party/dart/runtime/include/dart_api.h"
21 
22 namespace dart_runner {
23 
24 class DartComponentController : public fuchsia::sys::ComponentController {
25  public:
27  fuchsia::sys::Package package,
28  fuchsia::sys::StartupInfo startup_info,
29  std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
30  fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller);
31  ~DartComponentController() override;
32 
33  bool Setup();
34  void Run();
35  bool Main();
36  void SendReturnCode();
37 
38  private:
39  bool SetupNamespace();
40 
41  bool SetupFromKernel();
42  bool SetupFromAppSnapshot();
43 
44  bool CreateIsolate(const uint8_t* isolate_snapshot_data,
45  const uint8_t* isolate_snapshot_instructions);
46 
47  int SetupFileDescriptor(fuchsia::sys::FileDescriptorPtr fd);
48 
49  // |ComponentController|
50  void Kill() override;
51  void Detach() override;
52 
53  // Idle notification.
54  void MessageEpilogue(Dart_Handle result);
55  void OnIdleTimer(async_dispatcher_t* dispatcher,
56  async::WaitBase* wait,
57  zx_status_t status,
58  const zx_packet_signal* signal);
59 
60  // The loop must be the first declared member so that it gets destroyed after
61  // binding_ which expects the existence of a loop.
62  std::unique_ptr<async::Loop> loop_;
63  std::string label_;
64  std::string url_;
65  fuchsia::sys::Package package_;
66  fuchsia::sys::StartupInfo startup_info_;
67  std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
68  std::string data_path_;
69  fidl::Binding<fuchsia::sys::ComponentController> binding_;
70  std::unique_ptr<sys::ComponentContext> context_;
71 
72  fdio_ns_t* namespace_ = nullptr;
73  int stdoutfd_ = -1;
74  int stderrfd_ = -1;
75  dart_utils::ElfSnapshot elf_snapshot_; // AOT snapshot
76  dart_utils::MappedResource isolate_snapshot_data_; // JIT snapshot
77  dart_utils::MappedResource isolate_snapshot_instructions_; // JIT snapshot
78  std::vector<dart_utils::MappedResource> kernel_peices_;
79 
80  Dart_Isolate isolate_;
81  int32_t return_code_ = 0;
82 
83  zx::time idle_start_{0};
84  zx::timer idle_timer_;
85  async::WaitMethod<DartComponentController,
86  &DartComponentController::OnIdleTimer>
87  idle_wait_{this};
88 
89  // Disallow copy and assignment.
90  DartComponentController(const DartComponentController&) = delete;
91  DartComponentController& operator=(const DartComponentController&) = delete;
92 };
93 
94 } // namespace dart_runner
95 
96 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_
DartComponentController(fuchsia::sys::Package package, fuchsia::sys::StartupInfo startup_info, std::shared_ptr< sys::ServiceDirectory > runner_incoming_services, fidl::InterfaceRequest< fuchsia::sys::ComponentController > controller)