From 743edc1ccc3271c5d4c64659dfc80f028b1766ff Mon Sep 17 00:00:00 2001 From: moko256 Date: Fri, 8 Apr 2022 07:51:03 +0900 Subject: [PATCH] Windows: Add SystemSound support (flutter/engine#29811) --- .../platform/windows/platform_handler.cc | 6 ++++ .../shell/platform/windows/platform_handler.h | 6 ++++ .../windows/platform_handler_unittests.cc | 32 +++++++++++++++++++ .../windows/platform_handler_win32.cc | 11 +++++++ .../platform/windows/platform_handler_win32.h | 5 +++ .../windows/platform_handler_winuwp.cc | 6 ++++ .../windows/platform_handler_winuwp.h | 5 +++ 7 files changed, 71 insertions(+) diff --git a/engine/src/flutter/shell/platform/windows/platform_handler.cc b/engine/src/flutter/shell/platform/windows/platform_handler.cc index 3b6d87aebf..c3791f1523 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler.cc @@ -11,6 +11,7 @@ static constexpr char kChannelName[] = "flutter/platform"; static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings"; static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; +static constexpr char kPlaySoundMethod[] = "SystemSound.play"; static constexpr char kTextPlainFormat[] = "text/plain"; static constexpr char kTextKey[] = "text"; @@ -64,6 +65,11 @@ void PlatformHandler::HandleMethodCall( return; } SetPlainText(itr->value.GetString(), std::move(result)); + } else if (method.compare(kPlaySoundMethod) == 0) { + // Only one string argument is expected. + const rapidjson::Value& sound_type = method_call.arguments()[0]; + + SystemSoundPlay(sound_type.GetString(), std::move(result)); } else { result->NotImplemented(); } diff --git a/engine/src/flutter/shell/platform/windows/platform_handler.h b/engine/src/flutter/shell/platform/windows/platform_handler.h index a4ceaa837a..3483273e96 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler.h +++ b/engine/src/flutter/shell/platform/windows/platform_handler.h @@ -42,9 +42,15 @@ class PlatformHandler { const std::string& text, std::unique_ptr> result) = 0; + virtual void SystemSoundPlay( + const std::string& sound_type, + std::unique_ptr> result) = 0; + // A error type to use for error responses. static constexpr char kClipboardError[] = "Clipboard error"; + static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert"; + private: // Called when a method is called on |channel_|; void HandleMethodCall( diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc b/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc index 6340cb6aa0..bcbeb070e8 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler_unittests.cc @@ -23,10 +23,13 @@ static constexpr char kChannelName[] = "flutter/platform"; static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings"; static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; +static constexpr char kPlaySoundMethod[] = "SystemSound.play"; static constexpr char kTextPlainFormat[] = "text/plain"; static constexpr char kFakeContentType[] = "text/madeupcontenttype"; +static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert"; + // Test implementation of PlatformHandler to allow testing the PlatformHandler // logic. class TestPlatformHandler : public PlatformHandler { @@ -45,6 +48,9 @@ class TestPlatformHandler : public PlatformHandler { MOCK_METHOD2(SetPlainText, void(const std::string&, std::unique_ptr>)); + MOCK_METHOD2(SystemSoundPlay, + void(const std::string&, + std::unique_ptr>)); }; // Mock result to inspect results of PlatformHandler calls. @@ -201,5 +207,31 @@ TEST(PlatformHandler, RejectsSettingUnknownTypes) { })); } +TEST(PlatformHandler, PlayingSystemSoundCallsThrough) { + TestBinaryMessenger messenger; + TestPlatformHandler platform_handler(&messenger); + + auto args = std::make_unique(rapidjson::kStringType); + auto& allocator = args->GetAllocator(); + args->SetString(kSoundTypeAlert); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kPlaySoundMethod, std::move(args))); + + // Set up a handler to call a response on |result| so that it doesn't log + // on destruction about leaking. + ON_CALL(platform_handler, SystemSoundPlay) + .WillByDefault( + [](auto sound_type, + std::unique_ptr> result) { + result->NotImplemented(); + }); + + EXPECT_CALL(platform_handler, + SystemSoundPlay(::testing::StrEq(kSoundTypeAlert), _)); + EXPECT_TRUE(messenger.SimulateEngineMessage( + kChannelName, encoded->data(), encoded->size(), + [](const uint8_t* reply, size_t reply_size) {})); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_win32.cc b/engine/src/flutter/shell/platform/windows/platform_handler_win32.cc index 9e73e431dd..7f3b885748 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_win32.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler_win32.cc @@ -269,4 +269,15 @@ void PlatformHandlerWin32::SetPlainText( result->Success(); } +void PlatformHandlerWin32::SystemSoundPlay( + const std::string& sound_type, + std::unique_ptr> result) { + if (sound_type.compare(kSoundTypeAlert) == 0) { + MessageBeep(MB_OK); + result->Success(); + } else { + result->NotImplemented(); + } +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_win32.h b/engine/src/flutter/shell/platform/windows/platform_handler_win32.h index 8477830f12..d69915d787 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_win32.h +++ b/engine/src/flutter/shell/platform/windows/platform_handler_win32.h @@ -37,6 +37,11 @@ class PlatformHandlerWin32 : public PlatformHandler { const std::string& text, std::unique_ptr> result) override; + // |PlatformHandler| + void SystemSoundPlay( + const std::string& sound_type, + std::unique_ptr> result) override; + private: // A reference to the Flutter view. FlutterWindowsView* view_; diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.cc b/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.cc index 3134727847..fab8f27175 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.cc @@ -108,4 +108,10 @@ void PlatformHandlerWinUwp::SetPlainText( result->Success(); } +void PlatformHandlerWinUwp::SystemSoundPlay( + const std::string& sound_type, + std::unique_ptr> result) { + result->NotImplemented(); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.h b/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.h index cae4f5ae65..c05bd6387a 100644 --- a/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.h +++ b/engine/src/flutter/shell/platform/windows/platform_handler_winuwp.h @@ -37,6 +37,11 @@ class PlatformHandlerWinUwp : public PlatformHandler { const std::string& text, std::unique_ptr> result) override; + // |PlatformHandler| + void SystemSoundPlay( + const std::string& sound_type, + std::unique_ptr> result) override; + private: // A reference to the Flutter view. FlutterWindowsView* view_;