WebPluginContainerImpl.cpp [plain text]
#include "config.h"
#include "WebPluginContainerImpl.h"
#include "Chrome.h"
#include "ChromeClientImpl.h"
#include "WebCursorInfo.h"
#include "WebDataSourceImpl.h"
#include "WebInputEvent.h"
#include "WebInputEventConversion.h"
#include "WebKit.h"
#include "WebPlugin.h"
#include "WebRect.h"
#include "WebURLError.h"
#include "WebURLRequest.h"
#include "WebVector.h"
#include "WrappedResourceResponse.h"
#include "EventNames.h"
#include "FocusController.h"
#include "FormState.h"
#include "Frame.h"
#include "FrameLoadRequest.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HostWindow.h"
#include "HTMLFormElement.h"
#include "HTMLNames.h"
#include "HTMLPlugInElement.h"
#include "KeyboardEvent.h"
#include "MouseEvent.h"
#include "Page.h"
#include "ScrollView.h"
#include "WheelEvent.h"
#if WEBKIT_USING_SKIA
#include "PlatformContextSkia.h"
#endif
using namespace WebCore;
namespace WebKit {
void WebPluginContainerImpl::setFrameRect(const IntRect& frameRect)
{
Widget::setFrameRect(frameRect);
reportGeometry();
}
void WebPluginContainerImpl::paint(GraphicsContext* gc, const IntRect& damageRect)
{
if (gc->paintingDisabled())
return;
if (!parent())
return;
if (!frameRect().intersects(damageRect))
return;
gc->save();
ASSERT(parent()->isFrameView());
ScrollView* view = parent();
IntPoint origin = view->windowToContents(IntPoint(0, 0));
gc->translate(static_cast<float>(origin.x()), static_cast<float>(origin.y()));
#if WEBKIT_USING_SKIA
WebCanvas* canvas = gc->platformContext()->canvas();
#elif WEBKIT_USING_CG
WebCanvas* canvas = gc->platformContext();
#endif
IntRect windowRect =
IntRect(view->contentsToWindow(damageRect.location()), damageRect.size());
m_webPlugin->paint(canvas, windowRect);
gc->restore();
}
void WebPluginContainerImpl::invalidateRect(const IntRect& rect)
{
if (!parent())
return;
IntRect damageRect = convertToContainingWindow(rect);
IntRect clipRect = parent()->windowClipRect();
damageRect.intersect(clipRect);
parent()->hostWindow()->invalidateContentsAndWindow(damageRect, false );
}
void WebPluginContainerImpl::setFocus(bool focused)
{
Widget::setFocus(focused);
m_webPlugin->updateFocus(focused);
}
void WebPluginContainerImpl::show()
{
setSelfVisible(true);
m_webPlugin->updateVisibility(true);
Widget::show();
}
void WebPluginContainerImpl::hide()
{
setSelfVisible(false);
m_webPlugin->updateVisibility(false);
Widget::hide();
}
void WebPluginContainerImpl::handleEvent(Event* event)
{
if (!m_webPlugin->acceptsInputEvents())
return;
if (event->isMouseEvent())
handleMouseEvent(static_cast<MouseEvent*>(event));
else if (event->isWheelEvent())
handleWheelEvent(static_cast<WheelEvent*>(event));
else if (event->isKeyboardEvent())
handleKeyboardEvent(static_cast<KeyboardEvent*>(event));
}
void WebPluginContainerImpl::frameRectsChanged()
{
Widget::frameRectsChanged();
reportGeometry();
}
void WebPluginContainerImpl::widgetPositionsUpdated()
{
Widget::widgetPositionsUpdated();
reportGeometry();
}
void WebPluginContainerImpl::setParentVisible(bool parentVisible)
{
if (isParentVisible() == parentVisible)
return;
Widget::setParentVisible(parentVisible);
if (!isSelfVisible())
return;
m_webPlugin->updateVisibility(isVisible());
}
void WebPluginContainerImpl::setParent(ScrollView* view)
{
Widget::setParent(view);
if (view)
reportGeometry();
}
bool WebPluginContainerImpl::supportsPaginatedPrint() const
{
return m_webPlugin->supportsPaginatedPrint();
}
int WebPluginContainerImpl::printBegin(const IntRect& printableArea,
int printerDPI) const
{
return m_webPlugin->printBegin(printableArea, printerDPI);
}
bool WebPluginContainerImpl::printPage(int pageNumber,
WebCore::GraphicsContext* gc)
{
gc->save();
#if WEBKIT_USING_SKIA
WebCanvas* canvas = gc->platformContext()->canvas();
#elif WEBKIT_USING_CG
WebCanvas* canvas = gc->platformContext();
#endif
bool ret = m_webPlugin->printPage(pageNumber, canvas);
gc->restore();
return ret;
}
void WebPluginContainerImpl::printEnd()
{
return m_webPlugin->printEnd();
}
void WebPluginContainerImpl::invalidate()
{
Widget::invalidate();
}
void WebPluginContainerImpl::invalidateRect(const WebRect& rect)
{
invalidateRect(static_cast<IntRect>(rect));
}
void WebPluginContainerImpl::reportGeometry()
{
if (!parent())
return;
IntRect windowRect, clipRect;
Vector<IntRect> cutOutRects;
calculateGeometry(frameRect(), windowRect, clipRect, cutOutRects);
m_webPlugin->updateGeometry(windowRect, clipRect, cutOutRects, isVisible());
}
void WebPluginContainerImpl::clearScriptObjects()
{
Frame* frame = m_element->document()->frame();
if (!frame)
return;
frame->script()->cleanupScriptObjectsForPlugin(this);
}
NPObject* WebPluginContainerImpl::scriptableObjectForElement()
{
return m_element->getNPObject();
}
WebString WebPluginContainerImpl::executeScriptURL(const WebURL& url, bool popupsAllowed)
{
Frame* frame = m_element->document()->frame();
if (!frame)
return WebString();
const KURL& kurl = url;
ASSERT(kurl.protocolIs("javascript"));
String script = decodeURLEscapeSequences(
kurl.string().substring(strlen("javascript:")));
ScriptValue result = frame->script()->executeScript(script, popupsAllowed);
String resultStr;
result.getString(resultStr);
return resultStr;
}
void WebPluginContainerImpl::loadFrameRequest(
const WebURLRequest& request, const WebString& target, bool notifyNeeded, void* notifyData)
{
Frame* frame = m_element->document()->frame();
if (!frame)
return;
if (notifyNeeded) {
WebPluginLoadObserver* observer =
new WebPluginLoadObserver(this, request.url(), notifyData);
m_pluginLoadObservers.append(observer);
WebDataSourceImpl::setNextPluginLoadObserver(observer);
}
FrameLoadRequest frameRequest(request.toResourceRequest());
frameRequest.setFrameName(target);
frame->loader()->loadFrameRequest(
frameRequest,
false, false, 0, 0, SendReferrer);
}
void WebPluginContainerImpl::didReceiveResponse(const ResourceResponse& response)
{
frameRectsChanged();
WrappedResourceResponse urlResponse(response);
m_webPlugin->didReceiveResponse(urlResponse);
}
void WebPluginContainerImpl::didReceiveData(const char *data, int dataLength)
{
m_webPlugin->didReceiveData(data, dataLength);
}
void WebPluginContainerImpl::didFinishLoading()
{
m_webPlugin->didFinishLoading();
}
void WebPluginContainerImpl::didFailLoading(const ResourceError& error)
{
m_webPlugin->didFailLoading(error);
}
NPObject* WebPluginContainerImpl::scriptableObject()
{
return m_webPlugin->scriptableObject();
}
void WebPluginContainerImpl::willDestroyPluginLoadObserver(WebPluginLoadObserver* observer)
{
size_t pos = m_pluginLoadObservers.find(observer);
if (pos == notFound)
return;
m_pluginLoadObservers.remove(pos);
}
WebPluginContainerImpl::~WebPluginContainerImpl()
{
for (size_t i = 0; i < m_pluginLoadObservers.size(); ++i)
m_pluginLoadObservers[i]->clearPluginContainer();
m_webPlugin->destroy();
}
void WebPluginContainerImpl::handleMouseEvent(MouseEvent* event)
{
ASSERT(parent()->isFrameView());
FrameView* parentView = static_cast<FrameView*>(parent());
WebMouseEventBuilder webEvent(parentView, *event);
if (webEvent.type == WebInputEvent::Undefined)
return;
if (event->type() == eventNames().mousedownEvent) {
Frame* containingFrame = parentView->frame();
if (Page* currentPage = containingFrame->page())
currentPage->focusController()->setFocusedFrame(containingFrame);
containingFrame->document()->setFocusedNode(m_element);
}
WebCursorInfo cursorInfo;
if (m_webPlugin->handleInputEvent(webEvent, cursorInfo))
event->setDefaultHandled();
Page* page = parentView->frame()->page();
if (!page)
return;
ChromeClientImpl* chromeClient =
static_cast<ChromeClientImpl*>(page->chrome()->client());
chromeClient->setCursorForPlugin(cursorInfo);
}
void WebPluginContainerImpl::handleWheelEvent(WheelEvent* event)
{
FrameView* parentView = static_cast<FrameView*>(parent());
WebMouseWheelEventBuilder webEvent(parentView, *event);
if (webEvent.type == WebInputEvent::Undefined)
return;
WebCursorInfo cursorInfo;
if (m_webPlugin->handleInputEvent(webEvent, cursorInfo))
event->setDefaultHandled();
}
void WebPluginContainerImpl::handleKeyboardEvent(KeyboardEvent* event)
{
WebKeyboardEventBuilder webEvent(*event);
if (webEvent.type == WebInputEvent::Undefined)
return;
WebCursorInfo cursorInfo;
if (m_webPlugin->handleInputEvent(webEvent, cursorInfo))
event->setDefaultHandled();
}
void WebPluginContainerImpl::calculateGeometry(const IntRect& frameRect,
IntRect& windowRect,
IntRect& clipRect,
Vector<IntRect>& cutOutRects)
{
windowRect = IntRect(
parent()->contentsToWindow(frameRect.location()), frameRect.size());
clipRect = windowClipRect();
clipRect.move(-windowRect.x(), -windowRect.y());
windowCutOutRects(frameRect, cutOutRects);
for (size_t i = 0; i < cutOutRects.size(); i++)
cutOutRects[i].move(-frameRect.x(), -frameRect.y());
}
WebCore::IntRect WebPluginContainerImpl::windowClipRect() const
{
IntRect clipRect =
convertToContainingWindow(IntRect(0, 0, width(), height()));
if (m_element->renderer()->document()->renderer()) {
RenderLayer* layer = m_element->renderer()->enclosingLayer();
clipRect.intersect(
m_element->document()->view()->windowClipRectForLayer(layer, true));
}
return clipRect;
}
static void getObjectStack(const RenderObject* ro,
Vector<const RenderObject*>* roStack)
{
roStack->clear();
while (ro) {
roStack->append(ro);
ro = ro->parent();
}
}
static bool checkStackOnTop(
const Vector<const RenderObject*>& iframeZstack,
const Vector<const RenderObject*>& pluginZstack)
{
for (size_t i1 = 0, i2 = 0;
i1 < iframeZstack.size() && i2 < pluginZstack.size();
i1++, i2++) {
const RenderObject* ro1 = iframeZstack[iframeZstack.size() - 1 - i1];
const RenderObject* ro2 = pluginZstack[pluginZstack.size() - 1 - i2];
if (ro1 != ro2) {
if (ro1->style() && ro2->style()) {
int z1 = ro1->style()->zIndex();
int z2 = ro2->style()->zIndex();
if (z1 > z2)
return true;
if (z1 < z2)
return false;
}
if (ro2->style()->position() == StaticPosition)
return true;
const RenderObject* parent = ro1->parent();
if (!parent)
return false;
ASSERT(parent == ro2->parent());
for (const RenderObject* ro = parent->firstChild(); ro; ro = ro->nextSibling()) {
if (ro == ro1)
return false;
if (ro == ro2)
return true;
}
ASSERT(false); return false;
}
}
return true;
}
void WebPluginContainerImpl::windowCutOutRects(const IntRect& frameRect,
Vector<IntRect>& cutOutRects)
{
RenderObject* pluginNode = m_element->renderer();
ASSERT(pluginNode);
if (!pluginNode->style())
return;
Vector<const RenderObject*> pluginZstack;
Vector<const RenderObject*> iframeZstack;
getObjectStack(pluginNode, &pluginZstack);
Widget* parentWidget = this->parent();
if (!parentWidget->isFrameView())
return;
FrameView* parentFrameView = static_cast<FrameView*>(parentWidget);
const HashSet<RefPtr<Widget> >* children = parentFrameView->children();
for (HashSet<RefPtr<Widget> >::const_iterator it = children->begin(); it != children->end(); ++it) {
if (!(*it)->isFrameView())
continue;
const FrameView* frameView =
static_cast<const FrameView*>((*it).get());
if (!frameView->frame() || !frameView->frame()->ownerElement()
|| !frameView->frame()->ownerElement()->renderer())
continue;
HTMLElement* element = frameView->frame()->ownerElement();
RenderObject* iframeRenderer = element->renderer();
if (element->hasTagName(HTMLNames::iframeTag)
&& iframeRenderer->absoluteBoundingBoxRect().intersects(frameRect)
&& (!iframeRenderer->style() || iframeRenderer->style()->visibility() == VISIBLE)) {
getObjectStack(iframeRenderer, &iframeZstack);
if (checkStackOnTop(iframeZstack, pluginZstack)) {
IntPoint point =
roundedIntPoint(iframeRenderer->localToAbsolute());
RenderBox* rbox = toRenderBox(iframeRenderer);
IntSize size(rbox->width(), rbox->height());
cutOutRects.append(IntRect(point, size));
}
}
}
}
}