Refactor updater::getUserAgent() using new base::get_platform()

This commit is contained in:
David Capello 2021-11-02 14:18:14 -03:00
parent 713a2eac80
commit 43ca34bd35
5 changed files with 26 additions and 191 deletions

2
laf

@ -1 +1 @@
Subproject commit aa6fc88cb1f7f2abf17e135704e2c6a9bcb86447
Subproject commit 726481f27c8258aa8fba577bf1529a5f40acec59

View File

@ -1,21 +1,15 @@
# ASEPRITE
# Copyright (C) 2020 Igara Studio S.A.
# Copyright (C) 2020-2021 Igara Studio S.A.
# Copyright (C) 2001-2017 David Capello
set(UPDATER_LIB_SOURCES
check_update.cpp
user_agent.cpp)
if(WIN32)
set(UPDATER_LIB_SOURCES ${UPDATER_LIB_SOURCES} user_agent_win.c)
elseif(APPLE)
set(UPDATER_LIB_SOURCES ${UPDATER_LIB_SOURCES} user_agent_mac.mm)
endif()
add_library(updater-lib ${UPDATER_LIB_SOURCES})
target_link_libraries(updater-lib
laf-base
net-lib
cfg-lib
ver-lib
${TINYXML_LIBRARY})

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
@ -10,135 +10,64 @@
#endif
#include "updater/user_agent.h"
#include "base/platform.h"
#include "ver/info.h"
#include <string>
#include <sstream>
#if _WIN32 // Windows
#include <windows.h>
extern "C" BOOL IsWow64();
extern "C" const char* WineVersion();
#elif __APPLE__ // Mac OS X
void getMacOSXVersion(int& major, int& minor, int& patch);
#else // Unix-like system
#include "base/trim_string.h"
#include "cfg/cfg.h"
#include <cstring>
#include <sys/utsname.h>
#endif
namespace updater {
std::string getUserAgent()
{
base::Platform p = base::get_platform();
std::stringstream userAgent;
// App name and version
userAgent << get_app_name() << "/" << get_app_version() << " (";
#if _WIN32
#if LAF_WINDOWS
// ----------------------------------------------------------------------
// Windows
OSVERSIONINFOEX osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
::GetVersionEx((OSVERSIONINFO*)&osv);
userAgent << "Windows";
switch (osv.wProductType) {
case VER_NT_DOMAIN_CONTROLLER:
case VER_NT_SERVER:
switch (p.windowsType) {
case base::Platform::WindowsType::Server:
userAgent << " Server";
break;
case VER_NT_WORKSTATION:
case base::Platform::WindowsType::NT:
userAgent << " NT";
break;
}
userAgent << " " << osv.dwMajorVersion << "." << osv.dwMinorVersion;
userAgent << " " << p.osVer.major() << "." << p.osVer.minor();
if (osv.wServicePackMajor > 0)
userAgent << " SP" << osv.wServicePackMajor;
if (p.servicePack.major() > 0)
userAgent << " SP" << p.servicePack.major();
if (IsWow64())
if (p.isWow64)
userAgent << "; WOW64";
if (const char* wineVer = WineVersion())
userAgent << "; Wine " << wineVer;
if (p.wineVer)
userAgent << "; Wine " << p.wineVer;
#elif __APPLE__
#elif LAF_MACOS
// ----------------------------------------------------------------------
// Mac OS X
int major, minor, patch;
getMacOSXVersion(major, minor, patch);
userAgent << "Mac OS X " << major << "." << minor << "." << patch;
userAgent << "macOS "
<< p.osVer.major() << "."
<< p.osVer.minor() << "."
<< p.osVer.patch();
#else
// ----------------------------------------------------------------------
// Unix like
cfg::CfgFile file;
auto isQuote = [](int c) {
return
c == '"' ||
c == '\'' ||
std::isspace(c);
};
auto getValue = [&file, &isQuote](const char* varName) -> std::string {
std::string result;
const char* value = file.getValue("", varName, nullptr);
if (value && std::strlen(value) > 0)
base::trim_string(value, result, isQuote);
return result;
};
// Read information from /etc/os-release
file.load("/etc/os-release");
std::string name = getValue("PRETTY_NAME");
if (!name.empty()) {
userAgent << name;
}
else {
name = getValue("NAME");
std::string version = getValue("VERSION");
if (!name.empty() && !version.empty()) {
userAgent << name << " " << version;
}
else {
// Read information from /etc/lsb-release
file.load("/etc/lsb-release");
name = getValue("DISTRIB_DESCRIPTION");
if (!name.empty()) {
userAgent << name;
}
else {
name = getValue("DISTRIB_ID");
version = getValue("DISTRIB_RELEASE");
if (!name.empty() &&
!version.empty()) {
userAgent << name << " " << version;
}
else {
// Last resource, use uname() function
struct utsname utsn;
uname(&utsn);
userAgent << utsn.sysname << " " << utsn.release;
}
}
}
if (!p.distroName.empty()) {
userAgent << p.distroName;
if (!p.distroVer.empty())
userAgent << " " << p.distroVer;
}
#endif

View File

@ -1,43 +0,0 @@
// Aseprite
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Cocoa/Cocoa.h>
void getMacOSXVersion(int& major, int& minor, int& patch)
{
major = 10;
minor = 0;
patch = 0;
NSProcessInfo* info = [NSProcessInfo processInfo];
if ([info respondsToSelector:@selector(operatingSystemVersion)]) {
NSOperatingSystemVersion osVer = [info operatingSystemVersion];
major = osVer.majorVersion;
minor = osVer.minorVersion;
patch = osVer.patchVersion;
}
else {
SInt32 systemVersion, majorVersion, minorVersion, patchVersion;
if (Gestalt(gestaltSystemVersion, &systemVersion) != noErr)
return;
if (systemVersion < 0x1040) {
major = ((systemVersion & 0xF000) >> 12) * 10 + ((systemVersion & 0x0F00) >> 8);
minor = (systemVersion & 0x00F0) >> 4;
patch = (systemVersion & 0x000F);
}
else if (Gestalt(gestaltSystemVersionMajor, &majorVersion) == noErr &&
Gestalt(gestaltSystemVersionMinor, &minorVersion) == noErr &&
Gestalt(gestaltSystemVersionBugFix, &patchVersion) == noErr) {
major = majorVersion;
minor = minorVersion;
patch = patchVersion;
}
}
}

View File

@ -1,45 +0,0 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <windows.h>
typedef BOOL (WINAPI* LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
typedef const char* (CDECL* LPFN_WINE_GET_VERSION)(void);
static LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
static LPFN_WINE_GET_VERSION fnWineGetVersion = NULL;
BOOL IsWow64()
{
if (!fnIsWow64Process) {
fnIsWow64Process = (LPFN_ISWOW64PROCESS)
GetProcAddress(GetModuleHandle(L"kernel32"),
"IsWow64Process");
}
BOOL isWow64 = FALSE;
if (fnIsWow64Process)
fnIsWow64Process(GetCurrentProcess(), &isWow64);
return isWow64;
}
const char* WineVersion()
{
if (!fnWineGetVersion) {
fnWineGetVersion = (LPFN_WINE_GET_VERSION)
GetProcAddress(GetModuleHandle(L"ntdll.dll"),
"wine_get_version");
}
if (fnWineGetVersion)
return fnWineGetVersion();
else
return NULL;
}