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