Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
6
7#include <variant>
8
14
15namespace flutter {
16
17namespace {
18
19static constexpr char kAccessibilityChannelName[] = "flutter/accessibility";
20static constexpr char kTypeKey[] = "type";
21static constexpr char kDataKey[] = "data";
22static constexpr char kMessageKey[] = "message";
23static constexpr char kViewIdKey[] = "viewId";
24static constexpr char kAnnounceValue[] = "announce";
25
26// Handles messages like:
27// {"type": "announce", "data": {"message": "Hello"}}
28void 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
90
92 AccessibilityPlugin* plugin) {
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
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
GLenum type
AccessibilityPlugin(FlutterWindowsEngine *engine)
static void SetUp(BinaryMessenger *binary_messenger, AccessibilityPlugin *plugin)
virtual void Announce(const FlutterViewId view_id, const std::string_view message)
FlutterWindowsView * view(FlutterViewId view_id) const
static const StandardMessageCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
FlutterEngine engine
Definition main.cc:84
static constexpr char kViewIdKey[]
static constexpr char kMessageKey[]
static constexpr char kDataKey[]
FlView * view
const char * message
const gchar * channel
G_BEGIN_DECLS FlutterViewId view_id
static constexpr char kAccessibilityChannelName[]
#define FML_LOG(severity)
Definition logging.h:101
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
constexpr char kTypeKey[]
Definition shell.cc:54
int64_t FlutterViewId
std::function< void(const T &reply)> MessageReply
std::wstring Utf8ToWideString(const std::string_view str)