diff --git a/cobalt/extension/extension_test.cc b/cobalt/extension/extension_test.cc index b6cf3bc3465e..2dfdae913dd4 100644 --- a/cobalt/extension/extension_test.cc +++ b/cobalt/extension/extension_test.cc @@ -20,6 +20,7 @@ #include "cobalt/extension/font.h" #include "cobalt/extension/free_space.h" #include "cobalt/extension/graphics.h" +#include "cobalt/extension/ifa.h" #include "cobalt/extension/installation_manager.h" #include "cobalt/extension/javascript_cache.h" #include "cobalt/extension/media_session.h" @@ -382,5 +383,27 @@ TEST(ExtensionTest, FreeSpace) { EXPECT_EQ(second_extension_api, extension_api) << "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 cobalt diff --git a/cobalt/extension/ifa.h b/cobalt/extension/ifa.h new file mode 100644 index 000000000000..a2070492dabd --- /dev/null +++ b/cobalt/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/cobalt/h5vcc/h5vcc_system.cc b/cobalt/h5vcc/h5vcc_system.cc index b28e9412a1f9..ad4de95c76d9 100644 --- a/cobalt/h5vcc/h5vcc_system.cc +++ b/cobalt/h5vcc/h5vcc_system.cc @@ -20,6 +20,10 @@ #include "cobalt_build_id.h" // NOLINT(build/include_subdir) #include "starboard/system.h" +#if SB_API_VERSION < 14 +#include "cobalt/extension/ifa.h" +#endif // SB_API_VERSION < 14 + namespace cobalt { namespace h5vcc { @@ -54,29 +58,58 @@ std::string H5vccSystem::platform() const { std::string H5vccSystem::advertising_id() const { std::string result; -#if SB_API_VERSION >= 14 const size_t kSystemPropertyMaxLength = 1024; 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 const size_t kSystemPropertyMaxLength = 1024; 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/linux/shared/BUILD.gn b/starboard/linux/shared/BUILD.gn index f0974046e90d..041051fdae5f 100644 --- a/starboard/linux/shared/BUILD.gn +++ b/starboard/linux/shared/BUILD.gn @@ -64,6 +64,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..548f9bb78b8b --- /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 "cobalt/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 fba44bd83382..66d8fdc26450 100644 --- a/starboard/linux/shared/system_get_extensions.cc +++ b/starboard/linux/shared/system_get_extensions.cc @@ -18,9 +18,11 @@ #include "cobalt/extension/crash_handler.h" #include "cobalt/extension/demuxer.h" #include "cobalt/extension/free_space.h" +#include "cobalt/extension/ifa.h" #include "cobalt/extension/memory_mapped_file.h" #include "cobalt/extension/platform_service.h" #include "starboard/common/string.h" +#include "starboard/linux/shared/ifa.h" #include "starboard/linux/shared/soft_mic_platform_service.h" #include "starboard/shared/ffmpeg/ffmpeg_demuxer.h" #include "starboard/shared/posix/free_space.h" @@ -67,5 +69,10 @@ const void* SbSystemGetExtension(const char* name) { return use_ffmpeg_demuxer ? starboard::shared::ffmpeg::GetFFmpegDemuxerApi() : NULL; } +#if SB_API_VERSION < 14 + if (strcmp(name, kStarboardExtensionIfaName) == 0) { + return starboard::shared::GetIfaApi(); + } +#endif // SB_API_VERSION < 14 return NULL; }