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

@@ -1,7 +1,66 @@
#include "platform_api/platform_services.h"
#include "platform_api/platform_policy.h"
namespace pp::platform {
namespace {
class DefaultWebPlatformServices final : public WebPlatformServices {
public:
void publish_exported_image(std::string_view path) override
{
(void)path;
}
void flush_persistent_storage() override
{
}
[[nodiscard]] int default_canvas_resolution() override
{
return platform_default_canvas_resolution(PlatformFamily::webgl);
}
void save_prepared_file(
std::string_view path,
std::string_view suggested_name,
PreparedFileCallback callback) override
{
(void)suggested_name;
callback(std::string(path), false);
}
};
WebPlatformServices* g_injected_web_platform_services = nullptr;
static_assert(sizeof(PlatformServices*) == sizeof(void*));
}
WebPlatformServices& default_web_platform_services()
{
static DefaultWebPlatformServices services;
return services;
}
WebPlatformServices* injected_web_platform_services() noexcept
{
return g_injected_web_platform_services;
}
void set_injected_web_platform_services(WebPlatformServices* services) noexcept
{
g_injected_web_platform_services = services;
}
WebPlatformServices& resolve_web_platform_services(WebPlatformServices& fallback) noexcept
{
if (auto* services = injected_web_platform_services())
return *services;
return fallback;
}
WebPlatformServices& active_web_platform_services()
{
return resolve_web_platform_services(default_web_platform_services());
}
}

View File

@@ -90,4 +90,23 @@ public:
PreparedFileCallback callback) = 0;
};
class WebPlatformServices {
public:
virtual ~WebPlatformServices() = default;
virtual void publish_exported_image(std::string_view path) = 0;
virtual void flush_persistent_storage() = 0;
[[nodiscard]] virtual int default_canvas_resolution() = 0;
virtual void save_prepared_file(
std::string_view path,
std::string_view suggested_name,
PreparedFileCallback callback) = 0;
};
[[nodiscard]] WebPlatformServices& default_web_platform_services();
[[nodiscard]] WebPlatformServices* injected_web_platform_services() noexcept;
void set_injected_web_platform_services(WebPlatformServices* services) noexcept;
[[nodiscard]] WebPlatformServices& resolve_web_platform_services(WebPlatformServices& fallback) noexcept;
[[nodiscard]] WebPlatformServices& active_web_platform_services();
}