2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
27 #include "web/ValidationMessageClientImpl.h"
29 #include "core/dom/Element.h"
30 #include "core/frame/FrameView.h"
31 #include "core/rendering/RenderObject.h"
32 #include "platform/HostWindow.h"
33 #include "public/platform/WebRect.h"
34 #include "public/platform/WebString.h"
35 #include "public/web/WebTextDirection.h"
36 #include "public/web/WebViewClient.h"
37 #include "web/WebViewImpl.h"
38 #include "wtf/CurrentTime.h"
40 using namespace WebCore;
44 ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView)
47 , m_lastPageScaleFactor(1)
49 , m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus)
53 PassOwnPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView)
55 return adoptPtr(new ValidationMessageClientImpl(webView));
58 ValidationMessageClientImpl::~ValidationMessageClientImpl()
62 FrameView* ValidationMessageClientImpl::currentView()
64 return m_currentAnchor->document().view();
67 void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message)
69 if (message.isEmpty()) {
70 hideValidationMessage(anchor);
73 if (!anchor.renderBox())
76 hideValidationMessage(*m_currentAnchor);
77 m_currentAnchor = &anchor;
78 IntRect anchorInRootView = currentView()->contentsToRootView(anchor.pixelSnappedBoundingBox());
79 m_lastAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(anchorInRootView);
80 m_lastPageScaleFactor = m_webView.pageScaleFactor();
83 WebTextDirection dir = m_currentAnchor->renderer()->style()->direction() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight;
84 AtomicString title = m_currentAnchor->fastGetAttribute(HTMLNames::titleAttr);
85 m_webView.client()->showValidationMessage(anchorInRootView, m_message, title, dir);
87 const double minimumSecondToShowValidationMessage = 5.0;
88 const double secondPerCharacter = 0.05;
89 const double statusCheckInterval = 0.1;
90 m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + title.length()) * secondPerCharacter);
91 // FIXME: We should invoke checkAnchorStatus actively when layout, scroll,
92 // or page scale change happen.
93 m_timer.startRepeating(statusCheckInterval, FROM_HERE);
96 void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor)
98 if (!m_currentAnchor || !isValidationMessageVisible(anchor))
102 m_message = String();
104 m_webView.client()->hideValidationMessage();
107 bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor)
109 return m_currentAnchor == &anchor;
112 void ValidationMessageClientImpl::documentDetached(const Document& document)
114 if (m_currentAnchor && m_currentAnchor->document() == document)
115 hideValidationMessage(*m_currentAnchor);
118 void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*)
120 ASSERT(m_currentAnchor);
121 if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) {
122 hideValidationMessage(*m_currentAnchor);
126 // Check the visibility of the element.
127 // FIXME: Can we check invisibility by scrollable non-frame elements?
128 IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox());
129 newAnchorRect = intersection(currentView()->convertToRootView(currentView()->boundsRect()), newAnchorRect);
130 if (newAnchorRect.isEmpty()) {
131 hideValidationMessage(*m_currentAnchor);
135 IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect);
136 if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor)
138 m_lastAnchorRectInScreen = newAnchorRectInScreen;
139 m_lastPageScaleFactor = m_webView.pageScaleFactor();
140 m_webView.client()->moveValidationMessage(newAnchorRect);
143 void ValidationMessageClientImpl::willBeDestroyed()
146 hideValidationMessage(*m_currentAnchor);