Skip to content

Commit

Permalink
[fuchsia] Dart Runner for CF v2 components. (flutter#28923)
Browse files Browse the repository at this point in the history
Bug: fxb/79871

Implements the ComponentRunner protocol for the Dart runner, making the Dart runner compatible with CF v2.
  • Loading branch information
akbiggs authored Sep 30, 2021
1 parent 217d545 commit 0cc9bf7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
47 changes: 44 additions & 3 deletions shell/platform/fuchsia/dart_runner/dart_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <utility>

#include "dart_component_controller.h"
#include "dart_component_controller_v2.h"
#include "flutter/fml/trace_event.h"
#include "logging.h"
#include "runtime/dart/utils/inlines.h"
Expand All @@ -32,6 +33,7 @@ extern "C" uint8_t _kDartVmSnapshotInstructions[];
#endif

namespace dart_runner {

namespace {

const char* kDartVMArgs[] = {
Expand Down Expand Up @@ -100,16 +102,19 @@ void IsolateGroupCleanupCallback(void* isolate_group_data) {
delete static_cast<std::shared_ptr<tonic::DartState>*>(isolate_group_data);
}

void RunApplication(
// Runs the application for a V1 component.
void RunApplicationV1(
DartRunner* runner,
fuchsia::sys::Package package,
fuchsia::sys::StartupInfo startup_info,
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
::fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
int64_t start = Dart_TimelineGetMicros();

DartComponentController app(std::move(package), std::move(startup_info),
runner_incoming_services, std::move(controller));
bool success = app.Setup();

int64_t end = Dart_TimelineGetMicros();
Dart_TimelineEvent("DartComponentController::Setup", start, end,
Dart_Timeline_Event_Duration, 0, NULL, NULL);
Expand All @@ -122,6 +127,31 @@ void RunApplication(
}
}

// Runs the application for a V2 component.
void RunApplicationV2(
DartRunner* runner,
fuchsia::component::runner::ComponentStartInfo start_info,
std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
controller) {
const int64_t start = Dart_TimelineGetMicros();

DartComponentControllerV2 app(std::move(start_info), runner_incoming_services,
std::move(controller));
const bool success = app.SetUp();

const int64_t end = Dart_TimelineGetMicros();
Dart_TimelineEvent("DartComponentControllerV2::Setup", start, end,
Dart_Timeline_Event_Duration, 0, NULL, NULL);
if (success) {
app.Run();
}

if (Dart_CurrentIsolate()) {
Dart_ShutdownIsolate();
}
}

bool EntropySource(uint8_t* buffer, intptr_t count) {
zx_cprng_draw(buffer, count);
return true;
Expand Down Expand Up @@ -192,7 +222,7 @@ DartRunner::~DartRunner() {
void DartRunner::StartComponent(
fuchsia::sys::Package package,
fuchsia::sys::StartupInfo startup_info,
::fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller) {
// TRACE_DURATION currently requires that the string data does not change
// in the traced scope. Since |package| gets moved in the construction of
// |thread| below, we cannot ensure that |package.resolved_url| does not
Expand All @@ -201,10 +231,21 @@ void DartRunner::StartComponent(
// eagerly.
std::string url_copy = package.resolved_url;
TRACE_EVENT1("dart", "StartComponent", "url", url_copy.c_str());
std::thread thread(RunApplication, this, std::move(package),
std::thread thread(RunApplicationV1, this, std::move(package),
std::move(startup_info), context_->svc(),
std::move(controller));
thread.detach();
}

void DartRunner::Start(
fuchsia::component::runner::ComponentStartInfo start_info,
fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
controller) {
std::string url_copy = start_info.resolved_url();
TRACE_EVENT1("dart", "Start", "url", url_copy.c_str());
std::thread thread(RunApplicationV2, this, std::move(start_info),
context_->svc(), std::move(controller));
thread.detach();
}

} // namespace dart_runner
19 changes: 13 additions & 6 deletions shell/platform/fuchsia/dart_runner/dart_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_RUNNER_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_RUNNER_DART_RUNNER_H_

#include <fuchsia/component/runner/cpp/fidl.h>
#include <fuchsia/sys/cpp/fidl.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/sys/cpp/component_context.h>
Expand All @@ -13,18 +14,24 @@

namespace dart_runner {

class DartRunner : public fuchsia::sys::Runner {
class DartRunner : public fuchsia::sys::Runner,
public fuchsia::component::runner::ComponentRunner {
public:
explicit DartRunner(sys::ComponentContext* context);
~DartRunner() override;

private:
// |fuchsia::sys::Runner| implementation:
void StartComponent(
fuchsia::sys::Package package,
fuchsia::sys::StartupInfo startup_info,
::fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller)
override;
void StartComponent(fuchsia::sys::Package package,
fuchsia::sys::StartupInfo startup_info,
fidl::InterfaceRequest<fuchsia::sys::ComponentController>
controller) override;

// |fuchsia::component::runner::ComponentRunner| implementation:
void Start(
fuchsia::component::runner::ComponentStartInfo start_info,
fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
controller) override;

// Not owned by DartRunner.
sys::ComponentContext* context_;
Expand Down

0 comments on commit 0cc9bf7

Please sign in to comment.