1/*
2 * This file is part of the WebKit open source project.
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 "WebKitDOMMediaList.h"
22
23#include <WebCore/CSSImportRule.h>
24#include "DOMObjectCache.h"
25#include <WebCore/DOMException.h>
26#include <WebCore/Document.h>
27#include <WebCore/JSExecState.h>
28#include "WebKitDOMMediaListPrivate.h"
29#include "WebKitDOMPrivate.h"
30#include "ConvertToUTF8String.h"
31#include <wtf/GetPtr.h>
32#include <wtf/RefPtr.h>
33
34#define WEBKIT_DOM_MEDIA_LIST_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_MEDIA_LIST, WebKitDOMMediaListPrivate)
35
36typedef struct _WebKitDOMMediaListPrivate {
37 RefPtr<WebCore::MediaList> coreObject;
38} WebKitDOMMediaListPrivate;
39
40G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
41
42namespace WebKit {
43
44WebKitDOMMediaList* kit(WebCore::MediaList* obj)
45{
46 if (!obj)
47 return 0;
48
49 if (gpointer ret = DOMObjectCache::get(obj))
50 return WEBKIT_DOM_MEDIA_LIST(ret);
51
52 return wrapMediaList(obj);
53}
54
55WebCore::MediaList* core(WebKitDOMMediaList* request)
56{
57 return request ? static_cast<WebCore::MediaList*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
58}
59
60WebKitDOMMediaList* wrapMediaList(WebCore::MediaList* coreObject)
61{
62 ASSERT(coreObject);
63 return WEBKIT_DOM_MEDIA_LIST(g_object_new(WEBKIT_DOM_TYPE_MEDIA_LIST, "core-object", coreObject, nullptr));
64}
65
66} // namespace WebKit
67
68G_DEFINE_TYPE(WebKitDOMMediaList, webkit_dom_media_list, WEBKIT_DOM_TYPE_OBJECT)
69
70enum {
71 DOM_MEDIA_LIST_PROP_0,
72 DOM_MEDIA_LIST_PROP_MEDIA_TEXT,
73 DOM_MEDIA_LIST_PROP_LENGTH,
74};
75
76static void webkit_dom_media_list_finalize(GObject* object)
77{
78 WebKitDOMMediaListPrivate* priv = WEBKIT_DOM_MEDIA_LIST_GET_PRIVATE(object);
79
80 WebKit::DOMObjectCache::forget(priv->coreObject.get());
81
82 priv->~WebKitDOMMediaListPrivate();
83 G_OBJECT_CLASS(webkit_dom_media_list_parent_class)->finalize(object);
84}
85
86static void webkit_dom_media_list_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
87{
88 WebKitDOMMediaList* self = WEBKIT_DOM_MEDIA_LIST(object);
89
90 switch (propertyId) {
91 case DOM_MEDIA_LIST_PROP_MEDIA_TEXT:
92 webkit_dom_media_list_set_media_text(self, g_value_get_string(value), nullptr);
93 break;
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
96 break;
97 }
98}
99
100static void webkit_dom_media_list_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
101{
102 WebKitDOMMediaList* self = WEBKIT_DOM_MEDIA_LIST(object);
103
104 switch (propertyId) {
105 case DOM_MEDIA_LIST_PROP_MEDIA_TEXT:
106 g_value_take_string(value, webkit_dom_media_list_get_media_text(self));
107 break;
108 case DOM_MEDIA_LIST_PROP_LENGTH:
109 g_value_set_ulong(value, webkit_dom_media_list_get_length(self));
110 break;
111 default:
112 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
113 break;
114 }
115}
116
117static GObject* webkit_dom_media_list_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
118{
119 GObject* object = G_OBJECT_CLASS(webkit_dom_media_list_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
120
121 WebKitDOMMediaListPrivate* priv = WEBKIT_DOM_MEDIA_LIST_GET_PRIVATE(object);
122 priv->coreObject = static_cast<WebCore::MediaList*>(WEBKIT_DOM_OBJECT(object)->coreObject);
123 WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
124
125 return object;
126}
127
128static void webkit_dom_media_list_class_init(WebKitDOMMediaListClass* requestClass)
129{
130 GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);
131 g_type_class_add_private(gobjectClass, sizeof(WebKitDOMMediaListPrivate));
132 gobjectClass->constructor = webkit_dom_media_list_constructor;
133 gobjectClass->finalize = webkit_dom_media_list_finalize;
134 gobjectClass->set_property = webkit_dom_media_list_set_property;
135 gobjectClass->get_property = webkit_dom_media_list_get_property;
136
137 g_object_class_install_property(
138 gobjectClass,
139 DOM_MEDIA_LIST_PROP_MEDIA_TEXT,
140 g_param_spec_string(
141 "media-text",
142 "MediaList:media-text",
143 "read-write gchar* MediaList:media-text",
144 "",
145 WEBKIT_PARAM_READWRITE));
146
147 g_object_class_install_property(
148 gobjectClass,
149 DOM_MEDIA_LIST_PROP_LENGTH,
150 g_param_spec_ulong(
151 "length",
152 "MediaList:length",
153 "read-only gulong MediaList:length",
154 0, G_MAXULONG, 0,
155 WEBKIT_PARAM_READABLE));
156
157}
158
159static void webkit_dom_media_list_init(WebKitDOMMediaList* request)
160{
161 WebKitDOMMediaListPrivate* priv = WEBKIT_DOM_MEDIA_LIST_GET_PRIVATE(request);
162 new (priv) WebKitDOMMediaListPrivate();
163}
164
165gchar* webkit_dom_media_list_item(WebKitDOMMediaList* self, gulong index)
166{
167 WebCore::JSMainThreadNullState state;
168 g_return_val_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self), 0);
169 WebCore::MediaList* item = WebKit::core(self);
170 gchar* result = convertToUTF8String(item->item(index));
171 return result;
172}
173
174void webkit_dom_media_list_delete_medium(WebKitDOMMediaList* self, const gchar* oldMedium, GError** error)
175{
176 WebCore::JSMainThreadNullState state;
177 g_return_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self));
178 g_return_if_fail(oldMedium);
179 g_return_if_fail(!error || !*error);
180 WebCore::MediaList* item = WebKit::core(self);
181 WTF::String convertedOldMedium = WTF::String::fromUTF8(oldMedium);
182 auto result = item->deleteMedium(convertedOldMedium);
183 if (result.hasException()) {
184 auto description = WebCore::DOMException::description(result.releaseException().code());
185 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
186 }
187}
188
189void webkit_dom_media_list_append_medium(WebKitDOMMediaList* self, const gchar* newMedium, GError** error)
190{
191 WebCore::JSMainThreadNullState state;
192 g_return_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self));
193 g_return_if_fail(newMedium);
194 g_return_if_fail(!error || !*error);
195 WebCore::MediaList* item = WebKit::core(self);
196 WTF::String convertedNewMedium = WTF::String::fromUTF8(newMedium);
197 item->appendMedium(convertedNewMedium);
198}
199
200gchar* webkit_dom_media_list_get_media_text(WebKitDOMMediaList* self)
201{
202 WebCore::JSMainThreadNullState state;
203 g_return_val_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self), 0);
204 WebCore::MediaList* item = WebKit::core(self);
205 gchar* result = convertToUTF8String(item->mediaText());
206 return result;
207}
208
209void webkit_dom_media_list_set_media_text(WebKitDOMMediaList* self, const gchar* value, GError** error)
210{
211 WebCore::JSMainThreadNullState state;
212 g_return_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self));
213 g_return_if_fail(value);
214 g_return_if_fail(!error || !*error);
215 WebCore::MediaList* item = WebKit::core(self);
216 WTF::String convertedValue = WTF::String::fromUTF8(value);
217 auto result = item->setMediaText(convertedValue);
218 if (result.hasException()) {
219 auto description = WebCore::DOMException::description(result.releaseException().code());
220 g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), description.legacyCode, description.name);
221 }
222}
223
224gulong webkit_dom_media_list_get_length(WebKitDOMMediaList* self)
225{
226 WebCore::JSMainThreadNullState state;
227 g_return_val_if_fail(WEBKIT_DOM_IS_MEDIA_LIST(self), 0);
228 WebCore::MediaList* item = WebKit::core(self);
229 gulong result = item->length();
230 return result;
231}
232
233G_GNUC_END_IGNORE_DEPRECATIONS;
234