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 TOPAZ_RUNTIME_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_ 6 #define TOPAZ_RUNTIME_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" 19 #include "mapped_resource.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: 26 DartComponentController( 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 const uint8_t* shared_snapshot_data, 47 const uint8_t* shared_snapshot_instructions); 48 49 int SetupFileDescriptor(fuchsia::sys::FileDescriptorPtr fd); 50 51 // |ComponentController| 52 void Kill() override; 53 void Detach() override; 54 55 // Idle notification. 56 void MessageEpilogue(Dart_Handle result); 57 void OnIdleTimer(async_dispatcher_t* dispatcher, 58 async::WaitBase* wait, 59 zx_status_t status, 60 const zx_packet_signal* signal); 61 62 // The loop must be the first declared member so that it gets destroyed after 63 // binding_ which expects the existence of a loop. 64 std::unique_ptr<async::Loop> loop_; 65 std::string label_; 66 std::string url_; 67 fuchsia::sys::Package package_; 68 fuchsia::sys::StartupInfo startup_info_; 69 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_; 70 std::string data_path_; 71 fidl::Binding<fuchsia::sys::ComponentController> binding_; 72 std::unique_ptr<sys::ComponentContext> context_; 73 74 fdio_ns_t* namespace_ = nullptr; 75 int stdoutfd_ = -1; 76 int stderrfd_ = -1; 77 MappedResource isolate_snapshot_data_; 78 MappedResource isolate_snapshot_instructions_; 79 MappedResource shared_snapshot_data_; 80 MappedResource shared_snapshot_instructions_; 81 std::vector<MappedResource> kernel_peices_; 82 83 Dart_Isolate isolate_; 84 int32_t return_code_ = 0; 85 86 zx::time idle_start_{0}; 87 zx::timer idle_timer_; 88 async::WaitMethod<DartComponentController, 89 &DartComponentController::OnIdleTimer> 90 idle_wait_{this}; 91 92 // Disallow copy and assignment. 93 DartComponentController(const DartComponentController&) = delete; 94 DartComponentController& operator=(const DartComponentController&) = delete; 95 }; 96 97 } // namespace dart_runner 98 99 #endif // TOPAZ_RUNTIME_DART_RUNNER_DART_COMPONENT_CONTROLLER_H_ 100