Flutter Engine
The Flutter Engine
json_method_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_method_codec.h"
6
7#include "flutter/shell/platform/common/json_message_codec.h"
8
9namespace flutter {
10
11namespace {
12
13// Keys used in MethodCall encoding.
14constexpr char kMessageMethodKey[] = "method";
15constexpr char kMessageArgumentsKey[] = "args";
16
17// Returns a new document containing only |element|, which must be an element
18// in |document|. This is a move rather than a copy, so it is efficient but
19// destructive to the data in |document|.
20std::unique_ptr<rapidjson::Document> ExtractElement(
21 rapidjson::Document* document,
22 rapidjson::Value* subtree) {
23 auto extracted = std::make_unique<rapidjson::Document>();
24 // Pull the subtree up to the root of the document.
25 document->Swap(*subtree);
26 // Swap the entire document into |extracted|. Unlike the swap above this moves
27 // the allocator ownership, so the data won't be deleted when |document| is
28 // destroyed.
29 extracted->Swap(*document);
30 return extracted;
31}
32
33} // namespace
34
35// static
37 static JsonMethodCodec sInstance;
38 return sInstance;
39}
40
41std::unique_ptr<MethodCall<rapidjson::Document>>
43 size_t message_size) const {
44 std::unique_ptr<rapidjson::Document> json_message =
46 if (!json_message) {
47 return nullptr;
48 }
49
50 auto method_name_iter = json_message->FindMember(kMessageMethodKey);
51 if (method_name_iter == json_message->MemberEnd()) {
52 return nullptr;
53 }
54 if (!method_name_iter->value.IsString()) {
55 return nullptr;
56 }
57 std::string method_name(method_name_iter->value.GetString());
58 auto arguments_iter = json_message->FindMember(kMessageArgumentsKey);
59 std::unique_ptr<rapidjson::Document> arguments;
60 if (arguments_iter != json_message->MemberEnd()) {
61 arguments = ExtractElement(json_message.get(), &(arguments_iter->value));
62 }
63 return std::make_unique<MethodCall<rapidjson::Document>>(
64 method_name, std::move(arguments));
65}
66
67std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
69 // TODO(stuartmorgan): Consider revisiting the codec APIs to avoid the need
70 // to copy everything when doing encoding (e.g., by having a version that
71 // takes owership of the object to encode, so that it can be moved instead).
72 rapidjson::Document message(rapidjson::kObjectType);
73 auto& allocator = message.GetAllocator();
74 rapidjson::Value name(method_call.method_name(), allocator);
75 rapidjson::Value arguments;
76 if (method_call.arguments()) {
77 arguments.CopyFrom(*method_call.arguments(), allocator);
78 }
79 message.AddMember(kMessageMethodKey, name, allocator);
80 message.AddMember(kMessageArgumentsKey, arguments, allocator);
81
83}
84
85std::unique_ptr<std::vector<uint8_t>>
87 const rapidjson::Document* result) const {
88 rapidjson::Document envelope;
89 envelope.SetArray();
90 rapidjson::Value result_value;
91 if (result) {
92 result_value.CopyFrom(*result, envelope.GetAllocator());
93 }
94 envelope.PushBack(result_value, envelope.GetAllocator());
95
97}
98
99std::unique_ptr<std::vector<uint8_t>>
101 const std::string& error_code,
102 const std::string& error_message,
103 const rapidjson::Document* error_details) const {
104 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
105 rapidjson::Document envelope(rapidjson::kArrayType);
106 auto& allocator = envelope.GetAllocator();
107 envelope.PushBack(rapidjson::Value(error_code, allocator), allocator);
108 envelope.PushBack(rapidjson::Value(error_message, allocator), allocator);
109 rapidjson::Value details_value;
110 if (error_details) {
111 details_value.CopyFrom(*error_details, allocator);
112 }
113 envelope.PushBack(details_value, allocator);
114
116}
117
119 const uint8_t* response,
120 size_t response_size,
122 std::unique_ptr<rapidjson::Document> json_response =
123 JsonMessageCodec::GetInstance().DecodeMessage(response, response_size);
124 if (!json_response) {
125 return false;
126 }
127 if (!json_response->IsArray()) {
128 return false;
129 }
130 switch (json_response->Size()) {
131 case 1: {
132 std::unique_ptr<rapidjson::Document> value =
133 ExtractElement(json_response.get(), &((*json_response)[0]));
134 if (value->IsNull()) {
135 result->Success();
136 } else {
137 result->Success(*value);
138 }
139 return true;
140 }
141 case 3: {
142 std::string code = (*json_response)[0].GetString();
143 std::string message = (*json_response)[1].GetString();
144 std::unique_ptr<rapidjson::Document> details =
145 ExtractElement(json_response.get(), &((*json_response)[2]));
146 if (details->IsNull()) {
147 result->Error(code, message);
148 } else {
149 result->Error(code, message, *details);
150 }
151 return true;
152 }
153 default:
154 return false;
155 }
156}
157
158} // namespace flutter
static const JsonMessageCodec & GetInstance()
static const JsonMethodCodec & GetInstance()
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCallInternal(const MethodCall< rapidjson::Document > &method_call) const override
std::unique_ptr< MethodCall< rapidjson::Document > > DecodeMethodCallInternal(const uint8_t *message, const size_t message_size) const override
bool DecodeAndProcessResponseEnvelopeInternal(const uint8_t *response, const size_t response_size, MethodResult< rapidjson::Document > *result) const override
std::unique_ptr< std::vector< uint8_t > > EncodeErrorEnvelopeInternal(const std::string &error_code, const std::string &error_message, const rapidjson::Document *error_details) const override
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelopeInternal(const rapidjson::Document *result) const override
std::unique_ptr< T > DecodeMessage(const uint8_t *binary_message, const size_t message_size) const
Definition: message_codec.h:29
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
uint8_t value
GAsyncResult * result
Win32Message message
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32