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