Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / set_time_ui.cc
blobd4719eb4c2b2742491834785f1e98266a5576b6c
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/build_time.h"
10 #include "base/values.h"
11 #include "chrome/browser/chromeos/settings/cros_settings.h"
12 #include "chrome/browser/chromeos/system/timezone_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/system_clock_client.h"
18 #include "chromeos/login/login_state.h"
19 #include "chromeos/settings/timezone_settings.h"
20 #include "content/public/browser/web_ui.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "content/public/browser/web_ui_message_handler.h"
23 #include "grit/browser_resources.h"
25 namespace chromeos {
27 namespace {
29 class SetTimeMessageHandler : public content::WebUIMessageHandler,
30 public chromeos::SystemClockClient::Observer,
31 public system::TimezoneSettings::Observer {
32 public:
33 SetTimeMessageHandler() {
34 system::TimezoneSettings::GetInstance()->AddObserver(this);
35 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->AddObserver(
36 this);
39 ~SetTimeMessageHandler() override {
40 system::TimezoneSettings::GetInstance()->RemoveObserver(this);
41 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->RemoveObserver(
42 this);
45 // WebUIMessageHandler:
46 void RegisterMessages() override {
47 web_ui()->RegisterMessageCallback(
48 "setTimeInSeconds",
49 base::Bind(&SetTimeMessageHandler::OnSetTime, base::Unretained(this)));
50 web_ui()->RegisterMessageCallback(
51 "setTimezone",
52 base::Bind(&SetTimeMessageHandler::OnSetTimezone,
53 base::Unretained(this)));
56 private:
57 // system::SystemClockClient::Observer:
58 void SystemClockUpdated() override {
59 web_ui()->CallJavascriptFunction("settime.TimeSetter.updateTime");
62 // system::TimezoneSettings::Observer:
63 void TimezoneChanged(const icu::TimeZone& timezone) override {
64 base::StringValue timezone_id(
65 system::TimezoneSettings::GetTimezoneID(timezone));
66 web_ui()->CallJavascriptFunction("settime.TimeSetter.setTimezone",
67 timezone_id);
70 // Handler for Javascript call to set the system clock when the user sets a
71 // new time. Expects the time as the number of seconds since the Unix
72 // epoch, treated as a double.
73 void OnSetTime(const base::ListValue* args) {
74 double seconds;
75 if (!args->GetDouble(0, &seconds)) {
76 NOTREACHED();
77 return;
80 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->SetTime(
81 static_cast<int64>(seconds));
84 // Handler for Javascript call to change the system time zone when the user
85 // selects a new time zone. Expects the time zone ID as a string, as it
86 // appears in the time zone option values.
87 void OnSetTimezone(const base::ListValue* args) {
88 std::string timezone_id;
89 if (!args->GetString(0, &timezone_id)) {
90 NOTREACHED();
91 return;
94 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id);
97 DISALLOW_COPY_AND_ASSIGN(SetTimeMessageHandler);
100 } // namespace
102 SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) {
103 web_ui->AddMessageHandler(new SetTimeMessageHandler());
105 // Set up the chrome://set-time source.
106 content::WebUIDataSource* source =
107 content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost);
109 source->AddLocalizedString("setTimeTitle", IDS_SET_TIME_TITLE);
110 source->AddLocalizedString("prompt", IDS_SET_TIME_PROMPT);
111 source->AddLocalizedString("doneButton", IDS_SET_TIME_BUTTON_CLOSE);
112 source->AddLocalizedString("timezone",
113 IDS_OPTIONS_SETTINGS_TIMEZONE_DESCRIPTION);
114 source->AddLocalizedString("dateLabel", IDS_SET_TIME_DATE_LABEL);
115 source->AddLocalizedString("timeLabel", IDS_SET_TIME_TIME_LABEL);
117 base::DictionaryValue values;
118 values.Set("timezoneList", chromeos::system::GetTimezoneList().release());
120 // If we are not logged in, we need to show the time zone dropdown.
121 // Otherwise, we can leave |currentTimezoneId| blank.
122 std::string current_timezone_id;
123 if (!LoginState::Get()->IsUserLoggedIn())
124 CrosSettings::Get()->GetString(kSystemTimezone, &current_timezone_id);
125 values.SetString("currentTimezoneId", current_timezone_id);
126 values.SetDouble("buildTime", base::GetBuildTime().ToJsTime());
128 source->AddLocalizedStrings(values);
129 source->SetJsonPath("strings.js");
131 source->AddResourcePath("set_time.css", IDR_SET_TIME_CSS);
132 source->AddResourcePath("set_time.js", IDR_SET_TIME_JS);
133 source->SetDefaultResource(IDR_SET_TIME_HTML);
135 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source);
138 SetTimeUI::~SetTimeUI() {
141 } // namespace chromeos