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_AUTOTEST_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;
145 QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
146 QDeclarativeWorkerScriptEngine *workerScriptEngine;
153 : count(0), values(0) {}
155 : count(0), values(new T*[r]) {}
164 T *at(int idx) const {
173 static void clear(SimpleList<QDeclarativeAbstractBinding> &);
174 static void clear(SimpleList<QDeclarativeParserStatus> &);
176 QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
177 QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
178 QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
179 QDeclarativeComponentAttached *componentAttached;
181 void registerFinalizedParserStatusObject(QObject *obj, int index) {
182 finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
187 QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
188 QNetworkAccessManager *getNetworkAccessManager() const;
189 mutable QNetworkAccessManager *networkAccessManager;
190 mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
192 QHash<QString,QSharedPointer<QDeclarativeImageProvider> > imageProviders;
193 QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url);
194 QSGTexture *getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
195 QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
196 QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size);
198 // Scarce resources are "exceptionally high cost" QVariant types where allowing the
199 // normal JavaScript GC to clean them up is likely to lead to out-of-memory or other
200 // out-of-resource situations. When such a resource is passed into JavaScript we
201 // add it to the scarceResources list and it is destroyed when we return from the
202 // JavaScript execution that created it. The user can prevent this behavior by
203 // calling preserve() on the object which removes it from this scarceResource list.
204 class ScarceResourceData {
206 ScarceResourceData(const QVariant &data) : data(data) {}
208 QIntrusiveListNode node;
210 QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;
211 int scarceResourcesRefCount;
212 void referenceScarceResources();
213 void dereferenceScarceResources();
215 mutable QMutex mutex;
217 QDeclarativeTypeLoader typeLoader;
218 QDeclarativeImportDatabase importDatabase;
220 QString offlineStoragePath;
222 mutable quint32 uniqueId;
223 quint32 getUniqueId() const {
227 QDeclarativeValueTypeFactory valueTypes;
229 QHash<QDeclarativeMetaType::ModuleApi, QDeclarativeMetaType::ModuleApiInstance *> moduleApiInstances;
231 QHash<const QMetaObject *, QDeclarativePropertyCache *> propertyCache;
232 QHash<QPair<QDeclarativeType *, int>, QDeclarativePropertyCache *> typePropertyCache;
233 inline QDeclarativePropertyCache *cache(QObject *obj);
234 inline QDeclarativePropertyCache *cache(const QMetaObject *);
235 inline QDeclarativePropertyCache *cache(QDeclarativeType *, int, QDeclarativeError &error);
236 QDeclarativePropertyCache *createCache(const QMetaObject *);
237 QDeclarativePropertyCache *createCache(QDeclarativeType *, int, QDeclarativeError &error);
239 void registerCompositeType(QDeclarativeCompiledData *);
242 QObject *toQObject(const QVariant &, bool *ok = 0) const;
243 QDeclarativeMetaType::TypeCategory typeCategory(int) const;
244 bool isList(int) const;
245 int listType(int) const;
246 const QMetaObject *rawMetaObjectForType(int) const;
247 const QMetaObject *metaObjectForType(int) const;
248 QHash<int, int> m_qmlLists;
249 QHash<int, QDeclarativeCompiledData *> m_compositeTypes;
252 void warning(const QDeclarativeError &);
253 void warning(const QList<QDeclarativeError> &);
254 static void warning(QDeclarativeEngine *, const QDeclarativeError &);
255 static void warning(QDeclarativeEngine *, const QList<QDeclarativeError> &);
256 static void warning(QDeclarativeEnginePrivate *, const QDeclarativeError &);
257 static void warning(QDeclarativeEnginePrivate *, const QList<QDeclarativeError> &);
259 static QV8Engine *getV8Engine(QDeclarativeEngine *e) { return &e->d_func()->v8engine; }
260 static QDeclarativeEnginePrivate *get(QDeclarativeEngine *e) { return e->d_func(); }
261 static QDeclarativeEnginePrivate *get(QDeclarativeContext *c) { return (c && c->engine()) ? QDeclarativeEnginePrivate::get(c->engine()) : 0; }
262 static QDeclarativeEnginePrivate *get(QDeclarativeContextData *c) { return (c && c->engine) ? QDeclarativeEnginePrivate::get(c->engine) : 0; }
263 static QDeclarativeEngine *get(QDeclarativeEnginePrivate *p) { return p->q_func(); }
265 static QString urlToLocalFileOrQrc(const QUrl& url);
267 static void defineModule();
269 static bool qml_debugging_enabled;
271 QSGContext *sgContext;
275 Returns a QDeclarativePropertyCache for \a obj if one is available.
277 If \a obj is null, being deleted or contains a dynamic meta object 0
280 The returned cache is not referenced, so if it is to be stored, call addref().
282 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj)
284 if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted)
287 const QMetaObject *mo = obj->metaObject();
288 QDeclarativePropertyCache *rv = propertyCache.value(mo);
289 if (!rv) rv = createCache(mo);
294 Returns a QDeclarativePropertyCache for \a metaObject.
296 As the cache is persisted for the life of the engine, \a metaObject must be
297 a static "compile time" meta-object, or a meta-object that is otherwise known to
298 exist for the lifetime of the QDeclarativeEngine.
300 The returned cache is not referenced, so if it is to be stored, call addref().
302 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject)
304 Q_ASSERT(metaObject);
306 QDeclarativePropertyCache *rv = propertyCache.value(metaObject);
307 if (!rv) rv = createCache(metaObject);
312 Returns a QDeclarativePropertyCache for \a type with \a minorVersion.
314 The returned cache is not referenced, so if it is to be stored, call addref().
316 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QDeclarativeType *type, int minorVersion, QDeclarativeError &error)
320 if (minorVersion == -1 || !type->containsRevisionedAttributes())
321 return cache(type->metaObject());
323 QDeclarativePropertyCache *rv = typePropertyCache.value(qMakePair(type, minorVersion));
324 if (!rv) rv = createCache(type, minorVersion, error);
330 #endif // QDECLARATIVEENGINE_P_H