initial import
[vuplus_webkit] / Source / WebCore / bridge / runtime_root.cpp
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 "BridgeJSC.h"
30 #include "runtime_object.h"
31 #include <heap/Weak.h>
32 #include <runtime/JSGlobalObject.h>
33 #include <wtf/HashCountedSet.h>
34 #include <wtf/HashSet.h>
35 #include <wtf/StdLibExtras.h>
36
37 namespace JSC { namespace Bindings {
38
39 // This code attempts to solve two problems: (1) plug-ins leaking references to 
40 // JS and the DOM; (2) plug-ins holding stale references to JS and the DOM. Previous 
41 // comments in this file claimed that problem #1 was an issue in Java, in particular, 
42 // because Java, allegedly, didn't always call finalize when collecting an object.
43
44 typedef HashSet<RootObject*> RootObjectSet;
45
46 static RootObjectSet* rootObjectSet()
47 {
48     DEFINE_STATIC_LOCAL(RootObjectSet, staticRootObjectSet, ());
49     return &staticRootObjectSet;
50 }
51
52 // FIXME:  These two functions are a potential performance problem.  We could 
53 // fix them by adding a JSObject to RootObject dictionary.
54
55 RootObject* findProtectingRootObject(JSObject* jsObject)
56 {
57     RootObjectSet::const_iterator end = rootObjectSet()->end();
58     for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
59         if ((*it)->gcIsProtected(jsObject))
60             return *it;
61     }
62     return 0;
63 }
64
65 RootObject* findRootObject(JSGlobalObject* globalObject)
66 {
67     RootObjectSet::const_iterator end = rootObjectSet()->end();
68     for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
69         if ((*it)->globalObject() == globalObject)
70             return *it;
71     }
72     return 0;
73 }
74
75 RootObject::InvalidationCallback::~InvalidationCallback()
76 {
77 }
78
79 PassRefPtr<RootObject> RootObject::create(const void* nativeHandle, JSGlobalObject* globalObject)
80 {
81     return adoptRef(new RootObject(nativeHandle, globalObject));
82 }
83
84 RootObject::RootObject(const void* nativeHandle, JSGlobalObject* globalObject)
85     : m_isValid(true)
86     , m_nativeHandle(nativeHandle)
87     , m_globalObject(globalObject->globalData(), globalObject)
88 {
89     ASSERT(globalObject);
90     rootObjectSet()->add(this);
91 }
92
93 RootObject::~RootObject()
94 {
95     if (m_isValid)
96         invalidate();
97 }
98
99 void RootObject::invalidate()
100 {
101     if (!m_isValid)
102         return;
103
104     {
105         HashMap<RuntimeObject*, JSC::Weak<RuntimeObject> >::iterator end = m_runtimeObjects.end();
106         for (HashMap<RuntimeObject*, JSC::Weak<RuntimeObject> >::iterator it = m_runtimeObjects.begin(); it != end; ++it) {
107             it->second.get()->invalidate();
108         }
109
110         m_runtimeObjects.clear();
111     }
112
113     m_isValid = false;
114
115     m_nativeHandle = 0;
116     m_globalObject.clear();
117
118     {
119         HashSet<InvalidationCallback*>::iterator end = m_invalidationCallbacks.end();
120         for (HashSet<InvalidationCallback*>::iterator iter = m_invalidationCallbacks.begin(); iter != end; ++iter)
121             (**iter)(this);
122
123         m_invalidationCallbacks.clear();
124     }
125
126     ProtectCountSet::iterator end = m_protectCountSet.end();
127     for (ProtectCountSet::iterator it = m_protectCountSet.begin(); it != end; ++it)
128         JSC::gcUnprotect(it->first);
129     m_protectCountSet.clear();
130
131     rootObjectSet()->remove(this);
132 }
133
134 void RootObject::gcProtect(JSObject* jsObject)
135 {
136     ASSERT(m_isValid);
137     
138     if (!m_protectCountSet.contains(jsObject))
139         JSC::gcProtect(jsObject);
140     m_protectCountSet.add(jsObject);
141 }
142
143 void RootObject::gcUnprotect(JSObject* jsObject)
144 {
145     ASSERT(m_isValid);
146     
147     if (!jsObject)
148         return;
149
150     if (m_protectCountSet.count(jsObject) == 1)
151         JSC::gcUnprotect(jsObject);
152     m_protectCountSet.remove(jsObject);
153 }
154
155 bool RootObject::gcIsProtected(JSObject* jsObject)
156 {
157     ASSERT(m_isValid);
158     return m_protectCountSet.contains(jsObject);
159 }
160
161 const void* RootObject::nativeHandle() const 
162
163     ASSERT(m_isValid);
164     return m_nativeHandle; 
165 }
166
167 JSGlobalObject* RootObject::globalObject() const
168 {
169     ASSERT(m_isValid);
170     return m_globalObject.get();
171 }
172
173 void RootObject::updateGlobalObject(JSGlobalObject* globalObject)
174 {
175     m_globalObject.set(globalObject->globalData(), globalObject);
176 }
177
178 void RootObject::addRuntimeObject(JSGlobalData& globalData, RuntimeObject* object)
179 {
180     ASSERT(m_isValid);
181     ASSERT(!m_runtimeObjects.get(object));
182
183     m_runtimeObjects.set(object, JSC::Weak<RuntimeObject>(globalData, object, this));
184 }
185
186 void RootObject::removeRuntimeObject(RuntimeObject* object)
187 {
188     if (!m_isValid)
189         return;
190
191     ASSERT(m_runtimeObjects.get(object));
192
193     m_runtimeObjects.take(object);
194 }
195
196 void RootObject::finalize(JSC::Handle<JSC::Unknown> handle, void*)
197 {
198     RuntimeObject* object = static_cast<RuntimeObject*>(asObject(handle.get()));
199     ASSERT(m_runtimeObjects.contains(object));
200
201     object->invalidate();
202     m_runtimeObjects.remove(object);
203 }
204
205 } } // namespace JSC::Bindings