File indexing completed on 2025-11-09 11:08:57

0001 /*
0002     This file is part of the KDE File Manager
0003     SPDX-FileCopyrightText: 1998 Waldo Bastian <bastian@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only
0006 */
0007 
0008 // KDE Cookie Server
0009 
0010 #ifndef KCOOKIESERVER_H
0011 #define KCOOKIESERVER_H
0012 
0013 #include <KDEDModule>
0014 #include <QDBusConnection>
0015 #include <QDBusContext>
0016 #include <QStringList>
0017 
0018 class KHttpCookieList;
0019 class KCookieJar;
0020 class KHttpCookie;
0021 class QTimer;
0022 class RequestList;
0023 class KConfig;
0024 
0025 // IMPORTANT: Do *NOT* replace qlonglong with WId in this class.
0026 //
0027 // KCookieServer is exposed over DBus and DBus does not know how to handle the
0028 // WId type. If a method has a WId argument it is not exposed and one gets a
0029 // warning at compile time:
0030 //
0031 // "addFunction: Unregistered input type in parameter list: WId"
0032 
0033 class KCookieServer : public KDEDModule, protected QDBusContext
0034 {
0035     Q_OBJECT
0036     Q_CLASSINFO("D-Bus Interface", "org.kde.KCookieServer")
0037 public:
0038     KCookieServer(QObject *parent, const QList<QVariant> &);
0039     ~KCookieServer() override;
0040 
0041 public Q_SLOTS:
0042     // KDE5 TODO: don't overload names here, it prevents calling e.g. findCookies from the command-line using qdbus.
0043     QString listCookies(const QString &url);
0044     QString findCookies(const QString &url, qlonglong windowId);
0045     QStringList findDomains();
0046     // KDE5: rename
0047     QStringList findCookies(const QList<int> &fields, const QString &domain, const QString &fqdn, const QString &path, const QString &name);
0048     QString findDOMCookies(const QString &url);
0049     QString findDOMCookies(const QString &url, qlonglong windowId); // KDE5: merge with above, using default value (windowId = 0)
0050     void addCookies(const QString &url, const QByteArray &cookieHeader, qlonglong windowId);
0051     void deleteCookie(const QString &domain, const QString &fqdn, const QString &path, const QString &name);
0052     void deleteCookiesFromDomain(const QString &domain);
0053     void deleteSessionCookies(qlonglong windowId);
0054     void deleteSessionCookiesFor(const QString &fqdn, qlonglong windowId);
0055     void deleteAllCookies();
0056     void addDOMCookies(const QString &url, const QByteArray &cookieHeader, qlonglong windowId);
0057     /**
0058      * Sets the cookie policy for the domain associated with the specified URL.
0059      */
0060     bool setDomainAdvice(const QString &url, const QString &advice);
0061     /**
0062      * Returns the cookie policy in effect for the specified URL.
0063      */
0064     QString getDomainAdvice(const QString &url);
0065     void reloadPolicy();
0066     void shutdown();
0067 
0068 public:
0069     bool cookiesPending(const QString &url, KHttpCookieList *cookieList = nullptr);
0070     void addCookies(const QString &url, const QByteArray &cookieHeader, qlonglong windowId, bool useDOMFormat);
0071     void checkCookies(KHttpCookieList *cookieList);
0072     // TODO: KDE5 merge with above function and make all these public functions
0073     // private since they are not used externally.
0074     void checkCookies(KHttpCookieList *cookieList, qlonglong windowId);
0075 
0076 private Q_SLOTS:
0077     void slotSave();
0078     void slotDeleteSessionCookies(qlonglong windowId);
0079 
0080 private:
0081     KCookieJar *mCookieJar;
0082     KHttpCookieList *mPendingCookies;
0083     RequestList *mRequestList;
0084     QTimer *mTimer;
0085     bool mAdvicePending;
0086     KConfig *mConfig;
0087     QString mFilename;
0088 
0089 private:
0090     bool cookieMatches(const KHttpCookie &, const QString &, const QString &, const QString &, const QString &);
0091     void putCookie(QStringList &, const KHttpCookie &, const QList<int> &);
0092     void saveCookieJar();
0093 };
0094 
0095 #endif