OLD | NEW |
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 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 V8PerContextData_h | 31 // This file has been moved to platform/bindings/V8PerContextData.h. |
32 #define V8PerContextData_h | 32 // TODO(adithyas): Remove this file. |
33 | 33 #include "platform/bindings/V8PerContextData.h" |
34 #include <memory> | |
35 | |
36 #include "bindings/core/v8/ScopedPersistent.h" | |
37 #include "bindings/core/v8/V0CustomElementBinding.h" | |
38 #include "bindings/core/v8/V8GlobalValueMap.h" | |
39 #include "bindings/core/v8/WrapperTypeInfo.h" | |
40 #include "core/CoreExport.h" | |
41 #include "gin/public/context_holder.h" | |
42 #include "gin/public/gin_embedders.h" | |
43 #include "platform/wtf/Allocator.h" | |
44 #include "platform/wtf/HashMap.h" | |
45 #include "platform/wtf/Vector.h" | |
46 #include "platform/wtf/text/AtomicString.h" | |
47 #include "platform/wtf/text/AtomicStringHash.h" | |
48 #include "v8/include/v8.h" | |
49 | |
50 namespace blink { | |
51 | |
52 class V8DOMActivityLogger; | |
53 class V8PerContextData; | |
54 | |
55 enum V8ContextEmbedderDataField { | |
56 kV8ContextPerContextDataIndex = | |
57 static_cast<int>(gin::kPerContextDataStartIndex + gin::kEmbedderBlink), | |
58 }; | |
59 | |
60 class CORE_EXPORT V8PerContextData final { | |
61 USING_FAST_MALLOC(V8PerContextData); | |
62 WTF_MAKE_NONCOPYABLE(V8PerContextData); | |
63 | |
64 public: | |
65 static std::unique_ptr<V8PerContextData> Create(v8::Local<v8::Context>); | |
66 | |
67 static V8PerContextData* From(v8::Local<v8::Context>); | |
68 | |
69 ~V8PerContextData(); | |
70 | |
71 v8::Local<v8::Context> GetContext() { return context_.NewLocal(isolate_); } | |
72 | |
73 // To create JS Wrapper objects, we create a cache of a 'boiler plate' | |
74 // object, and then simply Clone that object each time we need a new one. | |
75 // This is faster than going through the full object creation process. | |
76 v8::Local<v8::Object> CreateWrapperFromCache(const WrapperTypeInfo* type) { | |
77 v8::Local<v8::Object> boilerplate = wrapper_boilerplates_.Get(type); | |
78 return !boilerplate.IsEmpty() ? boilerplate->Clone() | |
79 : CreateWrapperFromCacheSlowCase(type); | |
80 } | |
81 | |
82 v8::Local<v8::Function> ConstructorForType(const WrapperTypeInfo* type) { | |
83 v8::Local<v8::Function> interface_object = constructor_map_.Get(type); | |
84 return (!interface_object.IsEmpty()) ? interface_object | |
85 : ConstructorForTypeSlowCase(type); | |
86 } | |
87 | |
88 v8::Local<v8::Object> PrototypeForType(const WrapperTypeInfo*); | |
89 | |
90 // Gets the constructor and prototype for a type, if they have already been | |
91 // created. Returns true if they exist, and sets the existing values in | |
92 // |prototypeObject| and |interfaceObject|. Otherwise, returns false, and the | |
93 // values are set to empty objects (non-null). | |
94 bool GetExistingConstructorAndPrototypeForType( | |
95 const WrapperTypeInfo*, | |
96 v8::Local<v8::Object>* prototype_object, | |
97 v8::Local<v8::Function>* interface_object); | |
98 | |
99 void AddCustomElementBinding(std::unique_ptr<V0CustomElementBinding>); | |
100 | |
101 V8DOMActivityLogger* ActivityLogger() const { return activity_logger_; } | |
102 void SetActivityLogger(V8DOMActivityLogger* activity_logger) { | |
103 activity_logger_ = activity_logger; | |
104 } | |
105 | |
106 // Garbage collected classes that use V8PerContextData to hold an instance | |
107 // should subclass Data, and use addData / clearData / getData to manage the | |
108 // instance. | |
109 class CORE_EXPORT Data : public GarbageCollectedMixin {}; | |
110 | |
111 void AddData(const char* key, Data*); | |
112 void ClearData(const char* key); | |
113 Data* GetData(const char* key); | |
114 | |
115 private: | |
116 V8PerContextData(v8::Local<v8::Context>); | |
117 | |
118 v8::Local<v8::Object> CreateWrapperFromCacheSlowCase(const WrapperTypeInfo*); | |
119 v8::Local<v8::Function> ConstructorForTypeSlowCase(const WrapperTypeInfo*); | |
120 | |
121 v8::Isolate* isolate_; | |
122 | |
123 // For each possible type of wrapper, we keep a boilerplate object. | |
124 // The boilerplate is used to create additional wrappers of the same type. | |
125 typedef V8GlobalValueMap<const WrapperTypeInfo*, v8::Object, v8::kNotWeak> | |
126 WrapperBoilerplateMap; | |
127 WrapperBoilerplateMap wrapper_boilerplates_; | |
128 | |
129 typedef V8GlobalValueMap<const WrapperTypeInfo*, v8::Function, v8::kNotWeak> | |
130 ConstructorMap; | |
131 ConstructorMap constructor_map_; | |
132 | |
133 std::unique_ptr<gin::ContextHolder> context_holder_; | |
134 | |
135 ScopedPersistent<v8::Context> context_; | |
136 ScopedPersistent<v8::Value> error_prototype_; | |
137 | |
138 typedef Vector<std::unique_ptr<V0CustomElementBinding>> | |
139 V0CustomElementBindingList; | |
140 V0CustomElementBindingList custom_element_bindings_; | |
141 | |
142 // This is owned by a static hash map in V8DOMActivityLogger. | |
143 V8DOMActivityLogger* activity_logger_; | |
144 | |
145 using DataMap = PersistentHeapHashMap<const char*, Member<Data>>; | |
146 DataMap data_map_; | |
147 }; | |
148 | |
149 } // namespace blink | |
150 | |
151 #endif // V8PerContextData_h | |
OLD | NEW |