This source file includes following definitions.
- m_timer
- create
- currentView
- showValidationMessage
- hideValidationMessage
- isValidationMessageVisible
- documentDetached
- checkAnchorStatus
#include "config.h"
#include "ValidationMessageClientImpl.h"
#include "WebTextDirection.h"
#include "WebViewImpl.h"
#include "core/dom/Element.h"
#include "core/frame/FrameView.h"
#include "core/rendering/RenderObject.h"
#include "platform/HostWindow.h"
#include "public/platform/WebRect.h"
#include "public/platform/WebString.h"
#include "public/web/WebViewClient.h"
#include "wtf/CurrentTime.h"
using namespace WebCore;
namespace blink {
ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView)
: m_webView(webView)
, m_currentAnchor(0)
, m_lastPageScaleFactor(1)
, m_finishTime(0)
, m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus)
{
}
PassOwnPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView)
{
return adoptPtr(new ValidationMessageClientImpl(webView));
}
ValidationMessageClientImpl::~ValidationMessageClientImpl()
{
if (m_currentAnchor)
hideValidationMessage(*m_currentAnchor);
}
FrameView* ValidationMessageClientImpl::currentView()
{
return m_currentAnchor->document().view();
}
void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message)
{
if (message.isEmpty()) {
hideValidationMessage(anchor);
return;
}
if (!anchor.renderBox())
return;
if (m_currentAnchor)
hideValidationMessage(*m_currentAnchor);
m_currentAnchor = &anchor;
IntRect anchorInRootView = currentView()->contentsToRootView(anchor.pixelSnappedBoundingBox());
m_lastAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(anchorInRootView);
m_lastPageScaleFactor = m_webView.pageScaleFactor();
m_message = message;
WebTextDirection dir = m_currentAnchor->renderer()->style()->direction() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight;
AtomicString title = m_currentAnchor->fastGetAttribute(HTMLNames::titleAttr);
m_webView.client()->showValidationMessage(anchorInRootView, m_message, title, dir);
const double minimumSecondToShowValidationMessage = 5.0;
const double secondPerCharacter = 0.05;
const double statusCheckInterval = 0.1;
m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + title.length()) * secondPerCharacter);
m_timer.startRepeating(statusCheckInterval, FROM_HERE);
}
void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor)
{
if (!m_currentAnchor || !isValidationMessageVisible(anchor))
return;
m_timer.stop();
m_currentAnchor = 0;
m_message = String();
m_finishTime = 0;
m_webView.client()->hideValidationMessage();
}
bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor)
{
return m_currentAnchor == &anchor;
}
void ValidationMessageClientImpl::documentDetached(const Document& document)
{
if (m_currentAnchor && m_currentAnchor->document() == document)
hideValidationMessage(*m_currentAnchor);
}
void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*)
{
ASSERT(m_currentAnchor);
if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) {
hideValidationMessage(*m_currentAnchor);
return;
}
IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox());
newAnchorRect = intersection(currentView()->convertToRootView(currentView()->boundsRect()), newAnchorRect);
if (newAnchorRect.isEmpty()) {
hideValidationMessage(*m_currentAnchor);
return;
}
IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect);
if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor)
return;
m_lastAnchorRectInScreen = newAnchorRectInScreen;
m_lastPageScaleFactor = m_webView.pageScaleFactor();
m_webView.client()->moveValidationMessage(newAnchorRect);
}
}