summaryrefslogtreecommitdiffstats
path: root/content/renderer/renderer_date_time_picker.cc
blob: 47d22f7b56f55a8d7068ed06bfa61edd2b749e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/renderer_date_time_picker.h"

#include "base/strings/string_util.h"
#include "content/common/view_messages.h"
#include "content/renderer/date_time_formatter.h"
#include "content/renderer/render_view_impl.h"

#include "third_party/WebKit/public/web/WebDateTimeChooserCompletion.h"
#include "third_party/WebKit/public/web/WebDateTimeChooserParams.h"
#include "third_party/WebKit/public/web/WebDateTimeInputType.h"

using WebKit::WebString;

namespace content {

RendererDateTimePicker::RendererDateTimePicker(
    RenderViewImpl* sender,
    const WebKit::WebDateTimeChooserParams& params,
    WebKit::WebDateTimeChooserCompletion* completion)
    : RenderViewObserver(sender),
      chooser_params_(params),
      chooser_completion_(completion){
}

RendererDateTimePicker::~RendererDateTimePicker() {
}

bool RendererDateTimePicker::Open() {
  DateTimeFormatter parser(chooser_params_);
  ViewHostMsg_DateTimeDialogValue_Params message;
  message.dialog_type = parser.GetType();
  if (message.dialog_type == ui::TEXT_INPUT_TYPE_WEEK) {
    message.year = parser.GetWeekYear();
    message.week = parser.GetWeek();
  } else {
    message.year = parser.GetYear();
    message.month = parser.GetMonth();
    message.day = parser.GetDay();
    message.hour = parser.GetHour();
    message.minute = parser.GetMinute();
    message.second = parser.GetSecond();
  }
  message.minimum = chooser_params_.minimum;
  message.maximum = chooser_params_.maximum;
  Send(new ViewHostMsg_OpenDateTimeDialog(routing_id(), message));
  return true;
}

bool RendererDateTimePicker::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(RendererDateTimePicker, message)
    IPC_MESSAGE_HANDLER(ViewMsg_ReplaceDateTime, OnReplaceDateTime)
    IPC_MESSAGE_HANDLER(ViewMsg_CancelDateTimeDialog, OnCancel)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void RendererDateTimePicker::OnReplaceDateTime(
    const ViewHostMsg_DateTimeDialogValue_Params& value) {

  DateTimeFormatter formatter(
      static_cast<ui::TextInputType>(value.dialog_type),
      value.year, value.month, value.day,
      value.hour, value.minute, value.second, value.year, value.week);

  if (chooser_completion_)
    chooser_completion_->didChooseValue(WebString::fromUTF8(
        formatter.GetFormattedValue().c_str()));
}

void RendererDateTimePicker::OnCancel() {
  if (chooser_completion_)
    chooser_completion_->didCancelChooser();
}

}  // namespace content