5#include "flutter/lib/ui/plugins/callback_cache.h"
10#include "flutter/fml/build_config.h"
11#include "flutter/fml/logging.h"
12#include "flutter/fml/paths.h"
13#include "rapidjson/document.h"
14#include "rapidjson/stringbuffer.h"
15#include "rapidjson/writer.h"
18using rapidjson::Document;
19using rapidjson::StringBuffer;
20using rapidjson::Writer;
30static const char*
kCacheName =
"flutter_callback_cache.json";
31std::mutex DartCallbackCache::mutex_;
32std::string DartCallbackCache::cache_path_;
33std::map<int64_t, DartCallbackRepresentation> DartCallbackCache::cache_;
40 std::scoped_lock lock(mutex_);
41 auto iterator = cache_.find(handle);
42 if (iterator != cache_.end()) {
51 const std::string& library_path) {
52 std::scoped_lock lock(mutex_);
53 std::hash<std::string> hasher;
56 hash += hasher(library_path);
58 if (cache_.find(
hash) == cache_.end()) {
65std::unique_ptr<DartCallbackRepresentation>
67 std::scoped_lock lock(mutex_);
68 auto iterator = cache_.find(handle);
69 if (iterator != cache_.end()) {
70 return std::make_unique<DartCallbackRepresentation>(iterator->second);
75void DartCallbackCache::SaveCacheToDisk() {
91 Writer<StringBuffer> writer(
s);
93 for (
auto iterator = cache_.begin(); iterator != cache_.end(); ++iterator) {
94 int64_t
hash = iterator->first;
100 writer.StartObject();
102 writer.String(cb.
name.c_str());
112 std::ofstream
output(cache_path_);
118 std::scoped_lock lock(mutex_);
121 if (!cache_.empty()) {
124 std::ifstream input(cache_path_);
128 std::string cache_contents{std::istreambuf_iterator<char>(input),
129 std::istreambuf_iterator<char>()};
131 d.Parse(cache_contents.c_str());
132 if (
d.HasParseError() || !
d.IsArray()) {
136 const auto entries =
d.GetArray();
137 for (
auto* it = entries.begin(); it != entries.end(); ++it) {
138 const auto root_obj = it->GetObject();
151 const std::string&
name,
153 const std::string& library_path) {
static uint32_t hash(const SkShaderBase::GradientInfo &v)
static std::unique_ptr< DartCallbackRepresentation > GetCallbackInformation(int64_t handle)
static int64_t GetCallbackHandle(const std::string &name, const std::string &class_name, const std::string &library_path)
static void LoadCacheFromDisk()
static Dart_Handle GetCallback(int64_t handle)
static void SetCachePath(const std::string &path)
DART_EXPORT Dart_Handle Dart_GetStaticMethodClosure(Dart_Handle library, Dart_Handle cls_type, Dart_Handle function_name)
struct _Dart_Handle * Dart_Handle
DART_EXPORT Dart_Handle Dart_LookupLibrary(Dart_Handle url)
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT Dart_Handle Dart_GetClass(Dart_Handle library, Dart_Handle class_name)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_RootLibrary(void)
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
const char *const class_name
static const char * kCacheName
static const char * kHandleKey
static const char * kLibraryPathKey
static const char * kClassNameKey
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
DEF_SWITCHES_START aot vmservice shared library name
static const char * kNameKey
static const char * kRepresentationKey
std::string JoinPaths(std::initializer_list< std::string > components)
std::function< void()> closure
Dart_Handle ToDart(const T &object)