blob: 7d987b4085e6e1ec74436b2ed536e943c631b9b6 [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]1ff427972013-02-07 21:14:079#include "content/renderer/date_time_formatter.h"
[email protected]b18583c2012-12-18 06:55:2710#include "content/renderer/render_view_impl.h"
11
[email protected]2255a9332013-06-17 05:12:3112#include "third_party/WebKit/public/web/WebDateTimeChooserCompletion.h"
13#include "third_party/WebKit/public/web/WebDateTimeChooserParams.h"
14#include "third_party/WebKit/public/web/WebDateTimeInputType.h"
[email protected]b18583c2012-12-18 06:55:2715
[email protected]b18583c2012-12-18 06:55:2716using WebKit::WebString;
17
[email protected]1ff427972013-02-07 21:14:0718namespace content {
19
[email protected]b18583c2012-12-18 06:55:2720RendererDateTimePicker::RendererDateTimePicker(
21 RenderViewImpl* sender,
22 const WebKit::WebDateTimeChooserParams& params,
23 WebKit::WebDateTimeChooserCompletion* completion)
24 : RenderViewObserver(sender),
25 chooser_params_(params),
[email protected]1ff427972013-02-07 21:14:0726 chooser_completion_(completion){
[email protected]b18583c2012-12-18 06:55:2727}
28
29RendererDateTimePicker::~RendererDateTimePicker() {
30}
31
[email protected]b18583c2012-12-18 06:55:2732bool RendererDateTimePicker::Open() {
[email protected]1ff427972013-02-07 21:14:0733 DateTimeFormatter parser(chooser_params_);
[email protected]1ff427972013-02-07 21:14:0734 ViewHostMsg_DateTimeDialogValue_Params message;
35 message.year = parser.GetYear();
36 message.month = parser.GetMonth();
37 message.day = parser.GetDay();
38 message.hour = parser.GetHour();
39 message.minute = parser.GetMinute();
40 message.second = parser.GetSecond();
41 message.dialog_type = parser.GetType();
[email protected]2deae8c42013-06-04 16:38:0842 message.minimum = chooser_params_.minimum;
43 message.maximum = chooser_params_.maximum;
[email protected]1ff427972013-02-07 21:14:0744 Send(new ViewHostMsg_OpenDateTimeDialog(routing_id(), message));
[email protected]b18583c2012-12-18 06:55:2745 return true;
46}
47
48bool RendererDateTimePicker::OnMessageReceived(
49 const IPC::Message& message) {
50 bool handled = true;
51 IPC_BEGIN_MESSAGE_MAP(RendererDateTimePicker, message)
52 IPC_MESSAGE_HANDLER(ViewMsg_ReplaceDateTime, OnReplaceDateTime)
53 IPC_MESSAGE_HANDLER(ViewMsg_CancelDateTimeDialog, OnCancel)
54 IPC_MESSAGE_UNHANDLED(handled = false)
55 IPC_END_MESSAGE_MAP()
56 return handled;
57}
58
[email protected]1ff427972013-02-07 21:14:0759void RendererDateTimePicker::OnReplaceDateTime(
60 const ViewHostMsg_DateTimeDialogValue_Params& value) {
61
62 DateTimeFormatter formatter(
63 static_cast<ui::TextInputType>(value.dialog_type),
64 value.year, value.month, value.day,
65 value.hour, value.minute, value.second);
66
[email protected]b18583c2012-12-18 06:55:2767 if (chooser_completion_)
[email protected]1ff427972013-02-07 21:14:0768 chooser_completion_->didChooseValue(WebString::fromUTF8(
69 formatter.GetFormattedValue().c_str()));
[email protected]b18583c2012-12-18 06:55:2770}
71
72void RendererDateTimePicker::OnCancel() {
73 if (chooser_completion_)
74 chooser_completion_->didCancelChooser();
75}
76
77} // namespace content