1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/renderer_date_time_picker.h"
7 #include "base/strings/string_util.h"
8 #include "content/common/view_messages.h"
9 #include "content/renderer/date_time_suggestion_builder.h"
10 #include "content/renderer/render_view_impl.h"
11 #include "third_party/WebKit/public/web/WebDateTimeChooserCompletion.h"
12 #include "third_party/WebKit/public/web/WebDateTimeChooserParams.h"
13 #include "third_party/WebKit/public/web/WebDateTimeInputType.h"
14 #include "ui/base/ime/text_input_type.h"
16 using blink::WebString
;
20 static ui::TextInputType
ToTextInputType(int type
) {
22 case blink::WebDateTimeInputTypeDate
:
23 return ui::TEXT_INPUT_TYPE_DATE
;
25 case blink::WebDateTimeInputTypeDateTime
:
26 return ui::TEXT_INPUT_TYPE_DATE_TIME
;
28 case blink::WebDateTimeInputTypeDateTimeLocal
:
29 return ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
;
31 case blink::WebDateTimeInputTypeMonth
:
32 return ui::TEXT_INPUT_TYPE_MONTH
;
34 case blink::WebDateTimeInputTypeTime
:
35 return ui::TEXT_INPUT_TYPE_TIME
;
37 case blink::WebDateTimeInputTypeWeek
:
38 return ui::TEXT_INPUT_TYPE_WEEK
;
40 case blink::WebDateTimeInputTypeNone
:
42 return ui::TEXT_INPUT_TYPE_NONE
;
46 RendererDateTimePicker::RendererDateTimePicker(
47 RenderViewImpl
* sender
,
48 const blink::WebDateTimeChooserParams
& params
,
49 blink::WebDateTimeChooserCompletion
* completion
)
50 : RenderViewObserver(sender
),
51 chooser_params_(params
),
52 chooser_completion_(completion
) {
55 RendererDateTimePicker::~RendererDateTimePicker() {
58 bool RendererDateTimePicker::Open() {
59 ViewHostMsg_DateTimeDialogValue_Params message
;
60 message
.dialog_type
= ToTextInputType(chooser_params_
.type
);
61 message
.dialog_value
= chooser_params_
.doubleValue
;
62 message
.minimum
= chooser_params_
.minimum
;
63 message
.maximum
= chooser_params_
.maximum
;
64 message
.step
= chooser_params_
.step
;
65 for (size_t i
= 0; i
< chooser_params_
.suggestions
.size(); i
++) {
66 message
.suggestions
.push_back(
67 DateTimeSuggestionBuilder::Build(chooser_params_
.suggestions
[i
]));
69 Send(new ViewHostMsg_OpenDateTimeDialog(routing_id(), message
));
73 bool RendererDateTimePicker::OnMessageReceived(
74 const IPC::Message
& message
) {
76 IPC_BEGIN_MESSAGE_MAP(RendererDateTimePicker
, message
)
77 IPC_MESSAGE_HANDLER(ViewMsg_ReplaceDateTime
, OnReplaceDateTime
)
78 IPC_MESSAGE_HANDLER(ViewMsg_CancelDateTimeDialog
, OnCancel
)
79 IPC_MESSAGE_UNHANDLED(handled
= false)
84 void RendererDateTimePicker::OnReplaceDateTime(double value
) {
85 if (chooser_completion_
)
86 chooser_completion_
->didChooseValue(value
);
87 #if defined(OS_ANDROID)
88 static_cast<RenderViewImpl
*>(render_view())->DismissDateTimeDialog();
92 void RendererDateTimePicker::OnCancel() {
93 if (chooser_completion_
)
94 chooser_completion_
->didCancelChooser();
95 #if defined(OS_ANDROID)
96 static_cast<RenderViewImpl
*>(render_view())->DismissDateTimeDialog();
100 } // namespace content