OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 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 Loading... |
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 V8DOMWrapper_h | 31 // This file has been moved to platform/bindings/V8DOMWrapper.h. |
32 #define V8DOMWrapper_h | 32 // TODO(adithyas): Remove this file. |
33 | 33 #include "platform/bindings/V8DOMWrapper.h" |
34 #include "bindings/core/v8/DOMDataStore.h" | |
35 #include "bindings/core/v8/ScriptWrappable.h" | |
36 #include "bindings/core/v8/V8Binding.h" | |
37 #include "bindings/core/v8/WrapperCreationSecurityCheck.h" | |
38 #include "core/CoreExport.h" | |
39 #include "platform/wtf/Compiler.h" | |
40 #include "platform/wtf/text/AtomicString.h" | |
41 #include "v8/include/v8.h" | |
42 | |
43 namespace blink { | |
44 | |
45 struct WrapperTypeInfo; | |
46 | |
47 class V8DOMWrapper { | |
48 STATIC_ONLY(V8DOMWrapper); | |
49 | |
50 public: | |
51 static v8::Local<v8::Object> CreateWrapper( | |
52 v8::Isolate*, | |
53 v8::Local<v8::Object> creation_context, | |
54 const WrapperTypeInfo*); | |
55 static bool IsWrapper(v8::Isolate*, v8::Local<v8::Value>); | |
56 | |
57 // Associates the given ScriptWrappable with the given |wrapper| if the | |
58 // ScriptWrappable is not yet associated with any wrapper. Returns the | |
59 // wrapper already associated or |wrapper| if not yet associated. | |
60 // The caller should always use the returned value rather than |wrapper|. | |
61 WARN_UNUSED_RESULT static v8::Local<v8::Object> AssociateObjectWithWrapper( | |
62 v8::Isolate*, | |
63 ScriptWrappable*, | |
64 const WrapperTypeInfo*, | |
65 v8::Local<v8::Object> wrapper); | |
66 static void SetNativeInfo(v8::Isolate*, | |
67 v8::Local<v8::Object>, | |
68 const WrapperTypeInfo*, | |
69 ScriptWrappable*); | |
70 static void ClearNativeInfo(v8::Isolate*, v8::Local<v8::Object>); | |
71 | |
72 // hasInternalFieldsSet only checks if the value has the internal fields for | |
73 // wrapper obejct and type, and does not check if it's valid or not. The | |
74 // value may not be a Blink's wrapper object. In order to make sure of it, | |
75 // Use isWrapper function instead. | |
76 CORE_EXPORT static bool HasInternalFieldsSet(v8::Local<v8::Value>); | |
77 }; | |
78 | |
79 inline void V8DOMWrapper::SetNativeInfo( | |
80 v8::Isolate* isolate, | |
81 v8::Local<v8::Object> wrapper, | |
82 const WrapperTypeInfo* wrapper_type_info, | |
83 ScriptWrappable* script_wrappable) { | |
84 DCHECK_GE(wrapper->InternalFieldCount(), 2); | |
85 DCHECK(script_wrappable); | |
86 DCHECK(wrapper_type_info); | |
87 int indices[] = {kV8DOMWrapperObjectIndex, kV8DOMWrapperTypeIndex}; | |
88 void* values[] = {script_wrappable, | |
89 const_cast<WrapperTypeInfo*>(wrapper_type_info)}; | |
90 wrapper->SetAlignedPointerInInternalFields(WTF_ARRAY_LENGTH(indices), indices, | |
91 values); | |
92 auto per_isolate_data = V8PerIsolateData::From(isolate); | |
93 // We notify ScriptWrappableVisitor about the new wrapper association, | |
94 // so the visitor can make sure to trace the association (in case it is | |
95 // currently tracing). Because of some optimizations, V8 will not | |
96 // necessarily detect wrappers created during its incremental marking. | |
97 per_isolate_data->GetScriptWrappableVisitor()->RegisterV8Reference( | |
98 std::make_pair(const_cast<WrapperTypeInfo*>(wrapper_type_info), | |
99 script_wrappable)); | |
100 } | |
101 | |
102 inline void V8DOMWrapper::ClearNativeInfo(v8::Isolate* isolate, | |
103 v8::Local<v8::Object> wrapper) { | |
104 int indices[] = {kV8DOMWrapperObjectIndex, kV8DOMWrapperTypeIndex}; | |
105 void* values[] = {nullptr, nullptr}; | |
106 wrapper->SetAlignedPointerInInternalFields(WTF_ARRAY_LENGTH(indices), indices, | |
107 values); | |
108 } | |
109 | |
110 inline v8::Local<v8::Object> V8DOMWrapper::AssociateObjectWithWrapper( | |
111 v8::Isolate* isolate, | |
112 ScriptWrappable* impl, | |
113 const WrapperTypeInfo* wrapper_type_info, | |
114 v8::Local<v8::Object> wrapper) { | |
115 if (DOMDataStore::SetWrapper(isolate, impl, wrapper_type_info, wrapper)) { | |
116 WrapperTypeInfo::WrapperCreated(); | |
117 SetNativeInfo(isolate, wrapper, wrapper_type_info, impl); | |
118 DCHECK(HasInternalFieldsSet(wrapper)); | |
119 } | |
120 SECURITY_CHECK(ToScriptWrappable(wrapper) == impl); | |
121 return wrapper; | |
122 } | |
123 | |
124 class V8WrapperInstantiationScope { | |
125 STACK_ALLOCATED(); | |
126 | |
127 public: | |
128 V8WrapperInstantiationScope(v8::Local<v8::Object> creation_context, | |
129 v8::Isolate* isolate, | |
130 const WrapperTypeInfo* type) | |
131 : did_enter_context_(false), | |
132 context_(isolate->GetCurrentContext()), | |
133 try_catch_(isolate), | |
134 type_(type), | |
135 access_check_failed_(false) { | |
136 // creationContext should not be empty. Because if we have an | |
137 // empty creationContext, we will end up creating | |
138 // a new object in the context currently entered. This is wrong. | |
139 CHECK(!creation_context.IsEmpty()); | |
140 v8::Local<v8::Context> context_for_wrapper = | |
141 creation_context->CreationContext(); | |
142 | |
143 // For performance, we enter the context only if the currently running | |
144 // context is different from the context that we are about to enter. | |
145 if (context_for_wrapper == context_) | |
146 return; | |
147 | |
148 context_ = context_for_wrapper; | |
149 | |
150 if (!WrapperCreationSecurityCheck::VerifyContextAccess(context_, type_)) { | |
151 DCHECK(try_catch_.HasCaught()); | |
152 try_catch_.ReThrow(); | |
153 access_check_failed_ = true; | |
154 return; | |
155 } | |
156 | |
157 did_enter_context_ = true; | |
158 context_->Enter(); | |
159 } | |
160 | |
161 ~V8WrapperInstantiationScope() { | |
162 if (!did_enter_context_) { | |
163 try_catch_.ReThrow(); | |
164 return; | |
165 } | |
166 context_->Exit(); | |
167 | |
168 if (access_check_failed_ || !try_catch_.HasCaught()) | |
169 return; | |
170 | |
171 // Any exception caught here is a cross context exception and it may not be | |
172 // safe to directly rethrow the exception in the current context (without | |
173 // converting it). rethrowCrossContextException converts the exception in | |
174 // such a scenario. | |
175 v8::Local<v8::Value> caught_exception = try_catch_.Exception(); | |
176 try_catch_.Reset(); | |
177 WrapperCreationSecurityCheck::RethrowCrossContextException( | |
178 context_, type_, caught_exception); | |
179 try_catch_.ReThrow(); | |
180 } | |
181 | |
182 v8::Local<v8::Context> GetContext() const { return context_; } | |
183 bool AccessCheckFailed() const { return access_check_failed_; } | |
184 | |
185 private: | |
186 bool did_enter_context_; | |
187 v8::Local<v8::Context> context_; | |
188 v8::TryCatch try_catch_; | |
189 const WrapperTypeInfo* type_; | |
190 bool access_check_failed_; | |
191 }; | |
192 | |
193 } // namespace blink | |
194 | |
195 #endif // V8DOMWrapper_h | |
OLD | NEW |