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 <QtDeclarative/qjsvalue.h>
59 #include <QtDeclarative/qdeclarativeerror.h>
60 #include <QtDeclarative/qdeclarativeengine.h>
61 #include <private/qdeclarativecleanup_p.h>
62 #include <private/qdeclarativescriptparser_p.h>
63 #include <private/qdeclarativedirparser_p.h>
64 #include <private/qdeclarativeimport_p.h>
65 #include "private/qhashedstring_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 QString absoluteFilePath(const QString &path);
208 bool directoryExists(const QString &path);
209 const QDeclarativeDirParser *qmlDirParser(const QString &absoluteFilePath);
212 typedef QHash<QUrl, QDeclarativeTypeData *> TypeCache;
213 typedef QHash<QUrl, QDeclarativeScriptBlob *> ScriptCache;
214 typedef QHash<QUrl, QDeclarativeQmldirData *> QmldirCache;
215 typedef QStringHash<bool> StringSet;
216 typedef QStringHash<StringSet*> ImportDirCache;
217 typedef QStringHash<QDeclarativeDirParser*> ImportQmlDirCache;
219 TypeCache m_typeCache;
220 ScriptCache m_scriptCache;
221 QmldirCache m_qmldirCache;
222 ImportDirCache m_importDirCache;
223 ImportQmlDirCache m_importQmlDirCache;
226 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativeTypeLoader::Options)
228 class Q_AUTOTEST_EXPORT QDeclarativeTypeData : public QDeclarativeDataBlob
233 TypeReference() : type(0), majorVersion(0), minorVersion(0), typeData(0) {}
235 QDeclarativeParser::Location location;
236 QDeclarativeType *type;
239 QDeclarativeTypeData *typeData;
242 struct ScriptReference
244 ScriptReference() : script(0) {}
246 QDeclarativeParser::Location location;
248 QDeclarativeScriptBlob *script;
251 QDeclarativeTypeData(const QUrl &, QDeclarativeTypeLoader::Options, QDeclarativeTypeLoader *);
252 ~QDeclarativeTypeData();
254 QDeclarativeTypeLoader *typeLoader() const;
256 const QDeclarativeImports &imports() const;
257 const QDeclarativeScriptParser &parser() const;
259 const QList<TypeReference> &resolvedTypes() const;
260 const QList<ScriptReference> &resolvedScripts() const;
262 QDeclarativeCompiledData *compiledData() const;
264 // Used by QDeclarativeComponent to get notifications
265 struct TypeDataCallback {
266 ~TypeDataCallback() {}
267 virtual void typeDataProgress(QDeclarativeTypeData *, qreal) {}
268 virtual void typeDataReady(QDeclarativeTypeData *) {}
270 void registerCallback(TypeDataCallback *);
271 void unregisterCallback(TypeDataCallback *);
275 virtual void dataReceived(const QByteArray &);
276 virtual void allDependenciesDone();
277 virtual void downloadProgressChanged(qreal);
283 QDeclarativeTypeLoader::Options m_options;
285 QDeclarativeQmldirData *qmldirForUrl(const QUrl &);
287 QDeclarativeScriptParser scriptParser;
288 QDeclarativeImports m_imports;
290 QList<ScriptReference> m_scripts;
291 QList<QDeclarativeQmldirData *> m_qmldirs;
293 QList<TypeReference> m_types;
294 bool m_typesResolved:1;
296 QDeclarativeCompiledData *m_compiledData;
298 QList<TypeDataCallback *> m_callbacks;
300 QDeclarativeTypeLoader *m_typeLoader;
303 class Q_AUTOTEST_EXPORT QDeclarativeScriptData : public QDeclarativeRefCount, public QDeclarativeCleanup
306 QDeclarativeScriptData(QDeclarativeEngine *);
307 ~QDeclarativeScriptData();
310 QDeclarativeTypeNameCache *importCache;
311 QList<QDeclarativeScriptBlob *> scripts;
312 QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas;
315 virtual void clear(); // From QDeclarativeCleanup
318 friend class QDeclarativeVME;
319 friend class QDeclarativeScriptBlob;
322 v8::Persistent<v8::Script> m_program;
323 v8::Persistent<v8::Object> m_value;
324 // QScriptProgram m_program;
325 // QScriptValue m_value;
328 class Q_AUTOTEST_EXPORT QDeclarativeScriptBlob : public QDeclarativeDataBlob
331 QDeclarativeScriptBlob(const QUrl &, QDeclarativeTypeLoader *);
332 ~QDeclarativeScriptBlob();
334 struct ScriptReference
336 ScriptReference() : script(0) {}
338 QDeclarativeParser::Location location;
340 QDeclarativeScriptBlob *script;
343 QDeclarativeParser::Object::ScriptBlock::Pragmas pragmas() const;
344 QString scriptSource() const;
346 QDeclarativeTypeLoader *typeLoader() const;
347 const QDeclarativeImports &imports() const;
349 QDeclarativeScriptData *scriptData() const;
352 virtual void dataReceived(const QByteArray &);
356 QDeclarativeParser::Object::ScriptBlock::Pragmas m_pragmas;
359 QDeclarativeImports m_imports;
360 QList<ScriptReference> m_scripts;
361 QDeclarativeScriptData *m_scriptData;
363 QDeclarativeTypeLoader *m_typeLoader;
366 class Q_AUTOTEST_EXPORT QDeclarativeQmldirData : public QDeclarativeDataBlob
369 QDeclarativeQmldirData(const QUrl &);
371 const QDeclarativeDirComponents &dirComponents() const;
374 virtual void dataReceived(const QByteArray &);
377 QDeclarativeDirComponents m_components;
383 #endif // QDECLARATIVETYPELOADER_P_H