Skip to content

Commit

Permalink
Deprecate SbTime APIs (partial)
Browse files Browse the repository at this point in the history
- Add emulation of POSIX APIs for Windows platforms
- Provide common helper functions to simplify getting usec timestamps
- Replace usages of SbTimeGetNow and SbTimeGetMonotonicNow in `cobalt`

b/302733082

Test-On-Device: true
  • Loading branch information
gbournou committed Dec 15, 2023
1 parent 495a810 commit d5fea29
Show file tree
Hide file tree
Showing 137 changed files with 1,154 additions and 590 deletions.
3 changes: 2 additions & 1 deletion base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ int64_t FileEnumerator::FileInfo::GetSize() const {
}

base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const {
return base::Time::FromSbTime(sb_info_.last_modified);
return base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(sb_info_.last_modified));
}

// FileEnumerator --------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions base/files/file_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,12 @@ bool File::GetInfo(Info* info) {
info->is_directory = file_info.is_directory;
info->is_symbolic_link = file_info.is_symbolic_link;
info->size = file_info.size;
info->last_modified = base::Time::FromSbTime(file_info.last_modified);
info->last_accessed = base::Time::FromSbTime(file_info.last_accessed);
info->creation_time = base::Time::FromSbTime(file_info.creation_time);
info->last_modified = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(file_info.last_modified));
info->last_accessed = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(file_info.last_accessed));
info->creation_time = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(file_info.creation_time));
return true;
}

Expand Down
9 changes: 6 additions & 3 deletions base/files/file_util_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,12 @@ bool GetFileInfo(const FilePath &file_path, File::Info *results) {

results->is_directory = info.is_directory;
results->size = info.size;
results->last_modified = base::Time::FromSbTime(info.last_modified);
results->last_accessed = base::Time::FromSbTime(info.last_accessed);
results->creation_time = base::Time::FromSbTime(info.creation_time);
results->last_modified = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(info.last_modified));
results->last_accessed = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(info.last_accessed));
results->creation_time = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(info.creation_time));
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include "starboard/client_porting/eztime/eztime.h"
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/time.h"
#include "starboard/configuration.h"
#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/system.h"
#include "starboard/time.h"
typedef SbFile FileHandle;
typedef SbMutex MutexHandle;
#else
Expand Down Expand Up @@ -210,7 +210,7 @@ int32_t CurrentProcessId() {

uint64_t TickCount() {
#if defined(STARBOARD)
return static_cast<uint64_t>(SbTimeGetMonotonicNow());
return starboard::CurrentMonotonicTime();
#else
#if defined(OS_WIN)
return GetTickCount();
Expand Down
2 changes: 1 addition & 1 deletion base/message_loop/message_pump_io_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void MessagePumpIOStarboard::Run(Delegate* delegate) {
} else {
TimeDelta delay = delayed_work_time_ - TimeTicks::Now();
if (delay > TimeDelta()) {
SbSocketWaiterWaitTimed(waiter_, delay.ToSbTime());
SbSocketWaiterWaitTimed(waiter_, delay.InMicroseconds());
} else {
// It looks like delayed_work_time_ indicates a time in the past, so we
// need to call DoDelayedWork now.
Expand Down
2 changes: 1 addition & 1 deletion base/message_loop/message_pump_ui_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void MessagePumpUIStarboard::ScheduleDelayedWork(
CancelDelayedLocked();

outstanding_delayed_events_.insert(
SbEventSchedule(&CallMessagePumpDelayed, this, delay.ToSbTime()));
SbEventSchedule(&CallMessagePumpDelayed, this, delay.InMicroseconds()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion base/synchronization/condition_variable_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ConditionVariable::Wait() {
void ConditionVariable::TimedWait(const TimeDelta& max_time) {
internal::ScopedBlockingCallWithBaseSyncPrimitives scoped_blocking_call(
BlockingType::MAY_BLOCK);
SbTime duration = max_time.ToSbTime();
int64_t duration = max_time.InMicroseconds();

#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
user_lock_->CheckHeldAndUnmark();
Expand Down
2 changes: 1 addition & 1 deletion base/threading/platform_thread_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void PlatformThread::YieldCurrentThread() {

// static
void PlatformThread::Sleep(TimeDelta duration) {
SbThreadSleep(duration.ToSbTime());
SbThreadSleep(duration.InMicroseconds());
}

// static
Expand Down
9 changes: 0 additions & 9 deletions base/time/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ class BASE_EXPORT TimeDelta {
return delta_ == std::numeric_limits<int64_t>::min();
}

#if defined(STARBOARD)
SbTime ToSbTime() const;
#endif

#if defined(OS_POSIX) || defined(OS_FUCHSIA)
struct timespec ToTimeSpec() const;
#endif
Expand Down Expand Up @@ -578,11 +574,6 @@ class BASE_EXPORT Time : public time_internal::TimeBase<Time> {
static Time FromJsTime(double ms_since_epoch);
double ToJsTime() const;

#if defined(STARBOARD)
static Time FromSbTime(SbTime t);
SbTime ToSbTime() const;
#endif

// Converts to/from Java convention for times, a number of milliseconds since
// the epoch. Because the Java format has less resolution, converting to Java
// time is a lossy operation.
Expand Down
7 changes: 5 additions & 2 deletions base/time/time_now_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "starboard/client_porting/poem/eztime_poem.h"
#include "starboard/common/log.h"
#include "starboard/common/time.h"
#include "starboard/time.h"
#include "starboard/types.h"

Expand All @@ -30,7 +31,8 @@ namespace base {

namespace subtle {
Time TimeNowIgnoringOverride() {
return Time() + TimeDelta::FromMicroseconds(SbTimeGetNow());
return Time() + TimeDelta::FromMicroseconds(
starboard::PosixTimeToWindowsTime(starboard::CurrentPosixTime()));
}

Time TimeNowFromSystemTimeIgnoringOverride() {
Expand All @@ -43,7 +45,8 @@ Time TimeNowFromSystemTimeIgnoringOverride() {

namespace subtle {
TimeTicks TimeTicksNowIgnoringOverride() {
return TimeTicks() + TimeDelta::FromMicroseconds(SbTimeGetMonotonicNow());
return TimeTicks() + TimeDelta::FromMicroseconds(
starboard::CurrentMonotonicTime());
}
} // namespace subtle

Expand Down
22 changes: 7 additions & 15 deletions base/time/time_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ EzTimeZone GetTz(bool is_local) {
}
} // namespace

SbTime TimeDelta::ToSbTime() const {
return InMicroseconds();
}

void Time::Explode(bool is_local, Exploded *exploded) const {
EzTimeValue value = EzTimeValueFromSbTime(ToSbTime());
EzTimeValue value = EzTimeValueFromSbTime(us_);
EzTimeExploded ez_exploded;
int millisecond;
bool result = EzTimeValueExplode(&value, GetTz(is_local), &ez_exploded,
Expand Down Expand Up @@ -60,7 +56,12 @@ bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
ez_exploded.tm_isdst = -1;
EzTimeValue value = EzTimeValueImplode(&ez_exploded, exploded.millisecond,
GetTz(is_local));
base::Time converted_time(Time::FromSbTime(EzTimeValueToSbTime(&value)));
int64_t posix_microseconds = (value.tv_sec * Time::kMicrosecondsPerSecond) +
value.tv_usec;
int64_t windows_microseconds = posix_microseconds +
Time::kTimeTToMicrosecondsOffset;
base::Time converted_time = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(windows_microseconds));

// If |exploded.day_of_month| is set to 31 on a 28-30 day month, it will
// return the first day of the next month. Thus round-trip the time and
Expand All @@ -81,15 +82,6 @@ bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
return false;
}

// static
Time Time::FromSbTime(SbTime t) {
return Time(t);
}

SbTime Time::ToSbTime() const {
return us_;
}

// static
bool TimeTicks::IsHighResolution() {
return true;
Expand Down
2 changes: 1 addition & 1 deletion cobalt/base/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ inline int64_t DefaultSampleToValueFunc(int64_t dividend, int64_t divisor) {
// by `SampleToValueFunc` using its dividend and divisor.
//
// Example usages:
// 1. Set dividends to bytes in int and divisors to SbTime, to track the
// 1. Set dividends to bytes in int and divisors to int64_t, to track the
// statistics of bandwidth.
// 2. Set the divisor to always be 1, to track the statistics of a count or a
// duration.
Expand Down
5 changes: 2 additions & 3 deletions cobalt/base/wrap_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef int (*MainFunction)(int argc, char** argv);
// A start-style function.
typedef void (*StartFunction)(int argc, char** argv, const char* link,
const base::Closure& quit_closure,
SbTimeMonotonic timestamp);
int64_t timestamp);

// A function type that can be called at shutdown.
typedef void (*StopFunction)();
Expand All @@ -53,8 +53,7 @@ typedef void (*EventFunction)(const SbEvent* event);
// No-operation function that can be passed into start_function if no start work
// is needed.
void NoopStartFunction(int argc, char** argv, const char* link,
const base::Closure& quit_closure,
SbTimeMonotonic timestamp) {}
const base::Closure& quit_closure, int64_t timestamp) {}

// No-operation function that can be passed into event_function if no other
// event handling work is needed.
Expand Down
3 changes: 1 addition & 2 deletions cobalt/bindings/testing/bindings_sandbox_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ namespace {
cobalt::script::StandaloneJavascriptRunner* g_javascript_runner = NULL;

void StartApplication(int argc, char** argv, const char* link,
const base::Closure& quit_closure,
SbTimeMonotonic timestamp) {
const base::Closure& quit_closure, int64_t timestamp) {
scoped_refptr<Window> test_window = new Window();
cobalt::script::JavaScriptEngine::Options javascript_engine_options;

Expand Down
19 changes: 10 additions & 9 deletions cobalt/bindings/testing/date_bindings_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cobalt/bindings/testing/bindings_test_base.h"
#include "cobalt/bindings/testing/interface_with_date.h"
#include "starboard/client_porting/eztime/eztime.h"
#include "starboard/common/time.h"
#include "starboard/time_zone.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -77,7 +78,8 @@ TEST_F(DateBindingsTest, PosixEpoch) {

EvaluateScript("Date.now();", &result);
auto js_now_ms = std::stoll(result);
auto posix_now_ms = SbTimeToPosix(SbTimeGetNow()) / kSbTimeMillisecond;
auto posix_now_ms =
starboard::CurrentPosixTime() / base::Time::kMicrosecondsPerMillisecond;
EXPECT_LT(std::abs(posix_now_ms - js_now_ms), 1000);
}

Expand All @@ -104,15 +106,14 @@ TEST_F(DateBindingsTest, StarboardTimeZone) {
}

TEST_F(DateBindingsTest, TimezoneOffset) {
EzTimeExploded ez_exploded;

auto eztt = EzTimeTFromSbTime(SbTimeGetNow());
EzTimeTExplodeLocal(&eztt, &ez_exploded);
// ez_exploded is already local time, use UTC method to convert to
EzTimeT ezttnow = static_cast<EzTimeT>(starboard::CurrentPosixTime() /
base::Time::kMicrosecondsPerSecond);
EzTimeExploded ez_exploded_local;
EzTimeTExplodeLocal(&ezttnow, &ez_exploded_local);
// ez_exploded_local is already local time, use UTC method to convert to
// EzTimeT.
EzTimeT local_time_minutes = EzTimeTImplodeUTC(&ez_exploded) / 60;
EzTimeT utc_minutes = EzTimeTFromSbTime(SbTimeGetNow()) / 60;
EzTimeT timezone_offset = utc_minutes - local_time_minutes;
EzTimeT local_time_minutes = EzTimeTImplodeUTC(&ez_exploded_local) / 60;
EzTimeT utc_minutes = ezttnow / 60;

std::string result;
EvaluateScript("new Date().getTimezoneOffset();", &result);
Expand Down
12 changes: 5 additions & 7 deletions cobalt/browser/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
#include "starboard/extension/crash_handler.h"
#include "starboard/extension/installation_manager.h"
#include "starboard/system.h"
#include "starboard/time.h"
#include "url/gurl.h"

#if SB_IS(EVERGREEN)
Expand Down Expand Up @@ -626,7 +625,7 @@ ssize_t Application::available_memory_ = 0;
int64 Application::lifetime_in_ms_ = 0;

Application::Application(const base::Closure& quit_closure, bool should_preload,
SbTimeMonotonic timestamp)
int64_t timestamp)
: message_loop_(base::MessageLoop::current()), quit_closure_(quit_closure) {
DCHECK(!quit_closure_.is_null());
if (should_preload) {
Expand Down Expand Up @@ -1066,7 +1065,7 @@ Application::~Application() {
network_module_.reset();
}

void Application::Start(SbTimeMonotonic timestamp) {
void Application::Start(int64_t timestamp) {
if (base::MessageLoop::current() != message_loop_) {
message_loop_->task_runner()->PostTask(
FROM_HERE,
Expand Down Expand Up @@ -1182,7 +1181,7 @@ void Application::HandleStarboardEvent(const SbEvent* starboard_event) {
}

void Application::OnApplicationEvent(SbEventType event_type,
SbTimeMonotonic timestamp) {
int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "Application::OnApplicationEvent()");
DCHECK_CALLED_ON_VALID_THREAD(application_event_thread_checker_);

Expand Down Expand Up @@ -1462,8 +1461,7 @@ void Application::OnDeepLinkConsumedCallback(const std::string& link) {
}
}

void Application::DispatchDeepLink(const char* link,
SbTimeMonotonic timestamp) {
void Application::DispatchDeepLink(const char* link, int64_t timestamp) {
if (!link || *link == 0) {
return;
}
Expand Down Expand Up @@ -1491,7 +1489,7 @@ void Application::DispatchDeepLink(const char* link,

void Application::DispatchDeepLinkIfNotConsumed() {
std::string deep_link;
SbTimeMonotonic timestamp;
int64_t timestamp;
// This block exists to ensure that the lock is held while accessing
// unconsumed_deep_link_.
{
Expand Down
15 changes: 7 additions & 8 deletions cobalt/browser/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "cobalt/network/network_module.h"
#include "cobalt/persistent_storage/persistent_settings.h"
#include "cobalt/system_window/system_window.h"
#include "starboard/time.h"
#if SB_IS(EVERGREEN)
#include "cobalt/updater/updater_module.h"
#endif
Expand All @@ -55,11 +54,11 @@ class Application {
// The passed in |quit_closure| can be called internally by the Application
// to signal that it would like to quit.
Application(const base::Closure& quit_closure, bool should_preload,
SbTimeMonotonic timestamp);
int64_t timestamp);
virtual ~Application();

// Start from a preloaded state.
void Start(SbTimeMonotonic timestamp);
void Start(int64_t timestamp);
void Quit();
void HandleStarboardEvent(const SbEvent* event);

Expand All @@ -70,7 +69,7 @@ class Application {
void OnNetworkEvent(const base::Event* event);

// Called to handle an application event.
void OnApplicationEvent(SbEventType event_type, SbTimeMonotonic timestamp);
void OnApplicationEvent(SbEventType event_type, int64_t timestamp);

// Called to handle a window size change event.
void OnWindowSizeChangedEvent(const base::Event* event);
Expand Down Expand Up @@ -175,8 +174,8 @@ class Application {
void UpdatePeriodicStats();
void DispatchEventInternal(base::Event* event);

base::Optional<SbTimeMonotonic> preload_timestamp_;
base::Optional<SbTimeMonotonic> start_timestamp_;
base::Optional<int64_t> preload_timestamp_;
base::Optional<int64_t> start_timestamp_;

// Json PrefStore used for persistent settings.
std::unique_ptr<persistent_storage::PersistentSettings> persistent_settings_;
Expand Down Expand Up @@ -204,13 +203,13 @@ class Application {
// Lock for access to unconsumed_deep_link_ from different threads.
base::Lock unconsumed_deep_link_lock_;

SbTimeMonotonic deep_link_timestamp_ = 0;
int64_t deep_link_timestamp_ = 0;

// Called when deep links are consumed.
void OnDeepLinkConsumedCallback(const std::string& link);

// Dispatch events for deep links.
void DispatchDeepLink(const char* link, SbTimeMonotonic timestamp);
void DispatchDeepLink(const char* link, int64_t timestamp);
void DispatchDeepLinkIfNotConsumed();


Expand Down
Loading

0 comments on commit d5fea29

Please sign in to comment.