Skip to content

Commit cd8ca96

Browse files
authored
[linux] Add the beginning of BinaryMessenger (#151)
Adds the BinaryMessanger interface, containing only the send API for now. This eliminates the engine pointer from a public header, and is a small step toward moving toward supporting MessageChannel/MethodChannel. Technically this is a breaking change, but there's no expectation that any code outside of the library would be using set_engine, so should not actually impact anyone. Part of issue #102.
1 parent cf7ceeb commit cd8ca96

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_
15+
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_
16+
17+
#include <string>
18+
19+
namespace flutter_desktop_embedding {
20+
21+
// A protocol for a class that handles communication of binary data on named
22+
// channels to and from the Flutter engine.
23+
class BinaryMessenger {
24+
public:
25+
// Sends a binary message to the Flutter side on the specified channel,
26+
// expecting no reply.
27+
//
28+
// TODO: Consider adding absl as a dependency and using absl::Span.
29+
virtual void Send(const std::string &channel, const uint8_t *message,
30+
const size_t message_size) const = 0;
31+
32+
// TODO: Add support for a version of Send expecting a reply once
33+
// https://github.com/flutter/flutter/issues/18852 is fixed.
34+
35+
// TODO: Add SetMessageHandler. See Issue #102.
36+
};
37+
38+
} // namespace flutter_desktop_embedding
39+
40+
#endif // LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_

library/linux/include/flutter_desktop_embedding/plugin.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#include <memory>
1919
#include <string>
2020

21-
#include <flutter_embedder.h>
22-
21+
#include "binary_messenger.h"
2322
#include "method_call.h"
2423
#include "method_codec.h"
2524
#include "method_result.h"
@@ -60,19 +59,21 @@ class Plugin {
6059
// while waiting for this plugin to handle its platform message.
6160
virtual bool input_blocking() const { return input_blocking_; }
6261

63-
// Sets the pointer to the caller-owned Flutter Engine.
62+
// Sets the pointer to the caller-owned binary messenger.
6463
//
6564
// The embedder typically sets this pointer rather than the client.
66-
virtual void set_flutter_engine(FlutterEngine engine) { engine_ = engine; }
65+
virtual void set_binary_messenger(BinaryMessenger *messenger) {
66+
messenger_ = messenger;
67+
}
6768

6869
protected:
6970
// Calls a method in the Flutter engine on this Plugin's channel.
7071
void InvokeMethodCall(const MethodCall &method_call);
7172

7273
private:
7374
std::string channel_;
74-
// Caller-owned instance of the Flutter Engine.
75-
FlutterEngine engine_;
75+
// Caller-owned instance of the binary messenger used to message the engine.
76+
BinaryMessenger *messenger_;
7677
bool input_blocking_;
7778
};
7879

library/linux/src/embedder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ namespace flutter_desktop_embedding {
242242

243243
bool AddPlugin(GLFWwindow *flutter_window, std::unique_ptr<Plugin> plugin) {
244244
auto state = GetSavedEmbedderState(flutter_window);
245-
plugin->set_flutter_engine(state->engine);
245+
plugin->set_binary_messenger(state->plugin_handler.get());
246246
return state->plugin_handler->AddPlugin(std::move(plugin));
247247
}
248248

library/linux/src/internal/plugin_handler.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@ void PluginHandler::HandleMethodCallMessage(
7070
}
7171
}
7272

73+
void PluginHandler::Send(const std::string &channel, const uint8_t *message,
74+
const size_t message_size) const {
75+
FlutterPlatformMessage platform_message = {
76+
.struct_size = sizeof(FlutterPlatformMessage),
77+
.channel = channel.c_str(),
78+
.message = message,
79+
.message_size = message_size,
80+
};
81+
FlutterEngineSendPlatformMessage(engine_, &platform_message);
82+
}
83+
7384
} // namespace flutter_desktop_embedding

library/linux/src/internal/plugin_handler.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
#include <flutter_embedder.h>
2222

23+
#include "library/linux/include/flutter_desktop_embedding/binary_messenger.h"
2324
#include "library/linux/include/flutter_desktop_embedding/plugin.h"
2425

2526
namespace flutter_desktop_embedding {
2627

2728
// A class for managing a set of plugins.
2829
//
2930
// The plugins all map from a unique channel name to an actual plugin.
30-
class PluginHandler {
31+
class PluginHandler : public BinaryMessenger {
3132
public:
3233
// Creates a new PluginHandler. |engine| must remain valid as long as this
3334
// object exists.
@@ -59,6 +60,10 @@ class PluginHandler {
5960
std::function<void(void)> input_unblock_cb =
6061
[] {});
6162

63+
// BinaryMessenger implementation:
64+
void Send(const std::string &channel, const uint8_t *message,
65+
const size_t message_size) const override;
66+
6267
private:
6368
FlutterEngine engine_;
6469
std::map<std::string, std::unique_ptr<Plugin>> plugins_;

library/linux/src/plugin.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,17 @@
1818
namespace flutter_desktop_embedding {
1919

2020
Plugin::Plugin(const std::string &channel, bool input_blocking)
21-
: channel_(channel), engine_(nullptr), input_blocking_(input_blocking) {}
21+
: channel_(channel), messenger_(nullptr), input_blocking_(input_blocking) {}
2222

2323
Plugin::~Plugin() {}
2424

2525
void Plugin::InvokeMethodCall(const MethodCall &method_call) {
26-
if (!engine_) {
26+
if (!messenger_) {
2727
return;
2828
}
2929
std::unique_ptr<std::vector<uint8_t>> message =
3030
GetCodec().EncodeMethodCall(method_call);
31-
FlutterPlatformMessage platform_message_response = {
32-
.struct_size = sizeof(FlutterPlatformMessage),
33-
.channel = channel_.c_str(),
34-
.message = message->data(),
35-
.message_size = message->size(),
36-
};
37-
FlutterEngineSendPlatformMessage(engine_, &platform_message_response);
31+
messenger_->Send(channel_, message->data(), message->size());
3832
}
3933

4034
} // namespace flutter_desktop_embedding

0 commit comments

Comments
 (0)