📄 qwebframe.cpp
字号:
/* Copyright (C) 2008,2009 Nokia Corporation and/or its subsidiary(-ies) Copyright (C) 2007 Staikos Computing Services Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/#include "config.h"#include "qwebframe.h"#include "qwebpage.h"#include "qwebpage_p.h"#include "qwebframe_p.h"#include "qwebsecurityorigin.h"#include "qwebsecurityorigin_p.h"#include "DocumentLoader.h"#include "FocusController.h"#include "FrameLoaderClientQt.h"#include "Frame.h"#include "FrameTree.h"#include "FrameView.h"#include "IconDatabase.h"#include "InspectorController.h"#include "Page.h"#include "PutPropertySlot.h"#include "ResourceRequest.h"#include "RenderView.h"#include "SelectionController.h"#include "Scrollbar.h"#include "PrintContext.h"#include "SubstituteData.h"#include "markup.h"#include "htmlediting.h"#include "RenderTreeAsText.h"#include "Element.h"#include "Document.h"#include "DragData.h"#include "RenderView.h"#include "GraphicsContext.h"#include "PlatformMouseEvent.h"#include "PlatformWheelEvent.h"#include "GraphicsContext.h"#include "HitTestResult.h"#include "CallFrame.h"#include "JSDOMBinding.h"#include "JSDOMWindow.h"#include "JSLock.h"#include "JSObject.h"#include "qt_instance.h"#include "qt_runtime.h"#include "runtime.h"#include "runtime_object.h"#include "runtime_root.h"#include "ScriptController.h"#include "ScriptSourceCode.h"#include "ScriptValue.h"#include "wtf/HashMap.h"#include <qdebug.h>#include <qevent.h>#include <qfileinfo.h>#include <qpainter.h>#include <QMultiMap>#if QT_VERSION >= 0x040400#include <qnetworkrequest.h>#else#include "qwebnetworkinterface.h"#endif#include <qregion.h>#include <qprinter.h>#include "HTMLMetaElement.h"#include "NodeList.h"using namespace WebCore;// from text/qfont.cppQT_BEGIN_NAMESPACEextern Q_GUI_EXPORT int qt_defaultDpi();QT_END_NAMESPACEvoid QWEBKIT_EXPORT qt_drt_setJavaScriptProfilingEnabled(QWebFrame* qframe, bool enabled){ Frame* frame = QWebFramePrivate::core(qframe); InspectorController* controller = frame->page()->inspectorController(); if (!controller) return; if (enabled) controller->enableProfiler(); else controller->disableProfiler();}// Pause a given CSS animation or transition on the target node at a specific time.// If the animation or transition is already paused, it will update its pause time.// This method is only intended to be used for testing the CSS animation and transition system.bool QWEBKIT_EXPORT qt_drt_pauseAnimation(QWebFrame *qframe, const QString &animationName, double time, const QString &elementId){ Frame* frame = QWebFramePrivate::core(qframe); if (!frame) return false; AnimationController* controller = frame->animation(); if (!controller) return false; Document* doc = frame->document(); Q_ASSERT(doc); Node* coreNode = doc->getElementById(elementId); if (!coreNode || !coreNode->renderer()) return false; return controller->pauseAnimationAtTime(coreNode->renderer(), animationName, time);}bool QWEBKIT_EXPORT qt_drt_pauseTransitionOfProperty(QWebFrame *qframe, const QString &propertyName, double time, const QString &elementId){ Frame* frame = QWebFramePrivate::core(qframe); if (!frame) return false; AnimationController* controller = frame->animation(); if (!controller) return false; Document* doc = frame->document(); Q_ASSERT(doc); Node* coreNode = doc->getElementById(elementId); if (!coreNode || !coreNode->renderer()) return false; return controller->pauseTransitionAtTime(coreNode->renderer(), propertyName, time);}// Returns the total number of currently running animations (includes both CSS transitions and CSS animations).int QWEBKIT_EXPORT qt_drt_numberOfActiveAnimations(QWebFrame *qframe){ Frame* frame = QWebFramePrivate::core(qframe); if (!frame) return false; AnimationController* controller = frame->animation(); if (!controller) return false; return controller->numberOfActiveAnimations();}void QWebFramePrivate::init(QWebFrame *qframe, WebCore::Page *webcorePage, QWebFrameData *frameData){ q = qframe; allowsScrolling = frameData->allowsScrolling; marginWidth = frameData->marginWidth; marginHeight = frameData->marginHeight; frameLoaderClient = new FrameLoaderClientQt(); RefPtr<Frame> newFrame = Frame::create(webcorePage, frameData->ownerElement, frameLoaderClient); frame = newFrame.get(); frameLoaderClient->setFrame(qframe, frame); // FIXME: All of the below should probably be moved over into WebCore frame->tree()->setName(frameData->name); if (QWebFrame* _parentFrame = parentFrame()) QWebFramePrivate::core(_parentFrame)->tree()->appendChild(frame); // balanced by adoptRef in FrameLoaderClientQt::createFrame if (frameData->ownerElement) frame->ref(); frame->init();}WebCore::Scrollbar* QWebFramePrivate::horizontalScrollBar() const{ if (!frame->view()) return 0; return frame->view()->horizontalScrollbar();}WebCore::Scrollbar* QWebFramePrivate::verticalScrollBar() const{ if (!frame->view()) return 0; return frame->view()->verticalScrollbar();}void QWebFramePrivate::renderPrivate(QPainter *painter, const QRegion &clip, bool contents){ if (!frame->view() || !frame->contentRenderer()) return; QVector<QRect> vector = clip.rects(); if (vector.isEmpty()) return; WebCore::FrameView* view = frame->view(); view->layoutIfNeededRecursive(); GraphicsContext context(painter); if (!contents) view->paint(&context, vector.first()); else view->paintContents(&context, vector.first()); for (int i = 1; i < vector.size(); ++i) { const QRect& clipRect = vector.at(i); painter->save(); painter->setClipRect(clipRect, Qt::IntersectClip); if (!contents) view->paint(&context, clipRect); else view->paintContents(&context, clipRect); painter->restore(); }}/*! \class QWebFrame \since 4.4 \brief The QWebFrame class represents a frame in a web page. QWebFrame represents a frame inside a web page. Each QWebPage object contains at least one frame, the main frame, obtained using QWebPage::mainFrame(). Additional frames will be created for HTML \c{<frame>} or \c{<iframe>} elements. A frame can be loaded using load() or setUrl(). Alternatively, if you have the HTML content readily available, you can use setHtml() instead. The page() function returns a pointer to the web page object. See \l{Elements of QWebView} for an explanation of how web frames are related to a web page and web view. The title of an HTML frame can be accessed with the title() property. Additionally, a frame may also specify an icon, which can be accessed using the icon() property. If the title or the icon changes, the corresponding titleChanged() and iconChanged() signals will be emitted. The zoomFactor() property can be used to change the overall size of the content displayed in the frame. QWebFrame objects are created and controlled by the web page. You can connect to the web page's \l{QWebPage::}{frameCreated()} signal to be notified when a new frame is created. The hitTestContent() function can be used to programmatically examine the contents of a frame. A QWebFrame can be printed onto a QPrinter using the print() function. This function is marked as a slot and can be conveniently connected to \l{QPrintPreviewDialog}'s \l{QPrintPreviewDialog::}{paintRequested()} signal. \sa QWebPage*/QWebFrame::QWebFrame(QWebPage *parent, QWebFrameData *frameData) : QObject(parent) , d(new QWebFramePrivate){ d->page = parent; d->init(this, parent->d->page, frameData); if (!frameData->url.isEmpty()) { WebCore::ResourceRequest request(frameData->url, frameData->referrer); d->frame->loader()->load(request, frameData->name, false); }}QWebFrame::QWebFrame(QWebFrame *parent, QWebFrameData *frameData) : QObject(parent) , d(new QWebFramePrivate){ d->page = parent->d->page; d->init(this, parent->d->page->d->page, frameData);}QWebFrame::~QWebFrame(){ if (d->frame && d->frame->loader() && d->frame->loader()->client()) static_cast<FrameLoaderClientQt*>(d->frame->loader()->client())->m_webFrame = 0; delete d;}/*! Make \a object available under \a name from within the frame's JavaScript context. The \a object will be inserted as a child of the frame's window object. Qt properties will be exposed as JavaScript properties and slots as JavaScript methods. If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal. The \a object will never be explicitly deleted by QtWebKit.*/void QWebFrame::addToJavaScriptWindowObject(const QString &name, QObject *object){ addToJavaScriptWindowObject(name, object, QScriptEngine::QtOwnership);}/*! \fn void QWebFrame::addToJavaScriptWindowObject(const QString &name, QObject *object, QScriptEngine::ValueOwnership own) \overload Make \a object available under \a name from within the frame's JavaScript context. The \a object will be inserted as a child of the frame's window object. Qt properties will be exposed as JavaScript properties and slots as JavaScript methods. If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal. The ownership of \a object is specified using \a own.*/void QWebFrame::addToJavaScriptWindowObject(const QString &name, QObject *object, QScriptEngine::ValueOwnership ownership){ JSC::JSLock lock(false); JSDOMWindow* window = toJSDOMWindow(d->frame); JSC::Bindings::RootObject* root = d->frame->script()->bindingRootObject(); if (!window) { qDebug() << "Warning: couldn't get window object"; return; } JSC::ExecState* exec = window->globalExec(); JSC::JSObject* runtimeObject = JSC::Bindings::QtInstance::getQtInstance(object, root, ownership)->createRuntimeObject(exec); JSC::PutPropertySlot slot; window->put(exec, JSC::Identifier(exec, (const UChar *) name.constData(), name.length()), runtimeObject, slot);}/*! Returns the frame's content, converted to HTML. \sa setHtml(), toPlainText()*/QString QWebFrame::toHtml() const{ if (!d->frame->document()) return QString(); return createMarkup(d->frame->document());}/*! Returns the content of this frame converted to plain text. \sa toHtml()*/QString QWebFrame::toPlainText() const{ if (d->frame->view() && d->frame->view()->layoutPending()) d->frame->view()->layout(); Element *documentElement = d->frame->document()->documentElement(); return documentElement->innerText();}/*! Returns a dump of the rendering tree. This is mainly useful for debugging html.*/QString QWebFrame::renderTreeDump() const{ if (d->frame->view() && d->frame->view()->layoutPending()) d->frame->view()->layout(); return externalRepresentation(d->frame->contentRenderer());}/*! \property QWebFrame::title \brief the title of the frame as defined by the HTML <title> element \sa titleChanged()*/QString QWebFrame::title() const{ if (d->frame->document()) return d->frame->loader()->documentLoader()->title(); else return QString();}/*! \since 4.5 \brief Returns the meta data in this frame as a QMultiMap The meta data consists of the name and content attributes of the of the \c{<meta>} tags in the HTML document. For example: \code <html> <head> <meta name="description" content="This document is a tutorial about Qt development"> <meta name="keywords" content="Qt, WebKit, Programming"> </head> ... </html> \endcode Given the above HTML code the metaData() function will return a map with two entries: \table \header \o Key \o Value \row \o "description" \o "This document is a tutorial about Qt development" \row \o "keywords" \o "Qt, WebKit, Programming" \endtable This function returns a multi map to support multiple meta tags with the same attribute name.*/QMultiMap<QString, QString> QWebFrame::metaData() const{ if(!d->frame->document()) return QMap<QString,QString>(); QMultiMap<QString,QString> map; Document* doc = d->frame->document(); RefPtr<NodeList> list = doc->getElementsByTagName("meta"); unsigned len = list->length(); for (unsigned i = 0; i < len; i++) { HTMLMetaElement* meta = static_cast<HTMLMetaElement*>(list->item(i)); map.insert(meta->name(), meta->content()); } return map;}static inline QUrl ensureAbsoluteUrl(const QUrl &url){ if (!url.isRelative()) return url; return QUrl::fromLocalFile(QFileInfo(url.toLocalFile()).absoluteFilePath());}/*! \property QWebFrame::url \brief the url of the frame currently viewed \sa urlChanged()*/void QWebFrame::setUrl(const QUrl &url){ d->frame->loader()->begin(ensureAbsoluteUrl(url)); d->frame->loader()->end(); load(ensureAbsoluteUrl(url));}QUrl QWebFrame::url() const{ return d->frame->loader()->url();}/*! \property QWebFrame::icon \brief the icon associated with this frame \sa iconChanged(), QWebSettings::iconForUrl()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -