 Chromium Code Reviews
 Chromium Code Reviews Issue 247663003:
  Date and Time dialog for when the clock isn't synced.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 247663003:
  Date and Time dialog for when the clock isn't synced.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: chrome/browser/ui/webui/chromeos/set_time_ui.cc | 
| diff --git a/chrome/browser/ui/webui/chromeos/set_time_ui.cc b/chrome/browser/ui/webui/chromeos/set_time_ui.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..6f066392737a57e2777b1715a4949dabff08d75d | 
| --- /dev/null | 
| +++ b/chrome/browser/ui/webui/chromeos/set_time_ui.cc | 
| @@ -0,0 +1,139 @@ | 
| +// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "chrome/browser/ui/webui/chromeos/set_time_ui.h" | 
| + | 
| +#include "ash/shell.h" | 
| +#include "ash/system/tray/system_tray_delegate.h" | 
| +#include "ash/system/user/login_status.h" | 
| +#include "base/bind.h" | 
| +#include "base/bind_helpers.h" | 
| +#include "base/build_time.h" | 
| +#include "base/values.h" | 
| +#include "chrome/browser/chromeos/settings/cros_settings.h" | 
| +#include "chrome/browser/chromeos/system/timezone_util.h" | 
| +#include "chrome/browser/profiles/profile.h" | 
| +#include "chrome/common/url_constants.h" | 
| +#include "chromeos/dbus/dbus_thread_manager.h" | 
| +#include "chromeos/dbus/system_clock_client.h" | 
| +#include "content/public/browser/web_ui.h" | 
| +#include "content/public/browser/web_ui_data_source.h" | 
| +#include "content/public/browser/web_ui_message_handler.h" | 
| +#include "grit/browser_resources.h" | 
| +#include "grit/generated_resources.h" | 
| + | 
| +namespace chromeos { | 
| + | 
| +namespace { | 
| + | 
| +const char kStringsJsFile[] = "strings.js"; | 
| +const char kSetTimeCallback[] = "setTimeInSeconds"; | 
| +const char kSetTimezoneCallback[] = "setTimezone"; | 
| 
Dan Beam
2014/04/23 17:51:45
just inline these in RegisterMessageCallback()
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + | 
| +class SetTimeMessageHandler : public content::WebUIMessageHandler, | 
| + public system::TimezoneSettings::Observer { | 
| + public: | 
| + SetTimeMessageHandler(); | 
| + virtual ~SetTimeMessageHandler(); | 
| + | 
| + // WebUIMessageHandler: | 
| + virtual void RegisterMessages() OVERRIDE; | 
| + | 
| + private: | 
| 
stevenjb
2014/04/23 21:32:04
// system::TimezoneSettings::Observer
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + virtual void TimezoneChanged(const icu::TimeZone& timezone) OVERRIDE; | 
| + void OnSetTime(const base::ListValue* args); | 
| + void OnSetTimezone(const base::ListValue* args); | 
| 
Dan Beam
2014/04/23 17:51:45
DISALLOW_COPY_AND_ASSIGN(SetTimeMessageHandler); (
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| +}; | 
| + | 
| +SetTimeMessageHandler::SetTimeMessageHandler() { | 
| 
Dan Beam
2014/04/23 17:51:45
just put the implementations right after the metho
 
michaelpg
2014/04/24 01:32:23
We try to avoid inlining most functions.
 
stevenjb
2014/04/24 17:06:23
Actually, that is something of a judgement call. W
 
michaelpg
2014/04/24 18:52:31
The C++ Do's & Dont's file has a few arguments for
 
Dan Beam
2014/04/24 21:32:23
i think it'll result in shorter, simpler code (whi
 
michaelpg
2014/04/24 22:42:47
Done.
 | 
| + system::TimezoneSettings::GetInstance()->AddObserver(this); | 
| +} | 
| + | 
| +SetTimeMessageHandler::~SetTimeMessageHandler() { | 
| + system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 
| +} | 
| + | 
| +void SetTimeMessageHandler::RegisterMessages() { | 
| + web_ui()->RegisterMessageCallback( | 
| + kSetTimeCallback, | 
| + base::Bind(&SetTimeMessageHandler::OnSetTime, | 
| + base::Unretained(this))); | 
| + web_ui()->RegisterMessageCallback( | 
| + kSetTimezoneCallback, | 
| + base::Bind(&SetTimeMessageHandler::OnSetTimezone, | 
| + base::Unretained(this))); | 
| +} | 
| + | 
| +void SetTimeMessageHandler::OnSetTime(const base::ListValue* args) { | 
| + int seconds; | 
| + if (!args->GetInteger(0, &seconds)) | 
| 
Dan Beam
2014/04/23 17:51:45
DCHECK() or CHECK() or NOTREACHED() (as this is pr
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + return; | 
| + | 
| + chromeos::DBusThreadManager::Get()->GetSystemClockClient()->SetTime(seconds); | 
| +} | 
| + | 
| +void SetTimeMessageHandler::OnSetTimezone(const base::ListValue* args) { | 
| + std::string timezone_id; | 
| + if (!args->GetString(0, &timezone_id)) | 
| + return; | 
| 
stevenjb
2014/04/23 21:32:04
NOTREACHED() instead of return
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + | 
| + CrosSettings::Get()->SetString(kSystemTimezone, timezone_id); | 
| +} | 
| + | 
| + | 
| +void SetTimeMessageHandler::TimezoneChanged(const icu::TimeZone& timezone) { | 
| + base::StringValue timezone_id( | 
| + system::TimezoneSettings::GetTimezoneID(timezone)); | 
| + web_ui()->CallJavascriptFunction( | 
| + "setTime.TimeSetter.setTimezone", timezone_id); | 
| +} | 
| + | 
| +} // namespace | 
| + | 
| +SetTimeUI::SetTimeUI(content::WebUI* web_ui) : WebDialogUI(web_ui) { | 
| + web_ui->AddMessageHandler(new SetTimeMessageHandler()); | 
| 
Dan Beam
2014/04/23 17:51:45
indent off
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + | 
| + // Set up the chrome://set-time source. | 
| + content::WebUIDataSource* source = | 
| + content::WebUIDataSource::Create(chrome::kChromeUISetTimeHost); | 
| + source->SetUseJsonJSFormatV2(); | 
| + | 
| + source->AddLocalizedString("setTimeTitle", IDS_SET_TIME_TITLE); | 
| + source->AddLocalizedString("prompt", IDS_SET_TIME_PROMPT); | 
| + source->AddLocalizedString("doneButton", IDS_SET_TIME_BUTTON_CLOSE); | 
| + source->AddLocalizedString("timezone", | 
| + IDS_OPTIONS_SETTINGS_TIMEZONE_DESCRIPTION); | 
| + | 
| + base::DictionaryValue values; | 
| + values.Set("timezoneList", chromeos::system::GetTimezoneList().release()); | 
| + | 
| + // If we are logged in, the dialog launched from the settings page. | 
| 
stevenjb
2014/04/23 21:32:04
dialog was launched
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| + // Don't show the timezone dropdown in that case. | 
| + std::string current_timezone_id; | 
| + ash::user::LoginStatus login_status = | 
| + ash::Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus(); | 
| + if (login_status == ash::user::LOGGED_IN_LOCKED || | 
| + login_status == ash::user::LOGGED_IN_NONE) { | 
| 
stevenjb
2014/04/23 21:32:04
An easier way to do this is LoginState::Get()->IsU
 
michaelpg
2014/04/24 01:32:23
I'm intentionally using the Ash status. Specifical
 
michaelpg
2014/04/24 18:52:31
OK - so we won't show the dialog at all if the scr
 | 
| + CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id); | 
| + } | 
| + values.SetString("currentTimezoneId", current_timezone_id); | 
| + | 
| + base::Time build_time = base::GetBuildTime(); | 
| + values.SetDouble("buildTime", build_time.ToJsTime()); | 
| + | 
| + source->AddLocalizedStrings(values); | 
| + source->SetJsonPath(kStringsJsFile); | 
| + | 
| + source->AddResourcePath("set_time.css", IDR_SET_TIME_CSS); | 
| + source->AddResourcePath("set_time.js", IDR_SET_TIME_JS); | 
| + source->SetDefaultResource(IDR_SET_TIME_HTML); | 
| + | 
| + Profile* profile = Profile::FromWebUI(web_ui); | 
| + content::WebUIDataSource::Add(profile, source); | 
| 
Dan Beam
2014/04/23 17:51:45
nit: just inline |profile|, e.g.
  content::WebUI
 
michaelpg
2014/04/24 01:32:23
Done.
 | 
| +} | 
| + | 
| +SetTimeUI::~SetTimeUI() { | 
| +} | 
| + | 
| +} // namespace chromeos |