Skip to content

Commit

Permalink
Cherry pick PR #1929: Add Starboard IFA extension. (#1931)
Browse files Browse the repository at this point in the history
Refer to the original PR: #1929

This extension would allow IFA backporting to
Starboard version 12 or 13.

b/309547847

Change-Id: If3dc2a1eb13377ce33463d2d9bb0b14e33089724

Co-authored-by: y4vor <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored Nov 7, 2023
1 parent 36a7aa4 commit 9d52496
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 2 deletions.
23 changes: 23 additions & 0 deletions cobalt/extension/extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<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 cobalt
58 changes: 58 additions & 0 deletions cobalt/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_
37 changes: 35 additions & 2 deletions cobalt/h5vcc/h5vcc_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<const StarboardExtensionIfaApi*>(
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<const StarboardExtensionIfaApi*>(
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;
}
Expand Down
2 changes: 2 additions & 0 deletions starboard/linux/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 "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
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 @@ -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"
Expand Down Expand Up @@ -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;
}

0 comments on commit 9d52496

Please sign in to comment.