Update obsolete contact address.
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qv8profilerservice.cpp
index 279992a..f9cd4fb 100644 (file)
@@ -1,8 +1,8 @@
 /****************************************************************************
 **
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Contact: http://www.qt-project.org/
 **
 ** This file is part of the QtDeclarative module of the Qt Toolkit.
 **
@@ -41,7 +41,7 @@
 
 #include "qv8profilerservice_p.h"
 #include "qdeclarativedebugservice_p_p.h"
-#include <private/qdeclarativeengine_p.h>
+#include "private/qjsconverter_impl_p.h"
 #include <private/qv8profiler_p.h>
 
 #include <QtCore/QHash>
@@ -50,18 +50,20 @@ QT_BEGIN_NAMESPACE
 
 Q_GLOBAL_STATIC(QV8ProfilerService, v8ProfilerInstance)
 
-class ByteArrayOutputStream : public v8::OutputStream
+class DebugServiceOutputStream : public v8::OutputStream
 {
-    QByteArray *_buffer;
+    QDeclarativeDebugService &_service;
 public:
-    ByteArrayOutputStream(QByteArray *buffer)
+    DebugServiceOutputStream(QDeclarativeDebugService &service)
         : v8::OutputStream(),
-          _buffer(buffer) {}
+          _service(service) {}
     void EndOfStream() {}
-    WriteResult WriteAsciiChunk(char *data, int size)
+    WriteResult WriteAsciiChunk(char *rawData, int size)
     {
-        QByteArray b(data, size);
-        _buffer->append(b);
+        QByteArray data;
+        QDataStream ds(&data, QIODevice::WriteOnly);
+        ds << QV8ProfilerService::V8SnapshotChunk << QByteArray(rawData, size);
+        _service.sendMessage(data);
         return kContinue;
     }
 };
@@ -84,7 +86,6 @@ class QV8ProfilerServicePrivate : public QDeclarativeDebugServicePrivate
 public:
     QV8ProfilerServicePrivate()
         :initialized(false)
-        , isolate(0)
     {
     }
 
@@ -96,12 +97,10 @@ public:
     QList<QV8ProfilerData> m_data;
 
     bool initialized;
-    QList<QDeclarativeEngine *> engines;
-    v8::Isolate *isolate;
 };
 
 QV8ProfilerService::QV8ProfilerService(QObject *parent)
-    : QDeclarativeDebugService(*(new QV8ProfilerServicePrivate()), QLatin1String("V8Profiler"), parent)
+    : QDeclarativeDebugService(*(new QV8ProfilerServicePrivate()), QLatin1String("V8Profiler"), 1, parent)
 {
     Q_D(QV8ProfilerService);
 
@@ -121,22 +120,10 @@ QV8ProfilerService *QV8ProfilerService::instance()
     return v8ProfilerInstance();
 }
 
-void QV8ProfilerService::addEngine(QDeclarativeEngine *engine)
+void QV8ProfilerService::initialize()
 {
-    Q_D(QV8ProfilerService);
-    Q_ASSERT(engine);
-    Q_ASSERT(!d->engines.contains(engine));
-
-    d->engines.append(engine);
-}
-
-void QV8ProfilerService::removeEngine(QDeclarativeEngine *engine)
-{
-    Q_D(QV8ProfilerService);
-    Q_ASSERT(engine);
-    Q_ASSERT(d->engines.contains(engine));
-
-    d->engines.removeOne(engine);
+    // just make sure that the service is properly registered
+    v8ProfilerInstance();
 }
 
 void QV8ProfilerService::messageReceived(const QByteArray &message)
@@ -149,30 +136,22 @@ void QV8ProfilerService::messageReceived(const QByteArray &message)
     QByteArray title;
     ds >> command >> option;
 
-    if (!d->isolate) {
-        d->isolate = v8::Isolate::New();
-        v8::Isolate::Scope scope(d->isolate);
-        v8::V8::Initialize();
-    }
-
-    v8::Isolate::Scope scope(d->isolate);
-
     if (command == "V8PROFILER") {
         ds >>  title;
         if (option == "start") {
-            startProfiling(QString::fromUtf8(title));
+            QMetaObject::invokeMethod(this, "startProfiling", Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(title)));
         } else if (option == "stop") {
-            stopProfiling(QString::fromUtf8(title));
-            sendProfilingData();
+            QMetaObject::invokeMethod(this, "stopProfiling", Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(title)));
+            QMetaObject::invokeMethod(this, "sendProfilingData", Qt::QueuedConnection);
         }
         d->initialized = true;
     }
 
     if (command == "V8SNAPSHOT") {
         if (option == "full")
-            d->takeSnapshot(v8::HeapSnapshot::kFull);
+            QMetaObject::invokeMethod(this, "takeSnapshot", Qt::QueuedConnection);
         else if (option == "delete") {
-            v8::HeapProfiler::DeleteAllSnapshots();
+            QMetaObject::invokeMethod(this, "deleteSnapshots", Qt::QueuedConnection);
         }
     }
 
@@ -201,6 +180,17 @@ void QV8ProfilerService::stopProfiling(const QString &title)
     }
 }
 
+void QV8ProfilerService::takeSnapshot()
+{
+    Q_D(QV8ProfilerService);
+    d->takeSnapshot(v8::HeapSnapshot::kFull);
+}
+
+void QV8ProfilerService::deleteSnapshots()
+{
+    v8::HeapProfiler::DeleteAllSnapshots();
+}
+
 void QV8ProfilerService::sendProfilingData()
 {
     Q_D(QV8ProfilerService);
@@ -212,10 +202,11 @@ void QV8ProfilerServicePrivate::printProfileTree(const v8::CpuProfileNode *node,
 {
     for (int index = 0 ; index < node->GetChildrenCount() ; index++) {
         const v8::CpuProfileNode* childNode = node->GetChild(index);
-        if (QV8Engine::toStringStatic(childNode->GetScriptResourceName()).length() > 0) {
+        QString scriptResourceName = QJSConverter::toString(childNode->GetScriptResourceName());
+        if (scriptResourceName.length() > 0) {
 
-            QV8ProfilerData rd = {(int)QV8ProfilerService::V8Entry, QV8Engine::toStringStatic(childNode->GetScriptResourceName()),
-                QV8Engine::toStringStatic(childNode->GetFunctionName()),
+            QV8ProfilerData rd = {(int)QV8ProfilerService::V8Entry, scriptResourceName,
+                QJSConverter::toString(childNode->GetFunctionName()),
                 childNode->GetLineNumber(), childNode->GetTotalTime(), childNode->GetSelfTime(), level};
             m_data.append(rd);
 
@@ -234,14 +225,14 @@ void QV8ProfilerServicePrivate::takeSnapshot(v8::HeapSnapshot::Type snapshotType
     v8::HandleScope scope;
     v8::Local<v8::String> title = v8::String::New("");
 
-    QByteArray jsonSnapshot;
-    ByteArrayOutputStream bos(&jsonSnapshot);
+    DebugServiceOutputStream outputStream(*q);
     const v8::HeapSnapshot *snapshot = v8::HeapProfiler::TakeSnapshot(title, snapshotType);
-    snapshot->Serialize(&bos, v8::HeapSnapshot::kJSON);
+    snapshot->Serialize(&outputStream, v8::HeapSnapshot::kJSON);
 
+    //indicate completion
     QByteArray data;
     QDataStream ds(&data, QIODevice::WriteOnly);
-    ds << (int)QV8ProfilerService::V8Snapshot << jsonSnapshot;
+    ds << (int)QV8ProfilerService::V8SnapshotComplete;
 
     q->sendMessage(data);
 }
@@ -250,8 +241,10 @@ void QV8ProfilerServicePrivate::sendMessages()
 {
     Q_Q(QV8ProfilerService);
 
+    QList<QByteArray> messages;
     for (int i = 0; i < m_data.count(); ++i)
-        q->sendMessage(m_data.at(i).toByteArray());
+        messages << m_data.at(i).toByteArray();
+    q->sendMessages(messages);
     m_data.clear();
 
     //indicate completion