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 QDECLARATIVEENGINE_P_H
43 #define QDECLARATIVEENGINE_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 "qdeclarativeengine.h"
58 #include "private/qdeclarativetypeloader_p.h"
59 #include "private/qdeclarativeimport_p.h"
60 #include "private/qpodvector_p.h"
61 #include "qdeclarative.h"
62 #include "private/qdeclarativevaluetype_p.h"
63 #include "qdeclarativecontext.h"
64 #include "private/qdeclarativecontext_p.h"
65 #include "qdeclarativeexpression.h"
66 #include "qdeclarativeimageprovider.h"
67 #include "private/qdeclarativeproperty_p.h"
68 #include "private/qdeclarativepropertycache_p.h"
69 #include "private/qdeclarativemetatype_p.h"
70 #include "private/qdeclarativedirparser_p.h"
71 #include "private/qintrusivelist_p.h"
73 #include <QtCore/qstring.h>
74 #include <QtCore/qlist.h>
75 #include <QtCore/qpair.h>
76 #include <QtCore/qstack.h>
77 #include <QtCore/qmutex.h>
79 #include <private/qobject_p.h>
81 #include <private/qv8engine_p.h>
85 class QDeclarativeContext;
86 class QDeclarativeEngine;
87 class QDeclarativeContextPrivate;
88 class QDeclarativeExpression;
89 class QDeclarativeImportDatabase;
90 class ScarceResourceData;
92 class QNetworkAccessManager;
93 class QDeclarativeNetworkAccessManagerFactory;
94 class QDeclarativeAbstractBinding;
95 class QDeclarativeTypeNameCache;
96 class QDeclarativeComponentAttached;
97 class QDeclarativeCleanup;
98 class QDeclarativeDelayedError;
99 class QDeclarativeWorkerScriptEngine;
104 class Q_DECLARATIVE_EXPORT QDeclarativeEnginePrivate : public QObjectPrivate
106 Q_DECLARE_PUBLIC(QDeclarativeEngine)
108 QDeclarativeEnginePrivate(QDeclarativeEngine *);
109 ~QDeclarativeEnginePrivate();
113 struct CapturedProperty {
114 CapturedProperty(QObject *o, int c, int n)
115 : object(o), coreIndex(c), notifier(0), notifyIndex(n) {}
116 CapturedProperty(QDeclarativeNotifier *n)
117 : object(0), coreIndex(-1), notifier(n), notifyIndex(-1) {}
121 QDeclarativeNotifier *notifier;
124 bool captureProperties;
125 QPODVector<CapturedProperty> capturedProperties;
127 QDeclarativeContext *rootContext;
130 bool outputWarningsToStdErr;
132 QDeclarativeContextData *sharedContext;
133 QObject *sharedScope;
135 // Registered cleanup handlers
136 QDeclarativeCleanup *cleanup;
138 // Bindings that have had errors during startup
139 QDeclarativeDelayedError *erroredBindings;
140 int inProgressCreations;
142 QV8Engine *v8engine() const { return q_func()->handle(); }
144 QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
145 QDeclarativeWorkerScriptEngine *workerScriptEngine;
152 : count(0), values(0) {}
154 : count(0), values(new T*[r]) {}
163 T *at(int idx) const {
172 static void clear(SimpleList<QDeclarativeAbstractBinding> &);
173 static void clear(SimpleList<QDeclarativeParserStatus> &);
175 QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
176 QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
177 QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
178 QDeclarativeComponentAttached *componentAttached;
180 void registerFinalizedParserStatusObject(QObject *obj, int index) {
181 finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
186 QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
187 QNetworkAccessManager *getNetworkAccessManager() const;
188 mutable QNetworkAccessManager *networkAccessManager;
189 mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
191 QHash<QString,QSharedPointer<QDeclarativeImageProvider> > imageProviders;
192 QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url);
193 QSGTexture *getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
194 QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
195 QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
197 // Scarce resources are "exceptionally high cost" QVariant types where allowing the
198 // normal JavaScript GC to clean them up is likely to lead to out-of-memory or other
199 // out-of-resource situations. When such a resource is passed into JavaScript we
200 // add it to the scarceResources list and it is destroyed when we return from the
201 // JavaScript execution that created it. The user can prevent this behavior by
202 // calling preserve() on the object which removes it from this scarceResource list.
203 class ScarceResourceData {
205 ScarceResourceData(const QVariant &data) : data(data) {}
207 QIntrusiveListNode node;
209 QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;
210 int scarceResourcesRefCount;
211 void referenceScarceResources();
212 void dereferenceScarceResources();
214 mutable QMutex mutex;
216 QDeclarativeTypeLoader typeLoader;
217 QDeclarativeImportDatabase importDatabase;
219 QString offlineStoragePath;
221 mutable quint32 uniqueId;
222 quint32 getUniqueId() const {
226 QDeclarativeValueTypeFactory valueTypes;
228 QHash<QDeclarativeMetaType::ModuleApi, QDeclarativeMetaType::ModuleApiInstance *> moduleApiInstances;
230 QHash<const QMetaObject *, QDeclarativePropertyCache *> propertyCache;
231 QHash<QPair<QDeclarativeType *, int>, QDeclarativePropertyCache *> typePropertyCache;
232 inline QDeclarativePropertyCache *cache(QObject *obj);
233 inline QDeclarativePropertyCache *cache(const QMetaObject *);
234 inline QDeclarativePropertyCache *cache(QDeclarativeType *, int, QDeclarativeError &error);
235 QDeclarativePropertyCache *createCache(const QMetaObject *);
236 QDeclarativePropertyCache *createCache(QDeclarativeType *, int, QDeclarativeError &error);
238 void registerCompositeType(QDeclarativeCompiledData *);
241 QObject *toQObject(const QVariant &, bool *ok = 0) const;
242 QDeclarativeMetaType::TypeCategory typeCategory(int) const;
243 bool isList(int) const;
244 int listType(int) const;
245 const QMetaObject *rawMetaObjectForType(int) const;
246 const QMetaObject *metaObjectForType(int) const;
247 QHash<int, int> m_qmlLists;
248 QHash<int, QDeclarativeCompiledData *> m_compositeTypes;
251 void warning(const QDeclarativeError &);
252 void warning(const QList<QDeclarativeError> &);
253 static void warning(QDeclarativeEngine *, const QDeclarativeError &);
254 static void warning(QDeclarativeEngine *, const QList<QDeclarativeError> &);
255 static void warning(QDeclarativeEnginePrivate *, const QDeclarativeError &);
256 static void warning(QDeclarativeEnginePrivate *, const QList<QDeclarativeError> &);
258 static QV8Engine *getV8Engine(QDeclarativeEngine *e) { return e->d_func()->v8engine(); }
259 static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); }
260 static QDeclarativeEnginePrivate *get(QDeclarativeContext *c) { return (c && c->engine()) ? QDeclarativeEnginePrivate::get(c->engine()) : 0; }
261 static QDeclarativeEnginePrivate *get(QDeclarativeContextData *c) { return (c && c->engine) ? QDeclarativeEnginePrivate::get(c->engine) : 0; }
262 static QDeclarativeEngine *get(QDeclarativeEnginePrivate *p) { return p->q_func(); }
264 static QString urlToLocalFileOrQrc(const QUrl& url);
265 static QString urlToLocalFileOrQrc(const QString& url);
267 static void registerBaseTypes(const char *uri, int versionMajor, int versionMinor);
268 static void defineModule();
270 static bool qml_debugging_enabled;
272 QSGContext *sgContext;
276 Returns a QDeclarativePropertyCache for \a obj if one is available.
278 If \a obj is null, being deleted or contains a dynamic meta object 0
281 The returned cache is not referenced, so if it is to be stored, call addref().
283 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj)
285 if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted)
288 const QMetaObject *mo = obj->metaObject();
289 QDeclarativePropertyCache *rv = propertyCache.value(mo);
290 if (!rv) rv = createCache(mo);
295 Returns a QDeclarativePropertyCache for \a metaObject.
297 As the cache is persisted for the life of the engine, \a metaObject must be
298 a static "compile time" meta-object, or a meta-object that is otherwise known to
299 exist for the lifetime of the QDeclarativeEngine.
301 The returned cache is not referenced, so if it is to be stored, call addref().
303 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject)
305 Q_ASSERT(metaObject);
307 QDeclarativePropertyCache *rv = propertyCache.value(metaObject);
308 if (!rv) rv = createCache(metaObject);
313 Returns a QDeclarativePropertyCache for \a type with \a minorVersion.
315 The returned cache is not referenced, so if it is to be stored, call addref().
317 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QDeclarativeType *type, int minorVersion, QDeclarativeError &error)
321 if (minorVersion == -1 || !type->containsRevisionedAttributes())
322 return cache(type->metaObject());
324 QDeclarativePropertyCache *rv = typePropertyCache.value(qMakePair(type, minorVersion));
325 if (!rv) rv = createCache(type, minorVersion, error);
331 #endif // QDECLARATIVEENGINE_P_H