Skip to content

Commit 5fa4895

Browse files
stuartmorganawdavies
authored andcommitted
Split JsonMessageCodec out from JsonMethodCodec (#150)
Separates the basic JSON<->byte stream logic out into JsonMessageCodec, which is an eventual part of the final channel interface. Eventually this class will inherit from a generic MessageCodec interface, but in order to make this functionality available for library use while the type handling of the public interface is worked out, the new class is internal for now.
1 parent cd8ca96 commit 5fa4895

File tree

4 files changed

+123
-34
lines changed

4 files changed

+123
-34
lines changed

library/linux/include/flutter_desktop_embedding/json_method_codec.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#ifndef LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_METHOD_CODEC_H_
1515
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_METHOD_CODEC_H_
1616

17-
#include <json/json.h>
18-
1917
#include "method_codec.h"
2018

2119
namespace flutter_desktop_embedding {
@@ -49,11 +47,6 @@ class JsonMethodCodec : public MethodCodec {
4947
std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal(
5048
const std::string &error_code, const std::string &error_message,
5149
const void *error_details) const override;
52-
53-
private:
54-
// Serializes |json| into a byte stream.
55-
std::unique_ptr<std::vector<uint8_t>> EncodeJsonObject(
56-
const Json::Value &json) const;
5750
};
5851

5952
} // namespace flutter_desktop_embedding
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#include "library/linux/src/internal/json_message_codec.h"
15+
16+
#include <iostream>
17+
#include <string>
18+
19+
namespace flutter_desktop_embedding {
20+
21+
// static
22+
const JsonMessageCodec &JsonMessageCodec::GetInstance() {
23+
static JsonMessageCodec sInstance;
24+
return sInstance;
25+
}
26+
27+
std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessage(
28+
const Json::Value &message) const {
29+
Json::StreamWriterBuilder writer_builder;
30+
std::string serialization = Json::writeString(writer_builder, message);
31+
32+
return std::make_unique<std::vector<uint8_t>>(serialization.begin(),
33+
serialization.end());
34+
}
35+
36+
std::unique_ptr<Json::Value> JsonMessageCodec::DecodeMessage(
37+
const uint8_t *message, const size_t message_size) const {
38+
Json::CharReaderBuilder reader_builder;
39+
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());
40+
41+
auto raw_message = reinterpret_cast<const char *>(message);
42+
auto json_message = std::make_unique<Json::Value>();
43+
std::string parse_errors;
44+
bool parsing_successful =
45+
parser->parse(raw_message, raw_message + message_size, json_message.get(),
46+
&parse_errors);
47+
if (!parsing_successful) {
48+
std::cerr << "Unable to parse JSON message:" << std::endl
49+
<< parse_errors << std::endl;
50+
return nullptr;
51+
}
52+
return json_message;
53+
}
54+
55+
} // namespace flutter_desktop_embedding
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_JSON_MESSAGE_CODEC_H_
15+
#define LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_MESSAGE_CODEC_H_
16+
17+
#include <memory>
18+
#include <vector>
19+
20+
#include <json/json.h>
21+
22+
namespace flutter_desktop_embedding {
23+
24+
// A message encoding/decoding mechanism for communications to/from the
25+
// Flutter engine via JSON channels.
26+
//
27+
// TODO: Make this public, once generalizing a MessageCodec parent interface is
28+
// addressed; this is complicated by the return type of EncodeMessage.
29+
// Part of issue #102.
30+
class JsonMessageCodec {
31+
public:
32+
// Returns the shared instance of the codec.
33+
static const JsonMessageCodec &GetInstance();
34+
35+
~JsonMessageCodec() = default;
36+
37+
// Prevent copying.
38+
JsonMessageCodec(JsonMessageCodec const &) = delete;
39+
JsonMessageCodec &operator=(JsonMessageCodec const &) = delete;
40+
41+
// Returns a binary encoding of the given message, or nullptr if the
42+
// message cannot be serialized by this codec.
43+
std::unique_ptr<std::vector<uint8_t>> EncodeMessage(
44+
const Json::Value &message) const;
45+
46+
// Returns the JSON object encoded in |message|, or nullptr if it cannot be
47+
// decoded.
48+
// TODO: Consider adding absl as a dependency and using absl::Span.
49+
std::unique_ptr<Json::Value> DecodeMessage(const uint8_t *message,
50+
const size_t message_size) const;
51+
52+
protected:
53+
// Instances should be obtained via GetInstance.
54+
JsonMessageCodec() = default;
55+
};
56+
57+
} // namespace flutter_desktop_embedding
58+
59+
#endif // LIBRARY_LINUX_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_JSON_MESSAGE_CODEC_H_

library/linux/src/json_method_codec.cc

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
// limitations under the License.
1414
#include "library/linux/include/flutter_desktop_embedding/json_method_codec.h"
1515

16-
#include <iostream>
17-
1816
#include "library/linux/include/flutter_desktop_embedding/json_method_call.h"
17+
#include "library/linux/src/internal/json_message_codec.h"
1918

2019
namespace flutter_desktop_embedding {
2120

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

3433
std::unique_ptr<MethodCall> JsonMethodCodec::DecodeMethodCallInternal(
3534
const uint8_t *message, const size_t message_size) const {
36-
Json::CharReaderBuilder reader_builder;
37-
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());
38-
39-
auto raw_message = reinterpret_cast<const char *>(message);
40-
Json::Value json_message;
41-
std::string parse_errors;
42-
bool parsing_successful = parser->parse(
43-
raw_message, raw_message + message_size, &json_message, &parse_errors);
44-
if (!parsing_successful) {
45-
std::cerr << "Unable to parse JSON method call:" << std::endl
46-
<< parse_errors << std::endl;
35+
std::unique_ptr<Json::Value> json_message =
36+
JsonMessageCodec::GetInstance().DecodeMessage(message, message_size);
37+
if (!json_message) {
4738
return nullptr;
4839
}
4940

50-
Json::Value method = json_message[kMessageMethodKey];
41+
Json::Value method = (*json_message)[kMessageMethodKey];
5142
if (method.isNull()) {
5243
return nullptr;
5344
}
54-
Json::Value arguments = json_message[kMessageArgumentsKey];
45+
Json::Value arguments = (*json_message)[kMessageArgumentsKey];
5546
return std::make_unique<JsonMethodCall>(method.asString(), arguments);
5647
}
5748

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

65-
return EncodeJsonObject(message);
56+
return JsonMessageCodec::GetInstance().EncodeMessage(message);
6657
}
6758

6859
std::unique_ptr<std::vector<uint8_t>>
@@ -71,7 +62,7 @@ JsonMethodCodec::EncodeSuccessEnvelopeInternal(const void *result) const {
7162
envelope.append(result == nullptr
7263
? Json::Value()
7364
: *static_cast<const Json::Value *>(result));
74-
return EncodeJsonObject(envelope);
65+
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
7566
}
7667

7768
std::unique_ptr<std::vector<uint8_t>>
@@ -84,16 +75,7 @@ JsonMethodCodec::EncodeErrorEnvelopeInternal(const std::string &error_code,
8475
envelope.append(error_details == nullptr
8576
? Json::Value()
8677
: *static_cast<const Json::Value *>(error_details));
87-
return EncodeJsonObject(envelope);
88-
}
89-
90-
std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeJsonObject(
91-
const Json::Value &json) const {
92-
Json::StreamWriterBuilder writer_builder;
93-
std::string serialization = Json::writeString(writer_builder, json);
94-
95-
return std::make_unique<std::vector<uint8_t>>(serialization.begin(),
96-
serialization.end());
78+
return JsonMessageCodec::GetInstance().EncodeMessage(envelope);
9779
}
9880

9981
} // namespace flutter_desktop_embedding

0 commit comments

Comments
 (0)