File: loader.h

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 (736 lines) | stat: -rw-r--r-- 17,655 bytes parent folder | download | duplicates (5)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
    This file is part of the KDE libraries

    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
    Copyright (C) 2001-2003 Dirk Mueller <mueller@kde.org>
    Copyright (C) 2003 Apple Computer, Inc

    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.

    This class provides all functionality needed for loading images, style sheets and html
    pages from the web. It has a memory cache for these objects.
*/
#ifndef _khtml_loader_h
#define _khtml_loader_h

#include "loader_client.h"

#include <stdlib.h>
#include <QObject>
#include <QPixmap>
#include <QBuffer>
#include <QStringList>
#include <QTextCodec>
#include <QTimer>
#include <QSet>
#include <QDateTime>
#include <QLinkedList>
#include <QUrl>

#include <kio/global.h>

#include <khtml_settings.h>
#include <dom/dom_string.h>
#include "imload/image.h"
#include "imload/imageowner.h"

class KHTMLPart;

class KJob;
namespace KIO
{
class Job;
}

namespace DOM
{
class DocumentImpl;
}

namespace khtml
{
class CachedObject;
class Request;
class DocLoader;

/**
 * @internal
 *
 * A cached object. Classes who want to use this object should derive
 * from CachedObjectClient, to get the function calls in case the requested data has arrived.
 *
 * This class also does the actual communication with kio and loads the file.
 */
class CachedObject
{
public:
    enum Type {
        Image,
        CSSStyleSheet,
        Script,
        Sound,
        Font
    };

    enum Status {
        Unknown,      // let imagecache decide what to do with it
        New,          // inserting new image
        Pending,      // only partially loaded
        Persistent,   // never delete this pixmap
        Cached        // regular case
    };

    enum PreloadResult {
        PreloadNotReferenced = 0,
        PreloadReferenced,
        PreloadReferencedWhileLoading,
        PreloadReferencedWhileComplete
    };

    PreloadResult preloadResult() const
    {
        return m_preloadResult;
    }
    void setProspectiveRequest()
    {
        m_prospectiveRequest = true;
    }

    CachedObject(const DOM::DOMString &url, Type type, KIO::CacheControl _cachePolicy, int size)
        : m_url(url), m_type(type), m_cachePolicy(_cachePolicy),
          m_size(size)
    {
        m_status = Pending;
        m_accessCount = 0;
        m_cachePolicy = _cachePolicy;
        m_request = nullptr;
        m_deleted = false;
        m_free = false;
        m_hadError = false;
        m_wasBlocked = false;
        m_prev = m_next = nullptr;
        m_preloadCount = 0;
        m_preloadResult = PreloadNotReferenced;
        m_prospectiveRequest = false;
    }
    virtual ~CachedObject();

    virtual void data(QBuffer &buffer, bool eof) = 0;
    virtual void error(int err, const char *text) = 0;

    const DOM::DOMString &url() const
    {
        return m_url;
    }
    Type type() const
    {
        return m_type;
    }

    virtual void ref(CachedObjectClient *consumer);
    virtual void deref(CachedObjectClient *consumer);

    int count() const
    {
        return m_clients.count();
    }
    int accessCount() const
    {
        return m_accessCount;
    }

    bool isPreloaded() const
    {
        return m_preloadCount;
    }
    void increasePreloadCount()
    {
        ++m_preloadCount;
    }
    void decreasePreloadCount()
    {
        assert(m_preloadCount);
        --m_preloadCount;
    }

    void setStatus(Status s)
    {
        m_status = s;
    }
    Status status() const
    {
        return m_status;
    }

    virtual void setCharset(const QString & /*charset*/) {}

    QTextCodec *codecForBuffer(const QString &charset, const QByteArray &buffer) const;

    int size() const
    {
        return m_size;
    }

    bool isLoaded() const
    {
        return !m_loading;
    }
    bool hadError() const
    {
        return m_hadError;
    }

    bool free() const
    {
        return m_free;
    }

    KIO::CacheControl cachePolicy() const
    {
        return m_cachePolicy;
    }

    void setRequest(Request *_request);

    bool canDelete() const
    {
        return (m_clients.count() == 0 && !m_request && !m_preloadCount);
    }

    void setExpireDate(const QDateTime &_expireDate)
    {
        m_expireDate = _expireDate;
    }

    bool isExpired() const;

    virtual void finish();

    /**
     * List of acceptable mimetypes separated by ",". A mimetype may contain a wildcard.
     */
    // e.g. "text/*"
    QString accept() const
    {
        return m_accept;
    }
    void setAccept(const QString &_accept)
    {
        m_accept = _accept;
    }

    // the mimetype that was eventually used
    QString mimetype() const
    {
        return m_mimetype;
    }

protected:
    void setSize(int size);
    QHash<CachedObjectClient *, CachedObjectClient *> m_clients;
    DOM::DOMString m_url;
    QString m_accept;
    QString m_mimetype;
    Request *m_request;
    Type m_type;
    Status m_status;
    int m_accessCount;
    KIO::CacheControl m_cachePolicy;
    QDateTime m_expireDate;
    int m_size;
    unsigned m_preloadCount;
    PreloadResult m_preloadResult: 3;
    bool m_deleted : 1;
    bool m_loading : 1;
    bool m_free : 1;
    bool m_hadError : 1;
    bool m_wasBlocked : 1;
    bool m_prospectiveRequest: 1;

private:
    bool allowInLRUList() const
    {
        return canDelete() && !m_free && status() != Persistent;
    }
    CachedObject *m_next;
    CachedObject *m_prev;
    friend class Cache;
    friend class ::KHTMLPart;
    friend class Loader;
};

bool isAcceptableCSSMimetype(const DOM::DOMString &mimetype);

/**
 * a cached style sheet. also used for loading xml documents.
 *
 * ### rename to CachedTextDoc or something since it's more generic than just for css
 */
class CachedCSSStyleSheet : public CachedObject
{
public:
    CachedCSSStyleSheet(DocLoader *dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy,
                        const char *accept);
    CachedCSSStyleSheet(const DOM::DOMString &url, const QString &stylesheet_data);

    const DOM::DOMString &sheet() const
    {
        return m_sheet;
    }

    void ref(CachedObjectClient *consumer) override;

    void data(QBuffer &buffer, bool eof) override;
    void error(int err, const char *text) override;

    void setCharsetHint(const QString &charset)
    {
        m_charsetHint = charset;
    }
    void setCharset(const QString &charset) override
    {
        m_charset = charset;
    }
    QString checkCharset(const QByteArray &buffer) const;

protected:
    void checkNotify();

    DOM::DOMString m_sheet;
    QString m_charset;
    QString m_charsetHint;
    int m_err;
    QString m_errText;
};

/**
 * a cached script
 */
class CachedScript : public CachedObject
{
public:
    CachedScript(DocLoader *dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, const char *accept);
    CachedScript(const DOM::DOMString &url, const QString &script_data);

    const DOM::DOMString &script() const
    {
        return m_script;
    }

    void ref(CachedObjectClient *consumer) override;

    void data(QBuffer &buffer, bool eof) override;
    void error(int err, const char *text) override;

    void checkNotify();

    bool isLoaded() const
    {
        return !m_loading;
    }
    void setCharset(const QString &charset) override
    {
        m_charset = charset;
    }

protected:
    QString m_charset;
    DOM::DOMString m_script;
};

class ImageSource;

/**
 * a cached image
 */
class CachedImage : public QObject, public CachedObject, public khtmlImLoad::ImageOwner
{
    Q_OBJECT
public:
    CachedImage(DocLoader *dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, const char *accept);
    virtual ~CachedImage();

    QPixmap pixmap() const;
    QPixmap *scaled_pixmap(int xWidth, int xHeight);
    QPixmap  tiled_pixmap(const QColor &bg, int xWidth = -1, int xHeight = -1);

    QSize pixmap_size() const;    // returns the size of the complete (i.e. when finished) loading
    //QRect valid_rect() const;     // returns the rectangle of pixmap that has been loaded already

    bool canRender() const
    {
        return !isErrorImage() && pixmap_size().width() > 0 && pixmap_size().height() > 0;
    }
    void ref(CachedObjectClient *consumer) override;
    void deref(CachedObjectClient *consumer) override;

    void data(QBuffer &buffer, bool eof) override;
    void error(int err, const char *text) override;

    bool isComplete() const
    {
        return i && i->complete();
    }
    bool isTransparent() const
    {
        return false;
    } //### isFullyTransparent; }
    bool isErrorImage() const
    {
        return m_hadError;
    }
    bool isBlockedImage() const
    {
        return m_wasBlocked;
    }
    const QString &suggestedFilename() const
    {
        return m_suggestedFilename;
    }
    void setSuggestedFilename(const QString &s)
    {
        m_suggestedFilename = s;
    }
#ifdef IMAGE_TITLES
    const QString &suggestedTitle() const
    {
        return m_suggestedTitle;
    }
    void setSuggestedTitle(const QString &s)
    {
        m_suggestedTitle = s;
    }
#else
    const QString &suggestedTitle() const
    {
        return m_suggestedFilename;
    }
#endif

    void setShowAnimations(KHTMLSettings::KAnimationAdvice);

    void finish() override;

    khtmlImLoad::Image *image()
    {
        return i;
    }

protected:
    void clear();

private:
    /**
     Interface to the image
    */
    void imageHasGeometry(khtmlImLoad::Image *img, int width, int height) override;
    void imageChange(khtmlImLoad::Image *img, QRect region) override;
    void imageError(khtmlImLoad::Image *img) override;
    void imageDone(khtmlImLoad::Image *img) override;
private:
    void doNotifyFinished();

    void do_notify(const QRect &r);
    khtmlImLoad::Image *i;

    QString m_suggestedFilename;
#ifdef IMAGE_TITLES
    QString m_suggestedTitle;
#endif
    QPixmap *bg;
    QPixmap *scaled;
    QRgb bgColor;
    QSize bgSize;

    friend class Cache;
    friend class ::KHTMLPart;
};

/**
 * a cached sound
 */
class CachedSound : public CachedObject
{
public:
    CachedSound(DocLoader *dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, const char *accept);

    QByteArray sound() const
    {
        return m_sound;
    }

    void ref(CachedObjectClient *consumer) override;
    void data(QBuffer &buffer, bool eof) override;
    void error(int err, const char *text) override;

    void checkNotify();

    bool isLoaded() const
    {
        return !m_loading;
    }

protected:
    QByteArray m_sound;
};

/**
 * a cached font
 */
class CachedFont : public CachedObject
{
public:
    CachedFont(DocLoader *dl, const DOM::DOMString &url, KIO::CacheControl cachePolicy, const char *accept);

    QByteArray font() const
    {
        return m_font;
    }

    void ref(CachedObjectClient *consumer) override;
    void data(QBuffer &buffer, bool eof) override;
    void error(int err, const char *text) override;

    void checkNotify();

    bool isLoaded() const
    {
        return !m_loading;
    }

protected:
    QByteArray m_font;
};

/**
 * @internal
 *
 * Manages the loading of scripts/images/stylesheets for a particular document
 */
class DocLoader
{
public:
    DocLoader(KHTMLPart *, DOM::DocumentImpl *);
    ~DocLoader();

    CachedImage *requestImage(const DOM::DOMString &url);
    CachedCSSStyleSheet *requestStyleSheet(const DOM::DOMString &url, const QString &charsetHint,
                                           const char *accept = "text/css", bool userSheet = false);
    CachedScript *requestScript(const DOM::DOMString &url, const QString &charset);
    CachedSound *requestSound(const DOM::DOMString &url);
    CachedFont *requestFont(const DOM::DOMString &url);
    bool willLoadMediaElement(const DOM::DOMString &url);

    bool autoloadImages() const
    {
        return m_bautoloadImages;
    }
    KIO::CacheControl cachePolicy() const
    {
        return m_cachePolicy;
    }
    KHTMLSettings::KAnimationAdvice showAnimations() const
    {
        return m_showAnimations;
    }
    QDateTime expireDate() const
    {
        return m_expireDate;
    }
    KHTMLPart *part() const
    {
        return m_part;
    }
    DOM::DocumentImpl *doc() const
    {
        return m_doc;
    }

    void setCacheCreationDate(const QDateTime &);
    void setExpireDate(const QDateTime &);
    void setRelativeExpireDate(qint64 seconds);
    void setAutoloadImages(bool);
    void setCachePolicy(KIO::CacheControl cachePolicy)
    {
        m_cachePolicy = cachePolicy;
    }
    void setShowAnimations(KHTMLSettings::KAnimationAdvice);
    void insertCachedObject(CachedObject *o) const;
    void removeCachedObject(CachedObject *o) const
    {
        m_docObjects.remove(o);
    }
    void clearPreloads();
    void registerPreload(CachedObject *);
    void printPreloadStats();

private:
    bool needReload(CachedObject *existing, const QString &fullUrl);

    friend class Cache;
    friend class DOM::DocumentImpl;
    friend class ::KHTMLPart;

    QStringList m_reloadedURLs;
    mutable QSet<CachedObject *> m_docObjects;
    QSet<CachedObject *> m_preloads;
    QDateTime m_expireDate;
    QDateTime m_creationDate;
    KIO::CacheControl m_cachePolicy;
    bool m_bautoloadImages : 1;
    KHTMLSettings::KAnimationAdvice m_showAnimations : 2;
    KHTMLPart *m_part;
    DOM::DocumentImpl *m_doc;
};

/**
 * @internal
 */
class Request
{
public:
    Request(DocLoader *dl, CachedObject *_object, bool _incremental, int priority);
    ~Request();
    bool incremental;
    int priority; // -10 to 10, smaller values mean higher priority
    QBuffer m_buffer;
    CachedObject *object;
    DocLoader *m_docLoader;
};

/**
 * @internal
 */
class Loader : public QObject
{

    Q_OBJECT

public:
    Loader();
    ~Loader();

    void load(DocLoader *dl, CachedObject *object, bool incremental = true, int priority = 0);

    int numRequests(DocLoader *dl) const;
    void cancelRequests(DocLoader *dl);

    // may return 0L
    KIO::Job *jobForRequest(const DOM::DOMString &url) const;

Q_SIGNALS:
    void requestStarted(khtml::DocLoader *dl, khtml::CachedObject *obj);
    void requestDone(khtml::DocLoader *dl, khtml::CachedObject *obj);
    void requestFailed(khtml::DocLoader *dl, khtml::CachedObject *obj);

protected Q_SLOTS:
    void slotFinished(KJob *);
    void slotMimetype(KIO::Job *, const QString &s);
    void slotData(KIO::Job *, const QByteArray &);

protected:
    void scheduleRequest(Request *req);
    QHash<KIO::Job *, Request *> m_requestsLoading;
    QStringList m_supportedImageTypes;
};

/**
* @internal
*
* Provides a cache/loader for objects needed for displaying the html page.
* At the moment these are stylesheets, scripts and images
*/
class Cache
{
    friend class DocLoader;

    template<typename CachedObjectType, enum CachedObject::Type CachedType>
    static CachedObjectType *requestObject(DocLoader *dl, const QUrl &kurl, const char *accept);

public:
    /**
     * init the cache in case it's not already. This needs to get called once
     * before using it.
     */
    KHTML_EXPORT static void init();

    /**
     * Ask the cache for some url. Will return a cachedObject, and
     * load the requested data in case it's not cached
         * if the DocLoader is zero, the url must be full-qualified.
         * Otherwise, it is automatically base-url expanded
     */
//  static CachedImage *requestImage(const QUrl& url)
//         { return Cache::requestObject<CachedImage, CachedObject::Image>( 0, url, 0 ); }

    /**
     * Pre-loads a stylesheet into the cache.
     */
    static void preloadStyleSheet(const QString &url, const QString &stylesheet_data);

    /**
     * Pre-loads a script into the cache.
     */
    static void preloadScript(const QString &url, const QString &script_data);

    static void setSize(int bytes);
    static int size()
    {
        return maxSize;
    }
    static void statistics();
    KHTML_EXPORT static void flush(bool force = false);

    /**
     * clears the cache
     * Warning: call this only at the end of your program, to clean
     * up memory (useful for finding memory holes)
     */
    KHTML_EXPORT static void clear();

    static Loader *loader()
    {
        return m_loader;
    }

    static QPixmap *nullPixmap;
    static QPixmap *brokenPixmap;
    static QPixmap *blockedPixmap;
    static int cacheSize;

    static void removeCacheEntry(CachedObject *object);

private:

    static void checkLRUAndUncacheableListIntegrity();

    friend class CachedObject;

    static QHash<QString, CachedObject *> *cache;
    static QLinkedList<DocLoader *> *docloader;
    static QLinkedList<CachedObject *> *freeList;
    static void insertInLRUList(CachedObject *);
    static void removeFromLRUList(CachedObject *);

    static int totalSizeOfLRU;
    static int maxSize;

    static Loader *m_loader;
};

} // namespace

#endif