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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/npruntime.cpp

Issue 1483733002: Remove support for NPObjects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 8 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
(Empty)
1 /*
2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007-2009 Google, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "bindings/core/v8/NPV8Object.h"
28 #include "bindings/core/v8/V8NPObject.h"
29 #include "bindings/core/v8/npruntime_impl.h"
30 #include "bindings/core/v8/npruntime_priv.h"
31 #include "wtf/Assertions.h"
32 #include "wtf/HashMap.h"
33 #include "wtf/HashSet.h"
34 #include "wtf/HashTableDeletedValueType.h"
35 #include <stdlib.h>
36
37 using namespace blink;
38
39 // FIXME: Consider removing locks if we're singlethreaded already.
40 // The static initializer here should work okay, but we want to avoid
41 // static initialization in general.
42
43 namespace npruntime {
44
45 // We use StringKey here as the key-type to avoid a string copy to
46 // construct the map key and for faster comparisons than strcmp.
47 class StringKey {
48 public:
49 explicit StringKey(const char* str) : m_string(str), m_length(strlen(str)) { }
50 StringKey() : m_string(0), m_length(0) { }
51 explicit StringKey(WTF::HashTableDeletedValueType) : m_string(hashTableDelet edValue()), m_length(0) { }
52
53 StringKey& operator=(const StringKey& other)
54 {
55 this->m_string = other.m_string;
56 this->m_length = other.m_length;
57 return *this;
58 }
59
60 bool isHashTableDeletedValue() const
61 {
62 return m_string == hashTableDeletedValue();
63 }
64
65 const char* m_string;
66 size_t m_length;
67
68 private:
69 const char* hashTableDeletedValue() const
70 {
71 return reinterpret_cast<const char*>(-1);
72 }
73 };
74
75 inline bool operator==(const StringKey& x, const StringKey& y)
76 {
77 if (x.m_length != y.m_length)
78 return false;
79 if (x.m_string == y.m_string)
80 return true;
81
82 ASSERT(!x.isHashTableDeletedValue() && !y.isHashTableDeletedValue());
83 return !memcmp(x.m_string, y.m_string, y.m_length);
84 }
85
86 // Implement WTF::DefaultHash<StringKey>::Hash interface.
87 struct StringKeyHash {
88 static unsigned hash(const StringKey& key)
89 {
90 // Compute string hash.
91 unsigned hash = 0;
92 size_t len = key.m_length;
93 const char* str = key.m_string;
94 for (size_t i = 0; i < len; i++) {
95 char c = str[i];
96 hash += c;
97 hash += (hash << 10);
98 hash ^= (hash >> 6);
99 }
100 hash += (hash << 3);
101 hash ^= (hash >> 11);
102 hash += (hash << 15);
103 if (hash == 0)
104 hash = 27;
105 return hash;
106 }
107
108 static bool equal(const StringKey& x, const StringKey& y)
109 {
110 return x == y;
111 }
112
113 static const bool safeToCompareToEmptyOrDeleted = true;
114 };
115
116 } // namespace npruntime
117
118 using npruntime::StringKey;
119 using npruntime::StringKeyHash;
120
121 // Implement HashTraits<StringKey>
122 struct StringKeyHashTraits : WTF::GenericHashTraits<StringKey> {
123 static void constructDeletedValue(StringKey& slot, bool)
124 {
125 new (&slot) StringKey(WTF::HashTableDeletedValue);
126 }
127
128 static bool isDeletedValue(const StringKey& value)
129 {
130 return value.isHashTableDeletedValue();
131 }
132 };
133
134 typedef WTF::HashMap<StringKey, PrivateIdentifier*, StringKeyHash, StringKeyHash Traits> StringIdentifierMap;
135
136 static StringIdentifierMap* getStringIdentifierMap()
137 {
138 static StringIdentifierMap* stringIdentifierMap = 0;
139 if (!stringIdentifierMap)
140 stringIdentifierMap = new StringIdentifierMap();
141 return stringIdentifierMap;
142 }
143
144 typedef WTF::HashMap<int, PrivateIdentifier*> IntIdentifierMap;
145
146 static IntIdentifierMap* getIntIdentifierMap()
147 {
148 static IntIdentifierMap* intIdentifierMap = 0;
149 if (!intIdentifierMap)
150 intIdentifierMap = new IntIdentifierMap();
151 return intIdentifierMap;
152 }
153
154 extern "C" {
155
156 NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
157 {
158 ASSERT(name);
159
160 if (name) {
161
162 StringKey key(name);
163 StringIdentifierMap* identMap = getStringIdentifierMap();
164 StringIdentifierMap::iterator iter = identMap->find(key);
165 if (iter != identMap->end())
166 return static_cast<NPIdentifier>(iter->value);
167
168 size_t nameLen = key.m_length;
169
170 // We never release identifiers, so this dictionary will grow.
171 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(malloc(s izeof(PrivateIdentifier) + nameLen + 1));
172 char* nameStorage = reinterpret_cast<char*>(identifier + 1);
173 memcpy(nameStorage, name, nameLen + 1);
174 identifier->isString = true;
175 identifier->value.string = reinterpret_cast<NPUTF8*>(nameStorage);
176 key.m_string = nameStorage;
177 identMap->set(key, identifier);
178 return (NPIdentifier)identifier;
179 }
180
181 return 0;
182 }
183
184 void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdenti fier* identifiers)
185 {
186 ASSERT(names);
187 ASSERT(identifiers);
188
189 if (names && identifiers) {
190 for (int i = 0; i < nameCount; i++)
191 identifiers[i] = _NPN_GetStringIdentifier(names[i]);
192 }
193 }
194
195 NPIdentifier _NPN_GetIntIdentifier(int32_t intId)
196 {
197 // Special case for -1 and 0, both cannot be used as key in HashMap.
198 if (!intId || intId == -1) {
199 static PrivateIdentifier* minusOneOrZeroIds[2];
200 PrivateIdentifier* id = minusOneOrZeroIds[intId + 1];
201 if (!id) {
202 id = reinterpret_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdent ifier)));
203 id->isString = false;
204 id->value.number = intId;
205 minusOneOrZeroIds[intId + 1] = id;
206 }
207 return (NPIdentifier) id;
208 }
209
210 IntIdentifierMap* identMap = getIntIdentifierMap();
211 IntIdentifierMap::iterator iter = identMap->find(intId);
212 if (iter != identMap->end())
213 return static_cast<NPIdentifier>(iter->value);
214
215 // We never release identifiers, so this dictionary will grow.
216 PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(malloc( sizeof(PrivateIdentifier)));
217 identifier->isString = false;
218 identifier->value.number = intId;
219 identMap->set(intId, identifier);
220 return (NPIdentifier)identifier;
221 }
222
223 bool _NPN_IdentifierIsString(NPIdentifier identifier)
224 {
225 PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>( identifier);
226 return privateIdentifier->isString;
227 }
228
229 NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
230 {
231 PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>( identifier);
232 if (!privateIdentifier->isString || !privateIdentifier->value.string)
233 return 0;
234
235 return (NPUTF8*) strdup(privateIdentifier->value.string);
236 }
237
238 int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
239 {
240 PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>( identifier);
241 if (privateIdentifier->isString)
242 return 0;
243 return privateIdentifier->value.number;
244 }
245
246 void _NPN_ReleaseVariantValue(NPVariant* variant)
247 {
248 ASSERT(variant);
249
250 if (variant->type == NPVariantType_Object) {
251 _NPN_ReleaseObject(variant->value.objectValue);
252 variant->value.objectValue = 0;
253 } else if (variant->type == NPVariantType_String) {
254 free((void*)variant->value.stringValue.UTF8Characters);
255 variant->value.stringValue.UTF8Characters = 0;
256 variant->value.stringValue.UTF8Length = 0;
257 }
258
259 variant->type = NPVariantType_Void;
260 }
261
262 NPObject *_NPN_CreateObject(NPP npp, NPClass* npClass)
263 {
264 ASSERT(npClass);
265
266 if (npClass) {
267 NPObject* npObject;
268 if (npClass->allocate != 0)
269 npObject = npClass->allocate(npp, npClass);
270 else
271 npObject = reinterpret_cast<NPObject*>(malloc(sizeof(NPObject)));
272
273 npObject->_class = npClass;
274 npObject->referenceCount = 1;
275 return npObject;
276 }
277
278 return 0;
279 }
280
281 NPObject* _NPN_RetainObject(NPObject* npObject)
282 {
283 ASSERT(npObject);
284 ASSERT(npObject->referenceCount > 0);
285
286 if (npObject)
287 npObject->referenceCount++;
288
289 return npObject;
290 }
291
292 // _NPN_DeallocateObject actually deletes the object. Technically,
293 // callers should use _NPN_ReleaseObject. Webkit exposes this function
294 // to kill objects which plugins may not have properly released.
295 void _NPN_DeallocateObject(NPObject* npObject)
296 {
297 ASSERT(npObject);
298
299 if (npObject) {
300 // NPObjects that remain in pure C++ may never have wrappers.
301 // Hence, if it's not already alive, don't unregister it.
302 // If it is alive, unregister it as the *last* thing we do
303 // so that it can do as much cleanup as possible on its own.
304 if (_NPN_IsAlive(npObject))
305 _NPN_UnregisterObject(npObject);
306
307 npObject->referenceCount = 0xFFFFFFFF;
308 if (npObject->_class->deallocate)
309 npObject->_class->deallocate(npObject);
310 else
311 free(npObject);
312 }
313 }
314
315 void _NPN_ReleaseObject(NPObject* npObject)
316 {
317 ASSERT(npObject);
318 ASSERT(npObject->referenceCount >= 1);
319
320 if (npObject && npObject->referenceCount >= 1) {
321 if (!--npObject->referenceCount)
322 _NPN_DeallocateObject(npObject);
323 }
324 }
325
326 void _NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* va lue)
327 {
328 variant->type = NPVariantType_String;
329 variant->value.stringValue.UTF8Length = value->UTF8Length;
330 variant->value.stringValue.UTF8Characters = reinterpret_cast<NPUTF8*>(malloc (sizeof(NPUTF8) * value->UTF8Length));
331 memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characte rs, sizeof(NPUTF8) * value->UTF8Length);
332 }
333
334 } // extern "C"
335
336 // NPN_Registry
337 //
338 // The registry is designed for quick lookup of NPObjects.
339 // JS needs to be able to quickly lookup a given NPObject to determine
340 // if it is alive or not.
341 // The browser needs to be able to quickly lookup all NPObjects which are
342 // "owned" by an object.
343 //
344 // The liveObjectMap is a hash table of all live objects to their owner
345 // objects. Presence in this table is used primarily to determine if
346 // objects are live or not.
347 //
348 // The rootObjectMap is a hash table of root objects to a set of
349 // objects that should be deactivated in sync with the root. A
350 // root is defined as a top-level owner object. This is used on
351 // LocalFrame teardown to deactivate all objects associated
352 // with a particular plugin.
353
354 typedef WTF::HashSet<NPObject*> NPObjectSet;
355 typedef WTF::HashMap<NPObject*, NPObject*> NPObjectMap;
356 typedef WTF::HashMap<NPObject*, NPObjectSet*> NPRootObjectMap;
357
358 // A map of live NPObjects with pointers to their Roots.
359 static NPObjectMap& liveObjectMap()
360 {
361 DEFINE_STATIC_LOCAL(NPObjectMap, objectMap, ());
362 return objectMap;
363 }
364
365 // A map of the root objects and the list of NPObjects
366 // associated with that object.
367 static NPRootObjectMap& rootObjectMap()
368 {
369 DEFINE_STATIC_LOCAL(NPRootObjectMap, objectMap, ());
370 return objectMap;
371 }
372
373 extern "C" {
374
375 void _NPN_RegisterObject(NPObject* npObject, NPObject* owner)
376 {
377 ASSERT(npObject);
378
379 // Check if already registered.
380 if (liveObjectMap().find(npObject) != liveObjectMap().end())
381 return;
382
383 if (!owner) {
384 // Registering a new owner object.
385 ASSERT(rootObjectMap().find(npObject) == rootObjectMap().end());
386 rootObjectMap().set(npObject, new NPObjectSet());
387 } else {
388 // Always associate this object with it's top-most parent.
389 // Since we always flatten, we only have to look up one level.
390 NPObjectMap::iterator ownerEntry = liveObjectMap().find(owner);
391 NPObject* parent = 0;
392 if (liveObjectMap().end() != ownerEntry)
393 parent = ownerEntry->value;
394
395 if (parent)
396 owner = parent;
397 ASSERT(rootObjectMap().find(npObject) == rootObjectMap().end());
398 if (rootObjectMap().find(owner) != rootObjectMap().end())
399 rootObjectMap().get(owner)->add(npObject);
400 }
401
402 ASSERT(liveObjectMap().find(npObject) == liveObjectMap().end());
403 liveObjectMap().set(npObject, owner);
404 }
405
406 void _NPN_UnregisterObject(NPObject* npObject)
407 {
408 ASSERT(npObject);
409 ASSERT_WITH_SECURITY_IMPLICATION(liveObjectMap().find(npObject) != liveObjec tMap().end());
410
411 NPObject* owner = 0;
412 if (liveObjectMap().find(npObject) != liveObjectMap().end())
413 owner = liveObjectMap().find(npObject)->value;
414
415 if (!owner) {
416 // Unregistering a owner object; also unregister it's descendants.
417 ASSERT_WITH_SECURITY_IMPLICATION(rootObjectMap().find(npObject) != rootO bjectMap().end());
418 NPObjectSet* set = rootObjectMap().get(npObject);
419 while (set->size() > 0) {
420 #if ENABLE(ASSERT)
421 unsigned size = set->size();
422 #endif
423 NPObject* sub_object = *(set->begin());
424 // The sub-object should not be a owner!
425 ASSERT(rootObjectMap().find(sub_object) == rootObjectMap().end());
426
427 // First, unregister the object.
428 set->remove(sub_object);
429 liveObjectMap().remove(sub_object);
430
431 // Script objects hold a refernce to their LocalDOMWindow*, which is going away if
432 // we're unregistering the associated owner NPObject. Clear it out.
433 if (V8NPObject* v8npObject = npObjectToV8NPObject(sub_object))
434 v8npObject->rootObject = 0;
435
436 // Remove the JS references to the object.
437 forgetV8ObjectForNPObject(sub_object);
438
439 ASSERT(set->size() < size);
440 }
441 delete set;
442 rootObjectMap().remove(npObject);
443 } else {
444 NPRootObjectMap::iterator ownerEntry = rootObjectMap().find(owner);
445 if (ownerEntry != rootObjectMap().end()) {
446 NPObjectSet* list = ownerEntry->value;
447 ASSERT(list->find(npObject) != list->end());
448 list->remove(npObject);
449 }
450 }
451
452 liveObjectMap().remove(npObject);
453 forgetV8ObjectForNPObject(npObject);
454 }
455
456 bool _NPN_IsAlive(NPObject* npObject)
457 {
458 return liveObjectMap().find(npObject) != liveObjectMap().end();
459 }
460
461 } // extern "C"
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698