Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,14 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_codec.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_result.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_result_functions.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registry.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_result_functions_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar_unittests.cc
FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc
Expand Down
1 change: 1 addition & 0 deletions shell/platform/common/cpp/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ executable("common_cpp_unittests") {
deps = [
":common_cpp",
":common_cpp_fixtures",
"//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper",
"//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper_library_stubs",
"//flutter/testing",
]
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/common/cpp/client_wrapper/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ test_fixtures("client_wrapper_fixtures") {
executable("client_wrapper_unittests") {
testonly = true

# TODO: Add more unit tests.
sources = [
"basic_message_channel_unittests.cc",
"encodable_value_unittests.cc",
"method_call_unittests.cc",
"method_channel_unittests.cc",
"method_result_functions_unittests.cc",
"plugin_registrar_unittests.cc",
"standard_message_codec_unittests.cc",
"standard_method_codec_unittests.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ namespace {

class TestBinaryMessenger : public BinaryMessenger {
public:
void Send(const std::string& channel,
const uint8_t* message,
const size_t message_size) const override {}

void Send(const std::string& channel,
const uint8_t* message,
const size_t message_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ core_cpp_client_wrapper_includes =
"include/flutter/method_call.h",
"include/flutter/method_channel.h",
"include/flutter/method_codec.h",
"include/flutter/method_result_functions.h",
"include/flutter/method_result.h",
"include/flutter/plugin_registrar.h",
"include/flutter/plugin_registry.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
#include <functional>
#include <string>

// TODO: Consider adding absl as a dependency and using absl::Span for all of
// the message/message_size pairs.
namespace flutter {

// A binary message reply callback.
//
// Used for submitting a binary reply back to a Flutter message sender.
typedef std::function<void(const uint8_t* reply, const size_t reply_size)>
typedef std::function<void(const uint8_t* reply, size_t reply_size)>
BinaryReply;

// A message handler callback.
//
// Used for receiving messages from Flutter and providing an asynchronous reply.
typedef std::function<
void(const uint8_t* message, const size_t message_size, BinaryReply reply)>
void(const uint8_t* message, size_t message_size, BinaryReply reply)>
BinaryMessageHandler;

// A protocol for a class that handles communication of binary data on named
Expand All @@ -31,18 +29,14 @@ class BinaryMessenger {
public:
virtual ~BinaryMessenger() = default;

// Sends a binary message to the Flutter side on the specified channel,
// expecting no reply.
virtual void Send(const std::string& channel,
const uint8_t* message,
const size_t message_size) const = 0;

// Sends a binary message to the Flutter side on the specified channel,
// expecting a reply.
// Sends a binary message to the Flutter engine on the specified channel.
//
// If |reply| is provided, it will be called back with the response from the
// engine.
virtual void Send(const std::string& channel,
const uint8_t* message,
const size_t message_size,
BinaryReply reply) const = 0;
size_t message_size,
BinaryReply reply = nullptr) const = 0;

// Registers a message handler for incoming binary messages from the Flutter
// side on the specified channel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,46 @@ class MethodChannel {
MethodChannel& operator=(MethodChannel const&) = delete;

// Sends a message to the Flutter engine on this channel.
void InvokeMethod(const std::string& method, std::unique_ptr<T> arguments) {
MethodCall<T> method_call(method, std::move(arguments));
std::unique_ptr<std::vector<uint8_t>> message =
codec_->EncodeMethodCall(method_call);
messenger_->Send(name_, message->data(), message->size(), nullptr);
}

// Sends a message to the Flutter engine on this channel expecting a reply.
//
// If |result| is provided, one of its methods will be invoked with the
// response from the engine.
void InvokeMethod(const std::string& method,
std::unique_ptr<T> arguments,
flutter::BinaryReply reply) {
std::unique_ptr<MethodResult<T>> result = nullptr) {
MethodCall<T> method_call(method, std::move(arguments));
std::unique_ptr<std::vector<uint8_t>> message =
codec_->EncodeMethodCall(method_call);
messenger_->Send(name_, message->data(), message->size(), reply);
if (!result) {
messenger_->Send(name_, message->data(), message->size(), nullptr);
return;
}

// std::function requires a copyable lambda, so convert to a shared pointer.
// This is safe since only one copy of the shared_pointer will ever be
// accessed.
std::shared_ptr<MethodResult<T>> shared_result(result.release());
const auto* codec = codec_;
std::string channel_name = name_;
BinaryReply reply_handler = [shared_result, codec, channel_name](
const uint8_t* reply, size_t reply_size) {
if (reply_size == 0) {
shared_result->NotImplemented();
return;
}
// Use this channel's codec to decode and handle the
// reply.
bool decoded = codec->DecodeAndProcessResponseEnvelope(
reply, reply_size, shared_result.get());
if (!decoded) {
std::cerr << "Unable to decode reply to method "
"invocation on channel "
<< channel_name << std::endl;
shared_result->NotImplemented();
}
};

messenger_->Send(name_, message->data(), message->size(),
std::move(reply_handler));
}

// Registers a handler that should be called any time a method call is
Expand All @@ -76,7 +101,7 @@ class MethodChannel {
std::string channel_name = name_;
BinaryMessageHandler binary_handler = [handler, codec, channel_name](
const uint8_t* message,
const size_t message_size,
size_t message_size,
BinaryReply reply) {
// Use this channel's codec to decode the call and build a result handler.
auto result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "method_call.h"
#include "method_result.h"

namespace flutter {

Expand All @@ -28,9 +29,8 @@ class MethodCodec {

// Returns the MethodCall encoded in |message|, or nullptr if it cannot be
// decoded.
std::unique_ptr<MethodCall<T>> DecodeMethodCall(
const uint8_t* message,
const size_t message_size) const {
std::unique_ptr<MethodCall<T>> DecodeMethodCall(const uint8_t* message,
size_t message_size) const {
return std::move(DecodeMethodCallInternal(message, message_size));
}

Expand Down Expand Up @@ -67,11 +67,23 @@ class MethodCodec {
EncodeErrorEnvelopeInternal(error_code, error_message, error_details));
}

// Decodes the response envelope encoded in |response|, calling the
// appropriate method on |result|.
//
// Returns false if |response| cannot be decoded. In that case the caller is
// responsible for calling a |result| method.
bool DecodeAndProcessResponseEnvelope(const uint8_t* response,
size_t response_size,
MethodResult<T>* result) const {
return DecodeAndProcessResponseEnvelopeInternal(response, response_size,
result);
}

protected:
// Implementation of the public interface, to be provided by subclasses.
virtual std::unique_ptr<MethodCall<T>> DecodeMethodCallInternal(
const uint8_t* message,
const size_t message_size) const = 0;
size_t message_size) const = 0;

// Implementation of the public interface, to be provided by subclasses.
virtual std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal(
Expand All @@ -86,6 +98,12 @@ class MethodCodec {
const std::string& error_code,
const std::string& error_message,
const T* error_details) const = 0;

// Implementation of the public interface, to be provided by subclasses.
virtual bool DecodeAndProcessResponseEnvelopeInternal(
const uint8_t* response,
size_t response_size,
MethodResult<T>* result) const = 0;
};

} // namespace flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace flutter {

// Encapsulates a result sent back to the Flutter engine in response to a
// MethodCall. Only one method should be called on any given instance.
// Encapsulates a result returned from a MethodCall. Only one method should be
// called on any given instance.
template <typename T>
class MethodResult {
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_FUNCTIONS_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_FUNCTIONS_H_

#include <functional>
#include <string>

#include "method_result.h"

namespace flutter {

// Handler types for each of the MethodResult outcomes.
template <typename T>
using ResultHandlerSuccess = std::function<void(const T* result)>;
template <typename T>
using ResultHandlerError = std::function<void(const std::string& error_code,
const std::string& error_message,
const T* error_details)>;
template <typename T>
using ResultHandlerNotImplemented = std::function<void()>;

// An implementation of MethodResult that pass calls through to provided
// function objects, for ease of constructing one-off result handlers.
template <typename T>
class MethodResultFunctions : public MethodResult<T> {
public:
// Creates a result object that calls the provided functions for the
// corresponding MethodResult outcomes.
MethodResultFunctions(ResultHandlerSuccess<T> on_success,
ResultHandlerError<T> on_error,
ResultHandlerNotImplemented<T> on_not_implemented)
: on_success_(on_success),
on_error_(on_error),
on_not_implemented_(on_not_implemented) {}

virtual ~MethodResultFunctions() = default;

// Prevent copying.
MethodResultFunctions(MethodResultFunctions const&) = delete;
MethodResultFunctions& operator=(MethodResultFunctions const&) = delete;

protected:
// |flutter::MethodResult|
void SuccessInternal(const T* result) override {
if (on_success_) {
on_success_(result);
}
}

// |flutter::MethodResult|
void ErrorInternal(const std::string& error_code,
const std::string& error_message,
const T* error_details) override {
if (on_error_) {
on_error_(error_code, error_message, error_details);
}
}

// |flutter::MethodResult|
void NotImplementedInternal() override {
if (on_not_implemented_) {
on_not_implemented_();
}
}

private:
ResultHandlerSuccess<T> on_success_;
ResultHandlerError<T> on_error_;
ResultHandlerNotImplemented<T> on_not_implemented_;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_METHOD_RESULT_FUNCTIONS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class StandardMethodCodec : public MethodCodec<EncodableValue> {
// |flutter::MethodCodec|
std::unique_ptr<MethodCall<EncodableValue>> DecodeMethodCallInternal(
const uint8_t* message,
const size_t message_size) const override;
size_t message_size) const override;

// |flutter::MethodCodec|
std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal(
Expand All @@ -45,6 +45,12 @@ class StandardMethodCodec : public MethodCodec<EncodableValue> {
const std::string& error_code,
const std::string& error_message,
const EncodableValue* error_details) const override;

// |flutter::MethodCodec|
bool DecodeAndProcessResponseEnvelopeInternal(
const uint8_t* response,
size_t response_size,
MethodResult<EncodableValue>* result) const override;
};

} // namespace flutter
Expand Down
Loading