Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 27 | #if !ENABLE(INPUT_MULTIPLE_FIELDS_UI) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 28 | #include "ExternalDateTimeChooser.h" |
| 29 | |
| 30 | #include "ChromeClientImpl.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 31 | #include "InputTypeNames.h" |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 32 | #include "WebDateTimeChooserCompletion.h" |
| 33 | #include "WebDateTimeChooserParams.h" |
| 34 | #include "WebViewClient.h" |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 35 | #include "platform/DateTimeChooserClient.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 36 | #include "wtf/text/AtomicString.h" |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace WebCore; |
| 39 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 40 | namespace blink { |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 56 | virtual void didChooseValue(double value) OVERRIDE |
| 57 | { |
| 58 | m_chooser->didChooseValue(value); |
| 59 | delete this; |
| 60 | } |
| 61 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 62 | virtual void didCancelChooser() OVERRIDE |
| 63 | { |
| 64 | m_chooser->didCancelChooser(); |
| 65 | delete this; |
| 66 | } |
| 67 | |
| 68 | RefPtr<ExternalDateTimeChooser> m_chooser; |
| 69 | }; |
| 70 | |
| 71 | ExternalDateTimeChooser::~ExternalDateTimeChooser() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | ExternalDateTimeChooser::ExternalDateTimeChooser(WebCore::DateTimeChooserClient* client) |
| 76 | : m_client(client) |
| 77 | { |
| 78 | ASSERT(client); |
| 79 | } |
| 80 | |
| 81 | PassRefPtr<ExternalDateTimeChooser> ExternalDateTimeChooser::create(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, WebCore::DateTimeChooserClient* client, const WebCore::DateTimeChooserParameters& parameters) |
| 82 | { |
| 83 | ASSERT(chromeClient); |
| 84 | RefPtr<ExternalDateTimeChooser> chooser = adoptRef(new ExternalDateTimeChooser(client)); |
| 85 | if (!chooser->openDateTimeChooser(chromeClient, webViewClient, parameters)) |
| 86 | chooser.clear(); |
| 87 | return chooser.release(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static WebDateTimeInputType toWebDateTimeInputType(const AtomicString& source) |
| 92 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 93 | if (source == InputTypeNames::date) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 94 | return WebDateTimeInputTypeDate; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 95 | if (source == InputTypeNames::datetime) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 96 | return WebDateTimeInputTypeDateTime; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 97 | if (source == InputTypeNames::datetime_local) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 98 | return WebDateTimeInputTypeDateTimeLocal; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 99 | if (source == InputTypeNames::month) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 100 | return WebDateTimeInputTypeMonth; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 101 | if (source == InputTypeNames::time) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 102 | return WebDateTimeInputTypeTime; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 103 | if (source == InputTypeNames::week) |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 104 | return WebDateTimeInputTypeWeek; |
| 105 | return WebDateTimeInputTypeNone; |
| 106 | } |
| 107 | |
| 108 | bool ExternalDateTimeChooser::openDateTimeChooser(ChromeClientImpl* chromeClient, WebViewClient* webViewClient, const DateTimeChooserParameters& parameters) |
| 109 | { |
| 110 | if (!webViewClient) |
| 111 | return false; |
| 112 | |
| 113 | WebDateTimeChooserParams webParams; |
| 114 | webParams.type = toWebDateTimeInputType(parameters.type); |
| 115 | webParams.anchorRectInScreen = chromeClient->rootViewToScreen(parameters.anchorRectInRootView); |
| 116 | webParams.currentValue = parameters.currentValue; |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 117 | webParams.doubleValue = parameters.doubleValue; |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 118 | webParams.suggestions = parameters.suggestions; |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 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; |
| 125 | |
| 126 | WebDateTimeChooserCompletion* completion = new WebDateTimeChooserCompletionImpl(this); |
| 127 | if (webViewClient->openDateTimeChooser(webParams, completion)) |
| 128 | return true; |
| 129 | // We can't open a chooser. Calling |
| 130 | // WebDateTimeChooserCompletionImpl::didCancelChooser to delete the |
| 131 | // WebDateTimeChooserCompletionImpl object and deref this. |
| 132 | completion->didCancelChooser(); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | void ExternalDateTimeChooser::didChooseValue(const WebString& value) |
| 137 | { |
| 138 | if (m_client) |
| 139 | m_client->didChooseValue(value); |
| 140 | // didChooseValue might run JavaScript code, and endChooser() might be |
| 141 | // called. However DateTimeChooserCompletionImpl still has one reference to |
| 142 | // this object. |
| 143 | if (m_client) |
| 144 | m_client->didEndChooser(); |
| 145 | } |
| 146 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 147 | void ExternalDateTimeChooser::didChooseValue(double value) |
| 148 | { |
| 149 | if (m_client) |
| 150 | m_client->didChooseValue(value); |
| 151 | // didChooseValue might run JavaScript code, and endChooser() might be |
| 152 | // called. However DateTimeChooserCompletionImpl still has one reference to |
| 153 | // this object. |
| 154 | if (m_client) |
| 155 | m_client->didEndChooser(); |
| 156 | } |
| 157 | |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 158 | void ExternalDateTimeChooser::didCancelChooser() |
| 159 | { |
| 160 | if (m_client) |
| 161 | m_client->didEndChooser(); |
| 162 | } |
| 163 | |
| 164 | void ExternalDateTimeChooser::endChooser() |
| 165 | { |
| 166 | DateTimeChooserClient* client = m_client; |
| 167 | m_client = 0; |
| 168 | client->didEndChooser(); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | #endif |