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 #if !ENABLE(INPUT_MULTIPLE_FIELDS_UI)
     28 #include "ExternalDateTimeChooser.h"
     29 
     30 #include "ChromeClientImpl.h"
     31 #include "WebDateTimeChooserCompletion.h"
     32 #include "WebDateTimeChooserParams.h"
     33 #include "WebViewClient.h"
     34 #include "core/html/InputTypeNames.h"
     35 #include "core/platform/DateTimeChooserClient.h"
     36 #include "wtf/text/AtomicString.h"
     37 
     38 using namespace WebCore;
     39 
     40 namespace WebKit {
     41 
     42 class WebDateTimeChooserCompletionImpl : public WebDateTimeChooserCompletion {
     43 public:
     44     WebDateTimeChooserCompletionImpl(ExternalDateTimeChooser* chooser)
     45         : m_chooser(chooser)
     46     {
     47     }
     48 
     49 private:
     50     virtual void didChooseValue(const WebString& value) OVERRIDE
     51     {
     52         m_chooser->didChooseValue(value);
     53         delete this;
     54     }
     55 
     56     virtual void didCancelChooser() OVERRIDE
     57     {
     58         m_chooser->didCancelChooser();
     59         delete this;
     60     }
     61 
     62     RefPtr<ExternalDateTimeChooser> m_chooser;
     63 };
     64 
     65 ExternalDateTimeChooser::~ExternalDateTimeChooser()
     66 {
     67 }
     68 
     69 ExternalDateTimeChooser::ExternalDateTimeChooser(WebCore::DateTimeChooserClient* client)
     70     : m_client(client)
     71 {
     72     ASSERT(client);
     73 }
     74 
     75 PassRefPtr<ExternalDateTimeChooser> ExternalDateTimeChooser::create(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, WebCore::DateTimeChooserClient* client, const WebCore::DateTimeChooserParameters& parameters)
     76 {
     77     ASSERT(chromeClient);
     78     RefPtr<ExternalDateTimeChooser> chooser = adoptRef(new ExternalDateTimeChooser(client));
     79     if (!chooser->openDateTimeChooser(chromeClient, webViewClient, parameters))
     80         chooser.clear();
     81     return chooser.release();
     82 }
     83 
     84 
     85 static WebDateTimeInputType toWebDateTimeInputType(const AtomicString& source)
     86 {
     87     if (source == InputTypeNames::date())
     88         return WebDateTimeInputTypeDate;
     89     if (source == InputTypeNames::datetime())
     90         return WebDateTimeInputTypeDateTime;
     91     if (source == InputTypeNames::datetimelocal())
     92         return WebDateTimeInputTypeDateTimeLocal;
     93     if (source == InputTypeNames::month())
     94         return WebDateTimeInputTypeMonth;
     95     if (source == InputTypeNames::time())
     96         return WebDateTimeInputTypeTime;
     97     if (source == InputTypeNames::week())
     98         return WebDateTimeInputTypeWeek;
     99     return WebDateTimeInputTypeNone;
    100 }
    101 
    102 bool ExternalDateTimeChooser::openDateTimeChooser(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, const DateTimeChooserParameters& parameters)
    103 {
    104     if (!webViewClient)
    105         return false;
    106 
    107     WebDateTimeChooserParams webParams;
    108     webParams.type = toWebDateTimeInputType(parameters.type);
    109     webParams.anchorRectInScreen = chromeClient->rootViewToScreen(parameters.anchorRectInRootView);
    110     webParams.currentValue = parameters.currentValue;
    111     webParams.suggestionValues = parameters.suggestionValues;
    112     webParams.localizedSuggestionValues = parameters.localizedSuggestionValues;
    113     webParams.suggestionLabels = parameters.suggestionLabels;
    114     webParams.minimum = parameters.minimum;
    115     webParams.maximum = parameters.maximum;
    116     webParams.step = parameters.step;
    117     webParams.stepBase = parameters.stepBase;
    118     webParams.isRequired = parameters.required;
    119     webParams.isAnchorElementRTL = parameters.isAnchorElementRTL;
    120 
    121     WebDateTimeChooserCompletion* completion = new WebDateTimeChooserCompletionImpl(this);
    122     if (webViewClient->openDateTimeChooser(webParams, completion))
    123         return true;
    124     // We can't open a chooser. Calling
    125     // WebDateTimeChooserCompletionImpl::didCancelChooser to delete the
    126     // WebDateTimeChooserCompletionImpl object and deref this.
    127     completion->didCancelChooser();
    128     return false;
    129 }
    130 
    131 void ExternalDateTimeChooser::didChooseValue(const WebString& value)
    132 {
    133     if (m_client)
    134         m_client->didChooseValue(value);
    135     // didChooseValue might run JavaScript code, and endChooser() might be
    136     // called. However DateTimeChooserCompletionImpl still has one reference to
    137     // this object.
    138     if (m_client)
    139         m_client->didEndChooser();
    140 }
    141 
    142 void ExternalDateTimeChooser::didCancelChooser()
    143 {
    144     if (m_client)
    145         m_client->didEndChooser();
    146 }
    147 
    148 void ExternalDateTimeChooser::endChooser()
    149 {
    150     DateTimeChooserClient* client = m_client;
    151     m_client = 0;
    152     client->didEndChooser();
    153 }
    154 
    155 }
    156 
    157 #endif
    158