1 | // -*- c++ -*- |
2 | /* |
3 | This file is part of the KDE libraries |
4 | SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org> |
5 | SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org> |
6 | SPDX-FileCopyrightText: 2025 Harald Sitter <sitter@kde.org> |
7 | |
8 | SPDX-License-Identifier: LGPL-2.0-only |
9 | */ |
10 | |
11 | #ifndef KIO_WORKER_P_H |
12 | #define KIO_WORKER_P_H |
13 | |
14 | #include "workerinterface_p.h" |
15 | |
16 | #include <QDateTime> |
17 | #include <QElapsedTimer> |
18 | #include <QObject> |
19 | |
20 | namespace KIO |
21 | { |
22 | |
23 | class WorkerThread; |
24 | class WorkerManager; |
25 | class SimpleJob; |
26 | class SchedulerPrivate; |
27 | class DataProtocol; |
28 | class ProtoQueue; |
29 | class SimpleJobPrivate; |
30 | class UserNotificationHandler; |
31 | class WorkerFactory; |
32 | |
33 | // Do not use this class directly, outside of KIO. Only use the Worker pointer |
34 | // that is returned by the scheduler for passing it around. |
35 | class Worker : public KIO::WorkerInterface |
36 | { |
37 | Q_OBJECT |
38 | public: |
39 | explicit Worker(const QString &protocol, QObject *parent = nullptr); |
40 | |
41 | ~Worker() override; |
42 | |
43 | /*! |
44 | * Sends the given command to the KIO worker. |
45 | * Called by the jobs. |
46 | * \a cmd command id |
47 | * \a arr byte array containing data |
48 | */ |
49 | virtual void send(int cmd, const QByteArray &arr = QByteArray()); |
50 | |
51 | /*! |
52 | * The actual protocol used to handle the request. |
53 | * |
54 | * This method will return a different protocol than |
55 | * the one obtained by using protocol() if a |
56 | * proxy-server is used for the given protocol. This |
57 | * usually means that this method will return "http" |
58 | * when the actual request was to retrieve a resource |
59 | * from an "ftp" server by going through a proxy server. |
60 | * |
61 | * Returns the actual protocol (KIO worker) that handled the request |
62 | */ |
63 | QString workerProtocol() const; |
64 | |
65 | /*! |
66 | * Returns Host this worker is (was?) connected to |
67 | */ |
68 | QString host() const; |
69 | |
70 | /*! |
71 | * Returns port this worker is (was?) connected to |
72 | */ |
73 | quint16 port() const; |
74 | |
75 | /*! |
76 | * Returns User this worker is (was?) logged in as |
77 | */ |
78 | QString user() const; |
79 | |
80 | /*! |
81 | * Returns Passwd used to log in |
82 | */ |
83 | QString passwd() const; |
84 | |
85 | /*! |
86 | * Creates a new worker. |
87 | * |
88 | * \a protocol the protocol |
89 | * |
90 | * \a url is the url |
91 | * |
92 | * \a error is the error code on failure and undefined else. |
93 | * |
94 | * \a error_text is the error text on failure and undefined else. |
95 | * |
96 | * Returns 0 on failure, or a pointer to a worker otherwise. |
97 | */ |
98 | static Worker *createWorker(const QString &protocol, const QUrl &url, int &error, QString &error_text); |
99 | |
100 | #ifdef BUILD_TESTING |
101 | /*! |
102 | * Can be used for testing to inject a mock worker for the kio-test fake protocol.\ |
103 | * This function does not participate in ownership. The caller must ensure the factory is valid throughout worker creation needs. |
104 | */ |
105 | KIOCORE_EXPORT static void setTestWorkerFactory(const std::weak_ptr<KIO::WorkerFactory> &factory); |
106 | #endif |
107 | |
108 | // == communication with connected kioworker == |
109 | // whenever possible prefer these methods over the respective |
110 | // methods in connection() |
111 | /*! |
112 | * Suspends the operation of the attached kioworker. |
113 | */ |
114 | virtual void suspend(); |
115 | |
116 | /*! |
117 | * Resumes the operation of the attached kioworker. |
118 | */ |
119 | virtual void resume(); |
120 | |
121 | /*! |
122 | * Tells whether the kioworker is suspended. |
123 | * Returns true if the kioworker is suspended. |
124 | */ |
125 | virtual bool suspended(); |
126 | |
127 | // == end communication with connected kioworker == |
128 | private: |
129 | friend class SchedulerPrivate; |
130 | friend class DataProtocol; |
131 | friend class WorkerManager; |
132 | friend class ProtoQueue; |
133 | friend class SimpleJobPrivate; |
134 | friend class UserNotificationHandler; |
135 | |
136 | void setPID(qint64); |
137 | qint64 worker_pid() const; |
138 | |
139 | void setJob(KIO::SimpleJob *job); |
140 | KIO::SimpleJob *job() const; |
141 | |
142 | /*! |
143 | * Force termination |
144 | */ |
145 | void kill(); |
146 | |
147 | /*! |
148 | * Returns true if the worker survived the last mission. |
149 | */ |
150 | bool isAlive() const; |
151 | |
152 | /*! |
153 | * Set host for url |
154 | * |
155 | * \a host to connect to. |
156 | * |
157 | * \a port to connect to. |
158 | * |
159 | * \a user to login as |
160 | * |
161 | * \a passwd to login with |
162 | */ |
163 | virtual void setHost(const QString &host, quint16 port, const QString &user, const QString &passwd); |
164 | |
165 | /*! |
166 | * Clear host info. |
167 | */ |
168 | void resetHost(); |
169 | |
170 | /*! |
171 | * Configure worker |
172 | */ |
173 | virtual void setConfig(const MetaData &config); |
174 | |
175 | /*! |
176 | * The protocol this worker handles. |
177 | * |
178 | * Returns name of protocol handled by this worker, as seen by the user |
179 | */ |
180 | QString protocol() const; |
181 | |
182 | void setProtocol(const QString &protocol); |
183 | |
184 | /*! |
185 | * Returns The number of seconds this worker has been idle. |
186 | */ |
187 | int idleTime() const; |
188 | |
189 | /*! |
190 | * Marks this worker as idle. |
191 | */ |
192 | void setIdle(); |
193 | |
194 | void ref(); |
195 | void deref(); |
196 | void aboutToDelete(); |
197 | |
198 | void setWorkerThread(WorkerThread *thread); |
199 | |
200 | public Q_SLOTS: // TODO KF6: make all three slots private |
201 | void accept(); |
202 | void gotInput(); |
203 | void timeout(); |
204 | |
205 | Q_SIGNALS: |
206 | void workerDied(KIO::Worker *worker); |
207 | |
208 | private: |
209 | WorkerThread *m_workerThread = nullptr; // only set for in-process workers |
210 | QString m_protocol; |
211 | QString m_workerProtocol; |
212 | QString m_host; |
213 | QString m_user; |
214 | QString m_passwd; |
215 | KIO::ConnectionServer *m_workerConnServer; |
216 | KIO::SimpleJob *m_job = nullptr; |
217 | qint64 m_pid = 0; // only set for out-of-process workers |
218 | quint16 m_port = 0; |
219 | bool m_dead = false; |
220 | QElapsedTimer m_contact_started; |
221 | QElapsedTimer m_idleSince; |
222 | int m_refCount = 1; |
223 | #ifdef BUILD_TESTING |
224 | static inline std::weak_ptr<KIO::WorkerFactory> s_testFactory; // for testing purposes, can be set to a mock factory |
225 | #endif |
226 | }; |
227 | |
228 | } |
229 | |
230 | #endif |
231 | |