Merge the QJSEngine and QJSValue development branch into master.
[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_DECLARATIVE_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     QV8Engine *v8engine() const { return q_func()->handle(); }
143
144     QDeclarativeWorkerScriptEngine *getWorkerScriptEngine();
145     QDeclarativeWorkerScriptEngine *workerScriptEngine;
146
147     QUrl baseUrl;
148
149     template<class T>
150     struct SimpleList {
151         SimpleList()
152             : count(0), values(0) {}
153         SimpleList(int r)
154             : count(0), values(new T*[r]) {}
155
156         int count;
157         T **values;
158
159         void append(T *v) {
160             values[count++] = v;
161         }
162
163         T *at(int idx) const {
164             return values[idx];
165         }
166
167         void clear() {
168             delete [] values;
169         }
170     };
171
172     static void clear(SimpleList<QDeclarativeAbstractBinding> &);
173     static void clear(SimpleList<QDeclarativeParserStatus> &);
174
175     QList<SimpleList<QDeclarativeAbstractBinding> > bindValues;
176     QList<SimpleList<QDeclarativeParserStatus> > parserStatus;
177     QList<QPair<QDeclarativeGuard<QObject>,int> > finalizedParserStatus;
178     QDeclarativeComponentAttached *componentAttached;
179
180     void registerFinalizedParserStatusObject(QObject *obj, int index) {
181         finalizedParserStatus.append(qMakePair(QDeclarativeGuard<QObject>(obj), index));
182     }
183
184     bool inBeginCreate;
185
186     QNetworkAccessManager *createNetworkAccessManager(QObject *parent) const;
187     QNetworkAccessManager *getNetworkAccessManager() const;
188     mutable QNetworkAccessManager *networkAccessManager;
189     mutable QDeclarativeNetworkAccessManagerFactory *networkAccessManagerFactory;
190
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);
196
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 {
204     public:
205         ScarceResourceData(const QVariant &data) : data(data) {}
206         QVariant data;
207         QIntrusiveListNode node;
208     };
209     QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;
210     int scarceResourcesRefCount;
211     void referenceScarceResources();
212     void dereferenceScarceResources();
213
214     mutable QMutex mutex;
215
216     QDeclarativeTypeLoader typeLoader;
217     QDeclarativeImportDatabase importDatabase;
218
219     QString offlineStoragePath;
220
221     mutable quint32 uniqueId;
222     quint32 getUniqueId() const {
223         return uniqueId++;
224     }
225
226     QDeclarativeValueTypeFactory valueTypes;
227
228     QHash<QDeclarativeMetaType::ModuleApi, QDeclarativeMetaType::ModuleApiInstance *> moduleApiInstances;
229
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);
237
238     void registerCompositeType(QDeclarativeCompiledData *);
239
240     bool isQObject(int);
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;
249
250     void sendQuit();
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> &);
257
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(); }
263
264     static QString urlToLocalFileOrQrc(const QUrl& url);
265
266     static void registerBaseTypes(const char *uri, int versionMajor, int versionMinor);
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