diff options
Diffstat (limited to 'src/qml/qml/qqmlxmlhttprequest.cpp')
-rw-r--r-- | src/qml/qml/qqmlxmlhttprequest.cpp | 138 |
1 files changed, 78 insertions, 60 deletions
diff --git a/src/qml/qml/qqmlxmlhttprequest.cpp b/src/qml/qml/qqmlxmlhttprequest.cpp index 24f49eb6e7..10b1cbcfd4 100644 --- a/src/qml/qml/qqmlxmlhttprequest.cpp +++ b/src/qml/qml/qqmlxmlhttprequest.cpp @@ -70,7 +70,7 @@ using namespace QV4; -#ifndef QT_NO_XMLSTREAMREADER +#if !defined(QT_NO_XMLSTREAMREADER) && QT_CONFIG(qml_network) #define V4THROW_REFERENCE(string) { \ ScopedObject error(scope, ctx->engine()->newReferenceErrorObject(QStringLiteral(string))); \ @@ -158,7 +158,7 @@ class DocumentImpl : public QQmlRefCount, public NodeImpl public: DocumentImpl() : root(0) { type = Document; } virtual ~DocumentImpl() { - if (root) delete root; + delete root; } QString version; @@ -174,33 +174,43 @@ public: namespace Heap { struct NamedNodeMap : Object { - NamedNodeMap(NodeImpl *data, const QList<NodeImpl *> &list); - ~NamedNodeMap() { + void init(NodeImpl *data, const QList<NodeImpl *> &list); + void destroy() { + delete listPtr; if (d) d->release(); + Object::destroy(); } - QList<NodeImpl *> list; // Only used in NamedNodeMap + QList<NodeImpl *> &list() { + if (listPtr == nullptr) + listPtr = new QList<NodeImpl *>; + return *listPtr; + } + + QList<NodeImpl *> *listPtr; // Only used in NamedNodeMap NodeImpl *d; }; struct NodeList : Object { - NodeList(NodeImpl *data); - ~NodeList() { + void init(NodeImpl *data); + void destroy() { if (d) d->release(); + Object::destroy(); } NodeImpl *d; }; struct NodePrototype : Object { - NodePrototype(); + void init(); }; struct Node : Object { - Node(NodeImpl *data); - ~Node() { + void init(NodeImpl *data); + void destroy() { if (d) d->release(); + Object::destroy(); } NodeImpl *d; }; @@ -221,10 +231,11 @@ public: static ReturnedValue getIndexed(const Managed *m, uint index, bool *hasProperty); }; -Heap::NamedNodeMap::NamedNodeMap(NodeImpl *data, const QList<NodeImpl *> &list) - : list(list) - , d(data) +void Heap::NamedNodeMap::init(NodeImpl *data, const QList<NodeImpl *> &list) { + Object::init(); + d = data; + this->list() = list; if (d) d->addref(); } @@ -246,9 +257,10 @@ public: }; -Heap::NodeList::NodeList(NodeImpl *data) - : d(data) +void Heap::NodeList::init(NodeImpl *data) { + Object::init(); + d = data; if (d) d->addref(); } @@ -287,8 +299,9 @@ public: }; -Heap::NodePrototype::NodePrototype() +void Heap::NodePrototype::init() { + Object::init(); Scope scope(internalClass->engine); ScopedObject o(scope, this); @@ -320,9 +333,10 @@ struct Node : public Object bool isNull() const; }; -Heap::Node::Node(NodeImpl *data) - : d(data) +void Heap::Node::init(NodeImpl *data) { + Object::init(); + d = data; if (d) d->addref(); } @@ -502,7 +516,7 @@ ReturnedValue NodePrototype::method_get_firstChild(CallContext *ctx) if (r->d()->d->children.isEmpty()) return Encode::null(); else - return Node::create(scope.engine, r->d()->d->children.first()); + return Node::create(scope.engine, r->d()->d->children.constFirst()); } ReturnedValue NodePrototype::method_get_lastChild(CallContext *ctx) @@ -515,7 +529,7 @@ ReturnedValue NodePrototype::method_get_lastChild(CallContext *ctx) if (r->d()->d->children.isEmpty()) return Encode::null(); else - return Node::create(scope.engine, r->d()->d->children.last()); + return Node::create(scope.engine, r->d()->d->children.constLast()); } ReturnedValue NodePrototype::method_get_previousSibling(CallContext *ctx) @@ -715,7 +729,7 @@ ReturnedValue Text::method_isElementContentWhitespace(CallContext *ctx) Scoped<Node> r(scope, ctx->thisObject().as<Node>()); if (!r) return Encode::undefined(); - return Encode(r->d()->d->data.trimmed().isEmpty()); + return Encode(QStringRef(&r->d()->d->data).trimmed().isEmpty()); } ReturnedValue Text::method_wholeText(CallContext *ctx) @@ -877,10 +891,10 @@ ReturnedValue NamedNodeMap::getIndexed(const Managed *m, uint index, bool *hasPr const NamedNodeMap *r = static_cast<const NamedNodeMap *>(m); QV4::ExecutionEngine *v4 = r->engine(); - if ((int)index < r->d()->list.count()) { + if ((int)index < r->d()->list().count()) { if (hasProperty) *hasProperty = true; - return Node::create(v4, r->d()->list.at(index)); + return Node::create(v4, r->d()->list().at(index)); } if (hasProperty) *hasProperty = false; @@ -895,14 +909,14 @@ ReturnedValue NamedNodeMap::get(const Managed *m, String *name, bool *hasPropert name->makeIdentifier(v4); if (name->equals(v4->id_length())) - return Primitive::fromInt32(r->d()->list.count()).asReturnedValue(); + return Primitive::fromInt32(r->d()->list().count()).asReturnedValue(); QString str = name->toQString(); - for (int ii = 0; ii < r->d()->list.count(); ++ii) { - if (r->d()->list.at(ii)->name == str) { + for (int ii = 0; ii < r->d()->list().count(); ++ii) { + if (r->d()->list().at(ii)->name == str) { if (hasProperty) *hasProperty = true; - return Node::create(v4, r->d()->list.at(ii)); + return Node::create(v4, r->d()->list().at(ii)); } } @@ -1016,8 +1030,8 @@ public: ReturnedValue abort(Object *thisObject, QQmlContextData *context); void addHeader(const QString &, const QString &); - QString header(const QString &name); - QString headers(); + QString header(const QString &name) const; + QString headers() const; QString responseBody(); const QByteArray & rawResponseBody() const; @@ -1144,26 +1158,27 @@ void QQmlXMLHttpRequest::addHeader(const QString &name, const QString &value) } } -QString QQmlXMLHttpRequest::header(const QString &name) +QString QQmlXMLHttpRequest::header(const QString &name) const { - QByteArray utfname = name.toLower().toUtf8(); - - foreach (const HeaderPair &header, m_headersList) { - if (header.first == utfname) - return QString::fromUtf8(header.second); + if (!m_headersList.isEmpty()) { + const QByteArray utfname = name.toLower().toUtf8(); + for (const HeaderPair &header : m_headersList) { + if (header.first == utfname) + return QString::fromUtf8(header.second); + } } return QString(); } -QString QQmlXMLHttpRequest::headers() +QString QQmlXMLHttpRequest::headers() const { QString ret; - foreach (const HeaderPair &header, m_headersList) { + for (const HeaderPair &header : m_headersList) { if (ret.length()) ret.append(QLatin1String("\r\n")); - ret = ret % QString::fromUtf8(header.first) % QLatin1String(": ") - % QString::fromUtf8(header.second); + ret += QString::fromUtf8(header.first) + QLatin1String(": ") + + QString::fromUtf8(header.second); } return ret; } @@ -1235,7 +1250,8 @@ void QQmlXMLHttpRequest::requestFromUrl(const QUrl &url) } else if (m_method == QLatin1String("DELETE")) { m_network = networkAccessManager()->deleteResource(request); } else if ((m_method == QLatin1String("OPTIONS")) || - m_method == QLatin1String("PROPFIND")) { + m_method == QLatin1String("PROPFIND") || + m_method == QLatin1String("PATCH")) { QBuffer *buffer = new QBuffer; buffer->setData(m_data); buffer->open(QIODevice::ReadOnly); @@ -1559,7 +1575,7 @@ void QQmlXMLHttpRequest::dispatchCallback(Object *thisObj, QQmlContextData *cont QV4::ScopedCallData callData(scope); callData->thisObject = Encode::undefined(); - callback->call(callData); + callback->call(scope, callData); if (scope.engine->hasException) { QQmlError error = scope.engine->catchExceptionAsQmlError(); @@ -1585,15 +1601,20 @@ namespace QV4 { namespace Heap { struct QQmlXMLHttpRequestWrapper : Object { - QQmlXMLHttpRequestWrapper(QQmlXMLHttpRequest *request); - ~QQmlXMLHttpRequestWrapper() { + void init(QQmlXMLHttpRequest *request) { + Object::init(); + this->request = request; + } + + void destroy() { delete request; + Object::destroy(); } QQmlXMLHttpRequest *request; }; struct QQmlXMLHttpRequestCtor : FunctionObject { - QQmlXMLHttpRequestCtor(ExecutionEngine *engine); + void init(ExecutionEngine *engine); Pointer<Object> proto; }; @@ -1606,11 +1627,6 @@ struct QQmlXMLHttpRequestWrapper : public Object V4_NEEDS_DESTROY }; -Heap::QQmlXMLHttpRequestWrapper::QQmlXMLHttpRequestWrapper(QQmlXMLHttpRequest *request) - : request(request) -{ -} - struct QQmlXMLHttpRequestCtor : public FunctionObject { V4_OBJECT2(QQmlXMLHttpRequestCtor, FunctionObject) @@ -1620,22 +1636,23 @@ struct QQmlXMLHttpRequestCtor : public FunctionObject c->proto->mark(e); FunctionObject::markObjects(that, e); } - static ReturnedValue construct(const Managed *that, QV4::CallData *) + static void construct(const Managed *that, Scope &scope, QV4::CallData *) { - Scope scope(static_cast<const QQmlXMLHttpRequestCtor *>(that)->engine()); Scoped<QQmlXMLHttpRequestCtor> ctor(scope, that->as<QQmlXMLHttpRequestCtor>()); - if (!ctor) - return scope.engine->throwTypeError(); + if (!ctor) { + scope.result = scope.engine->throwTypeError(); + return; + } QQmlXMLHttpRequest *r = new QQmlXMLHttpRequest(scope.engine->v8Engine->networkAccessManager()); Scoped<QQmlXMLHttpRequestWrapper> w(scope, scope.engine->memoryManager->allocObject<QQmlXMLHttpRequestWrapper>(r)); ScopedObject proto(scope, ctor->d()->proto); w->setPrototype(proto); - return w.asReturnedValue(); + scope.result = w.asReturnedValue(); } - static ReturnedValue call(const Managed *, QV4::CallData *) { - return Primitive::undefinedValue().asReturnedValue(); + static void call(const Managed *, Scope &scope, QV4::CallData *) { + scope.result = Primitive::undefinedValue(); } void setupProto(); @@ -1661,9 +1678,9 @@ struct QQmlXMLHttpRequestCtor : public FunctionObject DEFINE_OBJECT_VTABLE(QQmlXMLHttpRequestWrapper); -Heap::QQmlXMLHttpRequestCtor::QQmlXMLHttpRequestCtor(ExecutionEngine *engine) - : Heap::FunctionObject(engine->rootContext(), QStringLiteral("XMLHttpRequest")) +void Heap::QQmlXMLHttpRequestCtor::init(ExecutionEngine *engine) { + Heap::FunctionObject::init(engine->rootContext(), QStringLiteral("XMLHttpRequest")); Scope scope(engine); Scoped<QV4::QQmlXMLHttpRequestCtor> ctor(scope, this); @@ -1735,7 +1752,8 @@ ReturnedValue QQmlXMLHttpRequestCtor::method_open(CallContext *ctx) method != QLatin1String("POST") && method != QLatin1String("DELETE") && method != QLatin1String("OPTIONS") && - method != QLatin1String("PROPFIND")) + method != QLatin1String("PROPFIND") && + method != QLatin1String("PATCH")) V4THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Unsupported HTTP method type"); // Argument 1 - URL @@ -2038,6 +2056,6 @@ void *qt_add_qmlxmlhttprequest(ExecutionEngine *v4) QT_END_NAMESPACE -#endif // QT_NO_XMLSTREAMREADER +#endif // QT_NO_XMLSTREAMREADER && qml_network #include <qqmlxmlhttprequest.moc> |