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#ifndef FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_
6#define FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <string_view>
12
13#include "flutter/fml/compiler_specific.h"
14#include "flutter/fml/macros.h"
15#include "flutter/fml/synchronization/atomic_object.h"
16#include "flutter/fml/synchronization/shared_mutex.h"
17#include "flutter/fml/task_runner.h"
18#include "rapidjson/document.h"
19
20namespace flutter {
21
22class ServiceProtocol {
23 public:
24 static const std::string_view kScreenshotExtensionName;
25 static const std::string_view kScreenshotSkpExtensionName;
26 static const std::string_view kRunInViewExtensionName;
27 static const std::string_view kFlushUIThreadTasksExtensionName;
28 static const std::string_view kSetAssetBundlePathExtensionName;
29 static const std::string_view kGetDisplayRefreshRateExtensionName;
30 static const std::string_view kGetSkSLsExtensionName;
31 static const std::string_view kEstimateRasterCacheMemoryExtensionName;
32 static const std::string_view kRenderFrameWithRasterStatsExtensionName;
33 static const std::string_view kReloadAssetFonts;
34
35 class Handler {
36 public:
37 struct Description {
38 int64_t isolate_port = 0 /* illegal port by default. */;
39 std::string isolate_name;
40
41 Description() {}
42
43 Description(int64_t p_isolate_port, std::string p_isolate_name)
44 : isolate_port(p_isolate_port),
45 isolate_name(std::move(p_isolate_name)) {}
46
47 void Write(Handler* handler,
48 rapidjson::Value& value,
49 rapidjson::MemoryPoolAllocator<>& allocator) const;
50 };
51
52 using ServiceProtocolMap = std::map<std::string_view, std::string_view>;
53
54 virtual fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner(
55 std::string_view method) const = 0;
56
57 virtual Description GetServiceProtocolDescription() const = 0;
58
59 virtual bool HandleServiceProtocolMessage(
60 std::string_view method, // one if the extension names specified above.
61 const ServiceProtocolMap& params,
62 rapidjson::Document* response) = 0;
63 };
64
65 ServiceProtocol();
66
67 ~ServiceProtocol();
68
69 void ToggleHooks(bool set);
70
71 void AddHandler(Handler* handler, const Handler::Description& description);
72
73 void RemoveHandler(Handler* handler);
74
75 void SetHandlerDescription(Handler* handler,
76 const Handler::Description& description);
77
78 private:
79 const std::set<std::string_view> endpoints_;
80 std::unique_ptr<fml::SharedMutex> handlers_mutex_;
81 std::map<Handler*, fml::AtomicObject<Handler::Description>> handlers_;
82
83 [[nodiscard]] static bool HandleMessage(const char* method,
84 const char** param_keys,
85 const char** param_values,
86 intptr_t num_params,
87 void* user_data,
88 const char** json_object);
89 [[nodiscard]] static bool HandleMessage(
90 std::string_view method,
91 const Handler::ServiceProtocolMap& params,
92 ServiceProtocol* service_protocol,
93 rapidjson::Document* response);
94 [[nodiscard]] bool HandleMessage(std::string_view method,
95 const Handler::ServiceProtocolMap& params,
96 rapidjson::Document* response) const;
97
98 [[nodiscard]] bool HandleListViewsMethod(rapidjson::Document* response) const;
99
100 FML_DISALLOW_COPY_AND_ASSIGN(ServiceProtocol);
101};
102
103} // namespace flutter
104
105#endif // FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_
106

source code of flutter_engine/flutter/runtime/service_protocol.h