Bug 1966309 - Use weak android symbol instead of dlopen/dlsym for AndroidSystemFontIterator.cpp r=jnicol

Differential Revision: https://phabricator.services.mozilla.com/D249224
This commit is contained in:
serge-sans-paille
2025-05-16 07:04:52 +00:00
committed by sguelton@mozilla.com
parent 0336e618cb
commit 5c575caf38
2 changed files with 29 additions and 37 deletions

View File

@@ -8,7 +8,7 @@
#include "mozilla/Assertions.h"
#include "nsDebug.h"
#include <dlfcn.h>
#include <android/system_fonts.h>
namespace mozilla {
@@ -36,26 +36,14 @@ AndroidSystemFontIterator::~AndroidSystemFontIterator() {
bool AndroidSystemFontIterator::Init() {
if (!sSystemFontIterator_open) {
void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
MOZ_ASSERT(handle);
sSystemFontIterator_open =
(_ASystemFontIterator_open)dlsym(handle, "ASystemFontIterator_open");
sSystemFontIterator_next =
(_ASystemFontIterator_next)dlsym(handle, "ASystemFontIterator_next");
sSystemFontIterator_close =
(_ASystemFontIterator_close)dlsym(handle, "ASystemFontIterator_close");
AndroidFont::sFont_getFontFilePath =
(_AFont_getFontFilePath)dlsym(handle, "AFont_getFontFilePath");
AndroidFont::sFont_close = (_AFont_close)dlsym(handle, "AFont_close");
if (NS_WARN_IF(!sSystemFontIterator_open) ||
NS_WARN_IF(!sSystemFontIterator_next) ||
NS_WARN_IF(!sSystemFontIterator_close) ||
NS_WARN_IF(!AndroidFont::sFont_getFontFilePath) ||
NS_WARN_IF(!AndroidFont::sFont_close)) {
sSystemFontIterator_open = nullptr;
return false;
if (__builtin_available(android 29, *)) {
sSystemFontIterator_open = ASystemFontIterator_open;
sSystemFontIterator_next = ASystemFontIterator_next;
sSystemFontIterator_close = ASystemFontIterator_close;
AndroidFont::sFont_getFontFilePath = AFont_getFontFilePath;
AndroidFont::sFont_close = AFont_close;
} else {
return NS_WARN_IF(false);
}
}
@@ -73,7 +61,7 @@ Maybe<AndroidFont> AndroidSystemFontIterator::Next() {
return Nothing();
}
void* font = sSystemFontIterator_next(mIterator);
AFont* font = sSystemFontIterator_next(mIterator);
if (!font) {
sSystemFontIterator_close(mIterator);
mIterator = nullptr;

View File

@@ -7,19 +7,23 @@
#define AndroidSystemFontIterator_h__
#include "mozilla/Maybe.h"
#include <android/font.h>
#include <android/system_fonts.h>
namespace mozilla {
typedef void* (*_ASystemFontIterator_open)();
typedef void* (*_ASystemFontIterator_next)(void*);
typedef void (*_ASystemFontIterator_close)(void*);
typedef const char* (*_AFont_getFontFilePath)(const void*);
typedef void (*_AFont_close)(void*);
typedef ASystemFontIterator* _Nullable (*_ASystemFontIterator_open)();
typedef AFont* _Nullable (*_ASystemFontIterator_next)(
ASystemFontIterator* _Nonnull iterator);
typedef void (*_ASystemFontIterator_close)(
ASystemFontIterator* _Nullable iterator);
typedef const char* _Nonnull (*_AFont_getFontFilePath)(
const AFont* _Nonnull font);
typedef void (*_AFont_close)(AFont* _Nullable font);
class AndroidFont final {
public:
explicit AndroidFont(void* aFont) : mFont(aFont) {};
explicit AndroidFont(AFont* _Nullable aFont) : mFont(aFont) {};
AndroidFont() = delete;
AndroidFont(AndroidFont&) = delete;
@@ -31,13 +35,13 @@ class AndroidFont final {
~AndroidFont();
const char* GetFontFilePath();
const char* _Nullable GetFontFilePath();
private:
void* mFont;
AFont* _Nullable mFont;
static _AFont_getFontFilePath sFont_getFontFilePath;
static _AFont_close sFont_close;
static _AFont_getFontFilePath _Nullable sFont_getFontFilePath;
static _AFont_close _Nullable sFont_close;
friend class AndroidSystemFontIterator;
};
@@ -53,11 +57,11 @@ class AndroidSystemFontIterator final {
Maybe<AndroidFont> Next();
private:
void* mIterator = nullptr;
ASystemFontIterator* _Nullable mIterator = nullptr;
static _ASystemFontIterator_open sSystemFontIterator_open;
static _ASystemFontIterator_next sSystemFontIterator_next;
static _ASystemFontIterator_close sSystemFontIterator_close;
static _ASystemFontIterator_open _Nullable sSystemFontIterator_open;
static _ASystemFontIterator_next _Nullable sSystemFontIterator_next;
static _ASystemFontIterator_close _Nullable sSystemFontIterator_close;
};
} // namespace mozilla