1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
// Copyright 2014 The Chromium Authors
// 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/ash/set_time/set_time_ui.h"
#include <stdint.h>
#include <memory>
#include <string>
#include "ash/public/cpp/child_accounts/parent_access_controller.h"
#include "ash/public/cpp/login_screen.h"
#include "base/build_time.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/scoped_observation.h"
#include "base/values.h"
#include "chrome/browser/ash/child_accounts/parent_access_code/parent_access_service.h"
#include "chrome/browser/ash/system/timezone_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/set_time/set_time_dialog.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/set_time_dialog_resources.h"
#include "chrome/grit/set_time_dialog_resources_map.h"
#include "chromeos/ash/components/dbus/system_clock/system_clock_client.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/timezone_settings.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/web_contents.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 "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/webui/color_change_listener/color_change_handler.h"
#include "ui/webui/mojo_web_ui_controller.h"
#include "ui/webui/webui_util.h"
namespace ash {
namespace {
class SetTimeMessageHandler : public content::WebUIMessageHandler,
public SystemClockClient::Observer,
public system::TimezoneSettings::Observer {
public:
SetTimeMessageHandler() : weak_factory_(this) {}
SetTimeMessageHandler(const SetTimeMessageHandler&) = delete;
SetTimeMessageHandler& operator=(const SetTimeMessageHandler&) = delete;
~SetTimeMessageHandler() override = default;
// WebUIMessageHandler:
void RegisterMessages() override {
web_ui()->RegisterMessageCallback(
"setTimePageReady",
base::BindRepeating(&SetTimeMessageHandler::OnPageReady,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setTimeInSeconds",
base::BindRepeating(&SetTimeMessageHandler::OnSetTime,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setTimezone",
base::BindRepeating(&SetTimeMessageHandler::OnSetTimezone,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"doneClicked", base::BindRepeating(&SetTimeMessageHandler::DoneClicked,
base::Unretained(this)));
}
void OnJavascriptAllowed() override {
clock_observation_.Observe(SystemClockClient::Get());
timezone_observation_.Observe(system::TimezoneSettings::GetInstance());
}
void OnJavascriptDisallowed() override {
clock_observation_.Reset();
timezone_observation_.Reset();
}
private:
void OnPageReady(const base::Value::List& args) { AllowJavascript(); }
// SystemClockClient::Observer:
void SystemClockUpdated() override {
FireWebUIListener("system-clock-updated");
}
// UI actually shows real device timezone, but only allows changing the user
// timezone. If user timezone settings are different from system, this means
// that user settings are overriden and must be disabled. (And we will still
// show the actual device timezone.)
// system::TimezoneSettings::Observer:
void TimezoneChanged(const icu::TimeZone& timezone) override {
base::Value timezone_id(system::TimezoneSettings::GetTimezoneID(timezone));
FireWebUIListener("system-timezone-changed", timezone_id);
}
// Handler for Javascript call to set the system clock when the user sets a
// new time. Expects the time as the number of seconds since the Unix
// epoch, treated as a double.
void OnSetTime(const base::Value::List& args) {
double seconds = args[0].GetDouble();
SystemClockClient::Get()->SetTime(static_cast<int64_t>(seconds));
}
// Handler for Javascript call to change the system time zone when the user
// selects a new time zone. Expects the time zone ID as a string, as it
// appears in the time zone option values.
void OnSetTimezone(const base::Value::List& args) {
if (args.empty() || !args[0].is_string()) {
NOTREACHED();
}
std::string timezone_id = args[0].GetString();
Profile* profile = Profile::FromWebUI(web_ui());
DCHECK(profile);
system::SetTimezoneFromUI(profile, timezone_id);
}
void DoneClicked(const base::Value::List& args) {
if (!parent_access::ParentAccessService::IsApprovalRequired(
SupervisedAction::kUpdateClock)) {
OnParentAccessValidation(true);
return;
}
double seconds = args[0].GetDouble();
AccountId account_id;
bool is_user_logged_in = user_manager::UserManager::Get()->IsUserLoggedIn();
if (is_user_logged_in) {
account_id =
user_manager::UserManager::Get()->GetActiveUser()->GetAccountId();
}
ParentAccessController::Get()->ShowWidget(
account_id,
base::BindOnce(&SetTimeMessageHandler::OnParentAccessValidation,
weak_factory_.GetWeakPtr()),
SupervisedAction::kUpdateClock, !is_user_logged_in /* extra_dimmer */,
base::Time::FromSecondsSinceUnixEpoch(seconds));
}
void OnParentAccessValidation(bool success) {
if (success) {
FireWebUIListener("validation-complete");
}
}
base::ScopedObservation<SystemClockClient, SystemClockClient::Observer>
clock_observation_{this};
base::ScopedObservation<system::TimezoneSettings,
system::TimezoneSettings::Observer>
timezone_observation_{this};
base::WeakPtrFactory<SetTimeMessageHandler> weak_factory_{this};
};
} // namespace
SetTimeUI::SetTimeUI(content::WebUI* web_ui) : MojoWebDialogUI(web_ui) {
web_ui->AddMessageHandler(std::make_unique<SetTimeMessageHandler>());
// Set up the chrome://set-time source.
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
Profile::FromWebUI(web_ui), chrome::kChromeUISetTimeHost);
webui::SetJSModuleDefaults(source);
webui::EnableTrustedTypesCSP(source);
static constexpr webui::LocalizedString kStrings[] = {
{"setTimeTitle", IDS_SET_TIME_TITLE},
{"prompt", IDS_SET_TIME_PROMPT},
{"timezoneLabel", IDS_SET_TIME_TIMEZONE_LABEL},
{"dateLabel", IDS_SET_TIME_DATE_LABEL},
{"timeLabel", IDS_SET_TIME_TIME_LABEL},
{"doneButton", IDS_DONE},
};
source->AddLocalizedStrings(kStrings);
base::Value::Dict values;
// List of list of strings: [[ID, name], [ID, name], ...]
values.Set("timezoneList", system::GetTimezoneList());
// If we are not logged in, we need to show the time zone dropdown.
values.Set("showTimezone", SetTimeDialog::ShouldShowTimezone());
std::string current_timezone_id;
CrosSettings::Get()->GetString(kSystemTimezone, ¤t_timezone_id);
values.Set("currentTimezoneId", current_timezone_id);
values.Set("buildTime", base::GetBuildTime().InMillisecondsFSinceUnixEpoch());
source->AddLocalizedStrings(values);
webui::SetupWebUIDataSource(source, kSetTimeDialogResources,
IDR_SET_TIME_DIALOG_SET_TIME_HTML);
}
SetTimeUI::~SetTimeUI() = default;
void SetTimeUI::BindInterface(
mojo::PendingReceiver<color_change_listener::mojom::PageHandler> receiver) {
color_provider_handler_ = std::make_unique<ui::ColorChangeHandler>(
web_ui()->GetWebContents(), std::move(receiver));
}
WEB_UI_CONTROLLER_TYPE_IMPL(SetTimeUI)
} // namespace ash
|