1/* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "ValidationMessageClientImpl.h" 28 29#include "WebTextDirection.h" 30#include "WebValidationMessageClient.h" 31#include "WebViewImpl.h" 32#include "core/dom/Element.h" 33#include "core/page/FrameView.h" 34#include "core/platform/HostWindow.h" 35#include "core/rendering/RenderObject.h" 36#include "wtf/CurrentTime.h" 37#include "public/platform/WebRect.h" 38#include "public/platform/WebString.h" 39 40using namespace WebCore; 41 42namespace WebKit { 43 44ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView, WebValidationMessageClient& client) 45 : m_webView(webView) 46 , m_client(client) 47 , m_currentAnchor(0) 48 , m_lastPageScaleFactor(1) 49 , m_finishTime(0) 50 , m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus) 51{ 52} 53 54PassOwnPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView, WebValidationMessageClient& client) 55{ 56 return adoptPtr(new ValidationMessageClientImpl(webView, client)); 57} 58 59ValidationMessageClientImpl::~ValidationMessageClientImpl() 60{ 61 if (m_currentAnchor) 62 hideValidationMessage(*m_currentAnchor); 63} 64 65FrameView* ValidationMessageClientImpl::currentView() 66{ 67 return m_currentAnchor->document()->view(); 68} 69 70void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message) 71{ 72 if (message.isEmpty()) { 73 hideValidationMessage(anchor); 74 return; 75 } 76 if (!anchor.renderBox()) 77 return; 78 if (m_currentAnchor) 79 hideValidationMessage(*m_currentAnchor); 80 m_currentAnchor = &anchor; 81 IntRect anchorInRootView = currentView()->contentsToRootView(anchor.pixelSnappedBoundingBox()); 82 m_lastAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(anchorInRootView); 83 m_lastPageScaleFactor = m_webView.pageScaleFactor(); 84 m_message = message; 85 86 WebTextDirection dir = m_currentAnchor->renderer()->style()->direction() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight; 87 AtomicString title = m_currentAnchor->fastGetAttribute(HTMLNames::titleAttr); 88 m_client.showValidationMessage(anchorInRootView, m_message, title, dir); 89 90 const double minimumSecondToShowValidationMessage = 5.0; 91 const double secondPerCharacter = 0.05; 92 const double statusCheckInterval = 0.1; 93 m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + title.length()) * secondPerCharacter); 94 // FIXME: We should invoke checkAnchorStatus actively when layout, scroll, 95 // or page scale change happen. 96 m_timer.startRepeating(statusCheckInterval); 97} 98 99void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor) 100{ 101 if (!m_currentAnchor || !isValidationMessageVisible(anchor)) 102 return; 103 m_timer.stop(); 104 m_currentAnchor = 0; 105 m_message = String(); 106 m_finishTime = 0; 107 m_client.hideValidationMessage(); 108} 109 110bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor) 111{ 112 return m_currentAnchor == &anchor; 113} 114 115void ValidationMessageClientImpl::documentDetached(const Document& document) 116{ 117 if (m_currentAnchor && m_currentAnchor->document() == &document) 118 hideValidationMessage(*m_currentAnchor); 119} 120 121void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*) 122{ 123 ASSERT(m_currentAnchor); 124 if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) { 125 hideValidationMessage(*m_currentAnchor); 126 return; 127 } 128 129 // Check the visibility of the element. 130 // FIXME: Can we check invisibility by scrollable non-frame elements? 131 IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox()); 132 newAnchorRect = intersection(currentView()->convertToRootView(currentView()->boundsRect()), newAnchorRect); 133 if (newAnchorRect.isEmpty()) { 134 hideValidationMessage(*m_currentAnchor); 135 return; 136 } 137 138 IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect); 139 if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor) 140 return; 141 m_lastAnchorRectInScreen = newAnchorRectInScreen; 142 m_lastPageScaleFactor = m_webView.pageScaleFactor(); 143 m_client.moveValidationMessage(newAnchorRect); 144} 145 146} 147