Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,24 +521,28 @@ void Engine::ScheduleSecondaryVsyncCallback(uintptr_t id,
}

void Engine::HandleAssetPlatformMessage(fml::RefPtr<PlatformMessage> message) {
fml::RefPtr<PlatformMessageResponse> response = message->response();
if (!response) {
return;
}
const auto& data = message->data();
std::string asset_name(reinterpret_cast<const char*>(data.data()),
data.size());

if (asset_manager_) {
std::unique_ptr<fml::Mapping> asset_mapping =
asset_manager_->GetAsMapping(asset_name);
if (asset_mapping) {
response->Complete(std::move(asset_mapping));
return;
}
}

response->CompleteEmpty();
task_runners_.GetIOTaskRunner()->PostTask(fml::MakeCopyable(
[message = std::move(message), asset_manager = asset_manager_]() mutable {
fml::RefPtr<PlatformMessageResponse> response = message->response();
if (!response) {
return;
}
Comment on lines +526 to +529
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this early out should happen on the UI thread since it should be low cost. What line of this code is the heaviest? GetAsMapping? I'd only defer to the IO thread if we are going to hit that since posting to another thread incurs extra cost.


const auto& data = message->data();
std::string asset_name(reinterpret_cast<const char*>(data.data()),
data.size());

if (asset_manager) {
std::unique_ptr<fml::Mapping> asset_mapping =
asset_manager->GetAsMapping(asset_name);
if (asset_mapping) {
response->Complete(std::move(asset_mapping));
return;
}
}

response->CompleteEmpty();
}));
}

const std::string& Engine::GetLastEntrypoint() const {
Expand Down
110 changes: 110 additions & 0 deletions shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "assets/directory_asset_bundle.h"
#include "flutter/assets/asset_resolver.h"
#include "flutter/common/graphics/persistent_cache.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/layers/picture_layer.h"
Expand Down Expand Up @@ -148,6 +149,83 @@ class TestAssetResolver : public AssetResolver {
AssetResolver::AssetResolverType type_;
};

class PlatformMessageResponseTest : public flutter::PlatformMessageResponse {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of these classes could be mocked with gmock which would be less code to maintain.

public:
void Complete(std::unique_ptr<fml::Mapping> data) override;

void CompleteEmpty() override;

private:
explicit PlatformMessageResponseTest();

~PlatformMessageResponseTest() override;

FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseTest);
};

PlatformMessageResponseTest::PlatformMessageResponseTest() {}

PlatformMessageResponseTest::~PlatformMessageResponseTest() = default;

void PlatformMessageResponseTest::Complete(std::unique_ptr<fml::Mapping> data) {
}

void PlatformMessageResponseTest::CompleteEmpty() {}

class AssetTestResolver : public AssetResolver {
public:
AssetTestResolver(Shell* shell);

~AssetTestResolver() override;

private:
Shell* shell_;
bool IsValid() const override;
// |AssetResolver|
AssetResolver::AssetResolverType GetType() const override;
// |AssetResolver|
std::unique_ptr<fml::Mapping> GetAsMapping(
const std::string& asset_name) const override;
void CheckQueue() const;
// |AssetResolver|
bool IsValidAfterAssetManagerChange() const override;
FML_DISALLOW_COPY_AND_ASSIGN(AssetTestResolver);
};

AssetTestResolver::AssetTestResolver(Shell* shell) : shell_(shell){};

AssetTestResolver::~AssetTestResolver() = default;

AssetResolver::AssetResolverType AssetTestResolver::GetType() const {
return AssetResolverType::kAssetManager;
}

void AssetTestResolver::CheckQueue() const {
const auto current_queue_id = fml::MessageLoop::GetCurrentTaskQueueId();
const auto io_queue_id =
shell_->GetTaskRunners().GetIOTaskRunner()->GetTaskQueueId();
ASSERT_TRUE(current_queue_id == io_queue_id);
}

// |AssetResolver|
std::unique_ptr<fml::Mapping> AssetTestResolver::GetAsMapping(
const std::string& asset_name) const {
if (asset_name.compare("TestFlutterLoadAssets.json") == 0) {
CheckQueue();
}
return nullptr;
}

// |AssetResolver|
bool AssetTestResolver::IsValid() const {
return true;
}

// |AssetResolver|
bool AssetTestResolver::IsValidAfterAssetManagerChange() const {
return false;
}

static bool ValidateShell(Shell* shell) {
if (!shell) {
return false;
Expand Down Expand Up @@ -222,6 +300,38 @@ static void TestDartVmFlags(std::vector<const char*>& flags) {
}
}

TEST_F(ShellTest, TestFlutterLoadAssetsOnIOThread) {
Settings settings = CreateSettingsForFixture();
auto task_runner = CreateNewThread();
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
ThreadHost::Type::IO | ThreadHost::Type::UI);

TaskRunners task_runners("test", thread_host.ui_thread->GetTaskRunner(),
thread_host.raster_thread->GetTaskRunner(),
thread_host.ui_thread->GetTaskRunner(),
thread_host.io_thread->GetTaskRunner());

std::unique_ptr<Shell> shell =
CreateShell(std::move(settings), std::move(task_runners));

auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("emptyMain");
configuration.AddAssetResolver(
std::make_unique<AssetTestResolver>(shell.get()));

RunEngine(shell.get(), std::move(configuration));

std::string request_json = "TestFlutterLoadAssets.json";
std::vector<uint8_t> data(request_json.begin(), request_json.end());
auto platform_message = fml::MakeRefCounted<PlatformMessage>(
"flutter/assets", std::move(data),
fml::MakeRefCounted<flutter::testing::PlatformMessageResponseTest>());
SendEnginePlatformMessage(shell.get(), std::move(platform_message));

DestroyShell(std::move(shell), std::move(task_runners));
Comment on lines +330 to +332
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has no assertion.

}

static void PostSync(const fml::RefPtr<fml::TaskRunner>& task_runner,
const fml::closure& task) {
fml::AutoResetWaitableEvent latch;
Expand Down