[fuchsia] Handle unknown Flatland events in vkcube

`fuchsia.ui.composition/Flatland` is becoming an open protocol. Its
generated C++ event handler would then declare a pure virtual
`handle_unknown_event()`, so `FlatlandView` would no longer compile.

Add a base class that implements `handle_unknown_event()` only when
the generated handler declares it, and log and ignore unknown events.
The check uses the handler type rather than an API level, so this
builds both before and after the protocol change.

Bug: 555887892
Fixed: 555908028
Change-Id: If91543bfd261466622b1a496d33951ab88bb65b4
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/Vulkan-Tools/+/1983702
Lint: android-build-ayeaye@system.gserviceaccount.com <android-build-ayeaye@system.gserviceaccount.com>
Fuchsia-Auto-Submit: Justin Kobler <jkobler@google.com>
Commit-Queue: Justin Kobler <jkobler@google.com>
Reviewed-by: Emircan Uysaler <emircan@google.com>
diff --git a/cube/fuchsia/flatland_view.cpp b/cube/fuchsia/flatland_view.cpp
index cb25d41..bce5898 100644
--- a/cube/fuchsia/flatland_view.cpp
+++ b/cube/fuchsia/flatland_view.cpp
@@ -25,6 +25,16 @@
 
 #include <cinttypes>
 
+// Checks that `handle_unknown_event()` is overridden for open protocols (`fuchsia_io::Directory`) and Flatland is not abstract.
+static_assert(!std::is_abstract_v<flatland_view_internal::AsyncEventHandlerIgnoringUnknownEvents<fuchsia_io::Directory>>);
+static_assert(
+    !std::is_abstract_v<flatland_view_internal::AsyncEventHandlerIgnoringUnknownEvents<fuchsia_ui_composition::Flatland>>);
+
+// Checks the closed-protocol primary template with a protocol that is closed at every API level.
+static_assert(!std::is_base_of_v<fidl::UnknownEventHandler<fuchsia_ui_app::ViewProvider>,
+                                 fidl::AsyncEventHandler<fuchsia_ui_app::ViewProvider>>);
+static_assert(!std::is_abstract_v<flatland_view_internal::AsyncEventHandlerIgnoringUnknownEvents<fuchsia_ui_app::ViewProvider>>);
+
 namespace {
 
 const char* const kTag = "FlatlandView";
diff --git a/cube/fuchsia/flatland_view.h b/cube/fuchsia/flatland_view.h
index 01a8c6f..31b69e4 100644
--- a/cube/fuchsia/flatland_view.h
+++ b/cube/fuchsia/flatland_view.h
@@ -19,19 +19,51 @@
 #include <fidl/fuchsia.ui.app/cpp/fidl.h>
 #include <fidl/fuchsia.ui.composition/cpp/fidl.h>
 #include <fidl/fuchsia.ui.views/cpp/fidl.h>
-#include <lib/fit/function.h>
 #include <lib/async/dispatcher.h>
+#include <lib/fidl/cpp/wire/connect_service.h>
+#include <lib/fidl/cpp/wire/unknown_interaction_handler.h>
+#include <lib/fit/function.h>
 #include <lib/syslog/cpp/macros.h>
 
 #include <cstdint>
+#include <memory>
+#include <type_traits>
 #include <utility>
 
-#include <memory>
+namespace flatland_view_internal {
+
+// `fidl::AsyncEventHandler<Protocol>`, plus, when `Protocol` is open or ajar, a `handle_unknown_event()`
+// that logs and ignores flexible unknown events. The generated handler declares
+// `handle_unknown_event()` as pure virtual only for open and ajar protocols. Whether
+// `fuchsia.ui.composition/Flatland` is open depends on the API level the bindings are generated
+// for: it is closed at older API levels and open at newer ones. So the override is chosen from the
+// generated handler type rather than from an API level check. An open or ajar `Protocol` must be
+// `@discoverable`, because the log message uses `fidl::DiscoverableProtocolName<Protocol>`.
+// TODO(https://fxbug.dev/555887892): Replace with an unconditional override once Flatland is open at every API level this code
+// supports.
+template <typename Protocol, typename = void>
+class AsyncEventHandlerIgnoringUnknownEvents : public fidl::AsyncEventHandler<Protocol> {};
+
+template <typename Protocol>
+class AsyncEventHandlerIgnoringUnknownEvents<
+    Protocol, std::enable_if_t<std::is_base_of_v<fidl::UnknownEventHandler<Protocol>, fidl::AsyncEventHandler<Protocol>>>>
+    : public fidl::AsyncEventHandler<Protocol> {
+  private:
+    void handle_unknown_event(fidl::UnknownEventMetadata<Protocol> metadata) override {
+        // Logs only the first 10 events, so a server that sends an unknown event every frame does
+        // not flood the log.
+        FX_LOGS_FIRST_N(WARNING, 10) << "handle_unknown_event(): ignoring unknown "
+                                     << fidl::DiscoverableProtocolName<Protocol> << " event with ordinal "
+                                     << metadata.event_ordinal;
+    }
+};
+
+}  // namespace flatland_view_internal
 
 // This class provides a convenient wrapper around the scenic fidl apis needed to get a Flatland
 // View. Only SDK available constructs should be used so that this code may be leveraged by Vulkan
 // applications built with the SDK.
-class FlatlandView : public fidl::AsyncEventHandler<fuchsia_ui_composition::Flatland> {
+class FlatlandView : public flatland_view_internal::AsyncEventHandlerIgnoringUnknownEvents<fuchsia_ui_composition::Flatland> {
   public:
     using ResizeCallback = fit::function<void(uint32_t width, uint32_t height)>;