| Flutter Windows Embedder |

accessibility_plugin.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/windows/accessibility_plugin.h"
6
7 #include <variant>
8
9 #include "flutter/fml/logging.h"
10 #include "flutter/fml/platform/win/wstring_conversion.h"
11 #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
12 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
13 #include "flutter/shell/platform/windows/flutter_windows_view.h"
14
15 namespace flutter {
16
17 namespace {
18
19 static constexpr char kAccessibilityChannelName[] = "flutter/accessibility";
20 static constexpr char kTypeKey[] = "type";
21 static constexpr char kDataKey[] = "data";
22 static constexpr char kMessageKey[] = "message";
23 static constexpr char kViewIdKey[] = "viewId";
24 static constexpr char kAnnounceValue[] = "announce";
25
26 // Handles messages like:
27 // {"type": "announce", "data": {"message": "Hello"}}
28 void HandleMessage(AccessibilityPlugin* plugin, const EncodableValue& message) {
29 const auto* map = std::get_if<EncodableMap>(&message);
30 if (!map) {
31 FML_LOG(ERROR) << "Accessibility message must be a map.";
32 return;
33 }
34 const auto& type_itr = map->find(EncodableValue{kTypeKey});
35 const auto& data_itr = map->find(EncodableValue{kDataKey});
36 if (type_itr == map->end()) {
37 FML_LOG(ERROR) << "Accessibility message must have a 'type' property.";
38 return;
39 }
40 if (data_itr == map->end()) {
41 FML_LOG(ERROR) << "Accessibility message must have a 'data' property.";
42 return;
43 }
44 const auto* type = std::get_if<std::string>(&type_itr->second);
45 const auto* data = std::get_if<EncodableMap>(&data_itr->second);
46 if (!type) {
47 FML_LOG(ERROR) << "Accessibility message 'type' property must be a string.";
48 return;
49 }
50 if (!data) {
51 FML_LOG(ERROR) << "Accessibility message 'data' property must be a map.";
52 return;
53 }
54
55 if (type->compare(kAnnounceValue) == 0) {
56 const auto& message_itr = data->find(EncodableValue{kMessageKey});
57 if (message_itr == data->end()) {
58 return;
59 }
60 const auto* message = std::get_if<std::string>(&message_itr->second);
61 if (!message) {
62 return;
63 }
64
65 const auto& view_itr = data->find(EncodableValue{kViewIdKey});
66 if (view_itr == data->end()) {
67 FML_LOG(ERROR) << "Announce message 'viewId' property is missing.";
68 return;
69 }
70
71 // The viewId may be encoded as either a 32-bit or 64-bit integer.
72 auto const view_id = view_itr->second.TryGetLongValue();
73 if (!view_id) {
74 FML_LOG(ERROR)
75 << "Announce message 'viewId' property must be a FlutterViewId.";
76 return;
77 }
78
79 plugin->Announce(*view_id, *message);
80 } else {
81 FML_LOG(WARNING) << "Accessibility message type '" << *type
82 << "' is not supported.";
83 }
84 }
85
86 } // namespace
87
88 AccessibilityPlugin::AccessibilityPlugin(FlutterWindowsEngine* engine)
89 : engine_(engine) {}
90
91 void AccessibilityPlugin::SetUp(BinaryMessenger* binary_messenger,
92 AccessibilityPlugin* plugin) {
93 BasicMessageChannel<> channel{binary_messenger, kAccessibilityChannelName,
94 &StandardMessageCodec::GetInstance()};
95
96 channel.SetMessageHandler(
97 [plugin](const EncodableValue& message,
98 const MessageReply<EncodableValue>& reply) {
99 HandleMessage(plugin, message);
100
101 // The accessibility channel does not support error handling.
102 // Always return an empty response even on failure.
103 reply(EncodableValue{std::monostate{}});
104 });
105 }
106
107 void AccessibilityPlugin::Announce(const FlutterViewId view_id,
108 const std::string_view message) {
109 if (!engine_->semantics_enabled()) {
110 return;
111 }
112
113 auto view = engine_->view(view_id);
114 if (!view) {
115 return;
116 }
117
118 std::wstring wide_text = fml::Utf8ToWideString(message);
119 view->AnnounceAlert(wide_text);
120 }
121
122 } // namespace flutter
accessibility_plugin.h
flutter::AccessibilityPlugin
Definition: accessibility_plugin.h:22
flutter::AccessibilityPlugin::AccessibilityPlugin
AccessibilityPlugin(FlutterWindowsEngine *engine)
Definition: accessibility_plugin.cc:88
flutter::AccessibilityPlugin::SetUp
static void SetUp(BinaryMessenger *binary_messenger, AccessibilityPlugin *plugin)
Definition: accessibility_plugin.cc:91
flutter::AccessibilityPlugin::Announce
virtual void Announce(const FlutterViewId view_id, const std::string_view message)
Definition: accessibility_plugin.cc:107
flutter::BasicMessageChannel
Definition: basic_message_channel.h:56
flutter::BinaryMessenger
Definition: binary_messenger.h:29
flutter::EncodableValue
Definition: encodable_value.h:166
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:92
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view(FlutterViewId view_id) const
Definition: flutter_windows_engine.cc:692
flutter::FlutterWindowsEngine::semantics_enabled
bool semantics_enabled() const
Definition: flutter_windows_engine.h:261
flutter::StandardMessageCodec::GetInstance
static const StandardMessageCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
Definition: standard_codec.cc:293
kAccessibilityChannelName
static constexpr char kAccessibilityChannelName[]
Definition: flutter_windows_engine.cc:36
flutter_windows_engine.h
flutter_windows_view.h
type
enum flutter::testing::@100::KeyboardChange::Type type
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterViewId
int64_t FlutterViewId
Definition: accessibility_plugin.h:15
flutter::MessageReply
std::function< void(const T &reply)> MessageReply
Definition: basic_message_channel.h:43
standard_message_codec.h