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/frame/FrameView.h" 34#include "core/rendering/RenderObject.h" 35#include "platform/HostWindow.h" 36#include "public/platform/WebRect.h" 37#include "public/platform/WebString.h" 38#include "public/web/WebViewClient.h" 39#include "wtf/CurrentTime.h" 40 41using namespace WebCore; 42 43namespace blink { 44 45ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView, WebValidationMessageClient* client) 46 : m_webView(webView) 47 , m_client(client) 48 , m_currentAnchor(0) 49 , m_lastPageScaleFactor(1) 50 , m_finishTime(0) 51 , m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus) 52{ 53} 54 55PassOwnPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView, WebValidationMessageClient* client) 56{ 57 return adoptPtr(new ValidationMessageClientImpl(webView, client)); 58} 59 60ValidationMessageClientImpl::~ValidationMessageClientImpl() 61{ 62 if (m_currentAnchor) 63 hideValidationMessage(*m_currentAnchor); 64} 65 66FrameView* ValidationMessageClientImpl::currentView() 67{ 68 return m_currentAnchor->document().view(); 69} 70 71void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message) 72{ 73 if (message.isEmpty()) { 74 hideValidationMessage(anchor); 75 return; 76 } 77 if (!anchor.renderBox()) 78 return; 79 if (m_currentAnchor) 80 hideValidationMessage(*m_currentAnchor); 81 m_currentAnchor = &anchor; 82 IntRect anchorInRootView = currentView()->contentsToRootView(anchor.pixelSnappedBoundingBox()); 83 m_lastAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(anchorInRootView); 84 m_lastPageScaleFactor = m_webView.pageScaleFactor(); 85 m_message = message; 86 87 WebTextDirection dir = m_currentAnchor->renderer()->style()->direction() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight; 88 AtomicString title = m_currentAnchor->fastGetAttribute(HTMLNames::titleAttr); 89 if (m_client) 90 m_client->showValidationMessage(anchorInRootView, m_message, title, dir); 91 m_webView.client()->showValidationMessage(anchorInRootView, m_message, title, dir); 92 93 const double minimumSecondToShowValidationMessage = 5.0; 94 const double secondPerCharacter = 0.05; 95 const double statusCheckInterval = 0.1; 96 m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + title.length()) * secondPerCharacter); 97 // FIXME: We should invoke checkAnchorStatus actively when layout, scroll, 98 // or page scale change happen. 99 m_timer.startRepeating(statusCheckInterval); 100} 101 102void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor) 103{ 104 if (!m_currentAnchor || !isValidationMessageVisible(anchor)) 105 return; 106 m_timer.stop(); 107 m_currentAnchor = 0; 108 m_message = String(); 109 m_finishTime = 0; 110 if (m_client) 111 m_client->hideValidationMessage(); 112 m_webView.client()->hideValidationMessage(); 113} 114 115bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor) 116{ 117 return m_currentAnchor == &anchor; 118} 119 120void ValidationMessageClientImpl::documentDetached(const Document& document) 121{ 122 if (m_currentAnchor && m_currentAnchor->document() == document) 123 hideValidationMessage(*m_currentAnchor); 124} 125 126void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*) 127{ 128 ASSERT(m_currentAnchor); 129 if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) { 130 hideValidationMessage(*m_currentAnchor); 131 return; 132 } 133 134 // Check the visibility of the element. 135 // FIXME: Can we check invisibility by scrollable non-frame elements? 136 IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox()); 137 newAnchorRect = intersection(currentView()->convertToRootView(currentView()->boundsRect()), newAnchorRect); 138 if (newAnchorRect.isEmpty()) { 139 hideValidationMessage(*m_currentAnchor); 140 return; 141 } 142 143 IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect); 144 if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor) 145 return; 146 m_lastAnchorRectInScreen = newAnchorRectInScreen; 147 m_lastPageScaleFactor = m_webView.pageScaleFactor(); 148 if (m_client) 149 m_client->moveValidationMessage(newAnchorRect); 150 m_webView.client()->moveValidationMessage(newAnchorRect); 151} 152 153} 154