• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "config.h"
26 #include "runtime_root.h"
27 
28 #include "runtime.h"
29 #include "runtime_object.h"
30 #include <runtime/JSGlobalObject.h>
31 #include <wtf/HashCountedSet.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/StdLibExtras.h>
34 
35 namespace JSC { namespace Bindings {
36 
37 // This code attempts to solve two problems: (1) plug-ins leaking references to
38 // JS and the DOM; (2) plug-ins holding stale references to JS and the DOM. Previous
39 // comments in this file claimed that problem #1 was an issue in Java, in particular,
40 // because Java, allegedly, didn't always call finalize when collecting an object.
41 
42 typedef HashSet<RootObject*> RootObjectSet;
43 
rootObjectSet()44 static RootObjectSet* rootObjectSet()
45 {
46     DEFINE_STATIC_LOCAL(RootObjectSet, staticRootObjectSet, ());
47     return &staticRootObjectSet;
48 }
49 
50 // FIXME:  These two functions are a potential performance problem.  We could
51 // fix them by adding a JSObject to RootObject dictionary.
52 
findProtectingRootObject(JSObject * jsObject)53 RootObject* findProtectingRootObject(JSObject* jsObject)
54 {
55     RootObjectSet::const_iterator end = rootObjectSet()->end();
56     for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
57         if ((*it)->gcIsProtected(jsObject))
58             return *it;
59     }
60     return 0;
61 }
62 
findRootObject(JSGlobalObject * globalObject)63 RootObject* findRootObject(JSGlobalObject* globalObject)
64 {
65     RootObjectSet::const_iterator end = rootObjectSet()->end();
66     for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) {
67         if ((*it)->globalObject() == globalObject)
68             return *it;
69     }
70     return 0;
71 }
72 
create(const void * nativeHandle,JSGlobalObject * globalObject)73 PassRefPtr<RootObject> RootObject::create(const void* nativeHandle, JSGlobalObject* globalObject)
74 {
75     return adoptRef(new RootObject(nativeHandle, globalObject));
76 }
77 
RootObject(const void * nativeHandle,JSGlobalObject * globalObject)78 RootObject::RootObject(const void* nativeHandle, JSGlobalObject* globalObject)
79     : m_isValid(true)
80     , m_nativeHandle(nativeHandle)
81     , m_globalObject(globalObject)
82 {
83     ASSERT(globalObject);
84     rootObjectSet()->add(this);
85 }
86 
~RootObject()87 RootObject::~RootObject()
88 {
89     if (m_isValid)
90         invalidate();
91 }
92 
invalidate()93 void RootObject::invalidate()
94 {
95     if (!m_isValid)
96         return;
97 
98     {
99         HashSet<RuntimeObjectImp*>::iterator end = m_runtimeObjects.end();
100         for (HashSet<RuntimeObjectImp*>::iterator it = m_runtimeObjects.begin(); it != end; ++it)
101             (*it)->invalidate();
102 
103         m_runtimeObjects.clear();
104     }
105 
106     m_isValid = false;
107 
108     m_nativeHandle = 0;
109     m_globalObject = 0;
110 
111     ProtectCountSet::iterator end = m_protectCountSet.end();
112     for (ProtectCountSet::iterator it = m_protectCountSet.begin(); it != end; ++it)
113         JSC::gcUnprotect(it->first);
114     m_protectCountSet.clear();
115 
116     rootObjectSet()->remove(this);
117 }
118 
gcProtect(JSObject * jsObject)119 void RootObject::gcProtect(JSObject* jsObject)
120 {
121     ASSERT(m_isValid);
122 
123     if (!m_protectCountSet.contains(jsObject))
124         JSC::gcProtect(jsObject);
125     m_protectCountSet.add(jsObject);
126 }
127 
gcUnprotect(JSObject * jsObject)128 void RootObject::gcUnprotect(JSObject* jsObject)
129 {
130     ASSERT(m_isValid);
131 
132     if (!jsObject)
133         return;
134 
135     if (m_protectCountSet.count(jsObject) == 1)
136         JSC::gcUnprotect(jsObject);
137     m_protectCountSet.remove(jsObject);
138 }
139 
gcIsProtected(JSObject * jsObject)140 bool RootObject::gcIsProtected(JSObject* jsObject)
141 {
142     ASSERT(m_isValid);
143     return m_protectCountSet.contains(jsObject);
144 }
145 
nativeHandle() const146 const void* RootObject::nativeHandle() const
147 {
148     ASSERT(m_isValid);
149     return m_nativeHandle;
150 }
151 
globalObject() const152 JSGlobalObject* RootObject::globalObject() const
153 {
154     ASSERT(m_isValid);
155     return m_globalObject;
156 }
157 
addRuntimeObject(RuntimeObjectImp * object)158 void RootObject::addRuntimeObject(RuntimeObjectImp* object)
159 {
160     ASSERT(m_isValid);
161     ASSERT(!m_runtimeObjects.contains(object));
162 
163     m_runtimeObjects.add(object);
164 }
165 
removeRuntimeObject(RuntimeObjectImp * object)166 void RootObject::removeRuntimeObject(RuntimeObjectImp* object)
167 {
168     ASSERT(m_isValid);
169     ASSERT(m_runtimeObjects.contains(object));
170 
171     m_runtimeObjects.remove(object);
172 }
173 
174 } } // namespace JSC::Bindings
175