Skip to content

Commit 64c1fe6

Browse files
committed
Add User Agent API
Allow a libwpe application to set user agent information. The libwpe API maps to values exposed by the User-Agent Client Hints API.
1 parent 4a760fa commit 64c1fe6

File tree

7 files changed

+243
-1
lines changed

7 files changed

+243
-1
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ set(WPE_BACKEND
2727
""
2828
CACHE STRING "Name of the backend library to load, instead of libWPEBackend-default.so"
2929
)
30+
set(WPE_ENABLE_UA
31+
ON
32+
CACHE STRING "Enable user agent platform information"
33+
)
3034
set(WPE_ENABLE_XKB
3135
ON
3236
CACHE STRING "Enable use of libxkbcommon for keyboard input"
@@ -69,6 +73,7 @@ set(WPE_PUBLIC_HEADERS
6973
include/wpe/pasteboard.h
7074
include/wpe/renderer-backend-egl.h
7175
include/wpe/renderer-host.h
76+
include/wpe/user-agent.h
7277
include/wpe/view-backend.h
7378
include/wpe/wpe-egl.h
7479
include/wpe/wpe.h
@@ -81,6 +86,7 @@ add_library(
8186
src/pasteboard.c
8287
src/pasteboard-generic.cpp
8388
src/pasteboard-noop.cpp
89+
src/user-agent.c
8490
src/renderer-backend-egl.c
8591
src/renderer-host.c
8692
src/version.c
@@ -112,9 +118,13 @@ target_compile_definitions(
112118
if (WPE_BACKEND)
113119
target_compile_definitions(wpe PRIVATE WPE_BACKEND=\"${WPE_BACKEND}\")
114120
endif ()
121+
if (WPE_ENABLE_UA)
122+
target_compile_definitions(wpe PUBLIC WPE_ENABLE_UA=1)
123+
list(APPEND WPE_PC_CFLAGS -DWPE_ENABLE_UA=1)
124+
endif ()
115125
if (WPE_ENABLE_XKB)
116126
target_compile_definitions(wpe PUBLIC WPE_ENABLE_XKB=1)
117-
set(WPE_PC_CFLAGS -DWPE_ENABLE_XKB=1)
127+
list(APPEND WPE_PC_CFLAGS -DWPE_ENABLE_XKB=1)
118128
endif ()
119129
target_compile_options(wpe PRIVATE $<TARGET_PROPERTY:GL::egl,INTERFACE_COMPILE_OPTIONS>)
120130

include/wpe/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ api_headers = [
88
'pasteboard.h',
99
'renderer-backend-egl.h',
1010
'renderer-host.h',
11+
'user-agent.h',
1112
'version.h',
1213
'version-deprecated.h',
1314
'view-backend.h',

include/wpe/user-agent.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2022 Igalia S.L.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#if !defined(__WPE_H_INSIDE__) && !defined(WPE_COMPILATION)
28+
#error "Only <wpe/wpe.h> can be included directly."
29+
#endif
30+
31+
#ifndef wpe_user_agent_h
32+
#define wpe_user_agent_h
33+
34+
/**
35+
* SECTION:user-agent
36+
* @short_description: Library User Agent
37+
* @title: User Agent
38+
*/
39+
40+
#if defined(WPE_ENABLE_UA) && WPE_ENABLE_UA
41+
42+
#if defined(WPE_COMPILATION)
43+
#include "export.h"
44+
#endif
45+
46+
#include <stdbool.h>
47+
#include <stdint.h>
48+
49+
#ifdef __cplusplus
50+
extern "C" {
51+
#endif
52+
53+
struct wpe_user_agent_provider;
54+
55+
struct wpe_user_agent_provider_interface {
56+
void* (*create)(struct wpe_user_agent_provider*);
57+
void (*destroy)(void*);
58+
const char* (*architecture)(void*);
59+
uint8_t (*bitness)(void*);
60+
bool (*mobile)(void*);
61+
const char* (*model)(void*);
62+
const char* (*platform)(void*);
63+
const char* (*platform_version)(void*);
64+
};
65+
66+
WPE_EXPORT
67+
struct wpe_user_agent_provider* wpe_user_agent_provider_create(void);
68+
69+
WPE_EXPORT
70+
void wpe_user_agent_provider_destroy(struct wpe_user_agent_provider*);
71+
72+
WPE_EXPORT
73+
const char* wpe_user_agent_get_architecture(struct wpe_user_agent_provider*);
74+
75+
WPE_EXPORT
76+
uint8_t wpe_user_agent_get_bitness(struct wpe_user_agent_provider*);
77+
78+
WPE_EXPORT
79+
bool wpe_user_agent_is_mobile(struct wpe_user_agent_provider*);
80+
81+
WPE_EXPORT
82+
const char* wpe_user_agent_get_model(struct wpe_user_agent_provider*);
83+
84+
WPE_EXPORT
85+
const char* wpe_user_agent_get_platform(struct wpe_user_agent_provider*);
86+
87+
WPE_EXPORT
88+
const char* wpe_user_agent_get_platform_version(struct wpe_user_agent_provider*);
89+
90+
WPE_EXPORT
91+
void wpe_user_agent_register_interface(struct wpe_user_agent_provider_interface*);
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif /* defined(WPE_ENABLE_UA) && WPE_ENABLE_UA */
98+
99+
#endif /* wpe_user_agent_h */

include/wpe/wpe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "renderer-host.h"
4444
#include "view-backend.h"
4545
#include "libwpe-version.h"
46+
#include "user-agent.h"
4647
#include "version.h"
4748
#include "version-deprecated.h"
4849

meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pkg_cflags = []
5252
#
5353
can_allow_fallback = meson.version().version_compare('>=0.55.0')
5454

55+
if get_option('enable-ua')
56+
pkg_cflags += ['-DWPE_ENABLE_UA=1']
57+
endif
58+
5559
if get_option('enable-xkb')
5660
pkg_cflags += ['-DWPE_ENABLE_XKB=1']
5761
if can_allow_fallback
@@ -98,6 +102,7 @@ libwpe = library('wpe-' + api_version,
98102
'src/pasteboard-noop.cpp',
99103
'src/renderer-backend-egl.c',
100104
'src/renderer-host.c',
105+
'src/user-agent.c',
101106
'src/version.c',
102107
'src/view-backend.c',
103108
get_option('default_library') == 'shared' ? 'src/loader.c' : 'src/loader-static.c',

meson_options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ option('default-backend',
33
value: '',
44
description: 'Name of the backend library to load, instead of libWPEBackend-default.so'
55
)
6+
option('enable-ua',
7+
type: 'boolean',
8+
value: true,
9+
description: '(Experimental) Enable user agent api'
10+
)
611
option('enable-xkb',
712
type: 'boolean',
813
value: true,

src/user-agent.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2022 Igalia S.L.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#if defined(WPE_ENABLE_UA) && WPE_ENABLE_UA
28+
29+
#include "../include/wpe/user-agent.h"
30+
#include <stdlib.h>
31+
32+
struct wpe_user_agent_provider {
33+
void* backend;
34+
};
35+
36+
static struct wpe_user_agent_provider_interface* provider_interface = NULL;
37+
38+
struct wpe_user_agent_provider*
39+
wpe_user_agent_provider_create()
40+
{
41+
if (!provider_interface)
42+
return NULL;
43+
44+
struct wpe_user_agent_provider* provider = calloc(1, sizeof(struct wpe_user_agent_provider));
45+
if (!provider)
46+
return NULL;
47+
48+
if (provider_interface->create)
49+
provider->backend = provider_interface->create(provider);
50+
51+
return provider;
52+
}
53+
54+
void
55+
wpe_user_agent_provider_destroy(struct wpe_user_agent_provider* provider)
56+
{
57+
if (!provider)
58+
return;
59+
60+
if (provider_interface && provider_interface->destroy)
61+
provider_interface->destroy(provider);
62+
provider->backend = NULL;
63+
free(provider);
64+
}
65+
66+
const char*
67+
wpe_user_agent_get_architecture(struct wpe_user_agent_provider* provider)
68+
{
69+
if (provider && provider_interface && provider_interface->architecture)
70+
return provider_interface->architecture(provider->backend);
71+
return NULL;
72+
}
73+
74+
uint8_t
75+
wpe_user_agent_get_bitness(struct wpe_user_agent_provider* provider)
76+
{
77+
if (provider && provider_interface && provider_interface->bitness)
78+
return provider_interface->bitness(provider->backend);
79+
return 0;
80+
}
81+
82+
bool
83+
wpe_user_agent_is_mobile(struct wpe_user_agent_provider* provider)
84+
{
85+
if (provider && provider_interface && provider_interface->mobile)
86+
return provider_interface->mobile(provider->backend);
87+
return false;
88+
}
89+
90+
const char*
91+
wpe_user_agent_get_model(struct wpe_user_agent_provider* provider)
92+
{
93+
if (provider && provider_interface && provider_interface->model)
94+
return provider_interface->model(provider->backend);
95+
return NULL;
96+
}
97+
98+
const char*
99+
wpe_user_agent_get_platform(struct wpe_user_agent_provider* provider)
100+
{
101+
if (provider && provider_interface && provider_interface->platform)
102+
return provider_interface->platform(provider->backend);
103+
return NULL;
104+
}
105+
106+
const char*
107+
wpe_user_agent_get_platform_version(struct wpe_user_agent_provider* provider)
108+
{
109+
if (provider && provider_interface && provider_interface->platform_version)
110+
return provider_interface->platform_version(provider->backend);
111+
return NULL;
112+
}
113+
114+
void
115+
wpe_user_agent_register_interface(struct wpe_user_agent_provider_interface* provider)
116+
{
117+
if (provider && !provider_interface)
118+
provider_interface = provider;
119+
}
120+
121+
#endif /* defined(WPE_ENABLE_UA) && WPE_ENABLE_UA */

0 commit comments

Comments
 (0)