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 "tonic/dart_class_library.h"
6
7#include "tonic/common/macros.h"
8#include "tonic/dart_wrapper_info.h"
9
10namespace tonic {
11
12DartClassLibrary::DartClassLibrary() {}
13
14DartClassLibrary::~DartClassLibrary() {
15 // Note that we don't need to delete these persistent handles because this
16 // object lives as long as the isolate. The handles will get deleted when the
17 // isolate dies.
18}
19
20Dart_PersistentHandle DartClassLibrary::GetClass(const DartWrapperInfo& info) {
21 const auto& result = info_cache_.insert(std::make_pair(&info, nullptr));
22 if (!result.second) {
23 // Already present, return value.
24 return result.first->second;
25 }
26 return GetAndCacheClass(info.library_name, info.interface_name,
27 &result.first->second);
28}
29
30Dart_PersistentHandle DartClassLibrary::GetClass(
31 const std::string& library_name,
32 const std::string& interface_name) {
33 auto key = std::make_pair(library_name, interface_name);
34 const auto& result = name_cache_.insert(std::make_pair(key, nullptr));
35 if (!result.second) {
36 // Already present, return value.
37 return result.first->second;
38 }
39 return GetAndCacheClass(library_name.c_str(), interface_name.c_str(),
40 &result.first->second);
41}
42
43Dart_PersistentHandle DartClassLibrary::GetAndCacheClass(
44 const char* library_name,
45 const char* interface_name,
46 Dart_PersistentHandle* cache_slot) {
47 auto it = providers_.find(library_name);
48 TONIC_DCHECK(it != providers_.end());
49
50 Dart_Handle class_handle = it->second->GetClassByName(interface_name);
51 *cache_slot = Dart_NewPersistentHandle(class_handle);
52 return *cache_slot;
53}
54
55} // namespace tonic
56