Upstream version 9.38.198.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 namespace blink {
41
42 ValidationMessageClientImpl::ValidationMessageClientImpl(WebViewImpl& webView)
43     : m_webView(webView)
44     , m_currentAnchor(nullptr)
45     , m_lastPageScaleFactor(1)
46     , m_finishTime(0)
47     , m_timer(this, &ValidationMessageClientImpl::checkAnchorStatus)
48 {
49 }
50
51 PassOwnPtrWillBeRawPtr<ValidationMessageClientImpl> ValidationMessageClientImpl::create(WebViewImpl& webView)
52 {
53     return adoptPtrWillBeNoop(new ValidationMessageClientImpl(webView));
54 }
55
56 ValidationMessageClientImpl::~ValidationMessageClientImpl()
57 {
58 }
59
60 FrameView* ValidationMessageClientImpl::currentView()
61 {
62     return m_currentAnchor->document().view();
63 }
64
65 void ValidationMessageClientImpl::showValidationMessage(const Element& anchor, const String& message, TextDirection messageDir, const String& subMessage, TextDirection subMessageDir)
66 {
67     if (message.isEmpty()) {
68         hideValidationMessage(anchor);
69         return;
70     }
71     if (!anchor.renderBox())
72         return;
73     if (m_currentAnchor)
74         hideValidationMessage(*m_currentAnchor);
75     m_currentAnchor = &anchor;
76     IntRect anchorInRootView = currentView()->contentsToRootView(anchor.pixelSnappedBoundingBox());
77     m_lastAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(anchorInRootView);
78     m_lastPageScaleFactor = m_webView.pageScaleFactor();
79     m_message = message;
80     const double minimumSecondToShowValidationMessage = 5.0;
81     const double secondPerCharacter = 0.05;
82     const double statusCheckInterval = 0.1;
83
84     m_webView.client()->showValidationMessage(anchorInRootView, m_message, toWebTextDirection(messageDir),
85         subMessage, toWebTextDirection(subMessageDir));
86     m_webView.client()->showValidationMessage(anchorInRootView, m_message, subMessage, toWebTextDirection(messageDir));
87
88     m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + subMessage.length()) * secondPerCharacter);
89     // FIXME: We should invoke checkAnchorStatus actively when layout, scroll,
90     // or page scale change happen.
91     m_timer.startRepeating(statusCheckInterval, FROM_HERE);
92 }
93
94 void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor)
95 {
96     if (!m_currentAnchor || !isValidationMessageVisible(anchor))
97         return;
98     m_timer.stop();
99     m_currentAnchor = nullptr;
100     m_message = String();
101     m_finishTime = 0;
102     m_webView.client()->hideValidationMessage();
103 }
104
105 bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor)
106 {
107     return m_currentAnchor == &anchor;
108 }
109
110 void ValidationMessageClientImpl::documentDetached(const Document& document)
111 {
112     if (m_currentAnchor && m_currentAnchor->document() == document)
113         hideValidationMessage(*m_currentAnchor);
114 }
115
116 void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*)
117 {
118     ASSERT(m_currentAnchor);
119     if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) {
120         hideValidationMessage(*m_currentAnchor);
121         return;
122     }
123
124     // Check the visibility of the element.
125     // FIXME: Can we check invisibility by scrollable non-frame elements?
126     IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox());
127     newAnchorRect = intersection(currentView()->convertToRootView(currentView()->boundsRect()), newAnchorRect);
128     if (newAnchorRect.isEmpty()) {
129         hideValidationMessage(*m_currentAnchor);
130         return;
131     }
132
133     IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect);
134     if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor)
135         return;
136     m_lastAnchorRectInScreen = newAnchorRectInScreen;
137     m_lastPageScaleFactor = m_webView.pageScaleFactor();
138     m_webView.client()->moveValidationMessage(newAnchorRect);
139 }
140
141 void ValidationMessageClientImpl::willBeDestroyed()
142 {
143     if (m_currentAnchor)
144         hideValidationMessage(*m_currentAnchor);
145 }
146
147 void ValidationMessageClientImpl::trace(Visitor* visitor)
148 {
149     visitor->trace(m_currentAnchor);
150     ValidationMessageClient::trace(visitor);
151 }
152
153 } // namespace blink