Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/DOMWrapperMap.h

Issue 2843603002: Move ScriptWrappable and dependencies to platform/bindings (Closed)
Patch Set: Rebase and try again Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 10 matching lines...) Expand all
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef DOMWrapperMap_h 31 // This file has been moved to platform/bindings/DOMWrapperMap.h.
32 #define DOMWrapperMap_h 32 // TODO(adithyas): Remove this file.
33 33 #include "platform/bindings/DOMWrapperMap.h"
34 #include <utility>
35
36 #include "bindings/core/v8/WrapperTypeInfo.h"
37 #include "platform/ScriptForbiddenScope.h"
38 #include "platform/wtf/Compiler.h"
39 #include "platform/wtf/HashMap.h"
40 #include "v8/include/v8-util.h"
41 #include "v8/include/v8.h"
42
43 namespace blink {
44
45 template <class KeyType>
46 class DOMWrapperMap {
47 USING_FAST_MALLOC(DOMWrapperMap);
48
49 public:
50 explicit DOMWrapperMap(v8::Isolate* isolate)
51 : isolate_(isolate), map_(isolate) {}
52
53 v8::Local<v8::Object> NewLocal(v8::Isolate* isolate, KeyType* key) {
54 return map_.Get(key);
55 }
56
57 bool SetReturnValueFrom(v8::ReturnValue<v8::Value> return_value,
58 KeyType* key) {
59 return map_.SetReturnValue(key, return_value);
60 }
61
62 void SetReference(v8::Isolate* isolate,
63 const v8::Persistent<v8::Object>& parent,
64 KeyType* key) {
65 map_.SetReference(key, parent);
66 }
67
68 bool ContainsKey(KeyType* key) { return map_.Contains(key); }
69
70 WARN_UNUSED_RESULT bool Set(KeyType* key,
71 const WrapperTypeInfo* wrapper_type_info,
72 v8::Local<v8::Object>& wrapper) {
73 if (UNLIKELY(ContainsKey(key))) {
74 wrapper = NewLocal(isolate_, key);
75 return false;
76 }
77 v8::Global<v8::Object> global(isolate_, wrapper);
78 wrapper_type_info->ConfigureWrapper(&global);
79 map_.Set(key, std::move(global));
80 return true;
81 }
82
83 void RemoveIfAny(KeyType* key) {
84 if (ContainsKey(key))
85 map_.Remove(key);
86 }
87
88 void Clear() { map_.Clear(); }
89
90 void RemoveAndDispose(KeyType* key) {
91 DCHECK(ContainsKey(key));
92 map_.Remove(key);
93 }
94
95 void MarkWrapper(KeyType* object) {
96 map_.RegisterExternallyReferencedObject(object);
97 }
98
99 private:
100 class PersistentValueMapTraits {
101 STATIC_ONLY(PersistentValueMapTraits);
102
103 public:
104 // Map traits:
105 typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl;
106 typedef typename Impl::iterator Iterator;
107 static size_t Size(const Impl* impl) { return impl->size(); }
108 static bool Empty(Impl* impl) { return impl->IsEmpty(); }
109 static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
110 static Iterator Begin(Impl* impl) { return impl->begin(); }
111 static Iterator End(Impl* impl) { return impl->end(); }
112 static v8::PersistentContainerValue Value(Iterator& iter) {
113 return iter->value;
114 }
115 static KeyType* Key(Iterator& iter) { return iter->key; }
116 static v8::PersistentContainerValue
117 Set(Impl* impl, KeyType* key, v8::PersistentContainerValue value) {
118 v8::PersistentContainerValue old_value = Get(impl, key);
119 impl->Set(key, value);
120 return old_value;
121 }
122 static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key) {
123 return impl->at(key);
124 }
125
126 static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key) {
127 return impl->Take(key);
128 }
129
130 // Weak traits:
131 static const v8::PersistentContainerCallbackType kCallbackType =
132 v8::kWeakWithInternalFields;
133 typedef v8::GlobalValueMap<KeyType*, v8::Object, PersistentValueMapTraits>
134 MapType;
135 typedef MapType WeakCallbackDataType;
136
137 static WeakCallbackDataType* WeakCallbackParameter(
138 MapType* map,
139 KeyType* key,
140 v8::Local<v8::Object>& value) {
141 return map;
142 }
143
144 static void DisposeCallbackData(WeakCallbackDataType* callback_data) {}
145
146 static MapType* MapFromWeakCallbackInfo(
147 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
148 return data.GetParameter();
149 }
150
151 static KeyType* KeyFromWeakCallbackInfo(
152 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
153 return reinterpret_cast<KeyType*>(
154 data.GetInternalField(kV8DOMWrapperObjectIndex));
155 }
156
157 static void OnWeakCallback(
158 const v8::WeakCallbackInfo<WeakCallbackDataType>&) {}
159
160 static void Dispose(v8::Isolate*, v8::Global<v8::Object>, KeyType*);
161
162 static void DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType>&);
163 };
164
165 v8::Isolate* isolate_;
166 typename PersistentValueMapTraits::MapType map_;
167 };
168
169 } // namespace blink
170
171 #endif // DOMWrapperMap_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698