• 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 #ifndef FLUTTER_SHELL_PLATFORM_CPP_INCOMING_MESSAGE_DISPATCHER_H_
6 #define FLUTTER_SHELL_PLATFORM_CPP_INCOMING_MESSAGE_DISPATCHER_H_
7 
8 #include <functional>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <utility>
13 
14 #include "flutter/shell/platform/common/cpp/public/flutter_messenger.h"
15 
16 namespace flutter {
17 
18 // Manages per-channel registration of callbacks for handling messages from the
19 // Flutter engine, and dispatching incoming messages to those handlers.
20 class IncomingMessageDispatcher {
21  public:
22   // Creates a new IncomingMessageDispatcher. |messenger| must remain valid as
23   // long as this object exists.
24   explicit IncomingMessageDispatcher(FlutterDesktopMessengerRef messenger);
25 
26   virtual ~IncomingMessageDispatcher();
27 
28   // Prevent copying.
29   IncomingMessageDispatcher(IncomingMessageDispatcher const&) = delete;
30   IncomingMessageDispatcher& operator=(IncomingMessageDispatcher const&) =
31       delete;
32 
33   // Routes |message| to to the registered handler for its channel, if any.
34   //
35   // If input blocking has been enabled on that channel, wraps the call to the
36   // handler with calls to the given callbacks to block and then unblock input.
37   //
38   // If no handler is registered for the message's channel, sends a
39   // NotImplemented response to the engine.
40   void HandleMessage(
41       const FlutterDesktopMessage& message,
42       std::function<void(void)> input_block_cb = [] {},
43       std::function<void(void)> input_unblock_cb = [] {});
44 
45   // Registers a message callback for incoming messages from the Flutter
46   // side on the specified channel. |callback| will be called with the message
47   // and |user_data| any time a message arrives on that channel.
48   //
49   // Replaces any existing callback. Pass a null callback to unregister the
50   // existing callback.
51   void SetMessageCallback(const std::string& channel,
52                           FlutterDesktopMessageCallback callback,
53                           void* user_data);
54 
55   // Enables input blocking on the given channel name.
56   //
57   // If set, then the parent window should disable input callbacks
58   // while waiting for the handler for messages on that channel to run.
59   void EnableInputBlockingForChannel(const std::string& channel);
60 
61  private:
62   // Handle for interacting with the C messaging API.
63   FlutterDesktopMessengerRef messenger_;
64 
65   // A map from channel names to the FlutterDesktopMessageCallback that should
66   // be called for incoming messages on that channel, along with the void* user
67   // data to pass to it.
68   std::map<std::string, std::pair<FlutterDesktopMessageCallback, void*>>
69       callbacks_;
70 
71   // Channel names for which input blocking should be enabled during the call to
72   // that channel's handler.
73   std::set<std::string> input_blocking_channels_;
74 };
75 
76 }  // namespace flutter
77 
78 #endif  // FLUTTER_SHELL_PLATFORM_CPP_INCOMING_MESSAGE_DISPATCHER_H_
79