| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium 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 "sky/engine/config.h" |
| 6 #include "sky/engine/tonic/dart_string_cache.h" |
| 7 |
| 8 #include "sky/engine/tonic/dart_state.h" |
| 9 #include "sky/engine/tonic/dart_string.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 DartStringCache::DartStringCache() : last_dart_string_(nullptr) { |
| 14 } |
| 15 |
| 16 DartStringCache::~DartStringCache() { |
| 17 } |
| 18 |
| 19 Dart_WeakPersistentHandle DartStringCache::GetSlow(StringImpl* string_impl, |
| 20 bool auto_scope) { |
| 21 if (Dart_WeakPersistentHandle string = cache_.get(string_impl)) { |
| 22 last_dart_string_ = string; |
| 23 last_string_impl_ = string_impl; |
| 24 return string; |
| 25 } |
| 26 |
| 27 if (!auto_scope) |
| 28 Dart_EnterScope(); |
| 29 |
| 30 Dart_Handle string = CreateDartString(string_impl); |
| 31 DCHECK(!Dart_IsError(string)); |
| 32 |
| 33 intptr_t size_in_bytes = string_impl->sizeInBytes(); |
| 34 Dart_WeakPersistentHandle wrapper = Dart_NewWeakPersistentHandle( |
| 35 string, string_impl, size_in_bytes, FinalizeCacheEntry); |
| 36 |
| 37 string_impl->ref(); // Balanced in FinalizeCacheEntry. |
| 38 cache_.set(string_impl, wrapper); |
| 39 |
| 40 last_dart_string_ = wrapper; |
| 41 last_string_impl_ = string_impl; |
| 42 |
| 43 if (!auto_scope) |
| 44 Dart_ExitScope(); |
| 45 |
| 46 return wrapper; |
| 47 } |
| 48 |
| 49 void DartStringCache::FinalizeCacheEntry(void* isolate_callback_data, |
| 50 Dart_WeakPersistentHandle handle, |
| 51 void* peer) { |
| 52 DartState* state = reinterpret_cast<DartState*>(isolate_callback_data); |
| 53 StringImpl* string_impl = reinterpret_cast<StringImpl*>(peer); |
| 54 DartStringCache& cache = state->string_cache(); |
| 55 |
| 56 Dart_WeakPersistentHandle cached_handle = cache.cache_.take(string_impl); |
| 57 ASSERT_UNUSED(cached_handle, handle == cached_handle); |
| 58 |
| 59 if (cache.last_dart_string_ == handle) { |
| 60 cache.last_dart_string_ = nullptr; |
| 61 cache.last_string_impl_ = nullptr; |
| 62 } |
| 63 |
| 64 string_impl->deref(); |
| 65 } |
| 66 |
| 67 } // namespace blink |
| OLD | NEW |