Extract web platform service boundary
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -60,6 +60,48 @@ void invoke_picked_path_if_selected(
|
||||
callback(path);
|
||||
}
|
||||
|
||||
#ifdef __WEB__
|
||||
class RetainedWebPlatformServices final : public pp::platform::WebPlatformServices {
|
||||
public:
|
||||
void publish_exported_image(std::string_view path) override
|
||||
{
|
||||
(void)path;
|
||||
}
|
||||
|
||||
void flush_persistent_storage() override
|
||||
{
|
||||
webgl_sync();
|
||||
}
|
||||
|
||||
[[nodiscard]] int default_canvas_resolution() override
|
||||
{
|
||||
return pp::platform::platform_default_canvas_resolution(pp::platform::PlatformFamily::webgl);
|
||||
}
|
||||
|
||||
void save_prepared_file(
|
||||
std::string_view path,
|
||||
std::string_view suggested_name,
|
||||
pp::platform::PreparedFileCallback callback) override
|
||||
{
|
||||
const std::string value(path);
|
||||
const std::string name(suggested_name);
|
||||
webgl_pick_file_save(value, name, [callback = std::move(callback), value](bool success) {
|
||||
callback(value, success);
|
||||
});
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
[[nodiscard]] pp::platform::WebPlatformServices& active_legacy_web_platform_services()
|
||||
{
|
||||
#ifdef __WEB__
|
||||
static RetainedWebPlatformServices services;
|
||||
return pp::platform::resolve_web_platform_services(services);
|
||||
#else
|
||||
return pp::platform::active_web_platform_services();
|
||||
#endif
|
||||
}
|
||||
|
||||
// DEBT-0017: fallback for platforms that do not inject PlatformServices yet.
|
||||
class LegacyPlatformServices final : public pp::platform::PlatformServices {
|
||||
public:
|
||||
@@ -300,7 +342,13 @@ public:
|
||||
|
||||
void publish_exported_image(std::string_view path) override
|
||||
{
|
||||
if (!pp::platform::platform_publishes_exported_images(pp::platform::current_platform_family()))
|
||||
const auto family = pp::platform::current_platform_family();
|
||||
if (family == pp::platform::PlatformFamily::webgl)
|
||||
{
|
||||
active_legacy_web_platform_services().publish_exported_image(path);
|
||||
return;
|
||||
}
|
||||
if (!pp::platform::platform_publishes_exported_images(family))
|
||||
{
|
||||
(void)path;
|
||||
return;
|
||||
@@ -314,11 +362,14 @@ public:
|
||||
|
||||
void flush_persistent_storage() override
|
||||
{
|
||||
if (!pp::platform::platform_flushes_persistent_storage(pp::platform::current_platform_family()))
|
||||
const auto family = pp::platform::current_platform_family();
|
||||
if (family == pp::platform::PlatformFamily::webgl)
|
||||
{
|
||||
active_legacy_web_platform_services().flush_persistent_storage();
|
||||
return;
|
||||
}
|
||||
if (!pp::platform::platform_flushes_persistent_storage(family))
|
||||
return;
|
||||
#ifdef __WEB__
|
||||
webgl_sync();
|
||||
#endif
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<std::string> document_browse_roots(
|
||||
@@ -501,8 +552,10 @@ public:
|
||||
|
||||
[[nodiscard]] int default_canvas_resolution() override
|
||||
{
|
||||
return pp::platform::platform_default_canvas_resolution(
|
||||
pp::platform::current_platform_family());
|
||||
const auto family = pp::platform::current_platform_family();
|
||||
if (family == pp::platform::PlatformFamily::webgl)
|
||||
return active_legacy_web_platform_services().default_canvas_resolution();
|
||||
return pp::platform::platform_default_canvas_resolution(family);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool draws_canvas_tip_for_pointer(
|
||||
@@ -591,6 +644,16 @@ public:
|
||||
std::string_view suggested_name,
|
||||
pp::platform::PreparedFileCallback callback) override
|
||||
{
|
||||
const auto family = pp::platform::current_platform_family();
|
||||
if (family == pp::platform::PlatformFamily::webgl)
|
||||
{
|
||||
active_legacy_web_platform_services().save_prepared_file(
|
||||
path,
|
||||
suggested_name,
|
||||
std::move(callback));
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string value(path);
|
||||
const std::string name(suggested_name);
|
||||
#ifdef __IOS__
|
||||
@@ -599,10 +662,6 @@ public:
|
||||
[App::I->ios_view pick_file_save:value];
|
||||
});
|
||||
callback(value, true);
|
||||
#elif __WEB__
|
||||
webgl_pick_file_save(value, name, [callback = std::move(callback), value](bool success) {
|
||||
callback(value, success);
|
||||
});
|
||||
#else
|
||||
(void)name;
|
||||
callback(value, false);
|
||||
|
||||
Reference in New Issue
Block a user