|
Line 0
a/Source/WebKit2/UIProcess/API/gtk/WebKitUserContent.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2014 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 "WebKitUserContent.h" |
| 22 |
|
| 23 |
#include "WebKitPrivate.h" |
| 24 |
#include "WebKitUserContentPrivate.h" |
| 25 |
|
| 26 |
#include <WebCore/page/UserScript.h> |
| 27 |
#include <WebCore/page/UserStyleSheet.h> |
| 28 |
#include <wtf/text/CString.h> |
| 29 |
#include <wtf/text/StringBuilder.h> |
| 30 |
|
| 31 |
using namespace WebCore; |
| 32 |
|
| 33 |
|
| 34 |
static inline UserContentInjectedFrames toUserContentInjectedFrames(WebKitUserContentInjectedFrames injectedFrames) |
| 35 |
{ |
| 36 |
switch (injectedFrames) { |
| 37 |
case WEBKIT_USER_CONTENT_INJECT_TOP_FRAME: |
| 38 |
return InjectInTopFrameOnly; |
| 39 |
case WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES: |
| 40 |
return InjectInAllFrames; |
| 41 |
default: |
| 42 |
ASSERT_NOT_REACHED(); |
| 43 |
return InjectInAllFrames; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
static inline UserScriptInjectionTime toUserScriptInjectionTime(WebKitUserScriptInjectionTime injectionTime) |
| 49 |
{ |
| 50 |
switch (injectionTime) { |
| 51 |
case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START: |
| 52 |
return InjectAtDocumentStart; |
| 53 |
case WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END: |
| 54 |
return InjectAtDocumentEnd; |
| 55 |
default: |
| 56 |
ASSERT_NOT_REACHED(); |
| 57 |
return InjectAtDocumentStart; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static inline UserStyleLevel toUserStyleLevel(WebKitUserStyleLevel styleLevel) |
| 63 |
{ |
| 64 |
switch (styleLevel) { |
| 65 |
case WEBKIT_USER_STYLE_LEVEL_USER: |
| 66 |
return UserStyleUserLevel; |
| 67 |
case WEBKIT_USER_STYLE_LEVEL_AUTHOR: |
| 68 |
return UserStyleAuthorLevel; |
| 69 |
default: |
| 70 |
ASSERT_NOT_REACHED(); |
| 71 |
return UserStyleAuthorLevel; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
struct _WebKitUserContentPrivate { |
| 77 |
_WebKitUserContentPrivate() |
| 78 |
: injectedFrames(WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES) { } |
| 79 |
|
| 80 |
CString source; |
| 81 |
Vector<String> whitelist; |
| 82 |
Vector<String> blacklist; |
| 83 |
WebKitUserContentInjectedFrames injectedFrames; |
| 84 |
}; |
| 85 |
|
| 86 |
WEBKIT_DEFINE_TYPE(WebKitUserContent, webkit_user_content, G_TYPE_OBJECT) |
| 87 |
|
| 88 |
|
| 89 |
static void webkit_user_content_class_init(WebKitUserContentClass*) |
| 90 |
{ |
| 91 |
// Empty |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* webkit_user_content_set_source: |
| 96 |
*/ |
| 97 |
void webkit_user_content_set_source(WebKitUserContent* user_content, const gchar* source) |
| 98 |
{ |
| 99 |
g_return_if_fail(WEBKIT_IS_USER_CONTENT(user_content)); |
| 100 |
if (!g_strcmp0(user_content->priv->source.data(), source)) |
| 101 |
return; |
| 102 |
user_content->priv->source = source; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* webkit_user_content_get_source: |
| 107 |
*/ |
| 108 |
const gchar* webkit_user_content_get_source(WebKitUserContent* user_content) |
| 109 |
{ |
| 110 |
g_return_val_if_fail(WEBKIT_IS_USER_CONTENT(user_content), nullptr); |
| 111 |
return user_content->priv->source.data(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* webkit_user_content_set_injected_frames: |
| 116 |
*/ |
| 117 |
void webkit_user_content_set_injected_frames(WebKitUserContent* user_content, WebKitUserContentInjectedFrames injected_frames) |
| 118 |
{ |
| 119 |
g_return_if_fail(WEBKIT_IS_USER_CONTENT(user_content)); |
| 120 |
user_content->priv->injectedFrames = injected_frames; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* webkit_user_content_get_injected_frames: |
| 125 |
*/ |
| 126 |
WebKitUserContentInjectedFrames webkit_user_content_get_injected_frames(WebKitUserContent* user_content) |
| 127 |
{ |
| 128 |
g_return_val_if_fail(WEBKIT_IS_USER_CONTENT(user_content), WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES); |
| 129 |
return user_content->priv->injectedFrames; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* webkit_user_content_set_whitelist: |
| 134 |
*/ |
| 135 |
void webkit_user_content_set_whitelist(WebKitUserContent* user_content, GList* whitelist) |
| 136 |
{ |
| 137 |
g_return_if_fail(WEBKIT_IS_USER_CONTENT(user_content)); |
| 138 |
|
| 139 |
user_content->priv->whitelist.clear(); |
| 140 |
for (GList *l = whitelist; l != nullptr; l = l->next) |
| 141 |
user_content->priv->whitelist.append(String::fromUTF8(reinterpret_cast<const gchar*>(l->data))); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* webkit_user_content_get_whitelist: |
| 146 |
*/ |
| 147 |
GList* webkit_user_content_get_whitelist(WebKitUserContent* user_content) |
| 148 |
{ |
| 149 |
g_return_val_if_fail(WEBKIT_IS_USER_CONTENT(user_content), nullptr); |
| 150 |
|
| 151 |
GList* result = nullptr; |
| 152 |
for (auto i = user_content->priv->whitelist.rend(); i != user_content->priv->whitelist.rbegin(); --i) |
| 153 |
result = g_list_prepend(result, g_strdup(i->utf8().data())); |
| 154 |
|
| 155 |
return result; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* webkit_user_content_set_blacklist: |
| 160 |
*/ |
| 161 |
void webkit_user_content_set_blacklist(WebKitUserContent* user_content, GList* blacklist) |
| 162 |
{ |
| 163 |
g_return_if_fail(WEBKIT_IS_USER_CONTENT(user_content)); |
| 164 |
|
| 165 |
user_content->priv->blacklist.clear(); |
| 166 |
for (GList *l = blacklist; l != nullptr; l = l->next) |
| 167 |
user_content->priv->blacklist.append(String::fromUTF8(reinterpret_cast<const gchar*>(l->data))); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* webkit_user_content_get_blacklist: |
| 172 |
*/ |
| 173 |
GList* webkit_user_content_get_blacklist(WebKitUserContent* user_content) |
| 174 |
{ |
| 175 |
g_return_val_if_fail(WEBKIT_IS_USER_CONTENT(user_content), nullptr); |
| 176 |
|
| 177 |
GList* result = nullptr; |
| 178 |
for (auto i = user_content->priv->blacklist.rend(); i != user_content->priv->blacklist.rbegin(); --i) |
| 179 |
result = g_list_prepend(result, g_strdup(i->utf8().data())); |
| 180 |
|
| 181 |
return result; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
struct _WebKitUserScriptPrivate { |
| 186 |
_WebKitUserScriptPrivate() |
| 187 |
: injectionTime(WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START) { } |
| 188 |
|
| 189 |
WebKitUserScriptInjectionTime injectionTime; |
| 190 |
}; |
| 191 |
|
| 192 |
WEBKIT_DEFINE_TYPE(WebKitUserScript, webkit_user_script, WEBKIT_TYPE_USER_CONTENT) |
| 193 |
|
| 194 |
|
| 195 |
static void webkit_user_script_class_init(WebKitUserScriptClass*) |
| 196 |
{ |
| 197 |
// Empty |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* webkit_user_script_new: |
| 202 |
* @source: (transfer none): Source code of the user script. |
| 203 |
* |
| 204 |
* Returns: (transfer full): A new #WebKitUserScript. |
| 205 |
*/ |
| 206 |
WebKitUserContent* webkit_user_script_new(const gchar* source) |
| 207 |
{ |
| 208 |
WebKitUserContent* userContent = WEBKIT_USER_CONTENT(g_object_new(WEBKIT_TYPE_USER_SCRIPT, nullptr)); |
| 209 |
userContent->priv->source = source; |
| 210 |
return userContent; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* webkit_user_script_set_injection_time: |
| 215 |
*/ |
| 216 |
void webkit_user_script_set_injection_time(WebKitUserScript* userScript, WebKitUserScriptInjectionTime injectionTime) |
| 217 |
{ |
| 218 |
g_return_if_fail(WEBKIT_IS_USER_SCRIPT(userScript)); |
| 219 |
userScript->priv->injectionTime = injectionTime; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* webkit_user_script_get_injection_time: |
| 224 |
*/ |
| 225 |
WebKitUserScriptInjectionTime webkit_user_script_get_injection_time(WebKitUserScript* userScript) |
| 226 |
{ |
| 227 |
g_return_val_if_fail(WEBKIT_IS_USER_SCRIPT(userScript), WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END); |
| 228 |
return userScript->priv->injectionTime; |
| 229 |
} |
| 230 |
|
| 231 |
UserScript webkitWebKitUserScriptToUserScript(WebKitUserScript* script, guint64 scriptId) |
| 232 |
{ |
| 233 |
g_assert(WEBKIT_IS_USER_SCRIPT(script)); |
| 234 |
|
| 235 |
StringBuilder urlStringBuilder; |
| 236 |
urlStringBuilder.append("user-script:"); |
| 237 |
urlStringBuilder.appendNumber(scriptId); |
| 238 |
|
| 239 |
return UserScript { |
| 240 |
String::fromUTF8(script->parent.priv->source.data(), script->parent.priv->source.length()), |
| 241 |
URL { URL { }, urlStringBuilder.toString() }, |
| 242 |
script->parent.priv->whitelist, |
| 243 |
script->parent.priv->blacklist, |
| 244 |
toUserScriptInjectionTime(script->priv->injectionTime), |
| 245 |
toUserContentInjectedFrames(script->parent.priv->injectedFrames) |
| 246 |
}; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
struct _WebKitUserStyleSheetPrivate { |
| 251 |
_WebKitUserStyleSheetPrivate() |
| 252 |
: level(WEBKIT_USER_STYLE_LEVEL_USER) { } |
| 253 |
|
| 254 |
WebKitUserStyleLevel level; |
| 255 |
}; |
| 256 |
|
| 257 |
WEBKIT_DEFINE_TYPE(WebKitUserStyleSheet, webkit_user_style_sheet, WEBKIT_TYPE_USER_CONTENT) |
| 258 |
|
| 259 |
|
| 260 |
static void webkit_user_style_sheet_class_init(WebKitUserStyleSheetClass*) |
| 261 |
{ |
| 262 |
// Empty |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* webkit_user_style_sheet_new: |
| 267 |
* @source: (transfer none): Source code of the user style sheet. |
| 268 |
* |
| 269 |
* Returns: (transfer full): A new #WebKitUserStyleSheet object. |
| 270 |
*/ |
| 271 |
WebKitUserContent* webkit_user_style_sheet_new(const gchar* source) |
| 272 |
{ |
| 273 |
WebKitUserContent* userContent = WEBKIT_USER_CONTENT(g_object_new(WEBKIT_TYPE_USER_STYLE_SHEET, nullptr)); |
| 274 |
userContent->priv->source = g_strdup(source); |
| 275 |
return userContent; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* webkit_user_style_sheet_set_level: |
| 280 |
*/ |
| 281 |
void webkit_user_style_sheet_set_level(WebKitUserStyleSheet* userStyleSheet, WebKitUserStyleLevel level) |
| 282 |
{ |
| 283 |
g_return_if_fail(WEBKIT_IS_USER_STYLE_SHEET(userStyleSheet)); |
| 284 |
userStyleSheet->priv->level = level; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* webkit_user_style_sheet_get_level: |
| 289 |
*/ |
| 290 |
WebKitUserStyleLevel webkit_user_style_sheet_get_level(WebKitUserStyleSheet* userStyleSheet) |
| 291 |
{ |
| 292 |
g_return_val_if_fail(WEBKIT_IS_USER_STYLE_SHEET(userStyleSheet), WEBKIT_USER_STYLE_LEVEL_USER); |
| 293 |
return userStyleSheet->priv->level; |
| 294 |
} |
| 295 |
|
| 296 |
UserStyleSheet webkitWebKitUserStyleSheetToUserStyleSheet(WebKitUserStyleSheet* styleSheet, guint64 styleSheetId) |
| 297 |
{ |
| 298 |
g_assert(WEBKIT_IS_USER_STYLE_SHEET(styleSheet)); |
| 299 |
|
| 300 |
StringBuilder urlStringBuilder; |
| 301 |
urlStringBuilder.append("user-style-sheet:"); |
| 302 |
urlStringBuilder.appendNumber(styleSheetId); |
| 303 |
|
| 304 |
return UserStyleSheet { |
| 305 |
String::fromUTF8(styleSheet->parent.priv->source.data(), styleSheet->parent.priv->source.length()), |
| 306 |
URL { URL { }, urlStringBuilder.toString() }, |
| 307 |
styleSheet->parent.priv->whitelist, |
| 308 |
styleSheet->parent.priv->blacklist, |
| 309 |
toUserContentInjectedFrames(styleSheet->parent.priv->injectedFrames), |
| 310 |
toUserStyleLevel(styleSheet->priv->level) |
| 311 |
}; |
| 312 |
} |