File: khtml_global.cpp

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (256 lines) | stat: -rw-r--r-- 7,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* This file is part of the KDE project
 *
 * Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
 * Copyright (C) 2007 David Faure <faure@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "khtml_global.h"
#include "khtml_part.h"
#include "khtml_settings.h"
#include "../khtml_version.h"

#include "css/cssstyleselector.h"
#include "css/css_mediaquery.h"
#include "html/html_imageimpl.h"
#include "rendering/render_style.h"
#include "rendering/break_lines.h"
#include "misc/htmlnames.h"
#include "misc/loader.h"
#include "misc/arena.h"
#include "misc/paintbuffer.h"

#include <QLinkedList>

#include <kiconloader.h>
#include <kaboutdata.h>
#include <klocalizedstring.h>

#include <assert.h>

#include "khtml_debug.h"

// SVG
#include "svg/SVGNames.h"

KHTMLGlobal *KHTMLGlobal::s_self = nullptr;
unsigned long int KHTMLGlobal::s_refcnt = 0;
KIconLoader *KHTMLGlobal::s_iconLoader = nullptr;
KAboutData *KHTMLGlobal::s_about = nullptr;
KHTMLSettings *KHTMLGlobal::s_settings = nullptr;

static QLinkedList<KHTMLPart *> *s_parts = nullptr;
static QLinkedList<DOM::DocumentImpl *> *s_docs = nullptr;

KHTMLGlobal::KHTMLGlobal()
{
    assert(!s_self);
    s_self = this;
    ref();

    khtml::Cache::init();

    khtml::NamespaceFactory::initIdTable();
    khtml::PrefixFactory::initIdTable();
    khtml::LocalNameFactory::initIdTable();
    DOM::emptyLocalName = DOM::LocalName::fromId(0);
    DOM::emptyPrefixName = DOM::PrefixName::fromId(0);
    DOM::emptyNamespaceName = DOM::NamespaceName::fromId(DOM::emptyNamespace);
    WebCore::SVGNames::init();
}

KHTMLGlobal::~KHTMLGlobal()
{
    //qCDebug(KHTML_LOG) << this;
    if (s_self == this) {
        finalCheck();
        delete s_iconLoader;
        delete s_about;
        delete s_settings;
        delete KHTMLSettings::avFamilies;
        if (s_parts) {
            assert(s_parts->isEmpty());
            delete s_parts;
        }
        if (s_docs) {
            assert(s_docs->isEmpty());
            delete s_docs;
        }

        s_iconLoader = nullptr;
        s_about = nullptr;
        s_settings = nullptr;
        s_parts = nullptr;
        s_docs = nullptr;
        KHTMLSettings::avFamilies = nullptr;

        // clean up static data
        khtml::CSSStyleSelector::clear();
        khtml::RenderStyle::cleanup();
        khtml::RenderObject::cleanup();
        khtml::PaintBuffer::cleanup();
        khtml::MediaQueryEvaluator::cleanup();
        khtml::Cache::clear();
        khtml::cleanup_thaibreaks();
        khtml::ArenaFinish();
    } else {
        deref();
    }
}

void KHTMLGlobal::ref()
{
    if (!s_refcnt && !s_self) {
        //qCDebug(KHTML_LOG) << "Creating KHTMLGlobal instance";
        // we can't use a staticdeleter here, because that would mean
        // that the KHTMLGlobal instance gets deleted from within a qPostRoutine, called
        // from the QApplication destructor. That however is too late, because
        // we want to destruct a KComponentData object, which involves destructing
        // a KConfig object, which might call KGlobal::dirs() (in sync()) which
        // probably is not going to work ;-)
        // well, perhaps I'm wrong here, but as I'm unsure I try to stay on the
        // safe side ;-) -> let's use a simple reference counting scheme
        // (Simon)
        new KHTMLGlobal; // does initial ref()
    } else {
        ++s_refcnt;
    }
    //qCDebug(KHTML_LOG) << "s_refcnt=" << s_refcnt;
}

void KHTMLGlobal::deref()
{
    //qCDebug(KHTML_LOG) << "s_refcnt=" << s_refcnt - 1;
    if (!--s_refcnt && s_self) {
        delete s_self;
        s_self = nullptr;
    }
}

void KHTMLGlobal::registerPart(KHTMLPart *part)
{
    //qCDebug(KHTML_LOG) << part;
    if (!s_parts) {
        s_parts = new QLinkedList<KHTMLPart *>;
    }

    if (!s_parts->contains(part)) {
        s_parts->append(part);
        ref();
    }
}

void KHTMLGlobal::deregisterPart(KHTMLPart *part)
{
    //qCDebug(KHTML_LOG) << part;
    assert(s_parts);

    if (s_parts->removeAll(part)) {
        if (s_parts->isEmpty()) {
            delete s_parts;
            s_parts = nullptr;
        }
        deref();
    }
}

void KHTMLGlobal::registerDocumentImpl(DOM::DocumentImpl *doc)
{
    //qCDebug(KHTML_LOG) << doc;
    if (!s_docs) {
        s_docs = new QLinkedList<DOM::DocumentImpl *>;
    }

    if (!s_docs->contains(doc)) {
        s_docs->append(doc);
        ref();
    }
}

void KHTMLGlobal::deregisterDocumentImpl(DOM::DocumentImpl *doc)
{
    //qCDebug(KHTML_LOG) << doc;
    assert(s_docs);

    if (s_docs->removeAll(doc)) {
        if (s_docs->isEmpty()) {
            delete s_docs;
            s_docs = nullptr;
        }
        deref();
    }
}

const KAboutData &KHTMLGlobal::aboutData()
{
    assert(s_self);

    if (!s_about) {
        s_about = new KAboutData("khtml", i18n("KHTML"), QStringLiteral(KHTML_VERSION_STRING),
                                 i18n("Embeddable HTML component"),
                                 KAboutLicense::LGPL);
        s_about->addAuthor(QStringLiteral("Lars Knoll"), QString(), "knoll@kde.org");
        s_about->addAuthor(QStringLiteral("Antti Koivisto"), QString(), "koivisto@kde.org");
        s_about->addAuthor(QStringLiteral("Waldo Bastian"), QString(), "bastian@kde.org");
        s_about->addAuthor(QStringLiteral("Dirk Mueller"), QString(), "mueller@kde.org");
        s_about->addAuthor(QStringLiteral("Peter Kelly"), QString(), "pmk@kde.org");
        s_about->addAuthor(QStringLiteral("Torben Weis"), QString(), "weis@kde.org");
        s_about->addAuthor(QStringLiteral("Martin Jones"), QString(), "mjones@kde.org");
        s_about->addAuthor(QStringLiteral("Simon Hausmann"), QString(), "hausmann@kde.org");
        s_about->addAuthor(QStringLiteral("Tobias Anton"), QString(), "anton@stud.fbi.fh-darmstadt.de");

    }

    return *s_about;
}

KIconLoader *KHTMLGlobal::iconLoader()
{
    if (!s_iconLoader) {
        s_iconLoader = new KIconLoader(aboutData().componentName());
    }

    return s_iconLoader;
}

KHTMLSettings *KHTMLGlobal::defaultHTMLSettings()
{
    assert(s_self);
    if (!s_settings) {
        s_settings = new KHTMLSettings();
    }

    return s_settings;
}

void KHTMLGlobal::finalCheck()
{
#ifndef NDEBUG
    if (s_refcnt) {
        if (s_parts && !s_parts->isEmpty()) {
            Q_FOREACH (KHTMLPart *part, *s_parts) {
                qCWarning(KHTML_LOG) << "Part" << part->url() << "was not deleted";
            }
        }
        if (s_docs && !s_docs->isEmpty()) {
            Q_FOREACH (DOM::DocumentImpl *doc, *s_docs) {
                qCWarning(KHTML_LOG) << "Document" << doc->URL() << "was not deleted";
            }
        }
    }
#endif
}