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 #if !ENABLE(INPUT_MULTIPLE_FIELDS_UI)
28 #include "web/ExternalDateTimeChooser.h"
30 #include "InputTypeNames.h"
31 #include "platform/DateTimeChooserClient.h"
32 #include "public/web/WebDateTimeChooserCompletion.h"
33 #include "public/web/WebDateTimeChooserParams.h"
34 #include "public/web/WebViewClient.h"
35 #include "web/ChromeClientImpl.h"
36 #include "wtf/text/AtomicString.h"
38 using namespace WebCore;
42 class WebDateTimeChooserCompletionImpl : public WebDateTimeChooserCompletion {
44 WebDateTimeChooserCompletionImpl(ExternalDateTimeChooser* chooser)
50 virtual void didChooseValue(const WebString& value) OVERRIDE
52 m_chooser->didChooseValue(value);
56 virtual void didChooseValue(double value) OVERRIDE
58 m_chooser->didChooseValue(value);
62 virtual void didCancelChooser() OVERRIDE
64 m_chooser->didCancelChooser();
68 RefPtr<ExternalDateTimeChooser> m_chooser;
71 ExternalDateTimeChooser::~ExternalDateTimeChooser()
75 ExternalDateTimeChooser::ExternalDateTimeChooser(WebCore::DateTimeChooserClient* client)
81 PassRefPtr<ExternalDateTimeChooser> ExternalDateTimeChooser::create(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, WebCore::DateTimeChooserClient* client, const WebCore::DateTimeChooserParameters& parameters)
84 RefPtr<ExternalDateTimeChooser> chooser = adoptRef(new ExternalDateTimeChooser(client));
85 if (!chooser->openDateTimeChooser(chromeClient, webViewClient, parameters))
87 return chooser.release();
91 static WebDateTimeInputType toWebDateTimeInputType(const AtomicString& source)
93 if (source == InputTypeNames::date)
94 return WebDateTimeInputTypeDate;
95 if (source == InputTypeNames::datetime)
96 return WebDateTimeInputTypeDateTime;
97 if (source == InputTypeNames::datetime_local)
98 return WebDateTimeInputTypeDateTimeLocal;
99 if (source == InputTypeNames::month)
100 return WebDateTimeInputTypeMonth;
101 if (source == InputTypeNames::time)
102 return WebDateTimeInputTypeTime;
103 if (source == InputTypeNames::week)
104 return WebDateTimeInputTypeWeek;
105 return WebDateTimeInputTypeNone;
108 bool ExternalDateTimeChooser::openDateTimeChooser(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, const DateTimeChooserParameters& parameters)
113 WebDateTimeChooserParams webParams;
114 webParams.type = toWebDateTimeInputType(parameters.type);
115 webParams.anchorRectInScreen = chromeClient->rootViewToScreen(parameters.anchorRectInRootView);
116 webParams.currentValue = parameters.currentValue;
117 webParams.doubleValue = parameters.doubleValue;
118 webParams.suggestions = parameters.suggestions;
119 webParams.minimum = parameters.minimum;
120 webParams.maximum = parameters.maximum;
121 webParams.step = parameters.step;
122 webParams.stepBase = parameters.stepBase;
123 webParams.isRequired = parameters.required;
124 webParams.isAnchorElementRTL = parameters.isAnchorElementRTL;
126 WebDateTimeChooserCompletion* completion = new WebDateTimeChooserCompletionImpl(this);
127 if (webViewClient->openDateTimeChooser(webParams, completion))
129 // We can't open a chooser. Calling
130 // WebDateTimeChooserCompletionImpl::didCancelChooser to delete the
131 // WebDateTimeChooserCompletionImpl object and deref this.
132 completion->didCancelChooser();
136 void ExternalDateTimeChooser::didChooseValue(const WebString& value)
139 m_client->didChooseValue(value);
140 // didChooseValue might run JavaScript code, and endChooser() might be
141 // called. However DateTimeChooserCompletionImpl still has one reference to
144 m_client->didEndChooser();
147 void ExternalDateTimeChooser::didChooseValue(double value)
150 m_client->didChooseValue(value);
151 // didChooseValue might run JavaScript code, and endChooser() might be
152 // called. However DateTimeChooserCompletionImpl still has one reference to
155 m_client->didEndChooser();
158 void ExternalDateTimeChooser::didCancelChooser()
161 m_client->didEndChooser();
164 void ExternalDateTimeChooser::endChooser()
166 DateTimeChooserClient* client = m_client;
168 client->didEndChooser();