5#define RAPIDJSON_HAS_STDSTRING 1
17#include "rapidjson/stringbuffer.h"
18#include "rapidjson/writer.h"
19#include "third_party/dart/runtime/include/dart_tools_api.h"
24 "_flutter.screenshot";
26 "_flutter.screenshotSkp";
30 "_flutter.flushUIThreadTasks";
32 "_flutter.setAssetBundlePath";
34 "_flutter.getDisplayRefreshRate";
39 "_flutter.estimateRasterCacheMemory";
41 "_flutter.reloadAssetFonts";
70 std::unique_lock lock(handlers_mutex_);
71 handlers_.emplace(
handler, description);
75 std::unique_lock lock(handlers_mutex_);
82 std::shared_lock lock(handlers_mutex_);
83 auto it = handlers_.find(
handler);
84 if (it != handlers_.end()) {
85 it->second.Store(description);
90 for (
const auto& endpoint : endpoints_) {
91 Dart_RegisterIsolateServiceRequestCallback(
93 &ServiceProtocol::HandleMessage,
101 document->SetObject();
102 document->AddMember(
"code", -32000, document->GetAllocator());
103 rapidjson::Value message_value;
104 message_value.SetString(
message, document->GetAllocator());
105 document->AddMember(
"message", message_value, document->GetAllocator());
108bool ServiceProtocol::HandleMessage(
const char* method,
109 const char** param_keys,
110 const char** param_values,
113 const char** json_object) {
115 for (intptr_t
i = 0;
i < num_params;
i++) {
116 params[std::string_view{param_keys[
i]}] = std::string_view{param_values[
i]};
120 FML_DLOG(INFO) <<
"Service protcol method: " << method;
122 for (intptr_t
i = 0;
i < num_params;
i++) {
123 FML_DLOG(INFO) <<
" " <<
i + 1 <<
": " << param_keys[
i] <<
" = "
128 rapidjson::Document document;
129 bool result = HandleMessage(std::string_view{method},
134 rapidjson::StringBuffer
buffer;
135 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
136 document.Accept(writer);
140 FML_DLOG(INFO) <<
"Response: " << *json_object;
141 FML_DLOG(INFO) <<
"RPC Result: " << result;
147bool ServiceProtocol::HandleMessage(std::string_view method,
148 const Handler::ServiceProtocolMap&
params,
149 ServiceProtocol* service_protocol,
150 rapidjson::Document* response) {
151 if (service_protocol ==
nullptr) {
156 return service_protocol->HandleMessage(method,
params, response);
161 std::string_view method,
163 rapidjson::Document* document) {
168 handler->GetServiceProtocolHandlerTaskRunner(method),
177 handler->HandleServiceProtocolMessage(method, params, document);
184bool ServiceProtocol::HandleMessage(std::string_view method,
185 const Handler::ServiceProtocolMap&
params,
186 rapidjson::Document* response)
const {
190 return HandleListViewsMethod(response);
193 std::shared_lock lock(handlers_mutex_);
195 if (handlers_.empty()) {
197 "There are no running service protocol handlers.");
202 auto view_id_param_found =
params.find(std::string_view{
"viewId"});
203 if (view_id_param_found !=
params.end()) {
205 view_id_param_found->second.data() +
kViewIdPrefx.size(),
nullptr, 16));
206 auto handler_found = handlers_.find(
handler);
207 if (handler_found != handlers_.end()) {
224 "Service protocol could not handle or find a handler for the "
225 "requested method.");
230 std::stringstream stream;
236 std::stringstream stream;
237 stream <<
"isolates/" << isolate;
243 rapidjson::Value&
view,
244 rapidjson::MemoryPoolAllocator<>& allocator)
const {
246 view.AddMember(
"type",
"FlutterView", allocator);
250 rapidjson::Value isolate(rapidjson::Type::kObjectType);
252 isolate.AddMember(
"type",
"@Isolate", allocator);
253 isolate.AddMember(
"fixedId",
true, allocator);
258 view.AddMember(
"isolate", isolate, allocator);
262bool ServiceProtocol::HandleListViewsMethod(
263 rapidjson::Document* response)
const {
264 std::shared_lock lock(handlers_mutex_);
265 std::vector<std::pair<intptr_t, Handler::Description>> descriptions;
266 descriptions.reserve(handlers_.size());
267 for (
const auto&
handler : handlers_) {
268 descriptions.emplace_back(
reinterpret_cast<intptr_t
>(
handler.first),
272 auto& allocator = response->GetAllocator();
275 response->SetObject();
276 response->AddMember(
"type",
"FlutterViewList", allocator);
278 rapidjson::Value viewsList(rapidjson::Type::kArrayType);
279 for (
const auto& description : descriptions) {
280 rapidjson::Value
view(rapidjson::Type::kObjectType);
281 description.second.Write(
reinterpret_cast<Handler*
>(description.first),
283 viewsList.PushBack(
view, allocator);
286 response->AddMember(
"views", viewsList, allocator);
std::map< std::string_view, std::string_view > ServiceProtocolMap
void AddHandler(Handler *handler, const Handler::Description &description)
static const std::string_view kSetAssetBundlePathExtensionName
static const std::string_view kReloadAssetFonts
static const std::string_view kScreenshotSkpExtensionName
static const std::string_view kScreenshotExtensionName
static const std::string_view kGetDisplayRefreshRateExtensionName
static const std::string_view kRunInViewExtensionName
static const std::string_view kEstimateRasterCacheMemoryExtensionName
static const std::string_view kGetSkSLsExtensionName
void RemoveHandler(Handler *handler)
void SetHandlerDescription(Handler *handler, const Handler::Description &description)
static const std::string_view kFlushUIThreadTasksExtensionName
void ToggleHooks(bool set)
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
const EmbeddedViewParams * params
const gchar FlBinaryMessengerMessageHandler handler
G_BEGIN_DECLS GBytes * message
#define FML_DLOG(severity)
#define FML_DCHECK(condition)
static std::string CreateIsolateID(int64_t isolate)
static std::string CreateFlutterViewID(intptr_t handler)
static void WriteServerErrorResponse(rapidjson::Document *document, const char *message)
static constexpr std::string_view kListViewsExtensionName
static constexpr std::string_view kViewIdPrefx
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 set
static bool HandleMessageOnHandler(ServiceProtocol::Handler *handler, std::string_view method, const ServiceProtocol::Handler::ServiceProtocolMap ¶ms, rapidjson::Document *document)
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
char * strdup(const char *str1)
void Write(Handler *handler, rapidjson::Value &value, rapidjson::MemoryPoolAllocator<> &allocator) const