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_LIB_UI_CALLBACK_CACHE_H_ 6 #define FLUTTER_LIB_UI_CALLBACK_CACHE_H_ 7 8 #include <map> 9 #include <memory> 10 #include <mutex> 11 #include <string> 12 13 #include "flutter/fml/macros.h" 14 #include "flutter/fml/synchronization/thread_annotations.h" 15 #include "third_party/dart/runtime/include/dart_api.h" 16 17 namespace flutter { 18 19 typedef struct { 20 std::string name; 21 std::string class_name; 22 std::string library_path; 23 } DartCallbackRepresentation; 24 25 class DartCallbackCache { 26 public: 27 static void SetCachePath(const std::string& path); GetCachePath()28 static std::string GetCachePath() { return cache_path_; } 29 30 static int64_t GetCallbackHandle(const std::string& name, 31 const std::string& class_name, 32 const std::string& library_path) 33 FML_LOCKS_EXCLUDED(mutex_); 34 35 static Dart_Handle GetCallback(int64_t handle) FML_LOCKS_EXCLUDED(mutex_); 36 37 static std::unique_ptr<DartCallbackRepresentation> GetCallbackInformation( 38 int64_t handle) FML_LOCKS_EXCLUDED(mutex_); 39 40 static void LoadCacheFromDisk() FML_LOCKS_EXCLUDED(mutex_); 41 42 private: 43 static Dart_Handle LookupDartClosure(const std::string& name, 44 const std::string& class_name, 45 const std::string& library_path); 46 47 static void SaveCacheToDisk() FML_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 48 49 static std::mutex mutex_; 50 static std::string cache_path_; 51 52 static std::map<int64_t, DartCallbackRepresentation> cache_ 53 FML_GUARDED_BY(mutex_); 54 55 FML_DISALLOW_IMPLICIT_CONSTRUCTORS(DartCallbackCache); 56 }; 57 58 } // namespace flutter 59 60 #endif // FLUTTER_LIB_UI_CALLBACK_CACHE_H_ 61