1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitApplicationInfo.h"
22
23#include <wtf/text/CString.h>
24
25struct _WebKitApplicationInfo {
26 CString name;
27 uint64_t majorVersion;
28 uint64_t minorVersion;
29 uint64_t microVersion;
30
31 int referenceCount { 1 };
32};
33
34G_DEFINE_BOXED_TYPE(WebKitApplicationInfo, webkit_application_info, webkit_application_info_ref, webkit_application_info_unref)
35
36/**
37 * webkit_application_info_new:
38 *
39 * Creates a new #WebKitApplicationInfo
40 *
41 * Returns: (transfer full): the newly created #WebKitApplicationInfo.
42 *
43 * since: 2.18
44 */
45WebKitApplicationInfo* webkit_application_info_new()
46{
47 WebKitApplicationInfo* info = static_cast<WebKitApplicationInfo*>(fastMalloc(sizeof(WebKitApplicationInfo)));
48 new (info) WebKitApplicationInfo();
49 return info;
50}
51
52/**
53 * webkit_application_info_ref:
54 * @info: a #WebKitApplicationInfo
55 *
56 * Atomically increments the reference count of @info by one. This
57 * function is MT-safe and may be called from any thread.
58 *
59 * Returns: The passed in #WebKitApplicationInfo
60 *
61 * Since: 2.18
62 */
63WebKitApplicationInfo* webkit_application_info_ref(WebKitApplicationInfo* info)
64{
65 g_atomic_int_inc(&info->referenceCount);
66 return info;
67}
68
69/**
70 * webkit_application_info_unref:
71 * @info: a #WebKitApplicationInfo
72 *
73 * Atomically decrements the reference count of @info by one. If the
74 * reference count drops to 0, all memory allocated by the #WebKitApplicationInfo is
75 * released. This function is MT-safe and may be called from any
76 * thread.
77 *
78 * Since: 2.18
79 */
80void webkit_application_info_unref(WebKitApplicationInfo* info)
81{
82 if (g_atomic_int_dec_and_test(&info->referenceCount)) {
83 info->~WebKitApplicationInfo();
84 fastFree(info);
85 }
86}
87
88/**
89 * webkit_application_info_set_name:
90 * @info: a #WebKitApplicationInfo
91 * @name: the application name
92 *
93 * Set the name of the application. If not provided, or %NULL is passed,
94 * g_get_prgname() will be used.
95 *
96 * Since: 2.18
97 */
98void webkit_application_info_set_name(WebKitApplicationInfo* info, const char* name)
99{
100 g_return_if_fail(info);
101
102 info->name = name;
103}
104
105/**
106 * webkit_application_info_get_name:
107 * @info: a #WebKitApplicationInfo
108 *
109 * Get the name of the application. If webkit_application_info_set_name() hasn't been
110 * called with a valid name, this returns g_get_prgname().
111 *
112 * Returns: the application name
113 *
114 * Since: 2.18
115 */
116const char* webkit_application_info_get_name(WebKitApplicationInfo* info)
117{
118 g_return_val_if_fail(info, nullptr);
119
120 if (!info->name.isNull())
121 return info->name.data();
122
123 return g_get_prgname();
124}
125
126/**
127 * webkit_application_info_set_version:
128 * @info: a #WebKitApplicationInfo
129 * @major: the major version number
130 * @minor: the minor version number
131 * @micro: the micro version number
132 *
133 * Set the application version. If the application doesn't use the format
134 * major.minor.micro you can pass 0 as the micro to use major.minor, or pass
135 * 0 as both micro and minor to use only major number. Any other format must
136 * be converted to major.minor.micro so that it can be used in version comparisons.
137 *
138 * Since: 2.18
139 */
140void webkit_application_info_set_version(WebKitApplicationInfo* info, guint64 major, guint64 minor, guint64 micro)
141{
142 g_return_if_fail(info);
143
144 info->majorVersion = major;
145 info->minorVersion = minor;
146 info->microVersion = micro;
147}
148
149/**
150 * webkit_application_info_get_version:
151 * @info: a #WebKitApplicationInfo
152 * @major: (out): return location for the major version number
153 * @minor: (out) (allow-none): return location for the minor version number
154 * @micro: (out) (allow-none): return location for the micro version number
155 *
156 * Get the application version previously set with webkit_application_info_set_version().
157 *
158 * Since: 2.18
159 */
160void webkit_application_info_get_version(WebKitApplicationInfo* info, guint64* major, guint64* minor, guint64* micro)
161{
162 g_return_if_fail(info && major);
163
164 *major = info->majorVersion;
165 if (minor)
166 *minor = info->minorVersion;
167 if (micro)
168 *micro = info->microVersion;
169}
170