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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8PerIsolateData.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) 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 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef V8PerIsolateData_h 26 // This file has been moved to platform/bindings/V8PerIsolateData.h.
27 #define V8PerIsolateData_h 27 // TODO(adithyas): Remove this file.
28 28 #include "platform/bindings/V8PerIsolateData.h"
29 #include <memory>
30
31 #include "bindings/core/v8/ScopedPersistent.h"
32 #include "bindings/core/v8/ScriptState.h"
33 #include "bindings/core/v8/ScriptWrappableVisitor.h"
34 #include "bindings/core/v8/WrapperTypeInfo.h"
35 #include "core/CoreExport.h"
36 #include "gin/public/isolate_holder.h"
37 #include "gin/public/v8_idle_task_runner.h"
38 #include "platform/heap/Handle.h"
39 #include "platform/wtf/HashMap.h"
40 #include "platform/wtf/Noncopyable.h"
41 #include "platform/wtf/Vector.h"
42 #include "v8/include/v8.h"
43
44 namespace blink {
45
46 class ActiveScriptWrappableBase;
47 class DOMDataStore;
48 class StringCache;
49 class V8PrivateProperty;
50 class WebTaskRunner;
51 struct WrapperTypeInfo;
52
53 typedef WTF::Vector<DOMDataStore*> DOMDataStoreList;
54
55 class CORE_EXPORT V8PerIsolateData {
56 USING_FAST_MALLOC(V8PerIsolateData);
57 WTF_MAKE_NONCOPYABLE(V8PerIsolateData);
58
59 public:
60 class EndOfScopeTask {
61 USING_FAST_MALLOC(EndOfScopeTask);
62
63 public:
64 virtual ~EndOfScopeTask() {}
65 virtual void Run() = 0;
66 };
67
68 // Disables the UseCounter.
69 // UseCounter depends on the current context, but it's not available during
70 // the initialization of v8::Context and the global object. So we need to
71 // disable the UseCounter while the initialization of the context and global
72 // object.
73 // TODO(yukishiino): Come up with an idea to remove this hack.
74 class UseCounterDisabledScope {
75 STACK_ALLOCATED();
76
77 public:
78 explicit UseCounterDisabledScope(V8PerIsolateData* per_isolate_data)
79 : per_isolate_data_(per_isolate_data),
80 original_use_counter_disabled_(
81 per_isolate_data_->use_counter_disabled_) {
82 per_isolate_data_->use_counter_disabled_ = true;
83 }
84 ~UseCounterDisabledScope() {
85 per_isolate_data_->use_counter_disabled_ = original_use_counter_disabled_;
86 }
87
88 private:
89 V8PerIsolateData* per_isolate_data_;
90 const bool original_use_counter_disabled_;
91 };
92
93 // Use this class to abstract away types of members that are pointers to core/
94 // objects, which are simply owned and released by V8PerIsolateData (see
95 // m_threadDebugger for an example).
96 class CORE_EXPORT Data {
97 public:
98 virtual ~Data() = default;
99 };
100
101 static v8::Isolate* Initialize(WebTaskRunner*);
102
103 static V8PerIsolateData* From(v8::Isolate* isolate) {
104 DCHECK(isolate);
105 DCHECK(isolate->GetData(gin::kEmbedderBlink));
106 return static_cast<V8PerIsolateData*>(
107 isolate->GetData(gin::kEmbedderBlink));
108 }
109
110 static void WillBeDestroyed(v8::Isolate*);
111 static void Destroy(v8::Isolate*);
112 static v8::Isolate* MainThreadIsolate();
113
114 static void EnableIdleTasks(v8::Isolate*,
115 std::unique_ptr<gin::V8IdleTaskRunner>);
116
117 v8::Isolate* GetIsolate() { return isolate_holder_.isolate(); }
118
119 StringCache* GetStringCache() { return string_cache_.get(); }
120
121 v8::Persistent<v8::Value>& EnsureLiveRoot();
122
123 bool IsHandlingRecursionLevelError() const {
124 return is_handling_recursion_level_error_;
125 }
126 void SetIsHandlingRecursionLevelError(bool value) {
127 is_handling_recursion_level_error_ = value;
128 }
129
130 bool IsReportingException() const { return is_reporting_exception_; }
131 void SetReportingException(bool value) { is_reporting_exception_ = value; }
132
133 bool IsUseCounterDisabled() const { return use_counter_disabled_; }
134
135 V8PrivateProperty* PrivateProperty() { return private_property_.get(); }
136
137 // Accessors to the cache of interface templates.
138 v8::Local<v8::FunctionTemplate> FindInterfaceTemplate(const DOMWrapperWorld&,
139 const void* key);
140 void SetInterfaceTemplate(const DOMWrapperWorld&,
141 const void* key,
142 v8::Local<v8::FunctionTemplate>);
143
144 // Accessor to the cache of cross-origin accessible operation's templates.
145 // Created templates get automatically cached.
146 v8::Local<v8::FunctionTemplate> FindOrCreateOperationTemplate(
147 const DOMWrapperWorld&,
148 const void* key,
149 v8::FunctionCallback,
150 v8::Local<v8::Value> data,
151 v8::Local<v8::Signature>,
152 int length);
153
154 // Obtains a pointer to an array of names, given a lookup key. If it does not
155 // yet exist, it is created from the given array of strings. Once created,
156 // these live for as long as the isolate, so this is appropriate only for a
157 // compile-time list of related names, such as IDL dictionary keys.
158 const v8::Eternal<v8::Name>* FindOrCreateEternalNameCache(
159 const void* lookup_key,
160 const char* const names[],
161 size_t count);
162
163 bool HasInstance(const WrapperTypeInfo* untrusted, v8::Local<v8::Value>);
164 v8::Local<v8::Object> FindInstanceInPrototypeChain(const WrapperTypeInfo*,
165 v8::Local<v8::Value>);
166
167 v8::Local<v8::Context> EnsureScriptRegexpContext();
168 void ClearScriptRegexpContext();
169
170 // EndOfScopeTasks are run when control is returning
171 // to C++ from script, after executing a script task (e.g. callback,
172 // event) or microtasks (e.g. promise). This is explicitly needed for
173 // Indexed DB transactions per spec, but should in general be avoided.
174 void AddEndOfScopeTask(std::unique_ptr<EndOfScopeTask>);
175 void RunEndOfScopeTasks();
176 void ClearEndOfScopeTasks();
177
178 void SetThreadDebugger(std::unique_ptr<Data>);
179 Data* ThreadDebugger();
180
181 using ActiveScriptWrappableSet =
182 HeapHashSet<WeakMember<ActiveScriptWrappableBase>>;
183 void AddActiveScriptWrappable(ActiveScriptWrappableBase*);
184 const ActiveScriptWrappableSet* ActiveScriptWrappables() const {
185 return active_script_wrappables_.Get();
186 }
187
188 class CORE_EXPORT TemporaryScriptWrappableVisitorScope {
189 WTF_MAKE_NONCOPYABLE(TemporaryScriptWrappableVisitorScope);
190 STACK_ALLOCATED();
191
192 public:
193 TemporaryScriptWrappableVisitorScope(
194 v8::Isolate* isolate,
195 std::unique_ptr<ScriptWrappableVisitor> visitor)
196 : isolate_(isolate), saved_visitor_(std::move(visitor)) {
197 SwapWithV8PerIsolateDataVisitor(saved_visitor_);
198 }
199 ~TemporaryScriptWrappableVisitorScope() {
200 SwapWithV8PerIsolateDataVisitor(saved_visitor_);
201 }
202
203 inline ScriptWrappableVisitor* CurrentVisitor() {
204 return V8PerIsolateData::From(isolate_)->GetScriptWrappableVisitor();
205 }
206
207 private:
208 void SwapWithV8PerIsolateDataVisitor(
209 std::unique_ptr<ScriptWrappableVisitor>&);
210
211 v8::Isolate* isolate_;
212 std::unique_ptr<ScriptWrappableVisitor> saved_visitor_;
213 };
214
215 void SetScriptWrappableVisitor(
216 std::unique_ptr<ScriptWrappableVisitor> visitor) {
217 script_wrappable_visitor_ = std::move(visitor);
218 }
219 ScriptWrappableVisitor* GetScriptWrappableVisitor() {
220 return script_wrappable_visitor_.get();
221 }
222
223 private:
224 explicit V8PerIsolateData(WebTaskRunner*);
225 ~V8PerIsolateData();
226
227 typedef HashMap<const void*, v8::Eternal<v8::FunctionTemplate>>
228 V8FunctionTemplateMap;
229 V8FunctionTemplateMap& SelectInterfaceTemplateMap(const DOMWrapperWorld&);
230 V8FunctionTemplateMap& SelectOperationTemplateMap(const DOMWrapperWorld&);
231 bool HasInstance(const WrapperTypeInfo* untrusted,
232 v8::Local<v8::Value>,
233 V8FunctionTemplateMap&);
234 v8::Local<v8::Object> FindInstanceInPrototypeChain(const WrapperTypeInfo*,
235 v8::Local<v8::Value>,
236 V8FunctionTemplateMap&);
237
238 gin::IsolateHolder isolate_holder_;
239
240 // m_interfaceTemplateMapFor{,Non}MainWorld holds function templates for
241 // the inerface objects.
242 V8FunctionTemplateMap interface_template_map_for_main_world_;
243 V8FunctionTemplateMap interface_template_map_for_non_main_world_;
244 // m_operationTemplateMapFor{,Non}MainWorld holds function templates for
245 // the cross-origin accessible DOM operations.
246 V8FunctionTemplateMap operation_template_map_for_main_world_;
247 V8FunctionTemplateMap operation_template_map_for_non_main_world_;
248
249 // Contains lists of eternal names, such as dictionary keys.
250 HashMap<const void*, Vector<v8::Eternal<v8::Name>>> eternal_name_cache_;
251
252 std::unique_ptr<StringCache> string_cache_;
253 std::unique_ptr<V8PrivateProperty> private_property_;
254 ScopedPersistent<v8::Value> live_root_;
255 RefPtr<ScriptState> script_regexp_script_state_;
256
257 bool constructor_mode_;
258 friend class ConstructorMode;
259
260 bool use_counter_disabled_;
261 friend class UseCounterDisabledScope;
262
263 bool is_handling_recursion_level_error_;
264 bool is_reporting_exception_;
265
266 Vector<std::unique_ptr<EndOfScopeTask>> end_of_scope_tasks_;
267 std::unique_ptr<Data> thread_debugger_;
268
269 Persistent<ActiveScriptWrappableSet> active_script_wrappables_;
270 std::unique_ptr<ScriptWrappableVisitor> script_wrappable_visitor_;
271 };
272
273 } // namespace blink
274
275 #endif // V8PerIsolateData_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698