Skip to content

Commit

Permalink
Deprecate SbTime APIs
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 microsecond timestamps
- Replace usages of SbTimeGetNow, SbTimeGetMonotonicNow, etc...

b/302733082
  • Loading branch information
gbournou committed Dec 6, 2023
1 parent a11bdbe commit 896589c
Show file tree
Hide file tree
Showing 119 changed files with 901 additions and 557 deletions.
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
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
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
37 changes: 20 additions & 17 deletions cobalt/browser/browser_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
#include "cobalt/web/navigator_ua_data.h"
#include "starboard/atomic.h"
#include "starboard/common/string.h"
#include "starboard/common/time.h"
#include "starboard/configuration.h"
#include "starboard/extension/graphics.h"
#include "starboard/system.h"
#include "starboard/time.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"

#if SB_HAS(CORE_DUMP_HANDLER_SUPPORT)
Expand All @@ -88,8 +88,10 @@ struct NonTrivialGlobalVariables {

NonTrivialGlobalVariables::NonTrivialGlobalVariables() {
last_render_timestamp = &cobalt::timestamp::g_last_render_timestamp;
SbAtomicNoBarrier_Exchange64(last_render_timestamp,
static_cast<SbAtomic64>(SbTimeGetNow()));
SbAtomicNoBarrier_Exchange64(
last_render_timestamp,
static_cast<SbAtomic64>(
starboard::PosixTimeToWindowsTime(starboard::CurrentPosixTime())));
}

base::LazyInstance<NonTrivialGlobalVariables>::DestructorAtExit
Expand Down Expand Up @@ -1578,7 +1580,7 @@ void BrowserModule::SetProxy(const std::string& proxy_rules) {
network_module_->SetProxy(proxy_rules);
}

void BrowserModule::Blur(SbTimeMonotonic timestamp) {
void BrowserModule::Blur(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Blur()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateStarted);
Expand All @@ -1592,23 +1594,23 @@ void BrowserModule::Blur(SbTimeMonotonic timestamp) {
}
}

void BrowserModule::Conceal(SbTimeMonotonic timestamp) {
void BrowserModule::Conceal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Conceal()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateBlurred);
application_state_ = base::kApplicationStateConcealed;
ConcealInternal(timestamp);
}

void BrowserModule::Freeze(SbTimeMonotonic timestamp) {
void BrowserModule::Freeze(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Freeze()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateConcealed);
application_state_ = base::kApplicationStateFrozen;
FreezeInternal(timestamp);
}

void BrowserModule::Unfreeze(SbTimeMonotonic timestamp) {
void BrowserModule::Unfreeze(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Unfreeze()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateFrozen);
Expand All @@ -1617,15 +1619,15 @@ void BrowserModule::Unfreeze(SbTimeMonotonic timestamp) {
NavigatePendingURL();
}

void BrowserModule::Reveal(SbTimeMonotonic timestamp) {
void BrowserModule::Reveal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Reveal()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateConcealed);
application_state_ = base::kApplicationStateBlurred;
RevealInternal(timestamp);
}

void BrowserModule::Focus(SbTimeMonotonic timestamp) {
void BrowserModule::Focus(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::Focus()");
DCHECK_EQ(base::MessageLoop::current(), self_message_loop_);
DCHECK(application_state_ == base::kApplicationStateBlurred);
Expand Down Expand Up @@ -1696,9 +1698,10 @@ void BrowserModule::OnWebModuleRendererSubmissionRasterized() {

#if defined(COBALT_CHECK_RENDER_TIMEOUT)
void BrowserModule::OnPollForRenderTimeout(const GURL& url) {
SbTime last_render_timestamp = static_cast<SbTime>(SbAtomicAcquire_Load64(
int64_t last_render_timestamp = static_cast<int64_t>(SbAtomicAcquire_Load64(
non_trivial_global_variables.Get().last_render_timestamp));
base::Time last_render = base::Time::FromSbTime(last_render_timestamp);
base::Time last_render = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(last_render_timestamp));
bool timeout_expiration = base::Time::Now() - base::TimeDelta::FromSeconds(
kLastRenderTimeoutSeconds) >
last_render;
Expand All @@ -1720,7 +1723,7 @@ void BrowserModule::OnPollForRenderTimeout(const GURL& url) {
#endif
SbAtomicNoBarrier_Exchange64(
non_trivial_global_variables.Get().last_render_timestamp,
static_cast<SbAtomic64>(kSbTimeMax));
static_cast<SbAtomic64>(kSbInt64Max));
if (SbSystemGetRandomUInt64() <
kRenderTimeoutErrorPercentage * (UINT64_MAX / 100)) {
OnError(url, std::string("Rendering Timeout"));
Expand Down Expand Up @@ -1889,7 +1892,7 @@ void BrowserModule::UpdateScreenSize() {
}
}

void BrowserModule::ConcealInternal(SbTimeMonotonic timestamp) {
void BrowserModule::ConcealInternal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::ConcealInternal()");
FOR_EACH_OBSERVER(LifecycleObserver, lifecycle_observers_,
Conceal(GetResourceProvider(), timestamp));
Expand Down Expand Up @@ -1917,7 +1920,7 @@ void BrowserModule::ConcealInternal(SbTimeMonotonic timestamp) {
}
}

void BrowserModule::FreezeInternal(SbTimeMonotonic timestamp) {
void BrowserModule::FreezeInternal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::FreezeInternal()");
FreezeMediaModule();
// First freeze all our web modules which implies that they will release
Expand All @@ -1930,7 +1933,7 @@ void BrowserModule::FreezeInternal(SbTimeMonotonic timestamp) {
}
}

void BrowserModule::RevealInternal(SbTimeMonotonic timestamp) {
void BrowserModule::RevealInternal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::RevealInternal()");
DCHECK(!renderer_module_);
if (!system_window_) {
Expand All @@ -1954,7 +1957,7 @@ void BrowserModule::RevealInternal(SbTimeMonotonic timestamp) {
}
}

void BrowserModule::UnfreezeInternal(SbTimeMonotonic timestamp) {
void BrowserModule::UnfreezeInternal(int64_t timestamp) {
TRACE_EVENT0("cobalt::browser", "BrowserModule::UnfreezeInternal()");
// Set the Stub resource provider to media module and to web module
// at Concealed state.
Expand Down Expand Up @@ -2243,7 +2246,7 @@ scoped_refptr<script::Wrappable> BrowserModule::CreateH5vccCallback(
return scoped_refptr<script::Wrappable>(h5vcc_object);
}

void BrowserModule::SetDeepLinkTimestamp(SbTimeMonotonic timestamp) {
void BrowserModule::SetDeepLinkTimestamp(int64_t timestamp) {
if (base::MessageLoop::current() != self_message_loop_) {
self_message_loop_->task_runner()->PostTask(
FROM_HERE, base::Bind(&BrowserModule::SetDeepLinkTimestamp,
Expand Down
Loading

0 comments on commit 896589c

Please sign in to comment.