OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/webui/chromeos/set_time_ui.h" |
| 6 |
| 7 #include "ash/shell.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" |
| 26 |
| 27 namespace chromeos { |
| 28 |
| 29 namespace { |
| 30 |
| 31 class SetTimeMessageHandler : public content::WebUIMessageHandler, |
| 32 public chromeos::SystemClockClient::Observer, |
| 33 public system::TimezoneSettings::Observer { |
| 34 public: |
| 35 SetTimeMessageHandler() { |
| 36 system::TimezoneSettings::GetInstance()->AddObserver(this); |
| 37 }; |
| 38 |
| 39 virtual ~SetTimeMessageHandler() { |
| 40 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
| 41 } |
| 42 |
| 43 // WebUIMessageHandler: |
| 44 virtual void RegisterMessages() OVERRIDE { |
| 45 web_ui()->RegisterMessageCallback( |
| 46 "setTimeInSeconds", |
| 47 base::Bind(&SetTimeMessageHandler::OnSetTime, base::Unretained(this))); |
| 48 web_ui()->RegisterMessageCallback( |
| 49 "setTimezone", |
| 50 base::Bind(&SetTimeMessageHandler::OnSetTimezone, |
| 51 base::Unretained(this))); |
| 52 } |
| 53 |
| 54 private: |
| 55 // system::SystemClockClient::Observer: |
| 56 virtual void SystemClockUpdated() OVERRIDE { |
| 57 web_ui()->CallJavascriptFunction("settime.TimeSetter.updateTime"); |
| 58 } |
| 59 |
| 60 // system::TimezoneSettings::Observer: |
| 61 virtual void TimezoneChanged(const icu::TimeZone& timezone) OVERRIDE { |
| 62 base::StringValue timezone_id( |
| 63 system::TimezoneSettings::GetTimezoneID(timezone)); |
| 64 web_ui()->CallJavascriptFunction("settime.TimeSetter.setTimezone", |
| 65 timezone_id); |
| 66 } |
| 67 |
| 68 // Handler for Javascript call to set the system clock when the user sets a |
| 69 // new time. Expects the time as the number of seconds since the Unix |
| 70 // epoch, treated as a double. |
| 71 void OnSetTime(const base::ListValue* args) { |
| 72 double seconds; |
| 73 if (!args->GetDouble(0, &seconds)) { |
| 74 NOTREACHED(); |
| 75 return; |
| 76 } |
| 77 |
| 78 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->SetTime( |
| 79 static_cast<int64>(seconds)); |
| 80 } |
| 81 |
| 82 // Handler for Javascript call to change the system time zone when the user |
| 83 // selects a new time zone. Expects the time zone ID as a string, as it |
| 84 // appears in the time zone option values. |
| 85 void OnSetTimezone(const base::ListValue* args) { |
| 86 std::string timezone_id; |
| 87 if (!args->GetString(0, &timezone_id)) { |
| 88 NOTREACHED(); |
| 89 return; |
| 90 } |
| 91 |
| 92 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id); |
| 93 } |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(SetTimeMessageHandler); |
| 96 }; |
| 97 |
| 98 } // namespace |
| 99 |
| 100 SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) { |
| 101 web_ui->AddMessageHandler(new SetTimeMessageHandler()); |
| 102 |
| 103 // Set up the chrome://set-time source. |
| 104 content::WebUIDataSource* source = |
| 105 content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost); |
| 106 source->SetUseJsonJSFormatV2(); |
| 107 |
| 108 source->AddLocalizedString("setTimeTitle", IDS_SET_TIME_TITLE); |
| 109 source->AddLocalizedString("prompt", IDS_SET_TIME_PROMPT); |
| 110 source->AddLocalizedString("doneButton", IDS_SET_TIME_BUTTON_CLOSE); |
| 111 source->AddLocalizedString("timezone", |
| 112 IDS_OPTIONS_SETTINGS_TIMEZONE_DESCRIPTION); |
| 113 |
| 114 base::DictionaryValue values; |
| 115 values.Set("timezoneList", chromeos::system::GetTimezoneList().release()); |
| 116 |
| 117 // If we are not logged in, we need to show the time zone dropdown. |
| 118 // Otherwise, we can leave |currentTimezoneId| blank. |
| 119 std::string current_timezone_id; |
| 120 if (ash::Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus() == |
| 121 ash::user::LOGGED_IN_NONE) { |
| 122 CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id); |
| 123 } |
| 124 values.SetString("currentTimezoneId", current_timezone_id); |
| 125 values.SetDouble("buildTime", base::GetBuildTime().ToJsTime()); |
| 126 |
| 127 source->AddLocalizedStrings(values); |
| 128 source->SetJsonPath("strings.js"); |
| 129 |
| 130 source->AddResourcePath("set_time.css", IDR_SET_TIME_CSS); |
| 131 source->AddResourcePath("set_time.js", IDR_SET_TIME_JS); |
| 132 source->SetDefaultResource(IDR_SET_TIME_HTML); |
| 133 |
| 134 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source); |
| 135 } |
| 136 |
| 137 SetTimeUI::~SetTimeUI() { |
| 138 } |
| 139 |
| 140 } // namespace chromeos |
OLD | NEW |