Flutter Engine
The Flutter Engine
json_message_codec.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/common/json_message_codec.h"
6
7#include <iostream>
8#include <string>
9
10#include "rapidjson/error/en.h"
11#include "rapidjson/stringbuffer.h"
12#include "rapidjson/writer.h"
13
14namespace flutter {
15
16// static
18 static JsonMessageCodec sInstance;
19 return sInstance;
20}
21
22std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
23 const rapidjson::Document& message) const {
24 rapidjson::StringBuffer buffer;
25 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
26 // clang-tidy has trouble reasoning about some of the complicated array and
27 // pointer-arithmetic code in rapidjson.
28 // NOLINTNEXTLINE(clang-analyzer-core.*)
29 message.Accept(writer);
30 const char* buffer_start = buffer.GetString();
31 return std::make_unique<std::vector<uint8_t>>(
32 buffer_start, buffer_start + buffer.GetSize());
33}
34
35std::unique_ptr<rapidjson::Document> JsonMessageCodec::DecodeMessageInternal(
36 const uint8_t* binary_message,
37 const size_t message_size) const {
38 auto raw_message = reinterpret_cast<const char*>(binary_message);
39 auto json_message = std::make_unique<rapidjson::Document>();
40 rapidjson::ParseResult result =
41 json_message->Parse(raw_message, message_size);
42 if (result.IsError()) {
43 std::cerr << "Unable to parse JSON message:" << std::endl
44 << rapidjson::GetParseError_En(result.Code()) << std::endl;
45 return nullptr;
46 }
47 return json_message;
48}
49
50} // namespace flutter
static const JsonMessageCodec & GetInstance()
std::unique_ptr< rapidjson::Document > DecodeMessageInternal(const uint8_t *binary_message, const size_t message_size) const override
std::unique_ptr< std::vector< uint8_t > > EncodeMessageInternal(const rapidjson::Document &message) const override
GAsyncResult * result
Win32Message message
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126