| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012, Google Inc. | |
| 2 // All rights reserved. | |
| 3 // | |
| 4 // Redistribution and use in source and binary forms, with or without | |
| 5 // modification, are permitted provided that the following conditions are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #include "config.h" | |
| 31 #include "bindings/core/dart/DartStringCache.h" | |
| 32 | |
| 33 #include "bindings/core/dart/DartDOMData.h" | |
| 34 #include "bindings/core/dart/DartUtilities.h" | |
| 35 | |
| 36 namespace blink { | |
| 37 | |
| 38 DartStringCache::DartStringCache() | |
| 39 { | |
| 40 m_lastStringImpl = nullptr; | |
| 41 m_lastDartString = 0; | |
| 42 } | |
| 43 | |
| 44 void DartStringCache::clearWeakHandles() | |
| 45 { | |
| 46 Dart_Isolate isolate = Dart_CurrentIsolate(); | |
| 47 for (StringCache::iterator it = m_stringCache.begin(); it != m_stringCache.e
nd(); ++it) { | |
| 48 it->key->deref(); | |
| 49 Dart_DeleteWeakPersistentHandle(isolate, it->value); | |
| 50 } | |
| 51 m_stringCache.clear(); | |
| 52 m_lastStringImpl = nullptr; | |
| 53 m_lastDartString = 0; | |
| 54 } | |
| 55 | |
| 56 Dart_WeakPersistentHandle DartStringCache::getSlow(StringImpl* stringImpl, bool
autoDartScope) | |
| 57 { | |
| 58 ASSERT(stringImpl); | |
| 59 | |
| 60 Dart_WeakPersistentHandle cachedString = m_stringCache.get(stringImpl); | |
| 61 if (cachedString) { | |
| 62 m_lastStringImpl = stringImpl; | |
| 63 m_lastDartString = cachedString; | |
| 64 return cachedString; | |
| 65 } | |
| 66 | |
| 67 if (!autoDartScope) | |
| 68 Dart_EnterScope(); | |
| 69 | |
| 70 ASSERT(stringImpl); | |
| 71 Dart_Handle newString = DartUtilities::stringImplToDartString(stringImpl); | |
| 72 ASSERT(!Dart_IsError(newString)); | |
| 73 | |
| 74 intptr_t peerSize = stringImpl->sizeInBytes(); | |
| 75 Dart_WeakPersistentHandle wrapper = Dart_NewWeakPersistentHandle(newString,
stringImpl, peerSize, handleFinalizer); | |
| 76 | |
| 77 stringImpl->ref(); | |
| 78 m_stringCache.set(stringImpl, wrapper); | |
| 79 | |
| 80 m_lastStringImpl = stringImpl; | |
| 81 m_lastDartString = wrapper; | |
| 82 | |
| 83 if (!autoDartScope) | |
| 84 Dart_ExitScope(); | |
| 85 | |
| 86 return wrapper; | |
| 87 } | |
| 88 | |
| 89 void DartStringCache::handleFinalizer(void* isolateCallbackData, Dart_WeakPersis
tentHandle handle, void* peer) | |
| 90 { | |
| 91 StringImpl* stringImpl = reinterpret_cast<StringImpl*>(peer); | |
| 92 DartDOMData* domData = reinterpret_cast<DartDOMData*>(isolateCallbackData); | |
| 93 DartStringCache& stringCache = domData->stringCache(); | |
| 94 Dart_WeakPersistentHandle ALLOW_UNUSED cached = stringCache.m_stringCache.ta
ke(stringImpl); | |
| 95 ASSERT(handle == cached); | |
| 96 if (stringCache.m_lastDartString == handle) { | |
| 97 stringCache.m_lastStringImpl = nullptr; | |
| 98 stringCache.m_lastDartString = 0; | |
| 99 } | |
| 100 stringImpl->deref(); | |
| 101 } | |
| 102 | |
| 103 } | |
| OLD | NEW |