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.
4
5#include "content/renderer/renderer_date_time_picker.h"
6
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"
15
16using blink::WebString;
17
18namespace content {
19
20static 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}
45
46RendererDateTimePicker::RendererDateTimePicker(
47    RenderViewImpl* sender,
48    const blink::WebDateTimeChooserParams& params,
49    blink::WebDateTimeChooserCompletion* completion)
50    : RenderViewObserver(sender),
51      chooser_params_(params),
52      chooser_completion_(completion) {
53}
54
55RendererDateTimePicker::~RendererDateTimePicker() {
56}
57
58bool 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]));
68  }
69  Send(new ViewHostMsg_OpenDateTimeDialog(routing_id(), message));
70  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
84void 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();
89#endif
90}
91
92void RendererDateTimePicker::OnCancel() {
93  if (chooser_completion_)
94    chooser_completion_->didCancelChooser();
95#if defined(OS_ANDROID)
96  static_cast<RenderViewImpl*>(render_view())->DismissDateTimeDialog();
97#endif
98}
99
100}  // namespace content
101