1 // Copyright 2014 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 "chrome/browser/ui/webui/chromeos/set_time_ui.h"
8 #include "ash/system/tray/system_tray_delegate.h"
9 #include "ash/system/user/login_status.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/build_time.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/chromeos/system/timezone_util.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/url_constants.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/system_clock_client.h"
20 #include "chromeos/settings/timezone_settings.h"
21 #include "content/public/browser/web_ui.h"
22 #include "content/public/browser/web_ui_data_source.h"
23 #include "content/public/browser/web_ui_message_handler.h"
24 #include "grit/browser_resources.h"
25 #include "grit/generated_resources.h"
31 class SetTimeMessageHandler : public content::WebUIMessageHandler,
32 public chromeos::SystemClockClient::Observer,
33 public system::TimezoneSettings::Observer {
35 SetTimeMessageHandler() {
36 system::TimezoneSettings::GetInstance()->AddObserver(this);
37 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->AddObserver(
41 virtual ~SetTimeMessageHandler() {
42 system::TimezoneSettings::GetInstance()->RemoveObserver(this);
43 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->RemoveObserver(
47 // WebUIMessageHandler:
48 virtual void RegisterMessages() OVERRIDE {
49 web_ui()->RegisterMessageCallback(
51 base::Bind(&SetTimeMessageHandler::OnSetTime, base::Unretained(this)));
52 web_ui()->RegisterMessageCallback(
54 base::Bind(&SetTimeMessageHandler::OnSetTimezone,
55 base::Unretained(this)));
59 // system::SystemClockClient::Observer:
60 virtual void SystemClockUpdated() OVERRIDE {
61 web_ui()->CallJavascriptFunction("settime.TimeSetter.updateTime");
64 // system::TimezoneSettings::Observer:
65 virtual void TimezoneChanged(const icu::TimeZone& timezone) OVERRIDE {
66 base::StringValue timezone_id(
67 system::TimezoneSettings::GetTimezoneID(timezone));
68 web_ui()->CallJavascriptFunction("settime.TimeSetter.setTimezone",
72 // Handler for Javascript call to set the system clock when the user sets a
73 // new time. Expects the time as the number of seconds since the Unix
74 // epoch, treated as a double.
75 void OnSetTime(const base::ListValue* args) {
77 if (!args->GetDouble(0, &seconds)) {
82 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->SetTime(
83 static_cast<int64>(seconds));
86 // Handler for Javascript call to change the system time zone when the user
87 // selects a new time zone. Expects the time zone ID as a string, as it
88 // appears in the time zone option values.
89 void OnSetTimezone(const base::ListValue* args) {
90 std::string timezone_id;
91 if (!args->GetString(0, &timezone_id)) {
96 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id);
99 DISALLOW_COPY_AND_ASSIGN(SetTimeMessageHandler);
104 SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) {
105 web_ui->AddMessageHandler(new SetTimeMessageHandler());
107 // Set up the chrome://set-time source.
108 content::WebUIDataSource* source =
109 content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost);
110 source->SetUseJsonJSFormatV2();
112 source->AddLocalizedString("setTimeTitle", IDS_SET_TIME_TITLE);
113 source->AddLocalizedString("prompt", IDS_SET_TIME_PROMPT);
114 source->AddLocalizedString("doneButton", IDS_SET_TIME_BUTTON_CLOSE);
115 source->AddLocalizedString("timezone",
116 IDS_OPTIONS_SETTINGS_TIMEZONE_DESCRIPTION);
117 source->AddLocalizedString("dateLabel", IDS_SET_TIME_DATE_LABEL);
118 source->AddLocalizedString("timeLabel", IDS_SET_TIME_TIME_LABEL);
120 base::DictionaryValue values;
121 values.Set("timezoneList", chromeos::system::GetTimezoneList().release());
123 // If we are not logged in, we need to show the time zone dropdown.
124 // Otherwise, we can leave |currentTimezoneId| blank.
125 std::string current_timezone_id;
126 if (ash::Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus() ==
127 ash::user::LOGGED_IN_NONE) {
128 CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id);
130 values.SetString("currentTimezoneId", current_timezone_id);
131 values.SetDouble("buildTime", base::GetBuildTime().ToJsTime());
133 source->AddLocalizedStrings(values);
134 source->SetJsonPath("strings.js");
136 source->AddResourcePath("set_time.css", IDR_SET_TIME_CSS);
137 source->AddResourcePath("set_time.js", IDR_SET_TIME_JS);
138 source->SetDefaultResource(IDR_SET_TIME_HTML);
140 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
143 SetTimeUI::~SetTimeUI() {
146 } // namespace chromeos