diff options
author | Steve Block <steveblock@google.com> | 2010-04-27 16:23:55 +0100 |
---|---|---|
committer | Steve Block <steveblock@google.com> | 2010-04-27 17:07:03 +0100 |
commit | 692e5dbf12901edacf14812a6fae25462920af42 (patch) | |
tree | d62802373a429e0a9dc093b6046c166b2c514285 /WebCore/bridge/qt | |
parent | e24bea4efef1c414137d36a9778aa4e142e10c7d (diff) | |
download | external_webkit-692e5dbf12901edacf14812a6fae25462920af42.zip external_webkit-692e5dbf12901edacf14812a6fae25462920af42.tar.gz external_webkit-692e5dbf12901edacf14812a6fae25462920af42.tar.bz2 |
Merge webkit.org at r55033 : Initial merge by git
Change-Id: I98a4af828067cc243ec3dc5e5826154dd88074b5
Diffstat (limited to 'WebCore/bridge/qt')
-rw-r--r-- | WebCore/bridge/qt/qt_pixmapruntime.cpp | 199 | ||||
-rw-r--r-- | WebCore/bridge/qt/qt_runtime.cpp | 36 |
2 files changed, 137 insertions, 98 deletions
diff --git a/WebCore/bridge/qt/qt_pixmapruntime.cpp b/WebCore/bridge/qt/qt_pixmapruntime.cpp index edae5a9..5978804 100644 --- a/WebCore/bridge/qt/qt_pixmapruntime.cpp +++ b/WebCore/bridge/qt/qt_pixmapruntime.cpp @@ -20,11 +20,7 @@ #include "qt_pixmapruntime.h" #include "CachedImage.h" -#include "DOMWindow.h" #include "HTMLImageElement.h" -#include "HTMLNames.h" -#include "JSDOMBinding.h" -#include "JSDOMWindow.h" #include "JSGlobalObject.h" #include "JSHTMLImageElement.h" #include "JSLock.h" @@ -54,18 +50,19 @@ public: class QtPixmapWidthField : public Field { public: static const char* name() { return "width"; } - virtual JSValue valueFromInstance(ExecState* exec, const Instance* pixmap) const + virtual JSValue valueFromInstance(ExecState* exec, const Instance* instance) const { - return jsNumber(exec, static_cast<const QtPixmapInstance*>(pixmap)->width()); + return jsNumber(exec, static_cast<const QtPixmapInstance*>(instance)->width()); } virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const {} }; + class QtPixmapHeightField : public Field { public: static const char* name() { return "height"; } - virtual JSValue valueFromInstance(ExecState* exec, const Instance* inst) const + virtual JSValue valueFromInstance(ExecState* exec, const Instance* instance) const { - return jsNumber(exec, static_cast<const QtPixmapInstance*>(inst)->height()); + return jsNumber(exec, static_cast<const QtPixmapInstance*>(instance)->height()); } virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const {} }; @@ -76,77 +73,68 @@ public: { return 0; } - virtual JSValue invoke(ExecState* exec, QVariant&, PassRefPtr<RootObject> root, QtPixmapInstance* inst) = 0; + virtual JSValue invoke(ExecState* exec, QtPixmapInstance*, const ArgList&) = 0; }; -class QtPixmapCreateElementMethod : public QtPixmapRuntimeMethod { +// this function receives an HTML image element as a parameter, makes it display the pixmap/image from Qt +class QtPixmapAssignToElementMethod : public QtPixmapRuntimeMethod { public: - static const char* name() { return "toHTMLImageElement"; } - JSValue invoke(ExecState* exec, QVariant& v, PassRefPtr<RootObject> root, QtPixmapInstance*) + static const char* name() { return "assignToHTMLImageElement"; } + JSValue invoke(ExecState* exec, QtPixmapInstance* instance, const ArgList& args) { - QPixmap pxm; - if (v.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>())) { - pxm = QPixmap::fromImage(v.value<QImage>()); - v = QVariant::fromValue<QPixmap>(pxm); - } else - pxm = v.value<QPixmap>(); - - Document* document = 0; - JSDOMGlobalObject* global = static_cast<JSDOMGlobalObject*>(root->globalObject()); - if (global) { - DOMWindow* dWindow = toDOMWindow(global); - if (dWindow) - document = dWindow->document(); - } - - if (document) { - PassRefPtr<StillImage> img = WebCore::StillImage::create(pxm); - RefPtr<HTMLImageElement> image = new HTMLImageElement(HTMLNames::imgTag, document); - image->setCachedImage(new CachedImage(img.get())); - toJS(exec, global, document); - return asObject(toJS(exec, global, image.release())); - } + if (!args.size()) + return jsUndefined(); + + JSObject* objectArg = args.at(0).toObject(exec); + if (!objectArg) + return jsUndefined(); + + if (!objectArg->inherits(&JSHTMLImageElement::s_info)) + return jsUndefined(); + + // we now know that we have a valid <img> element as the argument, we can attach the pixmap to it. + PassRefPtr<StillImage> stillImage = WebCore::StillImage::create(instance->toPixmap()); + HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(static_cast<JSHTMLImageElement*>(objectArg)->impl()); + imageElement->setCachedImage(new CachedImage(stillImage.get())); + JSDOMGlobalObject* global = static_cast<JSDOMGlobalObject*>(instance->rootObject()->globalObject()); + toJS(exec, global, imageElement->document()); return jsUndefined(); } + virtual int numParameters() const + { + return 1; + } }; +// this function encodes the image to a dataUrl, to be used in background etc. Note: very slow. class QtPixmapToDataUrlMethod : public QtPixmapRuntimeMethod { public: static const char* name() { return "toDataUrl"; } - JSValue invoke(ExecState* exec, QVariant& v, PassRefPtr<RootObject> root, QtPixmapInstance*) + JSValue invoke(ExecState* exec, QtPixmapInstance* instance, const ArgList&) { - QImage image; - // for getting the data url, we always prefer the image. - if (v.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>())) { - image = v.value<QPixmap>().toImage(); - v = QVariant::fromValue<QImage>(image); - } else - image = v.value<QImage>(); - QByteArray ba; - QBuffer b(&ba); - image.save(&b, "PNG"); - const QString b64 = QString("data:image/png;base64,") + ba.toBase64(); - const UString ustring((UChar*)b64.utf16(), b64.length()); + QByteArray byteArray; + QBuffer buffer(&byteArray); + instance->toImage().save(&buffer, "PNG"); + const QString encodedString = QString("data:image/png;base64,") + byteArray.toBase64(); + const UString ustring((UChar*)encodedString.utf16(), encodedString.length()); return jsString(exec, ustring); } - }; class QtPixmapToStringMethod : public QtPixmapRuntimeMethod { public: static const char* name() { return "toString"; } - JSValue invoke(ExecState* exec, QVariant& v, PassRefPtr<RootObject> root, QtPixmapInstance* inst) + JSValue invoke(ExecState* exec, QtPixmapInstance* instance, const ArgList&) { - return inst->valueOf(exec); + return instance->valueOf(exec); } - }; struct QtPixmapMetaData { QtPixmapToDataUrlMethod toDataUrlMethod; - QtPixmapCreateElementMethod createElementMethod; + QtPixmapAssignToElementMethod assignToElementMethod; QtPixmapToStringMethod toStringMethod; QtPixmapHeightField heightField; QtPixmapWidthField widthField; @@ -179,7 +167,6 @@ QtPixmapRuntimeObjectImp::QtPixmapRuntimeObjectImp(ExecState* exec, PassRefPtr<I const ClassInfo QtPixmapRuntimeObjectImp::s_info = { "QtPixmapRuntimeObject", &RuntimeObjectImp::s_info, 0, 0 }; - QtPixmapClass::QtPixmapClass() { } @@ -193,8 +180,8 @@ Class* QtPixmapInstance::getClass() const JSValue QtPixmapInstance::invokeMethod(ExecState* exec, const MethodList& methods, const ArgList& args) { if (methods.size() == 1) { - QtPixmapRuntimeMethod* mtd = static_cast<QtPixmapRuntimeMethod*>(methods[0]); - return mtd->invoke(exec, data, rootObject(), this); + QtPixmapRuntimeMethod* method = static_cast<QtPixmapRuntimeMethod*>(methods[0]); + return method->invoke(exec, this, args); } return jsUndefined(); } @@ -204,8 +191,8 @@ MethodList QtPixmapClass::methodsNamed(const Identifier& identifier, Instance*) MethodList methods; if (identifier == QtPixmapToDataUrlMethod::name()) methods.append(&qt_pixmap_metaData.toDataUrlMethod); - else if (identifier == QtPixmapCreateElementMethod::name()) - methods.append(&qt_pixmap_metaData.createElementMethod); + else if (identifier == QtPixmapAssignToElementMethod::name()) + methods.append(&qt_pixmap_metaData.assignToElementMethod); else if (identifier == QtPixmapToStringMethod::name()) methods.append(&qt_pixmap_metaData.toStringMethod); return methods; @@ -223,7 +210,7 @@ Field* QtPixmapClass::fieldNamed(const Identifier& identifier, Instance*) const void QtPixmapInstance::getPropertyNames(ExecState*exec, PropertyNameArray& arr) { arr.add(Identifier(exec, UString(QtPixmapToDataUrlMethod::name()))); - arr.add(Identifier(exec, UString(QtPixmapCreateElementMethod::name()))); + arr.add(Identifier(exec, UString(QtPixmapAssignToElementMethod::name()))); arr.add(Identifier(exec, UString(QtPixmapToStringMethod::name()))); arr.add(Identifier(exec, UString(QtPixmapWidthField::name()))); arr.add(Identifier(exec, UString(QtPixmapHeightField::name()))); @@ -236,15 +223,17 @@ JSValue QtPixmapInstance::defaultValue(ExecState* exec, PreferredPrimitiveType p (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>()) && !(data.value<QImage>()).isNull()) || (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>()) && !data.value<QPixmap>().isNull())); } + if (ptype == PreferString) return valueOf(exec); + return jsUndefined(); } JSValue QtPixmapInstance::valueOf(ExecState* exec) const { - const QString toStr = QString("[Qt Native Pixmap %1,%2]").arg(width()).arg(height()); - UString ustring((UChar*)toStr.utf16(), toStr.length()); + const QString stringValue = QString("[Qt Native Pixmap %1,%2]").arg(width()).arg(height()); + UString ustring((UChar*)stringValue.utf16(), stringValue.length()); return jsString(exec, ustring); } @@ -275,62 +264,78 @@ QPixmap QtPixmapInstance::toPixmap() { if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>())) return data.value<QPixmap>(); + if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>())) { - const QPixmap pxm = QPixmap::fromImage(data.value<QImage>()); - data = QVariant::fromValue<QPixmap>(pxm); - return pxm; + const QPixmap pixmap = QPixmap::fromImage(data.value<QImage>()); + data = QVariant::fromValue<QPixmap>(pixmap); + return pixmap; } - return QPixmap(); + return QPixmap(); } QImage QtPixmapInstance::toImage() { if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QImage>())) return data.value<QImage>(); + if (data.type() == static_cast<QVariant::Type>(qMetaTypeId<QPixmap>())) { - const QImage img = data.value<QPixmap>().toImage(); - data = QVariant::fromValue<QImage>(img); - return img; + const QImage image = data.value<QPixmap>().toImage(); + data = QVariant::fromValue<QImage>(image); + return image; } + return QImage(); } QVariant QtPixmapInstance::variantFromObject(JSObject* object, QMetaType::Type hint) { - if (!object) { + if (!object) + goto returnEmptyVariant; + + if (object->inherits(&JSHTMLImageElement::s_info)) { + JSHTMLImageElement* elementJSWrapper = static_cast<JSHTMLImageElement*>(object); + HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(elementJSWrapper->impl()); + + if (!imageElement) + goto returnEmptyVariant; + + CachedImage* cachedImage = imageElement->cachedImage(); + if (!cachedImage) + goto returnEmptyVariant; + + Image* image = cachedImage->image(); + if (!image) + goto returnEmptyVariant; + + QPixmap* pixmap = image->nativeImageForCurrentFrame(); + if (!pixmap) + goto returnEmptyVariant; + + return (hint == static_cast<QMetaType::Type>(qMetaTypeId<QPixmap>())) + ? QVariant::fromValue<QPixmap>(*pixmap) + : QVariant::fromValue<QImage>(pixmap->toImage()); + } + + if (object->inherits(&QtPixmapRuntimeObjectImp::s_info)) { + QtPixmapRuntimeObjectImp* imp = static_cast<QtPixmapRuntimeObjectImp*>(object); + QtPixmapInstance* instance = static_cast<QtPixmapInstance*>(imp->getInternalInstance()); + if (!instance) + goto returnEmptyVariant; + if (hint == qMetaTypeId<QPixmap>()) - return QVariant::fromValue<QPixmap>(QPixmap()); + return QVariant::fromValue<QPixmap>(instance->toPixmap()); + if (hint == qMetaTypeId<QImage>()) - return QVariant::fromValue<QImage>(QImage()); - } else if (object->inherits(&JSHTMLImageElement::s_info)) { - JSHTMLImageElement* el = static_cast<JSHTMLImageElement*>(object); - HTMLImageElement* imageElement = static_cast<HTMLImageElement*>(el->impl()); - if (imageElement) { - CachedImage* cImg = imageElement->cachedImage(); - if (cImg) { - Image* img = cImg->image(); - if (img) { - QPixmap* pxm = img->nativeImageForCurrentFrame(); - if (pxm) { - return (hint == static_cast<QMetaType::Type>(qMetaTypeId<QPixmap>())) - ? QVariant::fromValue<QPixmap>(*pxm) - : QVariant::fromValue<QImage>(pxm->toImage()); - } - } - } - } - } else if (object->inherits(&QtPixmapRuntimeObjectImp::s_info)) { - QtPixmapRuntimeObjectImp* imp = static_cast<QtPixmapRuntimeObjectImp*>(object); - QtPixmapInstance* inst = static_cast<QtPixmapInstance*>(imp->getInternalInstance()); - if (inst) { - if (hint == qMetaTypeId<QPixmap >()) - return QVariant::fromValue<QPixmap>(inst->toPixmap()); - if (hint == qMetaTypeId<QImage>()) - return QVariant::fromValue<QImage>(inst->toImage()); - } + return QVariant::fromValue<QImage>(instance->toImage()); } - return 0; + +returnEmptyVariant: + if (hint == qMetaTypeId<QPixmap>()) + return QVariant::fromValue<QPixmap>(QPixmap()); + if (hint == qMetaTypeId<QImage>()) + return QVariant::fromValue<QImage>(QImage()); + return QVariant(); } JSObject* QtPixmapInstance::createRuntimeObject(ExecState* exec, PassRefPtr<RootObject> root, const QVariant& data) { diff --git a/WebCore/bridge/qt/qt_runtime.cpp b/WebCore/bridge/qt/qt_runtime.cpp index 9601a4a..ada9f01 100644 --- a/WebCore/bridge/qt/qt_runtime.cpp +++ b/WebCore/bridge/qt/qt_runtime.cpp @@ -29,7 +29,10 @@ #include "JSArray.h" #include "JSByteArray.h" #include "JSDOMBinding.h" +#include "JSDOMWindow.h" +#include <JSFunction.h> #include "JSGlobalObject.h" +#include "JSHTMLElement.h" #include "JSLock.h" #include "JSObject.h" #include "ObjectPrototype.h" @@ -45,7 +48,7 @@ #include "qt_instance.h" #include "qt_pixmapruntime.h" #include "qvarlengtharray.h" -#include <JSFunction.h> +#include "qwebelement.h" #include <limits.h> #include <runtime/Error.h> #include <runtime_array.h> @@ -114,6 +117,21 @@ QDebug operator<<(QDebug dbg, const JSRealType &c) } #endif +// this is here as a proxy, so we'd have a class to friend in QWebElement, +// as getting/setting a WebCore in QWebElement is private +class QtWebElementRuntime { +public: + static QWebElement create(Element* element) + { + return QWebElement(element); + } + + static Element* get(const QWebElement& element) + { + return element.m_element; + } +}; + static JSRealType valueRealType(ExecState* exec, JSValue val) { if (val.isNumber()) @@ -722,6 +740,11 @@ QVariant convertValueToQVariant(ExecState* exec, JSValue value, QMetaType::Type break; } else if (QtPixmapInstance::canHandle(static_cast<QMetaType::Type>(hint))) { ret = QtPixmapInstance::variantFromObject(object, static_cast<QMetaType::Type>(hint)); + } else if (hint == (QMetaType::Type) qMetaTypeId<QWebElement>()) { + if (object && object->inherits(&JSHTMLElement::s_info)) + ret = QVariant::fromValue<QWebElement>(QtWebElementRuntime::create((static_cast<JSHTMLElement*>(object))->impl())); + else + ret = QVariant::fromValue<QWebElement>(QWebElement()); } else if (hint == (QMetaType::Type) qMetaTypeId<QVariant>()) { if (value.isUndefinedOrNull()) { if (distance) @@ -854,6 +877,17 @@ JSValue convertQVariantToValue(ExecState* exec, PassRefPtr<RootObject> root, con if (QtPixmapInstance::canHandle(static_cast<QMetaType::Type>(variant.type()))) return QtPixmapInstance::createRuntimeObject(exec, root, variant); + if (type == qMetaTypeId<QWebElement>()) { + if (!root->globalObject()->inherits(&JSDOMWindow::s_info)) + return jsUndefined(); + + Document* document = (static_cast<JSDOMWindow*>(root->globalObject()))->impl()->document(); + if (!document) + return jsUndefined(); + + return toJS(exec, toJSDOMGlobalObject(document, exec), QtWebElementRuntime::get(variant.value<QWebElement>())); + } + if (type == QMetaType::QVariantMap) { // create a new object, and stuff properties into it JSObject* ret = constructEmptyObject(exec); |