1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVETYPELOADER_P_H
43 #define QDECLARATIVETYPELOADER_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <QtCore/qobject.h>
57 #include <QtNetwork/qnetworkreply.h>
58 #include <QtScript/qscriptvalue.h>
59 #include <QtScript/qscriptprogram.h>
60 #include <QtDeclarative/qdeclarativeerror.h>
61 #include <QtDeclarative/qdeclarativeengine.h>
62 #include <private/qdeclarativecleanup_p.h>
63 #include <private/qdeclarativescriptparser_p.h>
64 #include <private/qdeclarativedirparser_p.h>
65 #include <private/qdeclarativeimport_p.h>
67 #include <private/qv8_p.h>
71 class QDeclarativeScriptData;
72 class QDeclarativeScriptBlob;
73 class QDeclarativeQmldirData;
74 class QDeclarativeTypeLoader;
75 class QDeclarativeCompiledData;
76 class QDeclarativeComponentPrivate;
77 class QDeclarativeTypeData;
78 class QDeclarativeDataLoader;
80 class Q_AUTOTEST_EXPORT QDeclarativeDataBlob : public QDeclarativeRefCount
84 Null, // Prior to QDeclarativeDataLoader::load()
85 Loading, // Prior to data being received and dataReceived() being called
86 WaitingForDependencies, // While there are outstanding addDependency()s
97 QDeclarativeDataBlob(const QUrl &, Type);
98 virtual ~QDeclarativeDataBlob();
102 Status status() const;
104 bool isLoading() const;
105 bool isWaiting() const;
106 bool isComplete() const;
107 bool isError() const;
108 bool isCompleteOrError() const;
110 qreal progress() const;
113 QUrl finalUrl() const;
115 QList<QDeclarativeError> errors() const;
117 void setError(const QDeclarativeError &);
118 void setError(const QList<QDeclarativeError> &errors);
120 void addDependency(QDeclarativeDataBlob *);
123 virtual void dataReceived(const QByteArray &) = 0;
126 virtual void networkError(QNetworkReply::NetworkError);
128 virtual void dependencyError(QDeclarativeDataBlob *);
129 virtual void dependencyComplete(QDeclarativeDataBlob *);
130 virtual void allDependenciesDone();
132 virtual void downloadProgressChanged(qreal);
135 friend class QDeclarativeDataLoader;
137 void cancelAllWaitingFor();
138 void notifyAllWaitingOnMe();
139 void notifyComplete(QDeclarativeDataBlob *);
148 // List of QDeclarativeDataBlob's that are waiting for me to complete.
149 QList<QDeclarativeDataBlob *> m_waitingOnMe;
151 // List of QDeclarativeDataBlob's that I am waiting for to complete.
152 QList<QDeclarativeDataBlob *> m_waitingFor;
154 // Manager that is currently fetching data for me
155 QDeclarativeDataLoader *m_manager;
156 int m_redirectCount:30;
160 QList<QDeclarativeError> m_errors;
163 class Q_AUTOTEST_EXPORT QDeclarativeDataLoader : public QObject
167 QDeclarativeDataLoader(QDeclarativeEngine *);
168 ~QDeclarativeDataLoader();
170 void load(QDeclarativeDataBlob *);
171 void loadWithStaticData(QDeclarativeDataBlob *, const QByteArray &);
173 QDeclarativeEngine *engine() const;
176 void networkReplyFinished();
177 void networkReplyProgress(qint64,qint64);
180 void setData(QDeclarativeDataBlob *, const QByteArray &);
182 QDeclarativeEngine *m_engine;
183 typedef QHash<QNetworkReply *, QDeclarativeDataBlob *> NetworkReplies;
184 NetworkReplies m_networkReplies;
187 class Q_AUTOTEST_EXPORT QDeclarativeTypeLoader : public QDeclarativeDataLoader
191 QDeclarativeTypeLoader(QDeclarativeEngine *);
192 ~QDeclarativeTypeLoader();
198 Q_DECLARE_FLAGS(Options, Option)
200 QDeclarativeTypeData *get(const QUrl &url);
201 QDeclarativeTypeData *get(const QByteArray &, const QUrl &url, Options = None);
204 QDeclarativeScriptBlob *getScript(const QUrl &);
205 QDeclarativeQmldirData *getQmldir(const QUrl &);
207 typedef QHash<QUrl, QDeclarativeTypeData *> TypeCache;
208 typedef QHash<QUrl, QDeclarativeScriptBlob *> ScriptCache;
209 typedef QHash<QUrl, QDeclarativeQmldirData *> QmldirCache;
211 TypeCache m_typeCache;
212 ScriptCache m_scriptCache;
213 QmldirCache m_qmldirCache;
216 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeTypeLoader::Options)
218 class Q_AUTOTEST_EXPORT QDeclarativeTypeData : public QDeclarativeDataBlob
223 TypeReference() : type(0), majorVersion(0), minorVersion(0), typeData(0) {}
225 QDeclarativeParser::Location location;
226 QDeclarativeType *type;
229 QDeclarativeTypeData *typeData;
232 struct ScriptReference
234 ScriptReference() : script(0) {}
236 QDeclarativeParser::Location location;
238 QDeclarativeScriptBlob *script;
241 QDeclarativeTypeData(const QUrl &, QDeclarativeTypeLoader::Options, QDeclarativeTypeLoader *);
242 ~QDeclarativeTypeData();
244 QDeclarativeTypeLoader *typeLoader() const;
246 const QDeclarativeImports &imports() const;
247 const QDeclarativeScriptParser &parser() const;
249 const QList<TypeReference> &resolvedTypes() const;
250 const QList<ScriptReference> &resolvedScripts() const;
252 QDeclarativeCompiledData *compiledData() const;
254 // Used by QDeclarativeComponent to get notifications
255 struct TypeDataCallback {
256 ~TypeDataCallback() {}
257 virtual void typeDataProgress(QDeclarativeTypeData *, qreal) {}
258 virtual void typeDataReady(QDeclarativeTypeData *) {}
260 void registerCallback(TypeDataCallback *);
261 void unregisterCallback(TypeDataCallback *);
265 virtual void dataReceived(const QByteArray &);
266 virtual void allDependenciesDone();
267 virtual void downloadProgressChanged(qreal);
273 QDeclarativeTypeLoader::Options m_options;
275 QDeclarativeQmldirData *qmldirForUrl(const QUrl &);
277 QDeclarativeScriptParser scriptParser;
278 QDeclarativeImports m_imports;
280 QList<ScriptReference> m_scripts;
281 QList<QDeclarativeQmldirData *> m_qmldirs;
283 QList<TypeReference> m_types;
284 bool m_typesResolved:1;
286 QDeclarativeCompiledData *m_compiledData;
288 QList<TypeDataCallback *> m_callbacks;
290 QDeclarativeTypeLoader *m_typeLoader;
293 class Q_AUTOTEST_EXPORT QDeclarativeScriptData : public QDeclarativeRefCount, public QDeclarativeCleanup
296 QDeclarativeScriptData(QDeclarativeEngine *);
297 ~QDeclarativeScriptData();
300 QDeclarativeTypeNameCache *importCache;
301 QList<QDeclarativeScriptBlob *> scripts;
302 QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas;
305 virtual void clear(); // From QDeclarativeCleanup
308 friend class QDeclarativeVME;
309 friend class QDeclarativeScriptBlob;
312 v8::Persistent<v8::Script> m_program;
313 v8::Persistent<v8::Object> m_value;
314 // QScriptProgram m_program;
315 // QScriptValue m_value;
318 class Q_AUTOTEST_EXPORT QDeclarativeScriptBlob : public QDeclarativeDataBlob
321 QDeclarativeScriptBlob(const QUrl &, QDeclarativeTypeLoader *);
322 ~QDeclarativeScriptBlob();
324 struct ScriptReference
326 ScriptReference() : script(0) {}
328 QDeclarativeParser::Location location;
330 QDeclarativeScriptBlob *script;
333 QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas() const;
334 QString scriptSource() const;
336 QDeclarativeTypeLoader *typeLoader() const;
337 const QDeclarativeImports &imports() const;
339 QDeclarativeScriptData *scriptData() const;
342 virtual void dataReceived(const QByteArray &);
346 QDeclarativeParser::Object::ScriptBlock::Pragmas m_pragmas;
349 QDeclarativeImports m_imports;
350 QList<ScriptReference> m_scripts;
351 QDeclarativeScriptData *m_scriptData;
353 QDeclarativeTypeLoader *m_typeLoader;
356 class Q_AUTOTEST_EXPORT QDeclarativeQmldirData : public QDeclarativeDataBlob
359 QDeclarativeQmldirData(const QUrl &);
361 const QDeclarativeDirComponents &dirComponents() const;
364 virtual void dataReceived(const QByteArray &);
367 QDeclarativeDirComponents m_components;
373 #endif // QDECLARATIVETYPELOADER_P_H