• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common/cpp/incoming_message_dispatcher.h"
6 
7 namespace flutter {
8 
IncomingMessageDispatcher(FlutterDesktopMessengerRef messenger)9 IncomingMessageDispatcher::IncomingMessageDispatcher(
10     FlutterDesktopMessengerRef messenger)
11     : messenger_(messenger) {}
12 
13 IncomingMessageDispatcher::~IncomingMessageDispatcher() = default;
14 
HandleMessage(const FlutterDesktopMessage & message,std::function<void (void)> input_block_cb,std::function<void (void)> input_unblock_cb)15 void IncomingMessageDispatcher::HandleMessage(
16     const FlutterDesktopMessage& message,
17     std::function<void(void)> input_block_cb,
18     std::function<void(void)> input_unblock_cb) {
19   std::string channel(message.channel);
20 
21   // Find the handler for the channel; if there isn't one, report the failure.
22   if (callbacks_.find(channel) == callbacks_.end()) {
23     FlutterDesktopMessengerSendResponse(messenger_, message.response_handle,
24                                         nullptr, 0);
25     return;
26   }
27   auto& callback_info = callbacks_[channel];
28   FlutterDesktopMessageCallback message_callback = callback_info.first;
29 
30   // Process the call, handling input blocking if requested.
31   bool block_input = input_blocking_channels_.count(channel) > 0;
32   if (block_input) {
33     input_block_cb();
34   }
35   message_callback(messenger_, &message, callback_info.second);
36   if (block_input) {
37     input_unblock_cb();
38   }
39 }
40 
SetMessageCallback(const std::string & channel,FlutterDesktopMessageCallback callback,void * user_data)41 void IncomingMessageDispatcher::SetMessageCallback(
42     const std::string& channel,
43     FlutterDesktopMessageCallback callback,
44     void* user_data) {
45   if (!callback) {
46     callbacks_.erase(channel);
47     return;
48   }
49   callbacks_[channel] = std::make_pair(callback, user_data);
50 }
51 
EnableInputBlockingForChannel(const std::string & channel)52 void IncomingMessageDispatcher::EnableInputBlockingForChannel(
53     const std::string& channel) {
54   input_blocking_channels_.insert(channel);
55 }
56 
57 }  // namespace flutter
58