5#define RAPIDJSON_HAS_STDSTRING 1
7#include "flutter/runtime/service_protocol.h"
15#include "flutter/fml/posix_wrappers.h"
16#include "flutter/fml/synchronization/waitable_event.h"
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";
42 "_flutter.renderFrameWithRasterStats";
44 "_flutter.reloadAssetFonts";
76 handlers_.emplace(handler, description);
81 handlers_.erase(handler);
88 auto it = handlers_.find(handler);
89 if (it != handlers_.end()) {
90 it->second.Store(description);
95 for (
const auto& endpoint : endpoints_) {
98 &ServiceProtocol::HandleMessage,
106 document->SetObject();
107 document->AddMember(
"code", -32000, document->GetAllocator());
108 rapidjson::Value message_value;
109 message_value.SetString(
message, document->GetAllocator());
110 document->AddMember(
"message", message_value, document->GetAllocator());
113bool ServiceProtocol::HandleMessage(
const char* method,
114 const char** param_keys,
115 const char** param_values,
118 const char** json_object) {
120 for (intptr_t
i = 0;
i < num_params;
i++) {
121 params[std::string_view{param_keys[
i]}] = std::string_view{param_values[
i]};
125 FML_DLOG(INFO) <<
"Service protcol method: " << method;
127 for (intptr_t
i = 0;
i < num_params;
i++) {
128 FML_DLOG(INFO) <<
" " <<
i + 1 <<
": " << param_keys[
i] <<
" = "
133 rapidjson::Document document;
134 bool result = HandleMessage(std::string_view{method},
139 rapidjson::StringBuffer
buffer;
140 rapidjson::Writer<rapidjson::StringBuffer> writer(
buffer);
141 document.Accept(writer);
145 FML_DLOG(INFO) <<
"Response: " << *json_object;
152bool ServiceProtocol::HandleMessage(std::string_view method,
153 const Handler::ServiceProtocolMap&
params,
154 ServiceProtocol* service_protocol,
155 rapidjson::Document* response) {
156 if (service_protocol ==
nullptr) {
161 return service_protocol->HandleMessage(method,
params, response);
166 std::string_view method,
168 rapidjson::Document* document) {
182 handler->HandleServiceProtocolMessage(method, params, document);
189bool ServiceProtocol::HandleMessage(std::string_view method,
190 const Handler::ServiceProtocolMap&
params,
191 rapidjson::Document* response)
const {
195 return HandleListViewsMethod(response);
200 if (handlers_.empty()) {
202 "There are no running service protocol handlers.");
207 auto view_id_param_found =
params.find(std::string_view{
"viewId"});
208 if (view_id_param_found !=
params.end()) {
209 auto* handler =
reinterpret_cast<Handler*
>(std::stoull(
210 view_id_param_found->second.data() +
kViewIdPrefx.size(),
nullptr, 16));
211 auto handler_found = handlers_.find(handler);
212 if (handler_found != handlers_.end()) {
229 "Service protocol could not handle or find a handler for the "
230 "requested method.");
242 stream <<
"isolates/" << isolate;
248 rapidjson::Value& view,
249 rapidjson::MemoryPoolAllocator<>& allocator)
const {
251 view.AddMember(
"type",
"FlutterView", allocator);
255 rapidjson::Value isolate(rapidjson::Type::kObjectType);
257 isolate.AddMember(
"type",
"@Isolate", allocator);
258 isolate.AddMember(
"fixedId",
true, allocator);
263 view.AddMember(
"isolate", isolate, allocator);
267bool ServiceProtocol::HandleListViewsMethod(
268 rapidjson::Document* response)
const {
270 std::vector<std::pair<intptr_t, Handler::Description>> descriptions;
271 descriptions.reserve(handlers_.size());
272 for (
const auto& handler : handlers_) {
273 descriptions.emplace_back(
reinterpret_cast<intptr_t
>(handler.first),
274 handler.second.Load());
277 auto& allocator = response->GetAllocator();
280 response->SetObject();
281 response->AddMember(
"type",
"FlutterViewList", allocator);
283 rapidjson::Value viewsList(rapidjson::Type::kArrayType);
284 for (
const auto& description : descriptions) {
285 rapidjson::Value view(rapidjson::Type::kObjectType);
286 description.second.Write(
reinterpret_cast<Handler*
>(description.first),
288 viewsList.PushBack(view, allocator);
291 response->AddMember(
"views", viewsList, allocator);
std::map< std::string_view, std::string_view > ServiceProtocolMap
virtual fml::RefPtr< fml::TaskRunner > GetServiceProtocolHandlerTaskRunner(std::string_view method) const =0
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)
static const std::string_view kRenderFrameWithRasterStatsExtensionName
void SetHandlerDescription(Handler *handler, const Handler::Description &description)
static const std::string_view kFlushUIThreadTasksExtensionName
void ToggleHooks(bool set)
static SharedMutex * Create()
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
const EmbeddedViewParams * params
#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 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
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 set
char * strdup(const char *str1)
void Write(Handler *handler, rapidjson::Value &value, rapidjson::MemoryPoolAllocator<> &allocator) const