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 "ExternalDateTimeChooser.h"
30 #include "ChromeClientImpl.h"
31 #include "WebDateTimeChooserCompletion.h"
32 #include "WebDateTimeChooserParams.h"
33 #include "WebViewClient.h"
34 #include "core/html/forms/InputTypeNames.h"
35 #include "platform/DateTimeChooserClient.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 didCancelChooser() OVERRIDE
58 m_chooser->didCancelChooser();
62 RefPtr<ExternalDateTimeChooser> m_chooser;
65 ExternalDateTimeChooser::~ExternalDateTimeChooser()
69 ExternalDateTimeChooser::ExternalDateTimeChooser(WebCore::DateTimeChooserClient* client)
75 PassRefPtr<ExternalDateTimeChooser> ExternalDateTimeChooser::create(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, WebCore::DateTimeChooserClient* client, const WebCore::DateTimeChooserParameters& parameters)
78 RefPtr<ExternalDateTimeChooser> chooser = adoptRef(new ExternalDateTimeChooser(client));
79 if (!chooser->openDateTimeChooser(chromeClient, webViewClient, parameters))
81 return chooser.release();
85 static WebDateTimeInputType toWebDateTimeInputType(const AtomicString& source)
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;
102 bool ExternalDateTimeChooser::openDateTimeChooser(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, const DateTimeChooserParameters& parameters)
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;
121 WebDateTimeChooserCompletion* completion = new WebDateTimeChooserCompletionImpl(this);
122 if (webViewClient->openDateTimeChooser(webParams, completion))
124 // We can't open a chooser. Calling
125 // WebDateTimeChooserCompletionImpl::didCancelChooser to delete the
126 // WebDateTimeChooserCompletionImpl object and deref this.
127 completion->didCancelChooser();
131 void ExternalDateTimeChooser::didChooseValue(const WebString& value)
134 m_client->didChooseValue(value);
135 // didChooseValue might run JavaScript code, and endChooser() might be
136 // called. However DateTimeChooserCompletionImpl still has one reference to
139 m_client->didEndChooser();
142 void ExternalDateTimeChooser::didCancelChooser()
145 m_client->didEndChooser();
148 void ExternalDateTimeChooser::endChooser()
150 DateTimeChooserClient* client = m_client;
152 client->didEndChooser();