From 6e2d67fa1d549060f8cfd701e9fcee1e9b8cd210 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Fri, 1 Sep 2017 13:17:20 -0700 Subject: [PATCH] [Fuchsia] Pass namespaces to Isolates (#4047) --- .../application_controller_impl.cc | 38 +++++++++++++++++++ content_handler/application_controller_impl.h | 4 ++ content_handler/runtime_holder.cc | 17 +++++++++ content_handler/runtime_holder.h | 6 ++- runtime/dart_vm_entry_points_fuchsia.txt | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/content_handler/application_controller_impl.cc b/content_handler/application_controller_impl.cc index 57fdf064be..f29b3aa770 100644 --- a/content_handler/application_controller_impl.cc +++ b/content_handler/application_controller_impl.cc @@ -6,6 +6,9 @@ #include +#include +#include + #include "application/lib/app/connect.h" #include "flutter/content_handler/app.h" #include "flutter/content_handler/runtime_holder.h" @@ -56,15 +59,50 @@ ApplicationControllerImpl::ApplicationControllerImpl( auto request = service_provider.NewRequest(); service_provider_bridge_.set_backend(std::move(service_provider)); + mxio_ns_t* mxio_ns = SetupNamespace(startup_info->flat_namespace); + if (mxio_ns == nullptr) { + FTL_LOG(ERROR) << "Failed to initialize namespace"; + } + url_ = startup_info->launch_info->url; runtime_holder_.reset(new RuntimeHolder()); runtime_holder_->Init( + mxio_ns, app::ApplicationContext::CreateFrom(std::move(startup_info)), std::move(request), std::move(bundle)); } ApplicationControllerImpl::~ApplicationControllerImpl() = default; +constexpr char kServiceRootPath[] = "/svc"; + +mxio_ns_t* ApplicationControllerImpl::SetupNamespace( + const app::FlatNamespacePtr& flat) { + mxio_ns_t* mxio_namespc; + mx_status_t status = mxio_ns_create(&mxio_namespc); + if (status != MX_OK) { + FTL_LOG(ERROR) << "Failed to create namespace"; + return nullptr; + } + for (size_t i = 0; i < flat->paths.size(); ++i) { + if (flat->paths[i] == kServiceRootPath) { + // Ownership of /svc goes to the ApplicationContext created above. + continue; + } + mx::channel dir = std::move(flat->directories[i]); + mx_handle_t dir_handle = dir.release(); + const char* path = flat->paths[i].data(); + status = mxio_ns_bind(mxio_namespc, path, dir_handle); + if (status != MX_OK) { + FTL_LOG(ERROR) << "Failed to bind " << flat->paths[i] << " to namespace"; + mx_handle_close(dir_handle); + mxio_ns_destroy(mxio_namespc); + return nullptr; + } + } + return mxio_namespc; +} + void ApplicationControllerImpl::Kill() { runtime_holder_.reset(); app_->Destroy(this); diff --git a/content_handler/application_controller_impl.h b/content_handler/application_controller_impl.h index 16a6e9cff1..1ea328c08b 100644 --- a/content_handler/application_controller_impl.h +++ b/content_handler/application_controller_impl.h @@ -7,6 +7,8 @@ #include +#include + #include "application/lib/svc/service_provider_bridge.h" #include "application/services/application_controller.fidl.h" #include "application/services/application_runner.fidl.h" @@ -50,6 +52,8 @@ class ApplicationControllerImpl : public app::ApplicationController, private: void StartRuntimeIfReady(); + mxio_ns_t* SetupNamespace(const app::FlatNamespacePtr& flat); + App* app_; fidl::Binding binding_; diff --git a/content_handler/runtime_holder.cc b/content_handler/runtime_holder.cc index 29eea068af..6d40be91ea 100644 --- a/content_handler/runtime_holder.cc +++ b/content_handler/runtime_holder.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include "application/lib/app/connect.h" @@ -94,6 +95,7 @@ RuntimeHolder::~RuntimeHolder() { } void RuntimeHolder::Init( + mxio_ns_t* namespc, std::unique_ptr context, fidl::InterfaceRequest outgoing_services, std::vector bundle) { @@ -101,6 +103,7 @@ void RuntimeHolder::Init( rasterizer_ = Rasterizer::Create(); FTL_DCHECK(rasterizer_); + namespc_ = namespc; context_ = std::move(context); outgoing_services_ = std::move(outgoing_services); @@ -351,10 +354,24 @@ void RuntimeHolder::HandlePlatformMessage( void RuntimeHolder::DidCreateMainIsolate(Dart_Isolate isolate) { if (asset_store_) blink::AssetFontSelector::Install(asset_store_); + InitDartIoInternal(); InitFidlInternal(); InitMozartInternal(); } +void RuntimeHolder::InitDartIoInternal() { + Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io")); + + Dart_Handle namespace_type = + Dart_GetType(io_lib, ToDart("_Namespace"), 0, nullptr); + DART_CHECK_VALID(namespace_type); + Dart_Handle namespace_args[1]; + namespace_args[0] = Dart_NewInteger(reinterpret_cast(namespc_)); + DART_CHECK_VALID(namespace_args[0]); + DART_CHECK_VALID(Dart_Invoke( + namespace_type, ToDart("_setupNamespace"), 1, namespace_args)); +} + void RuntimeHolder::InitFidlInternal() { fidl::InterfaceHandle environment; context_->ConnectToEnvironmentService(environment.NewRequest()); diff --git a/content_handler/runtime_holder.h b/content_handler/runtime_holder.h index e667af02b4..a74b86b98a 100644 --- a/content_handler/runtime_holder.h +++ b/content_handler/runtime_holder.h @@ -6,6 +6,7 @@ #define FLUTTER_CONTENT_HANDLER_RUNTIME_HOLDER_H_ #include +#include #include @@ -39,7 +40,8 @@ class RuntimeHolder : public blink::RuntimeDelegate, RuntimeHolder(); ~RuntimeHolder(); - void Init(std::unique_ptr context, + void Init(mxio_ns_t* namespc, + std::unique_ptr context, fidl::InterfaceRequest outgoing_services, std::vector bundle); void CreateView(const std::string& script_uri, @@ -83,6 +85,7 @@ class RuntimeHolder : public blink::RuntimeDelegate, bool HandleAssetPlatformMessage(blink::PlatformMessage* message); bool HandleTextInputPlatformMessage(blink::PlatformMessage* message); + void InitDartIoInternal(); void InitFidlInternal(); void InitMozartInternal(); @@ -92,6 +95,7 @@ class RuntimeHolder : public blink::RuntimeDelegate, void OnRedrawFrame(); void Invalidate(); + mxio_ns_t* namespc_; std::unique_ptr context_; fidl::InterfaceRequest outgoing_services_; std::vector root_bundle_data_; diff --git a/runtime/dart_vm_entry_points_fuchsia.txt b/runtime/dart_vm_entry_points_fuchsia.txt index 543c012029..7c438b3383 100644 --- a/runtime/dart_vm_entry_points_fuchsia.txt +++ b/runtime/dart_vm_entry_points_fuchsia.txt @@ -1,3 +1,4 @@ +dart:io,_Namespace,_setupNamespace dart:fidl.internal,::,_environment dart:fidl.internal,::,_outgoingServices dart:fidl.internal,GetSizeResult,GetSizeResult. -- GitLab