Flutter Engine
flutter::DartCallbackCache Class Reference

#include <callback_cache.h>

Static Public Member Functions

static void SetCachePath (const std::string &path)
 
static std::string GetCachePath ()
 
static int64_t GetCallbackHandle (const std::string &name, const std::string &class_name, const std::string &library_path)
 
static Dart_Handle GetCallback (int64_t handle)
 
static std::unique_ptr< DartCallbackRepresentationGetCallbackInformation (int64_t handle)
 
static void LoadCacheFromDisk ()
 

Detailed Description

Definition at line 24 of file callback_cache.h.

Member Function Documentation

◆ GetCachePath()

static std::string flutter::DartCallbackCache::GetCachePath ( )
inlinestatic

◆ GetCallback()

Dart_Handle flutter::DartCallbackCache::GetCallback ( int64_t  handle)
static

Definition at line 39 of file callback_cache.cc.

References flutter::DartCallbackRepresentation::class_name, flutter::DartCallbackRepresentation::library_path, and flutter::DartCallbackRepresentation::name.

Referenced by flutter::GetCallbackFromHandle().

39  {
40  std::scoped_lock lock(mutex_);
41  auto iterator = cache_.find(handle);
42  if (iterator != cache_.end()) {
43  DartCallbackRepresentation cb = iterator->second;
44  return LookupDartClosure(cb.name, cb.class_name, cb.library_path);
45  }
46  return Dart_Null();
47 }

◆ GetCallbackHandle()

int64_t flutter::DartCallbackCache::GetCallbackHandle ( const std::string &  name,
const std::string &  class_name,
const std::string &  library_path 
)
static

Definition at line 49 of file callback_cache.cc.

References name.

Referenced by flutter::GetCallbackHandle().

51  {
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(hash) == cache_.end()) {
59  cache_[hash] = {name, class_name, library_path};
60  SaveCacheToDisk();
61  }
62  return hash;
63 }
const char * name
Definition: fuchsia.cc:50

◆ GetCallbackInformation()

std::unique_ptr< DartCallbackRepresentation > flutter::DartCallbackCache::GetCallbackInformation ( int64_t  handle)
static

Definition at line 66 of file callback_cache.cc.

References flutter::DartCallbackRepresentation::class_name, flutter::DartCallbackRepresentation::library_path, and flutter::DartCallbackRepresentation::name.

Referenced by flutter::LookupCallbackInformation(), and FlutterCallbackCache::lookupCallbackInformation:.

66  {
67  std::scoped_lock lock(mutex_);
68  auto iterator = cache_.find(handle);
69  if (iterator != cache_.end()) {
70  return std::make_unique<DartCallbackRepresentation>(iterator->second);
71  }
72  return nullptr;
73 }

◆ LoadCacheFromDisk()

void flutter::DartCallbackCache::LoadCacheFromDisk ( )
static

Definition at line 117 of file callback_cache.cc.

References flutter::DartCallbackRepresentation::class_name, FML_LOG, flutter::kClassNameKey, flutter::kHandleKey, flutter::kLibraryPathKey, flutter::kNameKey, flutter::kRepresentationKey, flutter::DartCallbackRepresentation::library_path, flutter::DartCallbackRepresentation::name, name, and tonic::ToDart().

Referenced by FlutterPluginAppLifeCycleDelegate::application:willFinishLaunchingWithOptions:, and flutter::FlutterMain::GetSettings().

117  {
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(cache_contents.c_str());
132  if (d.HasParseError() || !d.IsArray()) {
133  FML_LOG(INFO) << "Could not parse callback cache, aborting restore";
134  // TODO(bkonyi): log and bail (delete cache?)
135  return;
136  }
137  const auto entries = d.GetArray();
138  for (auto* it = entries.begin(); it != entries.end(); ++it) {
139  const auto root_obj = it->GetObject();
140  const auto representation = root_obj[kRepresentationKey].GetObject();
141 
142  const int64_t hash = root_obj[kHandleKey].GetInt64();
143  DartCallbackRepresentation cb;
144  cb.name = representation[kNameKey].GetString();
145  cb.class_name = representation[kClassNameKey].GetString();
146  cb.library_path = representation[kLibraryPathKey].GetString();
147  cache_[hash] = cb;
148  }
149 }
static const char * kClassNameKey
static const char * kRepresentationKey
#define FML_LOG(severity)
Definition: logging.h:65
static const char * kLibraryPathKey
static const char * kNameKey
static const char * kHandleKey

◆ SetCachePath()

void flutter::DartCallbackCache::SetCachePath ( const std::string &  path)
static

Definition at line 35 of file callback_cache.cc.

References fml::paths::JoinPaths(), and flutter::path.

Referenced by flutter::FlutterMain::GetSettings(), and FlutterCallbackCache::lookupCallbackInformation:.

35  {
36  cache_path_ = fml::paths::JoinPaths({path, kCacheName});
37 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static const char * kCacheName
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14

The documentation for this class was generated from the following files: