Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / ValidationMessageClientImpl.cpp
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 "web/ValidationMessageClientImpl.h"
28
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"
39
40 using namespace WebCore;
41
42 namespace blink {
43
44 ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView)
45     : m_webView(webView)
46     , m_currentAnchor(0)
47     , m_lastPageScaleFactor(1)
48     , m_finishTime(0)
49     , m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus)
50 {
51 }
52
53 PassOwnPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView)
54 {
55     return adoptPtr(new ValidationMessageClientImpl(webView));
56 }
57
58 ValidationMessageClientImpl::~ValidationMessageClientImpl()
59 {
60 }
61
62 FrameView* ValidationMessageClientImpl::currentView()
63 {
64     return m_currentAnchor->document().view();
65 }
66
67 void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message)
68 {
69     if (message.isEmpty()) {
70         hideValidationMessage(anchor);
71         return;
72     }
73     if (!anchor.renderBox())
74         return;
75     if (m_currentAnchor)
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();
81     m_message = message;
82
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);
86
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);
94 }
95
96 void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor)
97 {
98     if (!m_currentAnchor || !isValidationMessageVisible(anchor))
99         return;
100     m_timer.stop();
101     m_currentAnchor = 0;
102     m_message = String();
103     m_finishTime = 0;
104     m_webView.client()->hideValidationMessage();
105 }
106
107 bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor)
108 {
109     return m_currentAnchor == &anchor;
110 }
111
112 void ValidationMessageClientImpl::documentDetached(const Document& document)
113 {
114     if (m_currentAnchor && m_currentAnchor->document() == document)
115         hideValidationMessage(*m_currentAnchor);
116 }
117
118 void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*)
119 {
120     ASSERT(m_currentAnchor);
121     if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) {
122         hideValidationMessage(*m_currentAnchor);
123         return;
124     }
125
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);
132         return;
133     }
134
135     IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect);
136     if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor)
137         return;
138     m_lastAnchorRectInScreen = newAnchorRectInScreen;
139     m_lastPageScaleFactor = m_webView.pageScaleFactor();
140     m_webView.client()->moveValidationMessage(newAnchorRect);
141 }
142
143 void ValidationMessageClientImpl::willBeDestroyed()
144 {
145     if (m_currentAnchor)
146         hideValidationMessage(*m_currentAnchor);
147 }
148
149 }