Extract web platform service boundary

This commit is contained in:
2026-06-12 19:01:45 +02:00
parent 4c0450c87f
commit 8cd384012f
4 changed files with 273 additions and 11 deletions

View File

@@ -14,6 +14,67 @@
namespace {
class FakeWebPlatformServices final : public pp::platform::WebPlatformServices {
public:
void publish_exported_image(std::string_view path) override
{
++exported_image_publishes;
exported_image_path.assign(path);
}
void flush_persistent_storage() override
{
++persistent_storage_flushes;
}
[[nodiscard]] int default_canvas_resolution() override
{
++default_canvas_resolution_requests;
return canvas_resolution;
}
void save_prepared_file(
std::string_view path,
std::string_view suggested_name,
pp::platform::PreparedFileCallback callback) override
{
++save_prepared_file_requests;
prepared_file_path.assign(path);
prepared_file_name.assign(suggested_name);
callback(prepared_file_path, prepared_file_saved);
}
int exported_image_publishes = 0;
int persistent_storage_flushes = 0;
int default_canvas_resolution_requests = 0;
int save_prepared_file_requests = 0;
int canvas_resolution = 2048;
bool prepared_file_saved = true;
std::string exported_image_path;
std::string prepared_file_path;
std::string prepared_file_name;
};
class ScopedInjectedWebPlatformServices final {
public:
explicit ScopedInjectedWebPlatformServices(pp::platform::WebPlatformServices* services)
: previous_(pp::platform::injected_web_platform_services())
{
pp::platform::set_injected_web_platform_services(services);
}
~ScopedInjectedWebPlatformServices()
{
pp::platform::set_injected_web_platform_services(previous_);
}
ScopedInjectedWebPlatformServices(const ScopedInjectedWebPlatformServices&) = delete;
ScopedInjectedWebPlatformServices& operator=(const ScopedInjectedWebPlatformServices&) = delete;
private:
pp::platform::WebPlatformServices* previous_ = nullptr;
};
class FakePlatformServices final : public pp::platform::PlatformServices {
public:
explicit FakePlatformServices(std::string clipboard_value)
@@ -715,6 +776,68 @@ void platform_services_dispatch_prepared_file_save(pp::tests::Harness& harness)
PP_EXPECT(harness, saved);
}
void web_platform_services_preserve_default_web_policy(pp::tests::Harness& harness)
{
ScopedInjectedWebPlatformServices scoped(nullptr);
auto& services = pp::platform::active_web_platform_services();
std::string saved_path;
bool saved = true;
services.publish_exported_image("/PanoPainter/export.png");
services.flush_persistent_storage();
services.save_prepared_file(
"/PanoPainter/export.png",
"export.png",
[&](std::string path, bool success) {
saved_path = std::move(path);
saved = success;
});
PP_EXPECT(harness, services.default_canvas_resolution() == 512);
PP_EXPECT(harness, saved_path == "/PanoPainter/export.png");
PP_EXPECT(harness, !saved);
}
void web_platform_services_resolve_injected_services(pp::tests::Harness& harness)
{
FakeWebPlatformServices fallback;
FakeWebPlatformServices injected;
ScopedInjectedWebPlatformServices cleared(nullptr);
PP_EXPECT(harness, &pp::platform::resolve_web_platform_services(fallback) == &fallback);
{
ScopedInjectedWebPlatformServices scoped(&injected);
auto& services = pp::platform::active_web_platform_services();
std::string saved_path;
bool saved = false;
services.publish_exported_image("/PanoPainter/export.png");
services.flush_persistent_storage();
services.save_prepared_file(
"/PanoPainter/export.png",
"export.png",
[&](std::string path, bool success) {
saved_path = std::move(path);
saved = success;
});
PP_EXPECT(harness, &pp::platform::resolve_web_platform_services(fallback) == &injected);
PP_EXPECT(harness, services.default_canvas_resolution() == 2048);
PP_EXPECT(harness, injected.exported_image_publishes == 1);
PP_EXPECT(harness, injected.exported_image_path == "/PanoPainter/export.png");
PP_EXPECT(harness, injected.persistent_storage_flushes == 1);
PP_EXPECT(harness, injected.default_canvas_resolution_requests == 1);
PP_EXPECT(harness, injected.save_prepared_file_requests == 1);
PP_EXPECT(harness, injected.prepared_file_path == "/PanoPainter/export.png");
PP_EXPECT(harness, injected.prepared_file_name == "export.png");
PP_EXPECT(harness, saved_path == "/PanoPainter/export.png");
PP_EXPECT(harness, saved);
}
PP_EXPECT(harness, pp::platform::injected_web_platform_services() == nullptr);
}
void platform_services_dispatch_writable_file_target(pp::tests::Harness& harness)
{
FakePlatformServices fake("unused");
@@ -1086,6 +1209,8 @@ int main()
"platform services dispatch working directory picker policy",
platform_services_dispatch_working_directory_picker_policy);
harness.run("platform services dispatch prepared file save", platform_services_dispatch_prepared_file_save);
harness.run("web platform services preserve default web policy", web_platform_services_preserve_default_web_policy);
harness.run("web platform services resolve injected services", web_platform_services_resolve_injected_services);
harness.run("platform services dispatch writable file target", platform_services_dispatch_writable_file_target);
harness.run(
"platform services dispatch document export collection policy",