From 64def2e8f9b4db65cfdd4f2801a565af99fe8455 Mon Sep 17 00:00:00 2001 From: cobalt-github-releaser-bot <95661244+cobalt-github-releaser-bot@users.noreply.github.com> Date: Tue, 7 Nov 2023 10:08:20 -0800 Subject: [PATCH] Cherry pick PR #1929: Add Starboard IFA extension. (#1932) Refer to the original PR: https://github.com/youtube/cobalt/pull/1929 This extension would allow IFA backporting to Starboard version 12 or 13. b/309547847 Change-Id: If3dc2a1eb13377ce33463d2d9bb0b14e33089724 Co-authored-by: y4vor --- cobalt/h5vcc/h5vcc_system.cc | 37 ++++++++++- starboard/extension/extension_test.cc | 22 +++++++ starboard/extension/ifa.h | 58 +++++++++++++++++ starboard/linux/shared/BUILD.gn | 2 + starboard/linux/shared/ifa.cc | 65 +++++++++++++++++++ starboard/linux/shared/ifa.h | 27 ++++++++ .../linux/shared/system_get_extensions.cc | 7 ++ 7 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 starboard/extension/ifa.h create mode 100644 starboard/linux/shared/ifa.cc create mode 100644 starboard/linux/shared/ifa.h diff --git a/cobalt/h5vcc/h5vcc_system.cc b/cobalt/h5vcc/h5vcc_system.cc index 89c9d9a9eac3..addb71767965 100644 --- a/cobalt/h5vcc/h5vcc_system.cc +++ b/cobalt/h5vcc/h5vcc_system.cc @@ -21,6 +21,10 @@ #include "starboard/common/system_property.h" #include "starboard/system.h" +#if SB_API_VERSION < 14 +#include "starboard/extension/ifa.h" +#endif // SB_API_VERSION < 14 + using starboard::kSystemPropertyMaxLength; namespace cobalt { @@ -57,27 +61,56 @@ std::string H5vccSystem::platform() const { std::string H5vccSystem::advertising_id() const { std::string result; -#if SB_API_VERSION >= 14 char property[kSystemPropertyMaxLength] = {0}; +#if SB_API_VERSION >= 14 if (!SbSystemGetProperty(kSbSystemPropertyAdvertisingId, property, SB_ARRAY_SIZE_INT(property))) { DLOG(FATAL) << "Failed to get kSbSystemPropertyAdvertisingId."; } else { result = property; } +#else + static auto const* ifa_extension = + static_cast( + SbSystemGetExtension(kStarboardExtensionIfaName)); + if (ifa_extension && + strcmp(ifa_extension->name, kStarboardExtensionIfaName) == 0 && + ifa_extension->version >= 1) { + if (!ifa_extension->GetAdvertisingId(property, + SB_ARRAY_SIZE_INT(property))) { + DLOG(FATAL) << "Failed to get AdvertisingId from IFA extension."; + } else { + result = property; + } + } #endif return result; } bool H5vccSystem::limit_ad_tracking() const { bool result = false; -#if SB_API_VERSION >= 14 char property[kSystemPropertyMaxLength] = {0}; +#if SB_API_VERSION >= 14 if (!SbSystemGetProperty(kSbSystemPropertyLimitAdTracking, property, SB_ARRAY_SIZE_INT(property))) { DLOG(FATAL) << "Failed to get kSbSystemPropertyAdvertisingId."; } else { result = std::atoi(property); } +#else + static auto const* ifa_extension = + static_cast( + SbSystemGetExtension(kStarboardExtensionIfaName)); + + if (ifa_extension && + strcmp(ifa_extension->name, kStarboardExtensionIfaName) == 0 && + ifa_extension->version >= 1) { + if (!ifa_extension->GetLimitAdTracking(property, + SB_ARRAY_SIZE_INT(property))) { + DLOG(FATAL) << "Failed to get LimitAdTracking from IFA extension."; + } else { + result = std::atoi(property); + } + } #endif return result; } diff --git a/starboard/extension/extension_test.cc b/starboard/extension/extension_test.cc index 0a40eef7c361..874cce11bbfd 100644 --- a/starboard/extension/extension_test.cc +++ b/starboard/extension/extension_test.cc @@ -21,6 +21,7 @@ #include "starboard/extension/font.h" #include "starboard/extension/free_space.h" #include "starboard/extension/graphics.h" +#include "starboard/extension/ifa.h" #include "starboard/extension/installation_manager.h" #include "starboard/extension/javascript_cache.h" #include "starboard/extension/media_session.h" @@ -459,5 +460,26 @@ TEST(ExtensionTest, TimeZone) { << "Extension struct should be a singleton"; } +TEST(ExtensionTest, Ifa) { + typedef StarboardExtensionIfaApi ExtensionApi; + const char* kExtensionName = kStarboardExtensionIfaName; + + const ExtensionApi* extension_api = + static_cast(SbSystemGetExtension(kExtensionName)); + if (!extension_api) { + return; + } + + EXPECT_STREQ(extension_api->name, kExtensionName); + EXPECT_EQ(extension_api->version, 1u); + EXPECT_NE(extension_api->GetAdvertisingId, nullptr); + EXPECT_NE(extension_api->GetLimitAdTracking, nullptr); + + const ExtensionApi* second_extension_api = + static_cast(SbSystemGetExtension(kExtensionName)); + EXPECT_EQ(second_extension_api, extension_api) + << "Extension struct should be a singleton"; +} + } // namespace extension } // namespace starboard diff --git a/starboard/extension/ifa.h b/starboard/extension/ifa.h new file mode 100644 index 000000000000..a2070492dabd --- /dev/null +++ b/starboard/extension/ifa.h @@ -0,0 +1,58 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef STARBOARD_EXTENSION_IFA_H_ +#define STARBOARD_EXTENSION_IFA_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define kStarboardExtensionIfaName "dev.cobalt.extension.Ifa" + +typedef struct StarboardExtensionIfaApi { + // Name should be the string |kCobaltExtensionIfaName|. + // This helps to validate that the extension API is correct. + const char* name; + + // This specifies the version of the API that is implemented. + uint32_t version; + + // The fields below this point were added in version 1 or later. + + // Advertising ID or IFA, typically a 128-bit UUID + // Please see https://iabtechlab.com/OTT-IFA for details. + // Corresponds to 'ifa' field. Note: `ifa_type` field is not provided. + // In Starboard 14 this the value is retrieved through the system + // property `kSbSystemPropertyAdvertisingId` defined in + // `starboard/system.h`. + bool (*GetAdvertisingId)(char* out_value, int value_length); + + // Limit advertising tracking, treated as boolean. Set to nonzero to indicate + // a true value. Corresponds to 'lmt' field. + // In Starboard 14 this the value is retrieved through the system + // property `kSbSystemPropertyLimitAdTracking` defined in + // `starboard/system.h`. + + bool (*GetLimitAdTracking)(char* out_value, int value_length); + +} CobaltExtensionIfaApi; + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // STARBOARD_EXTENSION_IFA_H_ diff --git a/starboard/linux/shared/BUILD.gn b/starboard/linux/shared/BUILD.gn index 536bfe554509..4b4e3fa49101 100644 --- a/starboard/linux/shared/BUILD.gn +++ b/starboard/linux/shared/BUILD.gn @@ -66,6 +66,8 @@ static_library("starboard_platform_sources") { "//starboard/linux/shared/decode_target_internal.cc", "//starboard/linux/shared/decode_target_internal.h", "//starboard/linux/shared/decode_target_release.cc", + "//starboard/linux/shared/ifa.cc", + "//starboard/linux/shared/ifa.h", "//starboard/linux/shared/media_is_audio_supported.cc", "//starboard/linux/shared/media_is_video_supported.cc", "//starboard/linux/shared/netlink.cc", diff --git a/starboard/linux/shared/ifa.cc b/starboard/linux/shared/ifa.cc new file mode 100644 index 000000000000..21b17dc745cd --- /dev/null +++ b/starboard/linux/shared/ifa.cc @@ -0,0 +1,65 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "starboard/linux/shared/ifa.h" + +#include "starboard/extension/ifa.h" + +#include "starboard/common/string.h" +#include "starboard/shared/environment.h" + +namespace starboard { +namespace shared { + +namespace { + +bool CopyStringAndTestIfSuccess(char* out_value, + int value_length, + const char* from_value) { + if (strlen(from_value) + 1 > value_length) + return false; + starboard::strlcpy(out_value, from_value, value_length); + return true; +} + +// Definitions of any functions included as components in the extension +// are added here. + +bool GetAdvertisingId(char* out_value, int value_length) { + return CopyStringAndTestIfSuccess( + out_value, value_length, + starboard::GetEnvironment("COBALT_ADVERTISING_ID").c_str()); +} + +bool GetLimitAdTracking(char* out_value, int value_length) { + return CopyStringAndTestIfSuccess( + out_value, value_length, + GetEnvironment("COBALT_LIMIT_AD_TRACKING").c_str()); +} + +const StarboardExtensionIfaApi kIfaApi = { + kStarboardExtensionIfaName, + 1, // API version that's implemented. + &GetAdvertisingId, + &GetLimitAdTracking, +}; + +} // namespace + +const void* GetIfaApi() { + return &kIfaApi; +} + +} // namespace shared +} // namespace starboard diff --git a/starboard/linux/shared/ifa.h b/starboard/linux/shared/ifa.h new file mode 100644 index 000000000000..fbe61abfeb78 --- /dev/null +++ b/starboard/linux/shared/ifa.h @@ -0,0 +1,27 @@ +// Copyright 2023 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef STARBOARD_LINUX_SHARED_IFA_H_ +#define STARBOARD_LINUX_SHARED_IFA_H_ + +// Omit namespace linux due to symbol name conflict. +namespace starboard { +namespace shared { + +const void* GetIfaApi(); + +} // namespace shared +} // namespace starboard + +#endif // STARBOARD_LINUX_SHARED_IFA_H_ diff --git a/starboard/linux/shared/system_get_extensions.cc b/starboard/linux/shared/system_get_extensions.cc index e1a44b4fe62c..efc9169f829d 100644 --- a/starboard/linux/shared/system_get_extensions.cc +++ b/starboard/linux/shared/system_get_extensions.cc @@ -20,9 +20,11 @@ #include "starboard/extension/demuxer.h" #include "starboard/extension/enhanced_audio.h" #include "starboard/extension/free_space.h" +#include "starboard/extension/ifa.h" #include "starboard/extension/memory_mapped_file.h" #include "starboard/extension/platform_service.h" #include "starboard/extension/time_zone.h" +#include "starboard/linux/shared/ifa.h" #include "starboard/linux/shared/soft_mic_platform_service.h" #include "starboard/linux/shared/time_zone.h" #include "starboard/shared/enhanced_audio/enhanced_audio.h" @@ -79,5 +81,10 @@ const void* SbSystemGetExtension(const char* name) { if (strcmp(name, kStarboardExtensionTimeZoneName) == 0) { return starboard::shared::GetTimeZoneApi(); } +#if SB_API_VERSION < 14 + if (strcmp(name, kStarboardExtensionIfaName) == 0) { + return starboard::shared::GetIfaApi(); + } +#endif // SB_API_VERSION < 14 return NULL; }