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 "chrome/grit/generated_resources.h" 19#include "chromeos/dbus/dbus_thread_manager.h" 20#include "chromeos/dbus/system_clock_client.h" 21#include "chromeos/settings/timezone_settings.h" 22#include "content/public/browser/web_ui.h" 23#include "content/public/browser/web_ui_data_source.h" 24#include "content/public/browser/web_ui_message_handler.h" 25#include "grit/browser_resources.h" 26 27namespace chromeos { 28 29namespace { 30 31class 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 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->AddObserver( 38 this); 39 } 40 41 virtual ~SetTimeMessageHandler() { 42 system::TimezoneSettings::GetInstance()->RemoveObserver(this); 43 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->RemoveObserver( 44 this); 45 } 46 47 // WebUIMessageHandler: 48 virtual void RegisterMessages() OVERRIDE { 49 web_ui()->RegisterMessageCallback( 50 "setTimeInSeconds", 51 base::Bind(&SetTimeMessageHandler::OnSetTime, base::Unretained(this))); 52 web_ui()->RegisterMessageCallback( 53 "setTimezone", 54 base::Bind(&SetTimeMessageHandler::OnSetTimezone, 55 base::Unretained(this))); 56 } 57 58 private: 59 // system::SystemClockClient::Observer: 60 virtual void SystemClockUpdated() OVERRIDE { 61 web_ui()->CallJavascriptFunction("settime.TimeSetter.updateTime"); 62 } 63 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", 69 timezone_id); 70 } 71 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) { 76 double seconds; 77 if (!args->GetDouble(0, &seconds)) { 78 NOTREACHED(); 79 return; 80 } 81 82 chromeos::DBusThreadManager::Get()->GetSystemClockClient()->SetTime( 83 static_cast<int64>(seconds)); 84 } 85 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)) { 92 NOTREACHED(); 93 return; 94 } 95 96 CrosSettings::Get()->SetString(kSystemTimezone, timezone_id); 97 } 98 99 DISALLOW_COPY_AND_ASSIGN(SetTimeMessageHandler); 100}; 101 102} // namespace 103 104SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) { 105 web_ui->AddMessageHandler(new SetTimeMessageHandler()); 106 107 // Set up the chrome://set-time source. 108 content::WebUIDataSource* source = 109 content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost); 110 source->SetUseJsonJSFormatV2(); 111 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); 119 120 base::DictionaryValue values; 121 values.Set("timezoneList", chromeos::system::GetTimezoneList().release()); 122 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); 129 } 130 values.SetString("currentTimezoneId", current_timezone_id); 131 values.SetDouble("buildTime", base::GetBuildTime().ToJsTime()); 132 133 source->AddLocalizedStrings(values); 134 source->SetJsonPath("strings.js"); 135 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); 139 140 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), source); 141} 142 143SetTimeUI::~SetTimeUI() { 144} 145 146} // namespace chromeos 147