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

View File

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