1 /*
2 * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "runtime_root.h"
28
29 #include "runtime.h"
30 #include "runtime_object.h"
31 #include <runtime/JSGlobalObject.h>
32 #include <wtf/HashCountedSet.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/StdLibExtras.h>
35
36 namespace JSC { namespace Bindings {
37
38 // This code attempts to solve two problems: (1) plug-ins leaking references to
39 // JS and the DOM; (2) plug-ins holding stale references to JS and the DOM. Previous
40 // comments in this file claimed that problem #1 was an issue in Java, in particular,
41 // because Java, allegedly, didn't always call finalize when collecting an object.
42
43 typedef HashSet<RootObject*> RootObjectSet;
44
rootObjectSet()45 static RootObjectSet* rootObjectSet()
46 {
47 DEFINE_STATIC_LOCAL(RootObjectSet, staticRootObjectSet, ());
48 return &staticRootObjectSet;
49 }
50
51 // FIXME: These two functions are a potential performance problem. We could
52 // fix them by adding a JSObject to RootObject dictionary.
53
findProtectingRootObject(JSObject * jsObject)54 RootObject* findProtectingRootObject(JSObject* jsObject)
55 {
56 RootObjectSet::const_iterator end = rootObjectSet()->end();
57 for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
58 if ((*it)->gcIsProtected(jsObject))
59 return *it;
60 }
61 return 0;
62 }
63
findRootObject(JSGlobalObject * globalObject)64 RootObject* findRootObject(JSGlobalObject* globalObject)
65 {
66 RootObjectSet::const_iterator end = rootObjectSet()->end();
67 for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
68 if ((*it)->globalObject() == globalObject)
69 return *it;
70 }
71 return 0;
72 }
73
create(const void * nativeHandle,JSGlobalObject * globalObject)74 PassRefPtr<RootObject> RootObject::create(const void* nativeHandle, JSGlobalObject* globalObject)
75 {
76 return adoptRef(new RootObject(nativeHandle, globalObject));
77 }
78
RootObject(const void * nativeHandle,JSGlobalObject * globalObject)79 RootObject::RootObject(const void* nativeHandle, JSGlobalObject* globalObject)
80 : m_isValid(true)
81 , m_nativeHandle(nativeHandle)
82 , m_globalObject(globalObject)
83 {
84 ASSERT(globalObject);
85 rootObjectSet()->add(this);
86 }
87
~RootObject()88 RootObject::~RootObject()
89 {
90 if (m_isValid)
91 invalidate();
92 }
93
invalidate()94 void RootObject::invalidate()
95 {
96 if (!m_isValid)
97 return;
98
99 {
100 HashSet<RuntimeObjectImp*>::iterator end = m_runtimeObjects.end();
101 for (HashSet<RuntimeObjectImp*>::iterator it = m_runtimeObjects.begin(); it != end; ++it)
102 (*it)->invalidate();
103
104 m_runtimeObjects.clear();
105 }
106
107 m_isValid = false;
108
109 m_nativeHandle = 0;
110 m_globalObject = 0;
111
112 ProtectCountSet::iterator end = m_protectCountSet.end();
113 for (ProtectCountSet::iterator it = m_protectCountSet.begin(); it != end; ++it)
114 JSC::gcUnprotect(it->first);
115 m_protectCountSet.clear();
116
117 rootObjectSet()->remove(this);
118 }
119
gcProtect(JSObject * jsObject)120 void RootObject::gcProtect(JSObject* jsObject)
121 {
122 ASSERT(m_isValid);
123
124 if (!m_protectCountSet.contains(jsObject))
125 JSC::gcProtect(jsObject);
126 m_protectCountSet.add(jsObject);
127 }
128
gcUnprotect(JSObject * jsObject)129 void RootObject::gcUnprotect(JSObject* jsObject)
130 {
131 ASSERT(m_isValid);
132
133 if (!jsObject)
134 return;
135
136 if (m_protectCountSet.count(jsObject) == 1)
137 JSC::gcUnprotect(jsObject);
138 m_protectCountSet.remove(jsObject);
139 }
140
gcIsProtected(JSObject * jsObject)141 bool RootObject::gcIsProtected(JSObject* jsObject)
142 {
143 ASSERT(m_isValid);
144 return m_protectCountSet.contains(jsObject);
145 }
146
nativeHandle() const147 const void* RootObject::nativeHandle() const
148 {
149 ASSERT(m_isValid);
150 return m_nativeHandle;
151 }
152
globalObject() const153 JSGlobalObject* RootObject::globalObject() const
154 {
155 ASSERT(m_isValid);
156 return m_globalObject;
157 }
158
addRuntimeObject(RuntimeObjectImp * object)159 void RootObject::addRuntimeObject(RuntimeObjectImp* object)
160 {
161 ASSERT(m_isValid);
162 ASSERT(!m_runtimeObjects.contains(object));
163
164 m_runtimeObjects.add(object);
165 }
166
removeRuntimeObject(RuntimeObjectImp * object)167 void RootObject::removeRuntimeObject(RuntimeObjectImp* object)
168 {
169 ASSERT(m_isValid);
170 ASSERT(m_runtimeObjects.contains(object));
171
172 m_runtimeObjects.remove(object);
173 }
174
175 } } // namespace JSC::Bindings
176