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 #include <fstream>
6 #include <iterator>
7
8 #include "flutter/fml/build_config.h"
9 #include "flutter/fml/logging.h"
10 #include "flutter/fml/paths.h"
11 #include "flutter/lib/ui/plugins/callback_cache.h"
12 #include "rapidjson/document.h"
13 #include "rapidjson/stringbuffer.h"
14 #include "rapidjson/writer.h"
15
16 using rapidjson::Document;
17 using rapidjson::StringBuffer;
18 using rapidjson::Writer;
19 using tonic::ToDart;
20
21 namespace flutter {
22
23 static const char* kHandleKey = "handle";
24 static const char* kRepresentationKey = "representation";
25 static const char* kNameKey = "name";
26 static const char* kClassNameKey = "class_name";
27 static const char* kLibraryPathKey = "library_path";
28 static const char* kCacheName = "flutter_callback_cache.json";
29 std::mutex DartCallbackCache::mutex_;
30 std::string DartCallbackCache::cache_path_;
31 std::map<int64_t, DartCallbackRepresentation> DartCallbackCache::cache_;
32
SetCachePath(const std::string & path)33 void DartCallbackCache::SetCachePath(const std::string& path) {
34 cache_path_ = fml::paths::JoinPaths({path, kCacheName});
35 }
36
GetCallback(int64_t handle)37 Dart_Handle DartCallbackCache::GetCallback(int64_t handle) {
38 std::scoped_lock lock(mutex_);
39 auto iterator = cache_.find(handle);
40 if (iterator != cache_.end()) {
41 DartCallbackRepresentation cb = iterator->second;
42 return LookupDartClosure(cb.name, cb.class_name, cb.library_path);
43 }
44 return Dart_Null();
45 }
46
GetCallbackHandle(const std::string & name,const std::string & class_name,const std::string & library_path)47 int64_t DartCallbackCache::GetCallbackHandle(const std::string& name,
48 const std::string& class_name,
49 const std::string& library_path) {
50 std::scoped_lock lock(mutex_);
51 std::hash<std::string> hasher;
52 int64_t hash = hasher(name);
53 hash += hasher(class_name);
54 hash += hasher(library_path);
55
56 if (cache_.find(hash) == cache_.end()) {
57 cache_[hash] = {name, class_name, library_path};
58 SaveCacheToDisk();
59 }
60 return hash;
61 }
62
63 std::unique_ptr<DartCallbackRepresentation>
GetCallbackInformation(int64_t handle)64 DartCallbackCache::GetCallbackInformation(int64_t handle) {
65 std::scoped_lock lock(mutex_);
66 auto iterator = cache_.find(handle);
67 if (iterator != cache_.end()) {
68 return std::make_unique<DartCallbackRepresentation>(iterator->second);
69 }
70 return nullptr;
71 }
72
SaveCacheToDisk()73 void DartCallbackCache::SaveCacheToDisk() {
74 // Cache JSON format
75 // [
76 // {
77 // "hash": 42,
78 // "representation": {
79 // "name": "...",
80 // "class_name": "...",
81 // "library_path": "..."
82 // }
83 // },
84 // {
85 // ...
86 // }
87 // ]
88 StringBuffer s;
89 Writer<StringBuffer> writer(s);
90 writer.StartArray();
91 for (auto iterator = cache_.begin(); iterator != cache_.end(); ++iterator) {
92 int64_t hash = iterator->first;
93 DartCallbackRepresentation cb = iterator->second;
94 writer.StartObject();
95 writer.Key(kHandleKey);
96 writer.Int64(hash);
97 writer.Key(kRepresentationKey);
98 writer.StartObject();
99 writer.Key(kNameKey);
100 writer.String(cb.name.c_str());
101 writer.Key(kClassNameKey);
102 writer.String(cb.class_name.c_str());
103 writer.Key(kLibraryPathKey);
104 writer.String(cb.library_path.c_str());
105 writer.EndObject();
106 writer.EndObject();
107 }
108 writer.EndArray();
109
110 std::ofstream output(cache_path_);
111 output << s.GetString();
112 output.close();
113 }
114
LoadCacheFromDisk()115 void DartCallbackCache::LoadCacheFromDisk() {
116 std::scoped_lock lock(mutex_);
117
118 // Don't reload the cache if it's already populated.
119 if (!cache_.empty()) {
120 return;
121 }
122 std::ifstream input(cache_path_);
123 if (!input) {
124 return;
125 }
126 std::string cache_contents{std::istreambuf_iterator<char>(input),
127 std::istreambuf_iterator<char>()};
128 Document d;
129 d.Parse(cache_contents.c_str());
130 if (d.HasParseError() || !d.IsArray()) {
131 FML_LOG(WARNING) << "Could not parse callback cache, aborting restore";
132 // TODO(bkonyi): log and bail (delete cache?)
133 return;
134 }
135 const auto entries = d.GetArray();
136 for (auto* it = entries.begin(); it != entries.end(); ++it) {
137 const auto root_obj = it->GetObject();
138 const auto representation = root_obj[kRepresentationKey].GetObject();
139
140 const int64_t hash = root_obj[kHandleKey].GetInt64();
141 DartCallbackRepresentation cb;
142 cb.name = representation[kNameKey].GetString();
143 cb.class_name = representation[kClassNameKey].GetString();
144 cb.library_path = representation[kLibraryPathKey].GetString();
145 cache_[hash] = cb;
146 }
147 }
148
LookupDartClosure(const std::string & name,const std::string & class_name,const std::string & library_path)149 Dart_Handle DartCallbackCache::LookupDartClosure(
150 const std::string& name,
151 const std::string& class_name,
152 const std::string& library_path) {
153 Dart_Handle closure_name = ToDart(name);
154 if (Dart_IsError(closure_name)) {
155 return closure_name;
156 }
157 Dart_Handle library_name =
158 library_path.empty() ? Dart_Null() : ToDart(library_path);
159 if (Dart_IsError(library_name)) {
160 return library_name;
161 }
162 Dart_Handle cls_name = class_name.empty() ? Dart_Null() : ToDart(class_name);
163 if (Dart_IsError(cls_name)) {
164 return cls_name;
165 }
166
167 Dart_Handle library;
168 if (library_name == Dart_Null()) {
169 library = Dart_RootLibrary();
170 } else {
171 library = Dart_LookupLibrary(library_name);
172 }
173 if (Dart_IsError(library)) {
174 return library;
175 }
176
177 Dart_Handle closure;
178 if (Dart_IsNull(cls_name)) {
179 closure = Dart_GetField(library, closure_name);
180 } else {
181 Dart_Handle cls = Dart_GetClass(library, cls_name);
182 if (Dart_IsError(cls)) {
183 return cls;
184 }
185 if (Dart_IsNull(cls)) {
186 closure = Dart_Null();
187 } else {
188 closure = Dart_GetStaticMethodClosure(library, cls, closure_name);
189 }
190 }
191 return closure;
192 }
193
194 } // namespace flutter
195