/platform/external/webkit/WebCore/bindings/v8/npruntime.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 454 lines · 306 code · 74 blank · 74 comment · 66 complexity · ebc98f44ad37e545adb511e864d78796 MD5 · raw file

  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. #include "config.h"
  27. #include "NPV8Object.h"
  28. #include "npruntime_impl.h"
  29. #include "npruntime_priv.h"
  30. #include "V8NPObject.h"
  31. #include <wtf/HashMap.h>
  32. #include <wtf/HashSet.h>
  33. #include <wtf/Assertions.h>
  34. // FIXME: Consider removing locks if we're singlethreaded already.
  35. // The static initializer here should work okay, but we want to avoid
  36. // static initialization in general.
  37. namespace npruntime {
  38. // We use StringKey here as the key-type to avoid a string copy to
  39. // construct the map key and for faster comparisons than strcmp.
  40. class StringKey {
  41. public:
  42. explicit StringKey(const char* str) : m_string(str), m_length(strlen(str)) { }
  43. StringKey() : m_string(0), m_length(0) { }
  44. explicit StringKey(WTF::HashTableDeletedValueType) : m_string(hashTableDeletedValue()), m_length(0) { }
  45. StringKey& operator=(const StringKey& other)
  46. {
  47. this->m_string = other.m_string;
  48. this->m_length = other.m_length;
  49. return *this;
  50. }
  51. bool isHashTableDeletedValue() const
  52. {
  53. return m_string == hashTableDeletedValue();
  54. }
  55. const char* m_string;
  56. size_t m_length;
  57. private:
  58. const char* hashTableDeletedValue() const
  59. {
  60. return reinterpret_cast<const char*>(-1);
  61. }
  62. };
  63. inline bool operator==(const StringKey& x, const StringKey& y)
  64. {
  65. if (x.m_length != y.m_length)
  66. return false;
  67. if (x.m_string == y.m_string)
  68. return true;
  69. ASSERT(!x.isHashTableDeletedValue() && !y.isHashTableDeletedValue());
  70. return !memcmp(x.m_string, y.m_string, y.m_length);
  71. }
  72. // Implement WTF::DefaultHash<StringKey>::Hash interface.
  73. struct StringKeyHash {
  74. static unsigned hash(const StringKey& key)
  75. {
  76. // Compute string hash.
  77. unsigned hash = 0;
  78. size_t len = key.m_length;
  79. const char* str = key.m_string;
  80. for (size_t i = 0; i < len; i++) {
  81. char c = str[i];
  82. hash += c;
  83. hash += (hash << 10);
  84. hash ^= (hash >> 6);
  85. }
  86. hash += (hash << 3);
  87. hash ^= (hash >> 11);
  88. hash += (hash << 15);
  89. if (hash == 0)
  90. hash = 27;
  91. return hash;
  92. }
  93. static bool equal(const StringKey& x, const StringKey& y)
  94. {
  95. return x == y;
  96. }
  97. static const bool safeToCompareToEmptyOrDeleted = true;
  98. };
  99. } // namespace npruntime
  100. using npruntime::StringKey;
  101. using npruntime::StringKeyHash;
  102. // Implement HashTraits<StringKey>
  103. struct StringKeyHashTraits : WTF::GenericHashTraits<StringKey> {
  104. static void constructDeletedValue(StringKey& slot)
  105. {
  106. new (&slot) StringKey(WTF::HashTableDeletedValue);
  107. }
  108. static bool isDeletedValue(const StringKey& value)
  109. {
  110. return value.isHashTableDeletedValue();
  111. }
  112. };
  113. typedef WTF::HashMap<StringKey, PrivateIdentifier*, StringKeyHash, StringKeyHashTraits> StringIdentifierMap;
  114. static StringIdentifierMap* getStringIdentifierMap()
  115. {
  116. static StringIdentifierMap* stringIdentifierMap = 0;
  117. if (!stringIdentifierMap)
  118. stringIdentifierMap = new StringIdentifierMap();
  119. return stringIdentifierMap;
  120. }
  121. typedef WTF::HashMap<int, PrivateIdentifier*> IntIdentifierMap;
  122. static IntIdentifierMap* getIntIdentifierMap()
  123. {
  124. static IntIdentifierMap* intIdentifierMap = 0;
  125. if (!intIdentifierMap)
  126. intIdentifierMap = new IntIdentifierMap();
  127. return intIdentifierMap;
  128. }
  129. extern "C" {
  130. NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name)
  131. {
  132. ASSERT(name);
  133. if (name) {
  134. StringKey key(name);
  135. StringIdentifierMap* identMap = getStringIdentifierMap();
  136. StringIdentifierMap::iterator iter = identMap->find(key);
  137. if (iter != identMap->end())
  138. return static_cast<NPIdentifier>(iter->second);
  139. size_t nameLen = key.m_length;
  140. // We never release identifiers, so this dictionary will grow.
  141. PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier) + nameLen + 1));
  142. char* nameStorage = reinterpret_cast<char*>(identifier + 1);
  143. memcpy(nameStorage, name, nameLen + 1);
  144. identifier->isString = true;
  145. identifier->value.string = reinterpret_cast<NPUTF8*>(nameStorage);
  146. key.m_string = nameStorage;
  147. identMap->set(key, identifier);
  148. return (NPIdentifier)identifier;
  149. }
  150. return 0;
  151. }
  152. void _NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount, NPIdentifier* identifiers)
  153. {
  154. ASSERT(names);
  155. ASSERT(identifiers);
  156. if (names && identifiers) {
  157. for (int i = 0; i < nameCount; i++)
  158. identifiers[i] = _NPN_GetStringIdentifier(names[i]);
  159. }
  160. }
  161. NPIdentifier _NPN_GetIntIdentifier(int32_t intId)
  162. {
  163. // Special case for -1 and 0, both cannot be used as key in HashMap.
  164. if (!intId || intId == -1) {
  165. static PrivateIdentifier* minusOneOrZeroIds[2];
  166. PrivateIdentifier* id = minusOneOrZeroIds[intId + 1];
  167. if (!id) {
  168. id = reinterpret_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier)));
  169. id->isString = false;
  170. id->value.number = intId;
  171. minusOneOrZeroIds[intId + 1] = id;
  172. }
  173. return (NPIdentifier) id;
  174. }
  175. IntIdentifierMap* identMap = getIntIdentifierMap();
  176. IntIdentifierMap::iterator iter = identMap->find(intId);
  177. if (iter != identMap->end())
  178. return static_cast<NPIdentifier>(iter->second);
  179. // We never release identifiers, so this dictionary will grow.
  180. PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(malloc(sizeof(PrivateIdentifier)));
  181. identifier->isString = false;
  182. identifier->value.number = intId;
  183. identMap->set(intId, identifier);
  184. return (NPIdentifier)identifier;
  185. }
  186. bool _NPN_IdentifierIsString(NPIdentifier identifier)
  187. {
  188. PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
  189. return privateIdentifier->isString;
  190. }
  191. NPUTF8 *_NPN_UTF8FromIdentifier(NPIdentifier identifier)
  192. {
  193. PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
  194. if (!privateIdentifier->isString || !privateIdentifier->value.string)
  195. return 0;
  196. return (NPUTF8*) strdup(privateIdentifier->value.string);
  197. }
  198. int32_t _NPN_IntFromIdentifier(NPIdentifier identifier)
  199. {
  200. PrivateIdentifier* privateIdentifier = reinterpret_cast<PrivateIdentifier*>(identifier);
  201. if (privateIdentifier->isString)
  202. return 0;
  203. return privateIdentifier->value.number;
  204. }
  205. void _NPN_ReleaseVariantValue(NPVariant* variant)
  206. {
  207. ASSERT(variant);
  208. if (variant->type == NPVariantType_Object) {
  209. _NPN_ReleaseObject(variant->value.objectValue);
  210. variant->value.objectValue = 0;
  211. } else if (variant->type == NPVariantType_String) {
  212. free((void*)variant->value.stringValue.UTF8Characters);
  213. variant->value.stringValue.UTF8Characters = 0;
  214. variant->value.stringValue.UTF8Length = 0;
  215. }
  216. variant->type = NPVariantType_Void;
  217. }
  218. NPObject *_NPN_CreateObject(NPP npp, NPClass* npClass)
  219. {
  220. ASSERT(npClass);
  221. if (npClass) {
  222. NPObject* npObject;
  223. if (npClass->allocate != 0)
  224. npObject = npClass->allocate(npp, npClass);
  225. else
  226. npObject = reinterpret_cast<NPObject*>(malloc(sizeof(NPObject)));
  227. npObject->_class = npClass;
  228. npObject->referenceCount = 1;
  229. return npObject;
  230. }
  231. return 0;
  232. }
  233. NPObject* _NPN_RetainObject(NPObject* npObject)
  234. {
  235. ASSERT(npObject);
  236. ASSERT(npObject->referenceCount > 0);
  237. if (npObject)
  238. npObject->referenceCount++;
  239. return npObject;
  240. }
  241. // _NPN_DeallocateObject actually deletes the object. Technically,
  242. // callers should use _NPN_ReleaseObject. Webkit exposes this function
  243. // to kill objects which plugins may not have properly released.
  244. void _NPN_DeallocateObject(NPObject* npObject)
  245. {
  246. ASSERT(npObject);
  247. ASSERT(npObject->referenceCount >= 0);
  248. if (npObject) {
  249. // NPObjects that remain in pure C++ may never have wrappers.
  250. // Hence, if it's not already alive, don't unregister it.
  251. // If it is alive, unregister it as the *last* thing we do
  252. // so that it can do as much cleanup as possible on its own.
  253. if (_NPN_IsAlive(npObject))
  254. _NPN_UnregisterObject(npObject);
  255. npObject->referenceCount = -1;
  256. if (npObject->_class->deallocate)
  257. npObject->_class->deallocate(npObject);
  258. else
  259. free(npObject);
  260. }
  261. }
  262. #if PLATFORM(ANDROID)
  263. // Android uses NPN_ReleaseObject (the 'public' version of _NPN_ReleaseObject)
  264. // in WebCoreFrameBridge.cpp. See http://trac.webkit.org/changeset/47021.
  265. // TODO: Upstream this to webkit.org.
  266. void NPN_ReleaseObject(NPObject *obj)
  267. {
  268. _NPN_ReleaseObject(obj);
  269. }
  270. #endif
  271. void _NPN_ReleaseObject(NPObject* npObject)
  272. {
  273. ASSERT(npObject);
  274. ASSERT(npObject->referenceCount >= 1);
  275. if (npObject && npObject->referenceCount >= 1) {
  276. if (!--npObject->referenceCount)
  277. _NPN_DeallocateObject(npObject);
  278. }
  279. }
  280. void _NPN_InitializeVariantWithStringCopy(NPVariant* variant, const NPString* value)
  281. {
  282. variant->type = NPVariantType_String;
  283. variant->value.stringValue.UTF8Length = value->UTF8Length;
  284. variant->value.stringValue.UTF8Characters = reinterpret_cast<NPUTF8*>(malloc(sizeof(NPUTF8) * value->UTF8Length));
  285. memcpy((void*)variant->value.stringValue.UTF8Characters, value->UTF8Characters, sizeof(NPUTF8) * value->UTF8Length);
  286. }
  287. // NPN_Registry
  288. //
  289. // The registry is designed for quick lookup of NPObjects.
  290. // JS needs to be able to quickly lookup a given NPObject to determine
  291. // if it is alive or not.
  292. // The browser needs to be able to quickly lookup all NPObjects which are
  293. // "owned" by an object.
  294. //
  295. // The liveObjectMap is a hash table of all live objects to their owner
  296. // objects. Presence in this table is used primarily to determine if
  297. // objects are live or not.
  298. //
  299. // The rootObjectMap is a hash table of root objects to a set of
  300. // objects that should be deactivated in sync with the root. A
  301. // root is defined as a top-level owner object. This is used on
  302. // Frame teardown to deactivate all objects associated
  303. // with a particular plugin.
  304. typedef WTF::HashSet<NPObject*> NPObjectSet;
  305. typedef WTF::HashMap<NPObject*, NPObject*> NPObjectMap;
  306. typedef WTF::HashMap<NPObject*, NPObjectSet*> NPRootObjectMap;
  307. // A map of live NPObjects with pointers to their Roots.
  308. NPObjectMap liveObjectMap;
  309. // A map of the root objects and the list of NPObjects
  310. // associated with that object.
  311. NPRootObjectMap rootObjectMap;
  312. void _NPN_RegisterObject(NPObject* npObject, NPObject* owner)
  313. {
  314. ASSERT(npObject);
  315. // Check if already registered.
  316. if (liveObjectMap.find(npObject) != liveObjectMap.end())
  317. return;
  318. if (!owner) {
  319. // Registering a new owner object.
  320. ASSERT(rootObjectMap.find(npObject) == rootObjectMap.end());
  321. rootObjectMap.set(npObject, new NPObjectSet());
  322. } else {
  323. // Always associate this object with it's top-most parent.
  324. // Since we always flatten, we only have to look up one level.
  325. NPObjectMap::iterator ownerEntry = liveObjectMap.find(owner);
  326. NPObject* parent = 0;
  327. if (liveObjectMap.end() != ownerEntry)
  328. parent = ownerEntry->second;
  329. if (parent)
  330. owner = parent;
  331. ASSERT(rootObjectMap.find(npObject) == rootObjectMap.end());
  332. if (rootObjectMap.find(owner) != rootObjectMap.end())
  333. rootObjectMap.get(owner)->add(npObject);
  334. }
  335. ASSERT(liveObjectMap.find(npObject) == liveObjectMap.end());
  336. liveObjectMap.set(npObject, owner);
  337. }
  338. void _NPN_UnregisterObject(NPObject* npObject)
  339. {
  340. ASSERT(npObject);
  341. ASSERT(liveObjectMap.find(npObject) != liveObjectMap.end());
  342. NPObject* owner = 0;
  343. if (liveObjectMap.find(npObject) != liveObjectMap.end())
  344. owner = liveObjectMap.find(npObject)->second;
  345. if (!owner) {
  346. // Unregistering a owner object; also unregister it's descendants.
  347. ASSERT(rootObjectMap.find(npObject) != rootObjectMap.end());
  348. NPObjectSet* set = rootObjectMap.get(npObject);
  349. while (set->size() > 0) {
  350. #ifndef NDEBUG
  351. int size = set->size();
  352. #endif
  353. NPObject* sub_object = *(set->begin());
  354. // The sub-object should not be a owner!
  355. ASSERT(rootObjectMap.find(sub_object) == rootObjectMap.end());
  356. // First, unregister the object.
  357. set->remove(sub_object);
  358. liveObjectMap.remove(sub_object);
  359. // Remove the JS references to the object.
  360. forgetV8ObjectForNPObject(sub_object);
  361. ASSERT(set->size() < size);
  362. }
  363. delete set;
  364. rootObjectMap.remove(npObject);
  365. } else {
  366. NPRootObjectMap::iterator ownerEntry = rootObjectMap.find(owner);
  367. if (ownerEntry != rootObjectMap.end()) {
  368. NPObjectSet* list = ownerEntry->second;
  369. ASSERT(list->find(npObject) != list->end());
  370. list->remove(npObject);
  371. }
  372. }
  373. liveObjectMap.remove(npObject);
  374. forgetV8ObjectForNPObject(npObject);
  375. }
  376. bool _NPN_IsAlive(NPObject* npObject)
  377. {
  378. return liveObjectMap.find(npObject) != liveObjectMap.end();
  379. }
  380. } // extern "C"