Skip to content

Split JsonMessageCodec out from JsonMethodCodec #150

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 2 commits into from
Nov 14, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#ifndef LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_METHOD_CODEC_H_
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_METHOD_CODEC_H_

#include <json/json.h>

#include "method_codec.h"

namespace flutter_desktop_embedding {
Expand Down Expand Up @@ -49,11 +47,6 @@ class JsonMethodCodec : public MethodCodec {
std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal(
const std::string &error_code, const std::string &error_message,
const void *error_details) const override;

private:
// Serializes |json| into a byte stream.
std::unique_ptr<std::vector<uint8_t>> EncodeJsonObject(
const Json::Value &json) const;
};

} // namespace flutter_desktop_embedding
Expand Down
55 changes: 55 additions & 0 deletions library/linux/src/internal/json_message_codec.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.
#include "library/linux/src/internal/json_message_codec.h"

#include <iostream>
#include <string>

namespace flutter_desktop_embedding {

// static
const JsonMessageCodec &JsonMessageCodec::GetInstance() {
static JsonMessageCodec sInstance;
return sInstance;
}

std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessage(
const Json::Value &message) const {
Json::StreamWriterBuilder writer_builder;
std::string serialization = Json::writeString(writer_builder, message);

return std::make_unique<std::vector<uint8_t>>(serialization.begin(),
serialization.end());
}

std::unique_ptr<Json::Value> JsonMessageCodec::DecodeMessage(
const uint8_t *message, const size_t message_size) const {
Json::CharReaderBuilder reader_builder;
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());

auto raw_message = reinterpret_cast<const char *>(message);
auto json_message = std::make_unique<Json::Value>();
std::string parse_errors;
bool parsing_successful =
parser->parse(raw_message, raw_message + message_size, json_message.get(),
&parse_errors);
if (!parsing_successful) {
std::cerr << "Unable to parse JSON message:" << std::endl
<< parse_errors << std::endl;
return nullptr;
}
return json_message;
}

} // namespace flutter_desktop_embedding
59 changes: 59 additions & 0 deletions library/linux/src/internal/json_message_codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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_JSON_MESSAGE_CODEC_H_
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_MESSAGE_CODEC_H_

#include <memory>
#include <vector>

#include <json/json.h>

namespace flutter_desktop_embedding {

// A message encoding/decoding mechanism for communications to/from the
// Flutter engine via JSON channels.
//
// TODO: Make this public, once generalizing a MessageCodec parent interface is
// addressed; this is complicated by the return type of EncodeMessage.
// Part of issue #102.
class JsonMessageCodec {
public:
// Returns the shared instance of the codec.
static const JsonMessageCodec &GetInstance();

~JsonMessageCodec() = default;

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

// Returns a binary encoding of the given message, or nullptr if the
// message cannot be serialized by this codec.
std::unique_ptr<std::vector<uint8_t>> EncodeMessage(
const Json::Value &message) const;

// Returns the JSON object encoded in |message|, or nullptr if it cannot be
// decoded.
// TODO: Consider adding absl as a dependency and using absl::Span.
std::unique_ptr<Json::Value> DecodeMessage(const uint8_t *message,
const size_t message_size) const;

protected:
// Instances should be obtained via GetInstance.
JsonMessageCodec() = default;
};

} // namespace flutter_desktop_embedding

#endif // LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_MESSAGE_CODEC_H_
36 changes: 9 additions & 27 deletions library/linux/src/json_method_codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// limitations under the License.
#include "library/linux/include/flutter_desktop_embedding/json_method_codec.h"

#include <iostream>

#include "library/linux/include/flutter_desktop_embedding/json_method_call.h"
#include "library/linux/src/internal/json_message_codec.h"

namespace flutter_desktop_embedding {

Expand All @@ -33,25 +32,17 @@ const JsonMethodCodec &JsonMethodCodec::GetInstance() {

std::unique_ptr<MethodCall> JsonMethodCodec::DecodeMethodCallInternal(
const uint8_t *message, const size_t message_size) const {
Json::CharReaderBuilder reader_builder;
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());

auto raw_message = reinterpret_cast<const char *>(message);
Json::Value json_message;
std::string parse_errors;
bool parsing_successful = parser->parse(
raw_message, raw_message + message_size, &json_message, &parse_errors);
if (!parsing_successful) {
std::cerr << "Unable to parse JSON method call:" << std::endl
<< parse_errors << std::endl;
std::unique_ptr<Json::Value> json_message =
JsonMessageCodec::GetInstance().DecodeMessage(message, message_size);
if (!json_message) {
return nullptr;
}

Json::Value method = json_message[kMessageMethodKey];
Json::Value method = (*json_message)[kMessageMethodKey];
if (method.isNull()) {
return nullptr;
}
Json::Value arguments = json_message[kMessageArgumentsKey];
Json::Value arguments = (*json_message)[kMessageArgumentsKey];
return std::make_unique<JsonMethodCall>(method.asString(), arguments);
}

Expand All @@ -62,7 +53,7 @@ std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
message[kMessageArgumentsKey] =
*static_cast<const Json::Value *>(method_call.arguments());

return EncodeJsonObject(message);
return JsonMessageCodec::GetInstance().EncodeMessage(message);
}

std::unique_ptr<std::vector<uint8_t>>
Expand All @@ -71,7 +62,7 @@ JsonMethodCodec::EncodeSuccessEnvelopeInternal(const void *result) const {
envelope.append(result == nullptr
? Json::Value()
: *static_cast<const Json::Value *>(result));
return EncodeJsonObject(envelope);
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
}

std::unique_ptr<std::vector<uint8_t>>
Expand All @@ -84,16 +75,7 @@ JsonMethodCodec::EncodeErrorEnvelopeInternal(const std::string &error_code,
envelope.append(error_details == nullptr
? Json::Value()
: *static_cast<const Json::Value *>(error_details));
return EncodeJsonObject(envelope);
}

std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeJsonObject(
const Json::Value &json) const {
Json::StreamWriterBuilder writer_builder;
std::string serialization = Json::writeString(writer_builder, json);

return std::make_unique<std::vector<uint8_t>>(serialization.begin(),
serialization.end());
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
}

} // namespace flutter_desktop_embedding