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
33 class Handler {
34 public:
35 struct Description {
36 int64_t isolate_port = 0 /* illegal port by default. */;
37 std::string isolate_name;
38
39 Description() {}
40
41 Description(int64_t p_isolate_port, std::string p_isolate_name)
42 : isolate_port(p_isolate_port),
43 isolate_name(std::move(p_isolate_name)) {}
44
45 void Write(Handler* handler,
46 rapidjson::Value& value,
47 rapidjson::MemoryPoolAllocator<>& allocator) const;
48 };
49
50 using ServiceProtocolMap = std::map<std::string_view, std::string_view>;
51
52 virtual fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner(
53 std::string_view method) const = 0;
54
55 virtual Description GetServiceProtocolDescription() const = 0;
56
57 virtual bool HandleServiceProtocolMessage(
58 std::string_view method, // one if the extension names specified above.
59 const ServiceProtocolMap& params,
60 rapidjson::Document* response) = 0;
61 };
62
63 ServiceProtocol();
64
65 ~ServiceProtocol();
66
67 void ToggleHooks(bool set);
68
69 void AddHandler(Handler* handler, Handler::Description description);
70
71 void RemoveHandler(Handler* handler);
72
73 void SetHandlerDescription(Handler* handler,
74 Handler::Description description);
75
76 private:
77 const std::set<std::string_view> endpoints_;
78 std::unique_ptr<fml::SharedMutex> handlers_mutex_;
79 std::map<Handler*, fml::AtomicObject<Handler::Description>> handlers_;
80
81 [[nodiscard]] static bool HandleMessage(const char* method,
82 const char** param_keys,
83 const char** param_values,
84 intptr_t num_params,
85 void* user_data,
86 const char** json_object);
87 [[nodiscard]] static bool HandleMessage(
88 std::string_view method,
89 const Handler::ServiceProtocolMap& params,
90 ServiceProtocol* service_protocol,
91 rapidjson::Document* response);
92 [[nodiscard]] bool HandleMessage(std::string_view method,
93 const Handler::ServiceProtocolMap& params,
94 rapidjson::Document* response) const;
95
96 [[nodiscard]] bool HandleListViewsMethod(rapidjson::Document* response) const;
97
98 FML_DISALLOW_COPY_AND_ASSIGN(ServiceProtocol);
99};
100
101} // namespace flutter
102
103#endif // FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_
104