Commit d8839a96 authored by Peter Kvitek's avatar Peter Kvitek Committed by Commit Bot

[DevTools] Implemented timezone id verification for Emulation

setTimezoneOverride API.

Previously Emulation.SetTimezoneOverride API if provided with invalid
timezone id, would quietly set ICU's predefined unknown timezone id
"Etc/Unknown". Apparently this is not what most users want, so the API
implementation was changed to detect this situation and return negative
condition, leaving currently selected timezone intact.


Bug: 1007380
Change-Id: Idbec5e2f630c409a25bb7a059b8729ff320640a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1822557
Commit-Queue: Peter Kvitek <kvitekp@chromium.org>
Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Reviewed-by: default avatarJohannes Henkel <johannes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699545}
parent 1c584a43
...@@ -463,8 +463,11 @@ Response InspectorEmulationAgent::setTimezoneOverride( ...@@ -463,8 +463,11 @@ Response InspectorEmulationAgent::setTimezoneOverride(
timezone_override_.reset(); timezone_override_.reset();
if (!timezone_id.IsEmpty()) { if (!timezone_id.IsEmpty()) {
timezone_override_ = TimeZoneController::SetTimeZoneOverride(timezone_id); timezone_override_ = TimeZoneController::SetTimeZoneOverride(timezone_id);
if (!timezone_override_) if (!timezone_override_) {
return Response::Error("Timezone override is already in effect"); return TimeZoneController::HasTimeZoneOverride()
? Response::Error("Timezone override is already in effect")
: Response::InvalidParams("Invalid timezone id");
}
} }
timezone_id_override_.Set(timezone_id); timezone_id_override_.Set(timezone_id);
......
...@@ -35,31 +35,41 @@ void NotifyTimezoneChangeOnWorkerThread(WorkerThread* worker_thread) { ...@@ -35,31 +35,41 @@ void NotifyTimezoneChangeOnWorkerThread(WorkerThread* worker_thread) {
NotifyTimezoneChangeToV8(worker_thread->GlobalScope()->GetIsolate()); NotifyTimezoneChangeToV8(worker_thread->GlobalScope()->GetIsolate());
} }
void SetIcuTimeZoneAndNotifyV8(const String& timezone_id) { String GetTimezoneId(const icu::TimeZone& timezone) {
icu::UnicodeString unicode_timezone_id;
timezone.getID(unicode_timezone_id);
return String(icu::toUCharPtr(unicode_timezone_id.getBuffer()),
static_cast<unsigned>(unicode_timezone_id.length()));
}
String GetCurrentTimezoneId() {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createDefault());
CHECK(timezone);
return GetTimezoneId(*timezone.get());
}
bool SetIcuTimeZoneAndNotifyV8(const String& timezone_id) {
DCHECK(!timezone_id.IsEmpty()); DCHECK(!timezone_id.IsEmpty());
DCHECK(timezone_id.ContainsOnlyASCIIOrEmpty()); std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createTimeZone(
icu::TimeZone* timezone = icu::TimeZone::createTimeZone( icu::UnicodeString(timezone_id.Ascii().data(), -1, US_INV)));
icu::UnicodeString(timezone_id.Ascii().data(), -1, US_INV)); CHECK(timezone);
icu::TimeZone::adoptDefault(timezone);
if (*timezone == icu::TimeZone::getUnknown())
return false;
icu::TimeZone::adoptDefault(timezone.release());
NotifyTimezoneChangeToV8(V8PerIsolateData::MainThreadIsolate()); NotifyTimezoneChangeToV8(V8PerIsolateData::MainThreadIsolate());
WorkerThread::CallOnAllWorkerThreads(&NotifyTimezoneChangeOnWorkerThread, WorkerThread::CallOnAllWorkerThreads(&NotifyTimezoneChangeOnWorkerThread,
TaskType::kInternalDefault); TaskType::kInternalDefault);
} return true;
String GetCurrentTimezone() {
std::unique_ptr<icu::TimeZone> timezone(icu::TimeZone::createDefault());
icu::UnicodeString unicode_timezone_id;
timezone->getID(unicode_timezone_id);
return String(icu::toUCharPtr(unicode_timezone_id.getBuffer()),
static_cast<unsigned>(unicode_timezone_id.length()));
} }
} // namespace } // namespace
TimeZoneController::TimeZoneController() { TimeZoneController::TimeZoneController() {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
host_timezone_id_ = GetCurrentTimezone(); host_timezone_id_ = GetCurrentTimezoneId();
} }
TimeZoneController::~TimeZoneController() = default; TimeZoneController::~TimeZoneController() = default;
...@@ -93,13 +103,21 @@ TimeZoneController::SetTimeZoneOverride(const String& timezone_id) { ...@@ -93,13 +103,21 @@ TimeZoneController::SetTimeZoneOverride(const String& timezone_id) {
return nullptr; return nullptr;
} }
SetIcuTimeZoneAndNotifyV8(timezone_id); if (!SetIcuTimeZoneAndNotifyV8(timezone_id)) {
VLOG(1) << "Invalid override timezone id: " << timezone_id;
return nullptr;
}
instance().has_timezone_id_override_ = true; instance().has_timezone_id_override_ = true;
return std::unique_ptr<TimeZoneOverride>(new TimeZoneOverride()); return std::unique_ptr<TimeZoneOverride>(new TimeZoneOverride());
} }
// static
bool TimeZoneController::HasTimeZoneOverride() {
return instance().has_timezone_id_override_;
}
// static // static
void TimeZoneController::ClearTimeZoneOverride() { void TimeZoneController::ClearTimeZoneOverride() {
DCHECK(instance().has_timezone_id_override_); DCHECK(instance().has_timezone_id_override_);
......
...@@ -41,6 +41,8 @@ class CORE_EXPORT TimeZoneController final ...@@ -41,6 +41,8 @@ class CORE_EXPORT TimeZoneController final
static std::unique_ptr<TimeZoneOverride> SetTimeZoneOverride( static std::unique_ptr<TimeZoneOverride> SetTimeZoneOverride(
const String& timezone_id); const String& timezone_id);
static bool HasTimeZoneOverride();
private: private:
TimeZoneController(); TimeZoneController();
static TimeZoneController& instance(); static TimeZoneController& instance();
......
Tests invalid timezone override handling.
{
code : -32602
message : Invalid timezone id
}
(async function(testRunner) {
const {page, session, dp} = await testRunner.startBlank(
'Tests invalid timezone override handling.');
async function setTimezoneOverride(timezoneId) {
const result = await dp.Emulation.setTimezoneOverride({ timezoneId });
return result.error;
}
testRunner.log(await setTimezoneOverride(`Foo/Bar`));
testRunner.completeTest();
})
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment