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";
43 "_flutter.getPipelineUsage";
73 std::unique_lock lock(handlers_mutex_);
74 handlers_.emplace(
handler, description);
78 std::unique_lock lock(handlers_mutex_);
85 std::shared_lock lock(handlers_mutex_);
86 auto it = handlers_.find(
handler);
87 if (it != handlers_.end()) {
88 it->second.Store(description);
93 for (
const auto& endpoint : endpoints_) {
94 Dart_RegisterIsolateServiceRequestCallback(
96 &ServiceProtocol::HandleMessage,
104 document->SetObject();
105 document->AddMember(
"code", -32000, document->GetAllocator());
106 rapidjson::Value message_value;
107 message_value.SetString(
message, document->GetAllocator());
108 document->AddMember(
"message", message_value, document->GetAllocator());
111bool ServiceProtocol::HandleMessage(
const char* method,
112 const char** param_keys,
113 const char** param_values,
116 const char** json_object) {
118 for (intptr_t
i = 0;
i < num_params;
i++) {
119 params[std::string_view{param_keys[
i]}] = std::string_view{param_values[
i]};
123 FML_DLOG(INFO) <<
"Service protcol method: " << method;
125 for (intptr_t
i = 0;
i < num_params;
i++) {
126 FML_DLOG(INFO) <<
" " <<
i + 1 <<
": " << param_keys[
i] <<
" = "
131 rapidjson::Document document;
132 bool result = HandleMessage(std::string_view{method},
137 rapidjson::StringBuffer
buffer;
138 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
139 document.Accept(writer);
143 FML_DLOG(INFO) <<
"Response: " << *json_object;
144 FML_DLOG(INFO) <<
"RPC Result: " << result;
150bool ServiceProtocol::HandleMessage(std::string_view method,
151 const Handler::ServiceProtocolMap&
params,
152 ServiceProtocol* service_protocol,
153 rapidjson::Document* response) {
154 if (service_protocol ==
nullptr) {
159 return service_protocol->HandleMessage(method,
params, response);
164 std::string_view method,
166 rapidjson::Document* document) {
171 handler->GetServiceProtocolHandlerTaskRunner(method),
180 handler->HandleServiceProtocolMessage(method, params, document);
187bool ServiceProtocol::HandleMessage(std::string_view method,
188 const Handler::ServiceProtocolMap&
params,
189 rapidjson::Document* response)
const {
193 return HandleListViewsMethod(response);
196 std::shared_lock lock(handlers_mutex_);
198 if (handlers_.empty()) {
200 "There are no running service protocol handlers.");
205 auto view_id_param_found =
params.find(std::string_view{
"viewId"});
206 if (view_id_param_found !=
params.end()) {
208 view_id_param_found->second.data() +
kViewIdPrefx.size(),
nullptr, 16));
209 auto handler_found = handlers_.find(
handler);
210 if (handler_found != handlers_.end()) {
227 "Service protocol could not handle or find a handler for the "
228 "requested method.");
233 std::stringstream stream;
239 std::stringstream stream;
240 stream <<
"isolates/" << isolate;
246 rapidjson::Value&
view,
247 rapidjson::MemoryPoolAllocator<>&
allocator)
const {
253 rapidjson::Value isolate(rapidjson::Type::kObjectType);
255 isolate.AddMember(
"type",
"@Isolate",
allocator);
256 isolate.AddMember(
"fixedId",
true,
allocator);
265bool ServiceProtocol::HandleListViewsMethod(
266 rapidjson::Document* response)
const {
267 std::shared_lock lock(handlers_mutex_);
268 std::vector<std::pair<intptr_t, Handler::Description>> descriptions;
269 descriptions.reserve(handlers_.size());
270 for (
const auto&
handler : handlers_) {
271 descriptions.emplace_back(
reinterpret_cast<intptr_t
>(
handler.first),
275 auto&
allocator = response->GetAllocator();
278 response->SetObject();
279 response->AddMember(
"type",
"FlutterViewList",
allocator);
281 rapidjson::Value viewsList(rapidjson::Type::kArrayType);
282 for (
const auto& description : descriptions) {
283 rapidjson::Value
view(rapidjson::Type::kObjectType);
284 description.second.Write(
reinterpret_cast<Handler*
>(description.first),
289 response->AddMember(
"views", viewsList,
allocator);
std::map< std::string_view, std::string_view > ServiceProtocolMap
static const std::string_view kGetPipelineUsageExtensionName
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
#define FML_DLOG(severity)
#define FML_DCHECK(condition)
std::shared_ptr< ImpellerAllocator > allocator
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