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