1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqmlnetworkaccessmanagerfactory.h"
5
6QT_BEGIN_NAMESPACE
7
8#if QT_CONFIG(qml_network)
9
10/*!
11 \class QQmlNetworkAccessManagerFactory
12 \since 5.0
13 \inmodule QtQml
14 \brief The QQmlNetworkAccessManagerFactory class creates QNetworkAccessManager instances for a QML engine.
15
16 A QML engine uses QNetworkAccessManager for all network access.
17 By implementing a factory, it is possible to provide the QML engine
18 with custom QNetworkAccessManager instances with specialized caching,
19 proxy and cookies support.
20
21 \list
22 \li The QNetworkDiskCache can be used as a request cache with \l {QNetworkDiskCache}.
23 \li Using \l {QNetworkProxy}, traffic sent by the QNetworkAccessManager can be tunnelled through a proxy.
24 \li Cookies can be saved for future requests by adding a \l {QNetworkCookieJar}.
25 \endlist
26
27 To implement a factory, subclass QQmlNetworkAccessManagerFactory and
28 implement the virtual create() method, then assign it to the relevant QML
29 engine using QQmlEngine::setNetworkAccessManagerFactory(). For instance, the QNetworkAccessManager
30 objects created by the following snippet will cache requests.
31 \snippet code/src_network_access_qnetworkaccessmanager.cpp 0
32
33 The factory can then be passed to the QML engine so it can instantiate the QNetworkAccessManager with the custom behavior.
34 \snippet code/src_network_access_qnetworkaccessmanager.cpp 1
35
36 Note the QML engine may create QNetworkAccessManager instances
37 from multiple threads. Because of this, the implementation of the create()
38 method must be \l{Reentrancy and Thread-Safety}{reentrant}. In addition,
39 the developer should be careful if the signals of the object to be
40 returned from create() are connected to the slots of an object that may
41 be created in a different thread:
42
43 \list
44 \li The QML engine internally handles all requests, and cleans up any
45 QNetworkReply objects it creates. Receiving the
46 QNetworkAccessManager::finished() signal in another thread may not
47 provide the receiver with a valid reply object if it has already
48 been deleted.
49 \li Authentication details provided to QNetworkAccessManager::authenticationRequired()
50 must be provided immediately, so this signal cannot be connected as a
51 Qt::QueuedConnection (or as the default Qt::AutoConnection from another
52 thread).
53 \endlist
54
55 For more information about signals and threads, see
56 \l {Threads and QObjects} and \l {Signals and Slots Across Threads}.
57
58 \sa QNetworkDiskCache
59*/
60
61/*!
62 Destroys the factory. The default implementation does nothing.
63 */
64QQmlNetworkAccessManagerFactory::~QQmlNetworkAccessManagerFactory()
65{
66}
67
68/*!
69 \fn QNetworkAccessManager *QQmlNetworkAccessManagerFactory::create(QObject *parent)
70
71 Creates and returns a network access manager with the specified \a parent.
72 This method must return a new QNetworkAccessManager instance each time
73 it is called.
74
75 Note: this method may be called by multiple threads, so ensure the
76 implementation of this method is reentrant.
77*/
78
79#endif // qml_network
80
81QT_END_NAMESPACE
82

source code of qtdeclarative/src/qml/qml/qqmlnetworkaccessmanagerfactory.cpp