Merge branch 'qtquick2' of scm.dev.nokia.troll.no:qt/qtdeclarative-staging into v8
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeengine_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEENGINE_P_H
43 #define QDECLARATIVEENGINE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarativeengine.h"
57
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"
72
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>
78
79 #include <private/qobject_p.h>
80
81 #include <private/qv8engine_p.h>
82
83 QT_BEGIN_NAMESPACE
84
85 class QDeclarativeContext;
86 class QDeclarativeEngine;
87 class QDeclarativeContextPrivate;
88 class QDeclarativeExpression;
89 class QDeclarativeImportDatabase;
90 class ScarceResourceData;
91 class QNetworkReply;
92 class QNetworkAccessManager;
93 class QDeclarativeNetworkAccessManagerFactory;
94 class QDeclarativeAbstractBinding;
95 class QDeclarativeTypeNameCache;
96 class QDeclarativeComponentAttached;
97 class QDeclarativeCleanup;
98 class QDeclarativeDelayedError;
99 class QDeclarativeWorkerScriptEngine;
100 class QDir;
101 class QSGTexture;
102 class QSGContext;
103
104 class Q_AUTOTEST_EXPORT QDeclarativeEnginePrivate : public QObjectPrivate
105 {
106     Q_DECLARE_PUBLIC(QDeclarativeEngine)
107 public:
108     QDeclarativeEnginePrivate(QDeclarativeEngine *);
109     ~QDeclarativeEnginePrivate();
110
111     void init();
112
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) {}
118
119         QObject *object;
120         int coreIndex;
121         QDeclarativeNotifier *notifier;
122         int notifyIndex;
123     };
124     bool captureProperties;
125     QPODVector<CapturedProperty> capturedProperties;
126
127     QDeclarativeContext *rootContext;
128     bool isDebugging;
129
130     bool outputWarningsToStdErr;
131
132     QDeclarativeContextData *sharedContext;
133     QObject *sharedScope;
134
135     // Registered cleanup handlers
136     QDeclarativeCleanup *cleanup;
137
138     // Bindings that have had errors during startup
139     QDeclarativeDelayedError *erroredBindings;
140     int inProgressCreations;
141
142     // V8 Engine
143     QV8Engine v8engine;
144
145     QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
146     QDeclarativeWorkerScriptEngine *workerScriptEngine;
147
148     QUrl baseUrl;
149
150     template<class T>
151     struct SimpleList {
152         SimpleList()
153             : count(0), values(0) {}
154         SimpleList(int r)
155             : count(0), values(new T*[r]) {}
156
157         int count;
158         T **values;
159
160         void append(T *v) {
161             values[count++] = v;
162         }
163
164         T *at(int idx) const {
165             return values[idx];
166         }
167
168         void clear() {
169             delete [] values;
170         }
171     };
172
173     static void clear(SimpleList<QDeclarativeAbstractBinding> &);
174     static void clear(SimpleList<QDeclarativeParserStatus> &);
175
176     QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
177     QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
178     QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
179     QDeclarativeComponentAttached *componentAttached;
180
181     void registerFinalizedParserStatusObject(QObject *obj, int index) {
182         finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
183     }
184
185     bool inBeginCreate;
186
187     QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
188     QNetworkAccessManager *getNetworkAccessManager() const;
189     mutable QNetworkAccessManager *networkAccessManager;
190     mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
191
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);
197
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 {
205     public:
206         ScarceResourceData(const QVariant &data) : data(data) {}
207         QVariant data;
208         QIntrusiveListNode node;
209     };
210     QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;
211     int scarceResourcesRefCount;
212     void referenceScarceResources();
213     void dereferenceScarceResources();
214
215     mutable QMutex mutex;
216
217     QDeclarativeTypeLoader typeLoader;
218     QDeclarativeImportDatabase importDatabase;
219
220     QString offlineStoragePath;
221
222     mutable quint32 uniqueId;
223     quint32 getUniqueId() const {
224         return uniqueId++;
225     }
226
227     QDeclarativeValueTypeFactory valueTypes;
228
229     QHash<QDeclarativeMetaType::ModuleApi, QDeclarativeMetaType::ModuleApiInstance *> moduleApiInstances;
230
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);
238
239     void registerCompositeType(QDeclarativeCompiledData *);
240
241     bool isQObject(int);
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;
250
251     void sendQuit();
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> &);
258
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(); }
264
265     static QString urlToLocalFileOrQrc(const QUrl& url);
266
267     static void defineModule();
268
269     static bool qml_debugging_enabled;
270
271     QSGContext *sgContext;
272 };
273
274 /*!
275 Returns a QDeclarativePropertyCache for \a obj if one is available.
276
277 If \a obj is null, being deleted or contains a dynamic meta object 0
278 is returned.
279
280 The returned cache is not referenced, so if it is to be stored, call addref().
281 */
282 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QObject *obj)
283 {
284     if (!obj || QObjectPrivate::get(obj)->metaObject || QObjectPrivate::get(obj)->wasDeleted)
285         return 0;
286
287     const QMetaObject *mo = obj->metaObject();
288     QDeclarativePropertyCache *rv = propertyCache.value(mo);
289     if (!rv) rv = createCache(mo);
290     return rv;
291 }
292
293 /*!
294 Returns a QDeclarativePropertyCache for \a metaObject.
295
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.
299
300 The returned cache is not referenced, so if it is to be stored, call addref().
301 */
302 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(const QMetaObject *metaObject)
303 {
304     Q_ASSERT(metaObject);
305
306     QDeclarativePropertyCache *rv = propertyCache.value(metaObject);
307     if (!rv) rv = createCache(metaObject);
308     return rv;
309 }
310
311 /*!
312 Returns a QDeclarativePropertyCache for \a type with \a minorVersion.
313
314 The returned cache is not referenced, so if it is to be stored, call addref().
315 */
316 QDeclarativePropertyCache *QDeclarativeEnginePrivate::cache(QDeclarativeType *type, int minorVersion, QDeclarativeError &error)
317 {
318     Q_ASSERT(type);
319
320     if (minorVersion == -1 || !type->containsRevisionedAttributes())
321         return cache(type->metaObject());
322
323     QDeclarativePropertyCache *rv = typePropertyCache.value(qMakePair(type, minorVersion));
324     if (!rv) rv = createCache(type, minorVersion, error);
325     return rv;
326 }
327
328 QT_END_NAMESPACE
329
330 #endif // QDECLARATIVEENGINE_P_H