Home | History | Annotate | Download | only in web
      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 
     87     m_finishTime = monotonicallyIncreasingTime() + std::max(minimumSecondToShowValidationMessage, (message.length() + subMessage.length()) * secondPerCharacter);
     88     // FIXME: We should invoke checkAnchorStatus actively when layout, scroll,
     89     // or page scale change happen.
     90     m_timer.startRepeating(statusCheckInterval, FROM_HERE);
     91 }
     92 
     93 void ValidationMessageClientImpl::hideValidationMessage(const Element& anchor)
     94 {
     95     if (!m_currentAnchor || !isValidationMessageVisible(anchor))
     96         return;
     97     m_timer.stop();
     98     m_currentAnchor = nullptr;
     99     m_message = String();
    100     m_finishTime = 0;
    101     m_webView.client()->hideValidationMessage();
    102 }
    103 
    104 bool ValidationMessageClientImpl::isValidationMessageVisible(const Element& anchor)
    105 {
    106     return m_currentAnchor == &anchor;
    107 }
    108 
    109 void ValidationMessageClientImpl::documentDetached(const Document& document)
    110 {
    111     if (m_currentAnchor && m_currentAnchor->document() == document)
    112         hideValidationMessage(*m_currentAnchor);
    113 }
    114 
    115 void ValidationMessageClientImpl::checkAnchorStatus(Timer<ValidationMessageClientImpl>*)
    116 {
    117     ASSERT(m_currentAnchor);
    118     if (monotonicallyIncreasingTime() >= m_finishTime || !currentView()) {
    119         hideValidationMessage(*m_currentAnchor);
    120         return;
    121     }
    122 
    123     // Check the visibility of the element.
    124     // FIXME: Can we check invisibility by scrollable non-frame elements?
    125     IntRect newAnchorRect = currentView()->contentsToRootView(m_currentAnchor->pixelSnappedBoundingBox());
    126     newAnchorRect = intersection(currentView()->convertToContainingWindow(currentView()->boundsRect()), newAnchorRect);
    127     if (newAnchorRect.isEmpty()) {
    128         hideValidationMessage(*m_currentAnchor);
    129         return;
    130     }
    131 
    132     IntRect newAnchorRectInScreen = currentView()->hostWindow()->rootViewToScreen(newAnchorRect);
    133     if (newAnchorRectInScreen == m_lastAnchorRectInScreen && m_webView.pageScaleFactor() == m_lastPageScaleFactor)
    134         return;
    135     m_lastAnchorRectInScreen = newAnchorRectInScreen;
    136     m_lastPageScaleFactor = m_webView.pageScaleFactor();
    137     m_webView.client()->moveValidationMessage(newAnchorRect);
    138 }
    139 
    140 void ValidationMessageClientImpl::willBeDestroyed()
    141 {
    142     if (m_currentAnchor)
    143         hideValidationMessage(*m_currentAnchor);
    144 }
    145 
    146 void ValidationMessageClientImpl::trace(Visitor* visitor)
    147 {
    148     visitor->trace(m_currentAnchor);
    149     ValidationMessageClient::trace(visitor);
    150 }
    151 
    152 } // namespace blink
    153