1/*
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "ObjectList.h"
27#include "awtmsg.h"
28
29///////////////////////////////////////////////////////////////////////////
30// AwtObject list -- track all created widgets for cleanup and debugging
31
32AwtObjectList theAwtObjectList;
33
34AwtObjectList::AwtObjectList()
35{
36    m_head = NULL;
37}
38
39void AwtObjectList::Add(AwtObject* obj)
40{
41    CriticalSection::Lock l(m_lock);
42
43    /* Verify that the object is not already in the list. */
44    DASSERT(LookUp(obj) == NULL);
45
46    AwtObjectListItem* item = new AwtObjectListItem(obj);
47    item->next = m_head;
48    m_head = item;
49}
50
51BOOL AwtObjectList::Remove(AwtObject* obj)
52{
53    CriticalSection::Lock l(m_lock);
54
55    AwtObjectListItem* item = m_head;
56    AwtObjectListItem* lastItem = NULL;
57
58    while (item != NULL) {
59        if (item->obj == obj) {
60            if (lastItem == NULL) {
61                m_head = item->next;
62            } else {
63                lastItem->next = item->next;
64            }
65            DASSERT(item != NULL);
66            delete item;
67            return TRUE;
68        }
69        lastItem = item;
70        item = item->next;
71    }
72
73    return FALSE;
74
75//    DASSERT(FALSE);  // should never get here...
76                      // even if it does it shouldn't be fatal.
77}
78
79#ifdef DEBUG
80AwtObject* AwtObjectList::LookUp(AwtObject* obj)
81{
82    CriticalSection::Lock l(m_lock);
83
84    AwtObjectListItem* item = m_head;
85
86    while (item != NULL) {
87        if (item->obj == obj) {
88            return obj;
89        }
90        item = item->next;
91    }
92    return NULL;
93}
94#endif /* DEBUG */
95
96void AwtObjectList::Cleanup()
97{
98    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
99    CHECK_IS_TOOLKIT_THREAD()
100
101    CriticalSection::Lock l(theAwtObjectList.m_lock);
102
103    CriticalSection &syncCS = AwtToolkit::GetInstance().GetSyncCS();
104    BOOL entered = syncCS.TryEnter();
105    if (entered) {
106        AwtObjectListItem* item = theAwtObjectList.m_head;
107        while (item != NULL) {
108            // AwtObject::Dispose() method will call AwtObjectList::Remove(),
109            // which will delete the item structure.
110            AwtObjectListItem* next = item->next;
111            // destructor for item->obj will be called from item->obj->Dispose() method
112            item->obj->Dispose();
113            item = next;
114        }
115        theAwtObjectList.m_head = NULL;
116        syncCS.Leave();
117    } else {
118        AwtToolkit::GetInstance().PostMessage(WM_AWT_OBJECTLISTCLEANUP, NULL, NULL);
119    }
120}
121