1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* Copyright (C) 2013 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitWebViewGroup.h"
#include "ImmutableArray.h"
#include "WebKitPrivate.h"
#include "WebKitSettingsPrivate.h"
#include "WebKitWebViewGroupPrivate.h"
#include <glib/gi18n-lib.h>
#include <wtf/gobject/GRefPtr.h>
#include <wtf/text/CString.h>
using namespace WebKit;
/**
* SECTION: WebKitWebViewGroup
* @Short_description: Group of web views
* @Title: WebKitWebViewGroup
* @See_also: #WebKitWebView, #WebKitSettings
*
* A WebKitWebViewGroup represents a group of #WebKitWebView<!-- -->s that
* share things like settings. There's a default WebKitWebViewGroup where
* all #WebKitWebView<!-- -->s of the same #WebKitWebContext are added by default.
* To create a #WebKitWebView in a different WebKitWebViewGroup you can use
* webkit_web_view_new_with_group().
*
* WebKitWebViewGroups are identified by a unique name given when the group is
* created with webkit_web_view_group_new().
* WebKitWebViewGroups have a #WebKitSettings to control the settings of all
* #WebKitWebView<!-- -->s of the group. You can get the settings with
* webkit_web_view_group_get_settings() to handle the settings, or you can set
* your own #WebKitSettings with webkit_web_view_group_set_settings(). When
* the #WebKitSettings of a WebKitWebViewGroup changes, the signal notify::settings
* is emitted on the group.
*/
enum {
PROP_0,
PROP_SETTINGS
};
struct _WebKitWebViewGroupPrivate {
RefPtr<WebPageGroup> pageGroup;
CString name;
GRefPtr<WebKitSettings> settings;
};
WEBKIT_DEFINE_TYPE(WebKitWebViewGroup, webkit_web_view_group, G_TYPE_OBJECT)
static void webkitWebViewGroupSetProperty(GObject* object, guint propId, const GValue* value, GParamSpec* paramSpec)
{
WebKitWebViewGroup* group = WEBKIT_WEB_VIEW_GROUP(object);
switch (propId) {
case PROP_SETTINGS:
webkit_web_view_group_set_settings(group, WEBKIT_SETTINGS(g_value_get_object(value)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
}
}
static void webkitWebViewGroupGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
WebKitWebViewGroup* group = WEBKIT_WEB_VIEW_GROUP(object);
switch (propId) {
case PROP_SETTINGS:
g_value_set_object(value, webkit_web_view_group_get_settings(group));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
}
}
static void webkitWebViewGroupConstructed(GObject* object)
{
G_OBJECT_CLASS(webkit_web_view_group_parent_class)->constructed(object);
WebKitWebViewGroupPrivate* priv = WEBKIT_WEB_VIEW_GROUP(object)->priv;
priv->settings = adoptGRef(webkit_settings_new());
}
static void webkit_web_view_group_class_init(WebKitWebViewGroupClass* hitTestResultClass)
{
GObjectClass* objectClass = G_OBJECT_CLASS(hitTestResultClass);
objectClass->set_property = webkitWebViewGroupSetProperty;
objectClass->get_property = webkitWebViewGroupGetProperty;
objectClass->constructed = webkitWebViewGroupConstructed;
/**
* WebKitWebViewGroup:settings:
*
* The #WebKitSettings of the web view group.
*/
g_object_class_install_property(
objectClass,
PROP_SETTINGS,
g_param_spec_object(
"settings",
_("Settings"),
_("The settings of the web view group"),
WEBKIT_TYPE_SETTINGS,
WEBKIT_PARAM_READWRITE));
}
static void webkitWebViewGroupAttachSettingsToPageGroup(WebKitWebViewGroup* group)
{
group->priv->pageGroup->setPreferences(webkitSettingsGetPreferences(group->priv->settings.get()));
}
WebKitWebViewGroup* webkitWebViewGroupCreate(WebPageGroup* pageGroup)
{
WebKitWebViewGroup* group = WEBKIT_WEB_VIEW_GROUP(g_object_new(WEBKIT_TYPE_WEB_VIEW_GROUP, NULL));
group->priv->pageGroup = pageGroup;
webkitWebViewGroupAttachSettingsToPageGroup(group);
return group;
}
WebPageGroup* webkitWebViewGroupGetPageGroup(WebKitWebViewGroup* group)
{
return group->priv->pageGroup.get();
}
/**
* webkit_web_view_group_new:
* @name: (allow-none): the name of the group
*
* Creates a new #WebKitWebViewGroup with the given @name.
* If @name is %NULL a unique identifier name will be created
* automatically.
* The newly created #WebKitWebViewGroup doesn't contain any
* #WebKitWebView, web views are added to the new group when created
* with webkit_web_view_new_with_group() passing the group.
*
* Returns: (transfer full): a new #WebKitWebViewGroup
*/
WebKitWebViewGroup* webkit_web_view_group_new(const char* name)
{
WebKitWebViewGroup* group = WEBKIT_WEB_VIEW_GROUP(g_object_new(WEBKIT_TYPE_WEB_VIEW_GROUP, NULL));
group->priv->pageGroup = WebPageGroup::create(name ? String::fromUTF8(name) : String());
webkitWebViewGroupAttachSettingsToPageGroup(group);
return group;
}
/**
* webkit_web_view_group_get_name:
* @group: a #WebKitWebViewGroup
*
* Gets the name that uniquely identifies the #WebKitWebViewGroup.
*
* Returns: the name of @group
*/
const char* webkit_web_view_group_get_name(WebKitWebViewGroup* group)
{
g_return_val_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group), 0);
WebKitWebViewGroupPrivate* priv = group->priv;
if (priv->name.isNull())
priv->name = priv->pageGroup->identifier().utf8();
return priv->name.data();
}
/**
* webkit_web_view_group_get_settings:
* @group: a #WebKitWebViewGroup
*
* Gets the #WebKitSettings of the #WebKitWebViewGroup.
*
* Returns: (transfer none): the settings of @group
*/
WebKitSettings* webkit_web_view_group_get_settings(WebKitWebViewGroup* group)
{
g_return_val_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group), 0);
return group->priv->settings.get();
}
/**
* webkit_web_view_group_set_settings:
* @group: a #WebKitWebViewGroup
* @settings: a #WebKitSettings
*
* Sets a new #WebKitSettings for the #WebKitWebViewGroup. The settings will
* affect to all the #WebKitWebView<!-- -->s of the group.
* #WebKitWebViewGroup<!-- -->s always have a #WebKitSettings so if you just want to
* modify a setting you can use webkit_web_view_group_get_settings() and modify the
* returned #WebKitSettings instead.
* Setting the same #WebKitSettings multiple times doesn't have any effect.
* You can monitor the settings of a #WebKitWebViewGroup by connecting to the
* notify::settings signal of @group.
*/
void webkit_web_view_group_set_settings(WebKitWebViewGroup* group, WebKitSettings* settings)
{
g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group));
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
if (group->priv->settings == settings)
return;
group->priv->settings = settings;
webkitWebViewGroupAttachSettingsToPageGroup(group);
g_object_notify(G_OBJECT(group), "settings");
}
COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_ALL, WebCore::InjectInAllFrames);
COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY, WebCore::InjectInTopFrameOnly);
static PassRefPtr<ImmutableArray> toImmutableArray(const char* const* list)
{
if (!list)
return 0;
Vector<RefPtr<APIObject> > entries;
while (*list) {
entries.append(WebString::createFromUTF8String(*list));
list++;
}
return ImmutableArray::adopt(entries);
}
/**
* webkit_web_view_group_add_user_style_sheet:
* @group: a #WebKitWebViewGroup
* @source: the source of the style_sheet to inject
* @base_uri: (allow-none): the base URI to use when processing the style_sheet contents or %NULL for about:blank
* @whitelist: (array zero-terminated=1) (allow-none): a whitelist of URI patterns or %NULL
* @blacklist: (array zero-terminated=1) (allow-none): a blacklist of URI patterns or %NULL
* @injected_frames: a #WebKitInjectedContentFrames describing to which frames the style_sheet should apply
*
* Inject an external style sheet into pages. It is possible to only apply the style sheet
* to some URIs by passing non-null values for @whitelist or @blacklist. Passing a %NULL
* whitelist implies that all URIs are on the whitelist. The style sheet is applied if a URI matches
* the whitelist and not the blacklist. URI patterns must be of the form [protocol]://[host]/[path]
* where the host and path components can contain the wildcard character ('*') to represent zero
* or more other characters.
*/
void webkit_web_view_group_add_user_style_sheet(WebKitWebViewGroup* group, const char* source, const char* baseURI, const char* const* whitelist, const char* const* blacklist, WebKitInjectedContentFrames injectedFrames)
{
g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group));
g_return_if_fail(source);
RefPtr<ImmutableArray> webWhitelist = toImmutableArray(whitelist);
RefPtr<ImmutableArray> webBlacklist = toImmutableArray(blacklist);
// We always use UserStyleUserLevel to match the behavior of WKPageGroupAddUserStyleSheet.
group->priv->pageGroup->addUserStyleSheet(
String::fromUTF8(source),
String::fromUTF8(baseURI),
webWhitelist.get(),
webBlacklist.get(),
static_cast<WebCore::UserContentInjectedFrames>(injectedFrames),
WebCore::UserStyleUserLevel);
}
/**
* webkit_web_view_group_remove_all_user_style_sheets:
* @group: a #WebKitWebViewGroup
*
* Remove all style sheets previously injected into this #WebKitWebViewGroup
* via webkit_web_view_group_add_user_style_sheet().
*/
void webkit_web_view_group_remove_all_user_style_sheets(WebKitWebViewGroup* group)
{
g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group));
group->priv->pageGroup->removeAllUserStyleSheets();
}
|