From 09e3b52bdf88e165730bc90cd2a0a2eec535fd7b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 21 Nov 2024 19:01:46 +0100 Subject: [PATCH] [native assets] Consume `NativeAssetsManifest.json` (flutter/engine#56727) This PR introduces a `NativeAssetsManifest.json` next to the `AssetManifest.json` and `FontManifest.json`. This removes the need for embedding the native assets mapping inside the kernel file and will enable decoupling native assets building and bundling from the kernel compilation in flutter tools. This will then allow us to remove dry-run from the build hook protocol. (It also means all isolate groups will have the same native assets. However, since Flutter does not support `Isolate.spawnUri` from kernel files anyways, this is not a regression.) This manifest is parsed eagerly on startup by the engine in a manner similar to how the font manifest is parsed. The manifest contents need to be available in the callback for resolving assets, which does not have access to the engine. Therefore the parsed manifest is `NativeAssetsManager` stored in the `IsolateGroupData`. The engine passes it in on isolate group creation, and the FFI callbacks access it from the isolate group data. Issue: * https://github.com/flutter/flutter/issues/154425 Related PRs: * https://dart-review.googlesource.com/c/sdk/+/388161 Follow up work: * This PR does not yet remove the engine callbacks registered via the dart_api that rely on kernel embedding. If we were to do that in this PR, it would require a manual roll of the engine into flutter/flutter with the PR that switches flutter_tools to emit the native assets manifest instead of embedding in kernel, and a manual roll into g3 to switch emitting a manifest instead of embedding in kernel. A TODO is left in the code for those callbacks to be removed. ## Testing Most of this PR cannot be tested in isolation. The code in this PR is heavily exercised in the follow up flutter_tools PR which creates the `NativeAssetsManifest.json` and removes the embedding of native assets in kernel files. * This PR adds a unit test for parsing the JSON manifest. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- engine/src/flutter/BUILD.gn | 1 + engine/src/flutter/CONTRIBUTING.md | 1 + engine/src/flutter/assets/BUILD.gn | 38 +++++ engine/src/flutter/assets/native_assets.cc | 136 ++++++++++++++++++ engine/src/flutter/assets/native_assets.h | 54 +++++++ .../flutter/assets/native_assets_unittests.cc | 90 ++++++++++++ .../flutter/ci/licenses_golden/excluded_files | 1 + .../ci/licenses_golden/licenses_flutter | 4 + engine/src/flutter/runtime/dart_isolate.cc | 75 ++++++++-- engine/src/flutter/runtime/dart_isolate.h | 13 +- .../runtime/dart_isolate_group_data.cc | 11 +- .../flutter/runtime/dart_isolate_group_data.h | 20 ++- engine/src/flutter/runtime/dart_snapshot.h | 1 - .../flutter/runtime/isolate_configuration.h | 2 - .../platform_isolate_manager_unittests.cc | 1 + .../src/flutter/runtime/runtime_controller.cc | 8 +- .../src/flutter/runtime/runtime_controller.h | 7 +- engine/src/flutter/shell/common/engine.cc | 15 +- engine/src/flutter/shell/common/engine.h | 4 +- .../flutter/shell/common/shell_unittests.cc | 6 +- engine/src/flutter/testing/run_tests.py | 1 + 21 files changed, 439 insertions(+), 50 deletions(-) create mode 100644 engine/src/flutter/assets/native_assets.cc create mode 100644 engine/src/flutter/assets/native_assets.h create mode 100644 engine/src/flutter/assets/native_assets_unittests.cc diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index fca0f172b1..b5720381d1 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -183,6 +183,7 @@ group("unittests") { # Compile all unittests targets if enabled. if (enable_unittests) { public_deps += [ + "//flutter/assets:assets_unittests", "//flutter/display_list:display_list_rendertests", "//flutter/display_list:display_list_unittests", "//flutter/flow:flow_unittests", diff --git a/engine/src/flutter/CONTRIBUTING.md b/engine/src/flutter/CONTRIBUTING.md index 21c0372adf..1fe14d3640 100644 --- a/engine/src/flutter/CONTRIBUTING.md +++ b/engine/src/flutter/CONTRIBUTING.md @@ -243,6 +243,7 @@ $ ./run_tests.py --variant=host_debug_unopt_arm64 --type=engine | Name | run_tests.py type | Description | | ---------------------------------------- | ------------------- | --------------------------------------------------------------- | | accessibility_unittests | engine | | +| assets_unittests | engine | | | client_wrapper_glfw_unittests | engine | | | client_wrapper_unittests | engine | | | common_cpp_core_unittests | engine | | diff --git a/engine/src/flutter/assets/BUILD.gn b/engine/src/flutter/assets/BUILD.gn index eefdaf5eb0..c470cb6971 100644 --- a/engine/src/flutter/assets/BUILD.gn +++ b/engine/src/flutter/assets/BUILD.gn @@ -2,6 +2,13 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//flutter/common/config.gni") +import("//flutter/testing/testing.gni") + +if (is_fuchsia) { + import("//flutter/tools/fuchsia/gn-sdk/src/gn_configs.gni") +} + source_set("assets") { sources = [ "asset_manager.cc", @@ -9,12 +16,43 @@ source_set("assets") { "asset_resolver.h", "directory_asset_bundle.cc", "directory_asset_bundle.h", + "native_assets.cc", + "native_assets.h", ] deps = [ "//flutter/common", "//flutter/fml", + "//flutter/third_party/rapidjson", ] public_configs = [ "//flutter:config" ] } + +test_fixtures("assets_fixtures") { + fixtures = [] +} + +if (enable_unittests) { + executable("assets_unittests") { + testonly = true + + sources = [ "native_assets_unittests.cc" ] + + deps = [ + ":assets", + ":assets_fixtures", + "//flutter/testing", + ] + + if (!defined(defines)) { + defines = [] + } + + # This is needed for //flutter/third_party/googletest for linking zircon + # symbols. + if (is_fuchsia) { + libs = [ "${fuchsia_arch_root}/sysroot/lib/libzircon.so" ] + } + } +} diff --git a/engine/src/flutter/assets/native_assets.cc b/engine/src/flutter/assets/native_assets.cc new file mode 100644 index 0000000000..afdf7ad86f --- /dev/null +++ b/engine/src/flutter/assets/native_assets.cc @@ -0,0 +1,136 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "assets/native_assets.h" + +#include "flutter/fml/build_config.h" +#include "rapidjson/document.h" + +namespace flutter { + +#if defined(FML_ARCH_CPU_ARMEL) +#define kTargetArchitectureName "arm" +#elif defined(FML_ARCH_CPU_ARM64) +#define kTargetArchitectureName "arm64" +#elif defined(FML_ARCH_CPU_X86) +#define kTargetArchitectureName "ia32" +#elif defined(FML_ARCH_CPU_X86_64) +#define kTargetArchitectureName "x64" +#else +#error Target architecture detection failed. +#endif + +#if defined(FML_OS_ANDROID) +#define kTargetOperatingSystemName "android" +#elif defined(OS_FUCHSIA) +#define kTargetOperatingSystemName "fuchsia" +#elif defined(FML_OS_LINUX) +#define kTargetOperatingSystemName "linux" +#elif defined(FML_OS_IOS) || defined(FML_OS_IOS_SIMULATOR) +#define kTargetOperatingSystemName "ios" +#elif defined(FML_OS_MACOSX) +#define kTargetOperatingSystemName "macos" +#elif defined(FML_OS_WIN) +#define kTargetOperatingSystemName "windows" +#else +#error Target operating system detection failed. +#endif + +#define kTarget kTargetOperatingSystemName "_" kTargetArchitectureName + +void NativeAssetsManager::RegisterNativeAssets(const uint8_t* manifest, + size_t manifest_size) { + parsed_mapping_.clear(); + + rapidjson::Document document; + static_assert(sizeof(decltype(document)::Ch) == sizeof(uint8_t), ""); + document.Parse(reinterpret_cast(manifest), + manifest_size); + if (document.HasParseError()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + return; + } + if (!document.IsObject()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + return; + } + auto native_assets = document.FindMember("native-assets"); + if (native_assets == document.MemberEnd() || + !native_assets->value.IsObject()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + return; + } + auto mapping = native_assets->value.FindMember(kTarget); + if (mapping == native_assets->value.MemberEnd() || + !mapping->value.IsObject()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + return; + } + for (auto entry = mapping->value.MemberBegin(); + entry != mapping->value.MemberEnd(); entry++) { + std::vector parsed_path; + entry->name.GetString(); + auto& value = entry->value; + if (!value.IsArray()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + continue; + } + for (const auto& element : value.GetArray()) { + if (!element.IsString()) { + FML_DLOG(WARNING) << "NativeAssetsManifest.json is malformed."; + continue; + } + parsed_path.push_back(element.GetString()); + } + parsed_mapping_[entry->name.GetString()] = std::move(parsed_path); + } +} + +void NativeAssetsManager::RegisterNativeAssets( + const std::shared_ptr& asset_manager) { + std::unique_ptr manifest_mapping = + asset_manager->GetAsMapping("NativeAssetsManifest.json"); + if (manifest_mapping == nullptr) { + FML_DLOG(WARNING) + << "Could not find NativeAssetsManifest.json in the asset store."; + return; + } + + RegisterNativeAssets(manifest_mapping->GetMapping(), + manifest_mapping->GetSize()); +} + +std::vector NativeAssetsManager::LookupNativeAsset( + std::string_view asset_id) { + // Cpp17 does not support unordered_map lookup with std::string_view on a + // std::string key. + std::string as_string = std::string(asset_id); + if (parsed_mapping_.find(as_string) == parsed_mapping_.end()) { + return std::vector(); + } + return parsed_mapping_[as_string]; +} + +std::string NativeAssetsManager::AvailableNativeAssets() { + if (parsed_mapping_.empty()) { + return std::string("No available native assets."); + } + + std::string result; + result.append("Available native assets: "); + bool first = true; + for (const auto& n : parsed_mapping_) { + if (first) { + first = false; + } else { + result.append(", "); + } + result.append(n.first); + } + + result.append("."); + return result; +} + +} // namespace flutter diff --git a/engine/src/flutter/assets/native_assets.h b/engine/src/flutter/assets/native_assets.h new file mode 100644 index 0000000000..4e35249484 --- /dev/null +++ b/engine/src/flutter/assets/native_assets.h @@ -0,0 +1,54 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_ASSETS_NATIVE_ASSETS_H_ +#define FLUTTER_ASSETS_NATIVE_ASSETS_H_ + +#include +#include + +#include "flutter/assets/asset_manager.h" + +namespace flutter { + +// Parses the `NativeAssetsManifest.json` and provides a way to look up assets +// and the available assets for the callbacks that are registered to the Dart VM +// via the dart_api.h. +// +// The engine eagerly populates a native assets manager on startup. This native +// assets manager is stored in the `IsolateGroupData` so it can be accessed on +// the native assets callbacks registered in `InitDartFFIForIsolateGroup`. +class NativeAssetsManager { + public: + NativeAssetsManager() = default; + ~NativeAssetsManager() = default; + + // Reads the `NativeAssetsManifest.json` bundled in the Flutter application. + void RegisterNativeAssets(const uint8_t* manifest, size_t manifest_size); + void RegisterNativeAssets(const std::shared_ptr& asset_manager); + + // Looks up the asset path for [asset_id]. + // + // The asset path consists of a type, and an optional path. For example: + // `["system", "libsqlite3.so"]`. + std::vector LookupNativeAsset(std::string_view asset_id); + + // Lists the available asset ids. + // + // Used when a user tries to look up an asset with an ID that does not exist + // to report the list of available asset ids. + std::string AvailableNativeAssets(); + + private: + std::unordered_map> parsed_mapping_; + + NativeAssetsManager(const NativeAssetsManager&) = delete; + NativeAssetsManager(NativeAssetsManager&&) = delete; + NativeAssetsManager& operator=(const NativeAssetsManager&) = delete; + NativeAssetsManager& operator=(NativeAssetsManager&&) = delete; +}; + +} // namespace flutter + +#endif // FLUTTER_ASSETS_NATIVE_ASSETS_H_ diff --git a/engine/src/flutter/assets/native_assets_unittests.cc b/engine/src/flutter/assets/native_assets_unittests.cc new file mode 100644 index 0000000000..006f72437c --- /dev/null +++ b/engine/src/flutter/assets/native_assets_unittests.cc @@ -0,0 +1,90 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/assets/native_assets.h" + +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +// Manifest containing all hosts on which this test is run. +// In practise, a manifest will only contain a single OS. +// A manifest might contain multiple architectures if the OS supports +// multi-arch apps. +const char* kTestManifest = R"({ + "format-version": [ + 1, + 0, + 0 + ], + "native-assets": { + "linux_arm64": { + "package:my_package/my_package_bindings_generated.dart": [ + "absolute", + "libmy_package.so" + ] + }, + "linux_x64": { + "package:my_package/my_package_bindings_generated.dart": [ + "absolute", + "libmy_package.so" + ] + }, + "macos_arm64": { + "package:my_package/my_package_bindings_generated.dart": [ + "absolute", + "my_package.framework/my_package" + ] + }, + "macos_x64": { + "package:my_package/my_package_bindings_generated.dart": [ + "absolute", + "my_package.framework/my_package" + ] + }, + "windows_x64": { + "package:my_package/my_package_bindings_generated.dart": [ + "absolute", + "my_package.dll" + ] + } + } +})"; + +TEST(NativeAssetsManagerTest, NoAvailableAssets) { + NativeAssetsManager manager; + std::string available_assets = manager.AvailableNativeAssets(); + ASSERT_EQ(available_assets, "No available native assets."); +} + +TEST(NativeAssetsManagerTest, NativeAssetsManifestParsing) { + NativeAssetsManager manager; + manager.RegisterNativeAssets(reinterpret_cast(kTestManifest), + strlen(kTestManifest)); + + std::string available_assets = manager.AvailableNativeAssets(); + ASSERT_EQ(available_assets, + "Available native assets: " + "package:my_package/my_package_bindings_generated.dart."); + + std::vector existing_asset = manager.LookupNativeAsset( + "package:my_package/my_package_bindings_generated.dart"); + ASSERT_EQ(existing_asset.size(), 2u); + ASSERT_EQ(existing_asset[0], "absolute"); +#if defined(FML_OS_MACOSX) + ASSERT_EQ(existing_asset[1], "my_package.framework/my_package"); +#elif defined(FML_OS_LINUX) + ASSERT_EQ(existing_asset[1], "libmy_package.so"); +#elif defined(FML_OS_WIN) + ASSERT_EQ(existing_asset[1], "my_package.dll"); +#endif + + std::vector non_existing_asset = + manager.LookupNativeAsset("non_existing_asset"); + ASSERT_EQ(non_existing_asset.size(), 0u); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 3dabdd8256..1883a2b7c4 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -24,6 +24,7 @@ ../../../flutter/Doxyfile ../../../flutter/README.md ../../../flutter/analysis_options.yaml +../../../flutter/assets/native_assets_unittests.cc ../../../flutter/build ../../../flutter/build_overrides ../../../flutter/buildtools diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 0a43072886..39e3072747 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -42477,6 +42477,8 @@ ORIGIN: ../../../flutter/assets/asset_manager.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/assets/asset_resolver.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/assets/directory_asset_bundle.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/assets/directory_asset_bundle.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/assets/native_assets.cc + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/assets/native_assets.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/benchmarking/benchmarking.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/benchmarking/benchmarking.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/benchmarking/library.cc + ../../../flutter/LICENSE @@ -45369,6 +45371,8 @@ FILE: ../../../flutter/assets/asset_manager.h FILE: ../../../flutter/assets/asset_resolver.h FILE: ../../../flutter/assets/directory_asset_bundle.cc FILE: ../../../flutter/assets/directory_asset_bundle.h +FILE: ../../../flutter/assets/native_assets.cc +FILE: ../../../flutter/assets/native_assets.h FILE: ../../../flutter/benchmarking/benchmarking.cc FILE: ../../../flutter/benchmarking/benchmarking.h FILE: ../../../flutter/benchmarking/library.cc diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 4af4758d61..1761147761 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -5,7 +5,6 @@ #include "flutter/runtime/dart_isolate.h" #include -#include #include #include "flutter/fml/logging.h" @@ -14,14 +13,13 @@ #include "flutter/lib/io/dart_io.h" #include "flutter/lib/ui/dart_runtime_hooks.h" #include "flutter/lib/ui/dart_ui.h" -#include "flutter/lib/ui/window/platform_isolate.h" #include "flutter/runtime/dart_isolate_group_data.h" #include "flutter/runtime/dart_plugin_registrant.h" #include "flutter/runtime/dart_service_isolate.h" #include "flutter/runtime/dart_vm.h" #include "flutter/runtime/dart_vm_lifecycle.h" #include "flutter/runtime/isolate_configuration.h" -#include "flutter/runtime/runtime_controller.h" +#include "flutter/runtime/platform_isolate_manager.h" #include "fml/message_loop_task_queues.h" #include "fml/task_source.h" #include "fml/time/time_point.h" @@ -33,7 +31,6 @@ #include "third_party/tonic/dart_class_provider.h" #include "third_party/tonic/dart_message_handler.h" #include "third_party/tonic/dart_state.h" -#include "third_party/tonic/file_loader/file_loader.h" #include "third_party/tonic/logging/dart_invoke.h" #include "third_party/tonic/scopes/dart_api_scope.h" #include "third_party/tonic/scopes/dart_isolate_scope.h" @@ -100,7 +97,8 @@ std::weak_ptr DartIsolate::CreateRunningRootIsolate( const std::vector& dart_entrypoint_args, std::unique_ptr isolate_configuration, const UIDartState::Context& context, - const DartIsolate* spawning_isolate) { + const DartIsolate* spawning_isolate, + std::shared_ptr native_assets_manager) { if (!isolate_snapshot) { FML_LOG(ERROR) << "Invalid isolate snapshot."; return {}; @@ -122,7 +120,8 @@ std::weak_ptr DartIsolate::CreateRunningRootIsolate( isolate_create_callback, // isolate_shutdown_callback, // context, // - spawning_isolate // + spawning_isolate, // + std::move(native_assets_manager) // ) .lock(); @@ -196,7 +195,8 @@ std::weak_ptr DartIsolate::CreateRootIsolate( const fml::closure& isolate_create_callback, const fml::closure& isolate_shutdown_callback, const UIDartState::Context& context, - const DartIsolate* spawning_isolate) { + const DartIsolate* spawning_isolate, + std::shared_ptr native_assets_manager) { TRACE_EVENT0("flutter", "DartIsolate::CreateRootIsolate"); // Only needed if this is the main isolate for the group. @@ -247,7 +247,8 @@ std::weak_ptr DartIsolate::CreateRootIsolate( context.advisory_script_entrypoint, // advisory entrypoint nullptr, // child isolate preparer isolate_create_callback, // isolate create callback - isolate_shutdown_callback // isolate shutdown callback + isolate_shutdown_callback, // isolate shutdown callback + std::move(native_assets_manager) // ))); isolate_maker = [](std::shared_ptr* isolate_group_data, @@ -1181,15 +1182,73 @@ static void* NativeAssetsDlopenRelative(const char* path, char** error) { error); } +static void* NativeAssetsDlopen(const char* asset_id, char** error) { + auto* isolate_group_data = + static_cast*>( + Dart_CurrentIsolateGroupData()); + auto native_assets_manager = (*isolate_group_data)->GetNativeAssetsManager(); + if (native_assets_manager == nullptr) { + return nullptr; + } + + std::vector asset_path = + native_assets_manager->LookupNativeAsset(asset_id); + if (asset_path.size() == 0) { + // The asset id was not in the mapping. + return nullptr; + } + + auto& path_type = asset_path[0]; + std::string path; + static constexpr const char* kAbsolute = "absolute"; + static constexpr const char* kExecutable = "executable"; + static constexpr const char* kProcess = "process"; + static constexpr const char* kRelative = "relative"; + static constexpr const char* kSystem = "system"; + if (path_type == kAbsolute || path_type == kRelative || + path_type == kSystem) { + path = asset_path[1]; + } + + if (path_type == kAbsolute) { + return dart::bin::NativeAssets::DlopenAbsolute(path.c_str(), error); + } else if (path_type == kRelative) { + return NativeAssetsDlopenRelative(path.c_str(), error); + } else if (path_type == kSystem) { + return dart::bin::NativeAssets::DlopenSystem(path.c_str(), error); + } else if (path_type == kProcess) { + return dart::bin::NativeAssets::DlopenProcess(error); + } else if (path_type == kExecutable) { + return dart::bin::NativeAssets::DlopenExecutable(error); + } + + return nullptr; +} + +static char* NativeAssetsAvailableAssets() { + auto* isolate_group_data = + static_cast*>( + Dart_CurrentIsolateGroupData()); + auto native_assets_manager = (*isolate_group_data)->GetNativeAssetsManager(); + FML_DCHECK(native_assets_manager != nullptr); + auto available_assets = native_assets_manager->AvailableNativeAssets(); + auto* result = fml::strdup(available_assets.c_str()); + return result; +} + static void InitDartFFIForIsolateGroup() { NativeAssetsApi native_assets; memset(&native_assets, 0, sizeof(native_assets)); + // TODO(dacoharkes): Remove after flutter_tools stops kernel embedding. native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute; native_assets.dlopen_relative = &NativeAssetsDlopenRelative; native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem; native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable; native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess; + // TODO(dacoharkes): End todo. native_assets.dlsym = &dart::bin::NativeAssets::Dlsym; + native_assets.dlopen = &NativeAssetsDlopen; + native_assets.available_assets = &NativeAssetsAvailableAssets; Dart_InitializeNativeAssetsResolver(&native_assets); }; diff --git a/engine/src/flutter/runtime/dart_isolate.h b/engine/src/flutter/runtime/dart_isolate.h index e22a0132c5..4b2116ed70 100644 --- a/engine/src/flutter/runtime/dart_isolate.h +++ b/engine/src/flutter/runtime/dart_isolate.h @@ -7,21 +7,16 @@ #include #include -#include #include #include -#include "flutter/common/task_runners.h" +#include "assets/native_assets.h" #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "flutter/lib/ui/io_manager.h" -#include "flutter/lib/ui/snapshot_delegate.h" #include "flutter/lib/ui/ui_dart_state.h" #include "flutter/lib/ui/window/platform_configuration.h" #include "flutter/runtime/dart_snapshot.h" -#include "flutter/runtime/isolate_configuration.h" #include "third_party/dart/runtime/include/dart_api.h" -#include "third_party/tonic/dart_state.h" namespace flutter { @@ -221,7 +216,8 @@ class DartIsolate : public UIDartState { const std::vector& dart_entrypoint_args, std::unique_ptr isolate_configuration, const UIDartState::Context& context, - const DartIsolate* spawning_isolate = nullptr); + const DartIsolate* spawning_isolate = nullptr, + std::shared_ptr native_assets_manager = nullptr); // |UIDartState| ~DartIsolate() override; @@ -447,7 +443,8 @@ class DartIsolate : public UIDartState { const fml::closure& isolate_create_callback, const fml::closure& isolate_shutdown_callback, const UIDartState::Context& context, - const DartIsolate* spawning_isolate = nullptr); + const DartIsolate* spawning_isolate = nullptr, + std::shared_ptr native_assets_manager = nullptr); DartIsolate(const Settings& settings, bool is_root_isolate, diff --git a/engine/src/flutter/runtime/dart_isolate_group_data.cc b/engine/src/flutter/runtime/dart_isolate_group_data.cc index 8c1c09b257..0e844d3e3c 100644 --- a/engine/src/flutter/runtime/dart_isolate_group_data.cc +++ b/engine/src/flutter/runtime/dart_isolate_group_data.cc @@ -17,14 +17,16 @@ DartIsolateGroupData::DartIsolateGroupData( std::string advisory_script_entrypoint, const ChildIsolatePreparer& child_isolate_preparer, const fml::closure& isolate_create_callback, - const fml::closure& isolate_shutdown_callback) + const fml::closure& isolate_shutdown_callback, + std::shared_ptr native_assets_manager) : settings_(settings), isolate_snapshot_(std::move(isolate_snapshot)), advisory_script_uri_(std::move(advisory_script_uri)), advisory_script_entrypoint_(std::move(advisory_script_entrypoint)), child_isolate_preparer_(child_isolate_preparer), isolate_create_callback_(isolate_create_callback), - isolate_shutdown_callback_(isolate_shutdown_callback) { + isolate_shutdown_callback_(isolate_shutdown_callback), + native_assets_manager_(std::move(native_assets_manager)) { FML_DCHECK(isolate_snapshot_) << "Must contain a valid isolate snapshot."; } @@ -66,6 +68,11 @@ void DartIsolateGroupData::SetChildIsolatePreparer( child_isolate_preparer_ = value; } +std::shared_ptr +DartIsolateGroupData::GetNativeAssetsManager() const { + return native_assets_manager_; +} + void DartIsolateGroupData::SetPlatformMessageHandler( int64_t root_isolate_token, std::weak_ptr handler) { diff --git a/engine/src/flutter/runtime/dart_isolate_group_data.h b/engine/src/flutter/runtime/dart_isolate_group_data.h index 8b862564d2..8ff2927215 100644 --- a/engine/src/flutter/runtime/dart_isolate_group_data.h +++ b/engine/src/flutter/runtime/dart_isolate_group_data.h @@ -9,6 +9,7 @@ #include #include +#include "assets/native_assets.h" #include "flutter/common/settings.h" #include "flutter/fml/closure.h" #include "flutter/fml/memory/ref_ptr.h" @@ -30,13 +31,15 @@ using ChildIsolatePreparer = std::function; // group cleanup callback on any thread. class DartIsolateGroupData : public PlatformMessageHandlerStorage { public: - DartIsolateGroupData(const Settings& settings, - fml::RefPtr isolate_snapshot, - std::string advisory_script_uri, - std::string advisory_script_entrypoint, - const ChildIsolatePreparer& child_isolate_preparer, - const fml::closure& isolate_create_callback, - const fml::closure& isolate_shutdown_callback); + DartIsolateGroupData( + const Settings& settings, + fml::RefPtr isolate_snapshot, + std::string advisory_script_uri, + std::string advisory_script_entrypoint, + const ChildIsolatePreparer& child_isolate_preparer, + const fml::closure& isolate_create_callback, + const fml::closure& isolate_shutdown_callback, + std::shared_ptr native_assets_manager = nullptr); ~DartIsolateGroupData(); @@ -56,6 +59,8 @@ class DartIsolateGroupData : public PlatformMessageHandlerStorage { void SetChildIsolatePreparer(const ChildIsolatePreparer& value); + std::shared_ptr GetNativeAssetsManager() const; + /// Adds a kernel buffer mapping to the kernels loaded for this isolate group. void AddKernelBuffer(const std::shared_ptr& buffer); @@ -82,6 +87,7 @@ class DartIsolateGroupData : public PlatformMessageHandlerStorage { ChildIsolatePreparer child_isolate_preparer_; const fml::closure isolate_create_callback_; const fml::closure isolate_shutdown_callback_; + std::shared_ptr native_assets_manager_; std::map> platform_message_handlers_; mutable std::mutex platform_message_handlers_mutex_; diff --git a/engine/src/flutter/runtime/dart_snapshot.h b/engine/src/flutter/runtime/dart_snapshot.h index 96cd3c7641..62125facd5 100644 --- a/engine/src/flutter/runtime/dart_snapshot.h +++ b/engine/src/flutter/runtime/dart_snapshot.h @@ -6,7 +6,6 @@ #define FLUTTER_RUNTIME_DART_SNAPSHOT_H_ #include -#include #include "flutter/common/settings.h" #include "flutter/fml/macros.h" diff --git a/engine/src/flutter/runtime/isolate_configuration.h b/engine/src/flutter/runtime/isolate_configuration.h index 05e2270b26..20aee674d8 100644 --- a/engine/src/flutter/runtime/isolate_configuration.h +++ b/engine/src/flutter/runtime/isolate_configuration.h @@ -7,14 +7,12 @@ #include #include -#include #include "flutter/assets/asset_manager.h" #include "flutter/assets/asset_resolver.h" #include "flutter/common/settings.h" #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "flutter/fml/memory/weak_ptr.h" #include "flutter/runtime/dart_isolate.h" namespace flutter { diff --git a/engine/src/flutter/runtime/platform_isolate_manager_unittests.cc b/engine/src/flutter/runtime/platform_isolate_manager_unittests.cc index 312beff516..7c604e07ef 100644 --- a/engine/src/flutter/runtime/platform_isolate_manager_unittests.cc +++ b/engine/src/flutter/runtime/platform_isolate_manager_unittests.cc @@ -4,6 +4,7 @@ #include "flutter/runtime/dart_vm.h" #include "flutter/runtime/dart_vm_lifecycle.h" +#include "flutter/runtime/isolate_configuration.h" #include "flutter/runtime/platform_isolate_manager.h" #include "flutter/testing/fixture_test.h" #include "flutter/testing/testing.h" diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index ece3b90242..27193cdf73 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -6,9 +6,7 @@ #include -#include "flutter/common/constants.h" #include "flutter/common/settings.h" -#include "flutter/fml/message_loop.h" #include "flutter/fml/trace_event.h" #include "flutter/lib/ui/compositing/scene.h" #include "flutter/lib/ui/ui_dart_state.h" @@ -530,7 +528,8 @@ bool RuntimeController::LaunchRootIsolate( std::optional dart_entrypoint, std::optional dart_entrypoint_library, const std::vector& dart_entrypoint_args, - std::unique_ptr isolate_configuration) { + std::unique_ptr isolate_configuration, + std::shared_ptr native_assets_manager) { if (root_isolate_.lock()) { FML_LOG(ERROR) << "Root isolate was already running."; return false; @@ -550,7 +549,8 @@ bool RuntimeController::LaunchRootIsolate( dart_entrypoint_args, // std::move(isolate_configuration), // context_, // - spawning_isolate_.lock().get()) // + spawning_isolate_.lock().get(), + std::move(native_assets_manager)) // .lock(); if (!strong_root_isolate) { diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index b90bcac0e7..3ed05aa945 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -8,9 +8,9 @@ #include #include +#include "assets/native_assets.h" #include "flutter/assets/asset_manager.h" #include "flutter/common/task_runners.h" -#include "flutter/flow/layers/layer_tree.h" #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "flutter/lib/ui/io_manager.h" @@ -23,8 +23,6 @@ #include "flutter/runtime/dart_vm.h" #include "flutter/runtime/platform_data.h" #include "flutter/runtime/platform_isolate_manager.h" -#include "rapidjson/document.h" -#include "rapidjson/stringbuffer.h" namespace flutter { @@ -167,7 +165,8 @@ class RuntimeController : public PlatformConfigurationClient, std::optional dart_entrypoint, std::optional dart_entrypoint_library, const std::vector& dart_entrypoint_args, - std::unique_ptr isolate_configuration); + std::unique_ptr isolate_configuration, + std::shared_ptr native_assets_manager); //---------------------------------------------------------------------------- /// @brief Clone the runtime controller. Launching an isolate with a diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 9269ce38d3..770f778759 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -10,17 +10,12 @@ #include #include +#include "flutter/assets/native_assets.h" #include "flutter/common/settings.h" -#include "flutter/fml/make_copyable.h" #include "flutter/fml/trace_event.h" -#include "flutter/lib/snapshot/snapshot.h" #include "flutter/lib/ui/text/font_collection.h" #include "flutter/shell/common/animator.h" -#include "flutter/shell/common/platform_view.h" -#include "flutter/shell/common/shell.h" -#include "impeller/runtime_stage/runtime_stage.h" #include "rapidjson/document.h" -#include "third_party/dart/runtime/include/dart_tools_api.h" namespace flutter { @@ -195,6 +190,11 @@ bool Engine::UpdateAssetManager( font_collection_->RegisterTestFonts(); } + if (native_assets_manager_ == nullptr) { + native_assets_manager_ = std::make_shared(); + } + native_assets_manager_->RegisterNativeAssets(asset_manager_); + return true; } @@ -244,7 +244,8 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) { configuration.GetEntrypoint(), // configuration.GetEntrypointLibrary(), // configuration.GetEntrypointArgs(), // - configuration.TakeIsolateConfiguration()) // + configuration.TakeIsolateConfiguration(), // + native_assets_manager_) // ) { return RunStatus::Failure; } diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 4f4823cbeb..754539ee82 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -25,11 +25,8 @@ #include "flutter/runtime/runtime_controller.h" #include "flutter/runtime/runtime_delegate.h" #include "flutter/shell/common/animator.h" -#include "flutter/shell/common/display_manager.h" -#include "flutter/shell/common/platform_view.h" #include "flutter/shell/common/pointer_data_dispatcher.h" #include "flutter/shell/common/run_configuration.h" -#include "flutter/shell/common/shell_io_manager.h" namespace flutter { @@ -1055,6 +1052,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { std::string initial_route_; std::shared_ptr asset_manager_; std::shared_ptr font_collection_; + std::shared_ptr native_assets_manager_; const std::unique_ptr image_decoder_; ImageGeneratorRegistry image_generator_registry_; TaskRunners task_runners_; diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 0719dee1e2..3621646601 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -5,11 +5,9 @@ #define FML_USED_ON_EMBEDDER #include -#include #include #include #include -#include #include #include #include @@ -30,7 +28,6 @@ #include "flutter/flow/layers/transform_layer.h" #include "flutter/fml/backtrace.h" #include "flutter/fml/command_line.h" -#include "flutter/fml/make_copyable.h" #include "flutter/fml/message_loop.h" #include "flutter/fml/synchronization/count_down_latch.h" #include "flutter/fml/synchronization/waitable_event.h" @@ -317,7 +314,8 @@ class ThreadCheckingAssetResolver : public AssetResolver { // |AssetResolver| std::unique_ptr GetAsMapping( const std::string& asset_name) const override { - if (asset_name == "FontManifest.json") { + if (asset_name == "FontManifest.json" || + asset_name == "NativeAssetsManifest.json") { // This file is loaded directly by the engine. return nullptr; } diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index eeba132163..d2cf7b0267 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -418,6 +418,7 @@ def run_cc_tests(build_dir, executable_filter, coverage, capture_core_dump): return (name, flags, extra_env) unittests = [ + make_test('assets_unittests'), make_test('client_wrapper_glfw_unittests'), make_test('client_wrapper_unittests'), make_test('common_cpp_core_unittests'),