Skip to content

Commit

Permalink
Add Starboard IFA extension.
Browse files Browse the repository at this point in the history
This extension would allow IFA backporting to
Starboard version 12 or 13.

b/309547847

Change-Id: If3dc2a1eb13377ce33463d2d9bb0b14e33089724
  • Loading branch information
y4vor committed Nov 7, 2023
1 parent d69b5a8 commit 59d96f4
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cobalt/h5vcc/h5vcc_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "starboard/common/system_property.h"
#include "starboard/system.h"

#if SB_AI_VERSION < 14
#include "starboard/extension/ifa.h"
#endif // SB_AI_VERSION < 14

using starboard::kSystemPropertyMaxLength;

namespace cobalt {
Expand Down Expand Up @@ -57,27 +61,51 @@ 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

Check warning on line 65 in cobalt/h5vcc/h5vcc_system.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/h5vcc/h5vcc_system.cc#L65

Added line #L65 was not covered by tests
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<const StarboardExtensionIfaApi*>(
SbSystemGetExtension(kStarboardExtensionIfaName));
if (ifa_extension) {
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

Check warning on line 90 in cobalt/h5vcc/h5vcc_system.cc

View check run for this annotation

Codecov / codecov/patch

cobalt/h5vcc/h5vcc_system.cc#L90

Added line #L90 was not covered by tests
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<const StarboardExtensionIfaApi*>(
SbSystemGetExtension(kStarboardExtensionIfaName));
if (ifa_extension) {
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;
}
Expand Down
22 changes: 22 additions & 0 deletions starboard/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<const ExtensionApi*>(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<const ExtensionApi*>(SbSystemGetExtension(kExtensionName));
EXPECT_EQ(second_extension_api, extension_api)
<< "Extension struct should be a singleton";
}

} // namespace extension
} // namespace starboard
58 changes: 58 additions & 0 deletions starboard/extension/ifa.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

#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_
2 changes: 2 additions & 0 deletions starboard/linux/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 65 additions & 0 deletions starboard/linux/shared/ifa.cc
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 33 in starboard/linux/shared/ifa.cc

View check run for this annotation

Codecov / codecov/patch

starboard/linux/shared/ifa.cc#L29-L33

Added lines #L29 - L33 were not covered by tests
}

// 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());

Check warning on line 42 in starboard/linux/shared/ifa.cc

View check run for this annotation

Codecov / codecov/patch

starboard/linux/shared/ifa.cc#L39-L42

Added lines #L39 - L42 were not covered by tests
}

bool GetLimitAdTracking(char* out_value, int value_length) {
return CopyStringAndTestIfSuccess(
out_value, value_length,
GetEnvironment("COBALT_LIMIT_AD_TRACKING").c_str());

Check warning on line 48 in starboard/linux/shared/ifa.cc

View check run for this annotation

Codecov / codecov/patch

starboard/linux/shared/ifa.cc#L45-L48

Added lines #L45 - L48 were not covered by tests
}

const StarboardExtensionIfaApi kIfaApi = {
kStarboardExtensionIfaName,
1, // API version that's implemented.
&GetAdvertisingId,
&GetLimitAdTracking,
};

} // namespace

const void* GetIfaApi() {
return &kIfaApi;
}

} // namespace shared
} // namespace starboard
27 changes: 27 additions & 0 deletions starboard/linux/shared/ifa.h
Original file line number Diff line number Diff line change
@@ -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_
7 changes: 7 additions & 0 deletions starboard/linux/shared/system_get_extensions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -79,5 +81,10 @@ const void* SbSystemGetExtension(const char* name) {
if (strcmp(name, kStarboardExtensionTimeZoneName) == 0) {
return starboard::shared::GetTimeZoneApi();
}
#if SB_AI_VERSION < 14
if (strcmp(name, kStarboardExtensionIfaName) == 0) {
return starboard::shared::GetIfaApi();
}
#endif // SB_AI_VERSION < 14
return NULL;
}

0 comments on commit 59d96f4

Please sign in to comment.