| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2014 Google Inc. 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 | |
| 31 #ifndef DartScriptWrappable_h | |
| 32 #define DartScriptWrappable_h | |
| 33 | |
| 34 #include "bindings/common/ScriptWrappable.h" | |
| 35 #include <dart_api.h> | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 class DartWrapperInfo { | |
| 40 public: | |
| 41 void* domData; | |
| 42 void* wrapper; | |
| 43 }; | |
| 44 | |
| 45 class DartMultiWrapperInfo { | |
| 46 public: | |
| 47 Vector<void*> domDatas; | |
| 48 Vector<void*> wrappers; | |
| 49 }; | |
| 50 | |
| 51 void ScriptWrappable::setDartWrapper(void* domData, void* wrapper) | |
| 52 { | |
| 53 ASSERT(domData); | |
| 54 if (LIKELY(m_dartWrapperInfo.isEmpty())) { | |
| 55 DartWrapperInfo* wrapperInfo = new DartWrapperInfo; | |
| 56 wrapperInfo->domData = domData; | |
| 57 wrapperInfo->wrapper = wrapper; | |
| 58 m_dartWrapperInfo = TaggedPointer(wrapperInfo); | |
| 59 } else if (m_dartWrapperInfo.isDartWrapperInfo()) { | |
| 60 DartWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartWrapperInfo(); | |
| 61 if (wrapperInfo->domData == domData) { | |
| 62 // Replace the current wrapper. | |
| 63 wrapperInfo->wrapper = wrapper; | |
| 64 } else { | |
| 65 // Inflate to a multiwrapper. | |
| 66 DartMultiWrapperInfo* multiWrapperInfo = new DartMultiWrapperInfo; | |
| 67 multiWrapperInfo->domDatas.append(wrapperInfo->domData); | |
| 68 multiWrapperInfo->wrappers.append(wrapperInfo->wrapper); | |
| 69 multiWrapperInfo->domDatas.append(domData); | |
| 70 multiWrapperInfo->wrappers.append(wrapper); | |
| 71 m_dartWrapperInfo = TaggedPointer(multiWrapperInfo); | |
| 72 delete wrapperInfo; | |
| 73 } | |
| 74 } else { | |
| 75 ASSERT(m_dartWrapperInfo.isDartMultiWrapperInfo()); | |
| 76 DartMultiWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartMultiWrapperIn
fo(); | |
| 77 size_t len = wrapperInfo->domDatas.size(); | |
| 78 for (size_t i = 0; i < len; i++) { | |
| 79 if (wrapperInfo->domDatas.at(i) == domData) { | |
| 80 // Replace the current wrapper. | |
| 81 wrapperInfo->wrappers[i] = wrapper; | |
| 82 return; | |
| 83 } | |
| 84 } | |
| 85 // Append wrapper for new isolate. | |
| 86 wrapperInfo->domDatas.append(domData); | |
| 87 wrapperInfo->wrappers.append(wrapper); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 void* ScriptWrappable::getDartWrapper(void* domData) const | |
| 92 { | |
| 93 ASSERT(domData); | |
| 94 if (m_dartWrapperInfo.isDartWrapperInfo()) { | |
| 95 DartWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartWrapperInfo(); | |
| 96 if (wrapperInfo->domData == domData) { | |
| 97 return wrapperInfo->wrapper; | |
| 98 } | |
| 99 return 0; | |
| 100 } | |
| 101 if (m_dartWrapperInfo.isDartMultiWrapperInfo()) { | |
| 102 DartMultiWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartMultiWrapperIn
fo(); | |
| 103 size_t len = wrapperInfo->domDatas.size(); | |
| 104 for (size_t i = 0; i < len; ++i) { | |
| 105 if (wrapperInfo->domDatas[i] == domData) { | |
| 106 return wrapperInfo->wrappers[i]; | |
| 107 } | |
| 108 } | |
| 109 return 0; | |
| 110 } | |
| 111 ASSERT(m_dartWrapperInfo.isWrapperTypeInfo() || m_dartWrapperInfo.isV8Wrappe
r()); | |
| 112 return 0; | |
| 113 } | |
| 114 | |
| 115 void ScriptWrappable::clearDartWrapper(void* domData) | |
| 116 { | |
| 117 if (LIKELY(m_dartWrapperInfo.isDartWrapperInfo())) { | |
| 118 DartWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartWrapperInfo(); | |
| 119 ASSERT(domData == wrapperInfo->domData); | |
| 120 m_dartWrapperInfo = TaggedPointer(); | |
| 121 delete wrapperInfo; | |
| 122 } else if (m_dartWrapperInfo.isDartMultiWrapperInfo()) { | |
| 123 // Remove. | |
| 124 DartMultiWrapperInfo* wrapperInfo = m_dartWrapperInfo.dartMultiWrapperIn
fo(); | |
| 125 size_t len = wrapperInfo->domDatas.size(); | |
| 126 for (size_t i = 0; i < len; i++) { | |
| 127 if (wrapperInfo->domDatas[i] == domData) { | |
| 128 wrapperInfo->domDatas.remove(i); | |
| 129 wrapperInfo->wrappers.remove(i); | |
| 130 if (len == 1) { | |
| 131 m_dartWrapperInfo = TaggedPointer(); | |
| 132 delete wrapperInfo; | |
| 133 } | |
| 134 return; | |
| 135 } | |
| 136 } | |
| 137 // Could not find wrapper. | |
| 138 ASSERT_NOT_REACHED(); | |
| 139 } else { | |
| 140 // No Dart wrappers. | |
| 141 ASSERT_NOT_REACHED(); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 } // namespace blink | |
| 146 | |
| 147 #endif // DartScriptWrappable_h | |
| OLD | NEW |