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