Skip to content

[linux] Add the beginning of BinaryMessenger #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions library/linux/include/flutter_desktop_embedding/binary_messenger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_

#include <string>

namespace flutter_desktop_embedding {

// A protocol for a class that handles communication of binary data on named
// channels to and from the Flutter engine.
class BinaryMessenger {
public:
// Sends a binary message to the Flutter side on the specified channel,
// expecting no reply.
//
// TODO: Consider adding absl as a dependency and using absl::Span.
virtual void Send(const std::string &channel, const uint8_t *message,
const size_t message_size) const = 0;

// TODO: Add support for a version of Send expecting a reply once
// https://github.com/flutter/flutter/issues/18852 is fixed.

// TODO: Add SetMessageHandler. See Issue #102.
};

} // namespace flutter_desktop_embedding

#endif // LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BINARY_MESSENGER_H_
13 changes: 7 additions & 6 deletions library/linux/include/flutter_desktop_embedding/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#include <memory>
#include <string>

#include <flutter_embedder.h>

#include "binary_messenger.h"
#include "method_call.h"
#include "method_codec.h"
#include "method_result.h"
Expand Down Expand Up @@ -60,19 +59,21 @@ class Plugin {
// while waiting for this plugin to handle its platform message.
virtual bool input_blocking() const { return input_blocking_; }

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

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

private:
std::string channel_;
// Caller-owned instance of the Flutter Engine.
FlutterEngine engine_;
// Caller-owned instance of the binary messenger used to message the engine.
BinaryMessenger *messenger_;
bool input_blocking_;
};

Expand Down
2 changes: 1 addition & 1 deletion library/linux/src/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ namespace flutter_desktop_embedding {

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

Expand Down
11 changes: 11 additions & 0 deletions library/linux/src/internal/plugin_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ void PluginHandler::HandleMethodCallMessage(
}
}

void PluginHandler::Send(const std::string &channel, const uint8_t *message,
const size_t message_size) const {
FlutterPlatformMessage platform_message = {
.struct_size = sizeof(FlutterPlatformMessage),
.channel = channel.c_str(),
.message = message,
.message_size = message_size,
};
FlutterEngineSendPlatformMessage(engine_, &platform_message);
}

} // namespace flutter_desktop_embedding
7 changes: 6 additions & 1 deletion library/linux/src/internal/plugin_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

#include <flutter_embedder.h>

#include "library/linux/include/flutter_desktop_embedding/binary_messenger.h"
#include "library/linux/include/flutter_desktop_embedding/plugin.h"

namespace flutter_desktop_embedding {

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

// BinaryMessenger implementation:
void Send(const std::string &channel, const uint8_t *message,
const size_t message_size) const override;

private:
FlutterEngine engine_;
std::map<std::string, std::unique_ptr<Plugin>> plugins_;
Expand Down
12 changes: 3 additions & 9 deletions library/linux/src/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,17 @@
namespace flutter_desktop_embedding {

Plugin::Plugin(const std::string &channel, bool input_blocking)
: channel_(channel), engine_(nullptr), input_blocking_(input_blocking) {}
: channel_(channel), messenger_(nullptr), input_blocking_(input_blocking) {}

Plugin::~Plugin() {}

void Plugin::InvokeMethodCall(const MethodCall &method_call) {
if (!engine_) {
if (!messenger_) {
return;
}
std::unique_ptr<std::vector<uint8_t>> message =
GetCodec().EncodeMethodCall(method_call);
FlutterPlatformMessage platform_message_response = {
.struct_size = sizeof(FlutterPlatformMessage),
.channel = channel_.c_str(),
.message = message->data(),
.message_size = message->size(),
};
FlutterEngineSendPlatformMessage(engine_, &platform_message_response);
messenger_->Send(channel_, message->data(), message->size());
}

} // namespace flutter_desktop_embedding