blob: 592e2491451e20687c44524d34d1436582540fe0 [file] [log] [blame]
[email protected]b18583c2012-12-18 06:55:271// 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.
4
5#include "content/renderer/renderer_date_time_picker.h"
6
[email protected]21aa99682013-06-11 07:17:017#include "base/strings/string_util.h"
[email protected]b18583c2012-12-18 06:55:278#include "content/common/view_messages.h"
[email protected]6e08bfb2013-12-07 02:56:179#include "content/renderer/date_time_suggestion_builder.h"
[email protected]b18583c2012-12-18 06:55:2710#include "content/renderer/render_view_impl.h"
[email protected]2255a9332013-06-17 05:12:3111#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"
[email protected]e8072562013-12-04 06:04:1314#include "ui/base/ime/text_input_type.h"
[email protected]b18583c2012-12-18 06:55:2715
[email protected]180ef242013-11-07 06:50:4616using blink::WebString;
[email protected]b18583c2012-12-18 06:55:2717
[email protected]1ff427972013-02-07 21:14:0718namespace content {
19
[email protected]94be4852013-12-06 07:54:4120static ui::TextInputType ToTextInputType(int type) {
21 switch (type) {
22 case blink::WebDateTimeInputTypeDate:
23 return ui::TEXT_INPUT_TYPE_DATE;
24 break;
25 case blink::WebDateTimeInputTypeDateTime:
26 return ui::TEXT_INPUT_TYPE_DATE_TIME;
27 break;
28 case blink::WebDateTimeInputTypeDateTimeLocal:
29 return ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL;
30 break;
31 case blink::WebDateTimeInputTypeMonth:
32 return ui::TEXT_INPUT_TYPE_MONTH;
33 break;
34 case blink::WebDateTimeInputTypeTime:
35 return ui::TEXT_INPUT_TYPE_TIME;
36 break;
37 case blink::WebDateTimeInputTypeWeek:
38 return ui::TEXT_INPUT_TYPE_WEEK;
39 break;
40 case blink::WebDateTimeInputTypeNone:
41 default:
42 return ui::TEXT_INPUT_TYPE_NONE;
43 }
44}
[email protected]e8072562013-12-04 06:04:1345
[email protected]b18583c2012-12-18 06:55:2746RendererDateTimePicker::RendererDateTimePicker(
47 RenderViewImpl* sender,
[email protected]180ef242013-11-07 06:50:4648 const blink::WebDateTimeChooserParams& params,
49 blink::WebDateTimeChooserCompletion* completion)
[email protected]b18583c2012-12-18 06:55:2750 : RenderViewObserver(sender),
51 chooser_params_(params),
[email protected]e8072562013-12-04 06:04:1352 chooser_completion_(completion) {
[email protected]b18583c2012-12-18 06:55:2753}
54
55RendererDateTimePicker::~RendererDateTimePicker() {
56}
57
[email protected]b18583c2012-12-18 06:55:2758bool RendererDateTimePicker::Open() {
[email protected]1ff427972013-02-07 21:14:0759 ViewHostMsg_DateTimeDialogValue_Params message;
[email protected]94be4852013-12-06 07:54:4160 message.dialog_type = ToTextInputType(chooser_params_.type);
[email protected]e8072562013-12-04 06:04:1361 message.dialog_value = chooser_params_.doubleValue;
[email protected]2deae8c42013-06-04 16:38:0862 message.minimum = chooser_params_.minimum;
63 message.maximum = chooser_params_.maximum;
[email protected]ee59cbec2013-08-16 14:59:0964 message.step = chooser_params_.step;
[email protected]6e08bfb2013-12-07 02:56:1765 for (size_t i = 0; i < chooser_params_.suggestions.size(); i++) {
66 message.suggestions.push_back(
67 DateTimeSuggestionBuilder::Build(chooser_params_.suggestions[i]));
68 }
[email protected]1ff427972013-02-07 21:14:0769 Send(new ViewHostMsg_OpenDateTimeDialog(routing_id(), message));
[email protected]b18583c2012-12-18 06:55:2770 return true;
71}
72
73bool RendererDateTimePicker::OnMessageReceived(
74 const IPC::Message& message) {
75 bool handled = true;
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)
80 IPC_END_MESSAGE_MAP()
81 return handled;
82}
83
[email protected]e8072562013-12-04 06:04:1384void RendererDateTimePicker::OnReplaceDateTime(double value) {
[email protected]b18583c2012-12-18 06:55:2785 if (chooser_completion_)
[email protected]e8072562013-12-04 06:04:1386 chooser_completion_->didChooseValue(value);
87#if defined(OS_ANDROID)
88 static_cast<RenderViewImpl*>(render_view())->DismissDateTimeDialog();
89#endif
[email protected]b18583c2012-12-18 06:55:2790}
91
92void RendererDateTimePicker::OnCancel() {
93 if (chooser_completion_)
94 chooser_completion_->didCancelChooser();
[email protected]e8072562013-12-04 06:04:1395#if defined(OS_ANDROID)
96 static_cast<RenderViewImpl*>(render_view())->DismissDateTimeDialog();
97#endif
[email protected]b18583c2012-12-18 06:55:2798}
99
100} // namespace content