Changeset 149219 in webkit
- Timestamp:
- Apr 26, 2013 4:06:10 PM (11 years ago)
- Location:
- trunk/Source/WebKit2
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit2/ChangeLog
r149217 r149219 1 2013-04-26 Martin Robinson <mrobinson@igalia.com> 2 3 [GTK] Add methods to add a user style sheet to the WebKit2 GTK+ API 4 https://bugs.webkit.org/show_bug.cgi?id=99081 5 6 Reviewed by Carlos Garcia Campos, Gustavo Noronha Silva, and Benjamin Poulain. 7 8 Add methods to WebKitWebViewGroup to add and remove user style sheets. 9 This allows clients to inject style sheets into pages with a set of 10 rules for when those style sheets apply. 11 12 * UIProcess/API/gtk/WebKitWebViewGroup.cpp: 13 (toImmutableArray): Added this helper which converts the GList* parameters into 14 ImmutableArrays for use with the WebKit2 internal API. 15 (webkit_web_view_group_add_user_style_sheet): Added new API for adding a style sheet. 16 (webkit_web_view_group_remove_all_user_style_sheets): Add new API for clearing out all style sheets. 17 * UIProcess/API/gtk/WebKitWebViewGroup.h: Added new method declarations. 18 * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Added new API to the documentation. 19 * UIProcess/API/gtk/tests/TestWebKitWebViewGroup.cpp: Added a test for the new API. 20 (isStyleSheetInjectedForURLAtPath): Function to check whether the style sheet has been injected for a given URL. 21 (fillURLListFromPaths): Helper which converts paths passed via varargs into a whitelist or blacklist. 22 (removeOldInjectedStyleSheetsAndResetLists): Function to start afresh. 23 (testWebViewGroupInjectedStyleSheet): The actual test. 24 (serverCallback): Server callback for use with the test. We cannot use loadHTML or 25 loadAlternateHTML, because that checks the whitelist and blacklist against about:blank. 26 (beforeAll): Initialize the server and new test. 27 (afterAll): Clean up the server. 28 1 29 2013-04-26 Simon Cooper <scooper@apple.com> 2 30 -
trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewGroup.cpp
r149117 r149219 21 21 #include "WebKitWebViewGroup.h" 22 22 23 #include "ImmutableArray.h" 23 24 #include "WebKitPrivate.h" 24 25 #include "WebKitSettingsPrivate.h" … … 222 223 g_object_notify(G_OBJECT(group), "settings"); 223 224 } 225 226 COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_ALL, WebCore::InjectInAllFrames); 227 COMPILE_ASSERT_MATCHING_ENUM(WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY, WebCore::InjectInTopFrameOnly); 228 229 static PassRefPtr<ImmutableArray> toImmutableArray(const char* const* list) 230 { 231 if (!list) 232 return 0; 233 234 Vector<RefPtr<APIObject> > entries; 235 while (*list) { 236 entries.append(WebString::createFromUTF8String(*list)); 237 list++; 238 } 239 return ImmutableArray::adopt(entries); 240 } 241 242 /** 243 * webkit_web_view_group_add_user_style_sheet: 244 * @group: a #WebKitWebViewGroup 245 * @source: the source of the style_sheet to inject 246 * @base_uri: (allow-none): the base URI to use when processing the style_sheet contents or %NULL for about:blank 247 * @whitelist: (array zero-terminated=1) (allow-none): a whitelist of URI patterns or %NULL 248 * @blacklist: (array zero-terminated=1) (allow-none): a blacklist of URI patterns or %NULL 249 * @injected_frames: a #WebKitInjectedContentFrames describing to which frames the style_sheet should apply 250 * 251 * Inject an external style sheet into pages. It is possible to only apply the style sheet 252 * to some URIs by passing non-null values for @whitelist or @blacklist. Passing a %NULL 253 * whitelist implies that all URIs are on the whitelist. The style sheet is applied if a URI matches 254 * the whitelist and not the blacklist. URI patterns must be of the form [protocol]://[host]/[path] 255 * where the host and path components can contain the wildcard character ('*') to represent zero 256 * or more other characters. 257 */ 258 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) 259 { 260 g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group)); 261 g_return_if_fail(source); 262 263 RefPtr<ImmutableArray> webWhitelist = toImmutableArray(whitelist); 264 RefPtr<ImmutableArray> webBlacklist = toImmutableArray(blacklist); 265 266 // We always use UserStyleUserLevel to match the behavior of WKPageGroupAddUserStyleSheet. 267 group->priv->pageGroup->addUserStyleSheet( 268 String::fromUTF8(source), 269 String::fromUTF8(baseURI), 270 webWhitelist.get(), 271 webBlacklist.get(), 272 static_cast<WebCore::UserContentInjectedFrames>(injectedFrames), 273 WebCore::UserStyleUserLevel); 274 } 275 276 /** 277 * webkit_web_view_group_remove_all_user_style_sheets: 278 * @group: a #WebKitWebViewGroup 279 * 280 * Remove all style sheets previously injected into this #WebKitWebViewGroup 281 * via webkit_web_view_group_add_user_style_sheet(). 282 */ 283 void webkit_web_view_group_remove_all_user_style_sheets(WebKitWebViewGroup* group) 284 { 285 g_return_if_fail(WEBKIT_IS_WEB_VIEW_GROUP(group)); 286 group->priv->pageGroup->removeAllUserStyleSheets(); 287 } -
trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebViewGroup.h
r149117 r149219 52 52 }; 53 53 54 /** 55 * WebKitInjectedContentFrames: 56 * @WEBKIT_INJECTED_CONTENT_FRAMES_ALL: Content will be injected into all frames. 57 * @WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY: Content will only be injected into the main frame. 58 * 59 * Enum values used for determining into which frames content is injected. 60 */ 61 typedef enum { 62 WEBKIT_INJECTED_CONTENT_FRAMES_ALL, 63 WEBKIT_INJECTED_CONTENT_FRAMES_TOP_ONLY, 64 } WebKitInjectedContentFrames; 65 54 66 WEBKIT_API GType 55 webkit_web_view_group_get_type (void);67 webkit_web_view_group_get_type (void); 56 68 57 69 WEBKIT_API WebKitWebViewGroup * 58 webkit_web_view_group_new (const gchar*name);70 webkit_web_view_group_new (const gchar *name); 59 71 60 72 WEBKIT_API const gchar * 61 webkit_web_view_group_get_name (WebKitWebViewGroup*group);73 webkit_web_view_group_get_name (WebKitWebViewGroup *group); 62 74 63 75 WEBKIT_API WebKitSettings * 64 webkit_web_view_group_get_settings (WebKitWebViewGroup*group);76 webkit_web_view_group_get_settings (WebKitWebViewGroup *group); 65 77 66 78 WEBKIT_API void 67 webkit_web_view_group_set_settings (WebKitWebViewGroup *group, 68 WebKitSettings *settings); 79 webkit_web_view_group_set_settings (WebKitWebViewGroup *group, 80 WebKitSettings *settings); 81 82 WEBKIT_API void 83 webkit_web_view_group_add_user_style_sheet (WebKitWebViewGroup *group, 84 const gchar *source, 85 const gchar *base_uri, 86 const gchar * const *whitelist, 87 const gchar * const *blacklist, 88 WebKitInjectedContentFrames injected_frames); 89 90 WEBKIT_API void 91 webkit_web_view_group_remove_all_user_style_sheets (WebKitWebViewGroup *group); 69 92 70 93 G_END_DECLS -
trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt
r149117 r149219 946 946 <FILE>WebKitWebViewGroup</FILE> 947 947 WebKitWebViewGroup 948 WebKitInjectedContentFrames 948 949 webkit_web_view_group_new 949 950 webkit_web_view_group_get_name 950 951 webkit_web_view_group_get_settings 951 952 webkit_web_view_group_set_settings 953 webkit_web_view_group_add_user_style_sheet 954 webkit_web_view_group_remove_all_user_style_sheets 952 955 953 956 <SUBSECTION Standard> -
trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebViewGroup.cpp
r149117 r149219 20 20 #include "config.h" 21 21 22 #include "WebKitTestServer.h" 22 23 #include "WebViewTest.h" 24 #include <cstdarg> 23 25 #include <gtk/gtk.h> 24 26 #include <webkit2/webkit2.h> 25 27 #include <wtf/gobject/GRefPtr.h> 28 29 static WebKitTestServer* kServer; 30 31 // These are all here so that they can be changed easily, if necessary. 32 static const char* kStyleSheetHTML = "<html><div id=\"styledElement\">Sweet stylez!</div></html>"; 33 static const char* kInjectedStyleSheet = "#styledElement { font-weight: bold; }"; 34 static const char* kStyleSheetTestScript = "getComputedStyle(document.getElementById('styledElement'))['font-weight']"; 35 static const char* kStyleSheetTestScriptResult = "bold"; 26 36 27 37 static void testWebViewGroupDefault(Test* test, gconstpointer) … … 89 99 } 90 100 101 static bool isStyleSheetInjectedForURLAtPath(WebViewTest* test, const char* path) 102 { 103 test->loadURI(kServer->getURIForPath(path).data()); 104 test->waitUntilLoadFinished(); 105 106 GOwnPtr<GError> error; 107 WebKitJavascriptResult* javascriptResult = test->runJavaScriptAndWaitUntilFinished(kStyleSheetTestScript, &error.outPtr()); 108 g_assert(javascriptResult); 109 g_assert(!error.get()); 110 111 GOwnPtr<char> resultString(WebViewTest::javascriptResultToCString(javascriptResult)); 112 return !g_strcmp0(resultString.get(), kStyleSheetTestScriptResult); 113 } 114 115 static void fillURLListFromPaths(char** list, const char* path, ...) 116 { 117 va_list argumentList; 118 va_start(argumentList, path); 119 120 int i = 0; 121 while (path) { 122 // FIXME: We must use a wildcard for the host here until http://wkbug.com/112476 is fixed. 123 // Until that time patterns with port numbers in them will not properly match URLs with port numbers. 124 list[i++] = g_strdup_printf("http://*/%s*", path); 125 path = va_arg(argumentList, const char*); 126 } 127 } 128 129 static void removeOldInjectedStyleSheetsAndResetLists(WebKitWebViewGroup* group, char** whitelist, char** blacklist) 130 { 131 webkit_web_view_group_remove_all_user_style_sheets(group); 132 133 while (*whitelist) { 134 g_free(*whitelist); 135 *whitelist = 0; 136 whitelist++; 137 } 138 139 while (*blacklist) { 140 g_free(*blacklist); 141 *blacklist = 0; 142 blacklist++; 143 } 144 } 145 146 static void testWebViewGroupInjectedStyleSheet(WebViewTest* test, gconstpointer) 147 { 148 WebKitWebViewGroup* group = webkit_web_view_get_group(test->m_webView); 149 char* whitelist[3] = { 0, 0, 0 }; 150 char* blacklist[3] = { 0, 0, 0 }; 151 152 removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist); 153 154 // Without a whitelist or a blacklist all URLs should have the injected style sheet. 155 static const char* randomPath = "somerandompath"; 156 g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath)); 157 webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, 0, WEBKIT_INJECTED_CONTENT_FRAMES_ALL); 158 g_assert(isStyleSheetInjectedForURLAtPath(test, randomPath)); 159 160 removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist); 161 162 fillURLListFromPaths(blacklist, randomPath, 0); 163 webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, 0, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL); 164 g_assert(!isStyleSheetInjectedForURLAtPath(test, randomPath)); 165 g_assert(isStyleSheetInjectedForURLAtPath(test, "someotherrandompath")); 166 167 removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist); 168 169 static const char* inTheWhiteList = "inthewhitelist"; 170 static const char* notInWhitelist = "notinthewhitelist"; 171 static const char* inTheWhiteListAndBlackList = "inthewhitelistandblacklist"; 172 173 fillURLListFromPaths(whitelist, inTheWhiteList, inTheWhiteListAndBlackList, 0); 174 fillURLListFromPaths(blacklist, inTheWhiteListAndBlackList, 0); 175 webkit_web_view_group_add_user_style_sheet(group, kInjectedStyleSheet, 0, whitelist, blacklist, WEBKIT_INJECTED_CONTENT_FRAMES_ALL); 176 g_assert(isStyleSheetInjectedForURLAtPath(test, inTheWhiteList)); 177 g_assert(!isStyleSheetInjectedForURLAtPath(test, inTheWhiteListAndBlackList)); 178 g_assert(!isStyleSheetInjectedForURLAtPath(test, notInWhitelist)); 179 180 // It's important to clean up the environment before other tests. 181 removeOldInjectedStyleSheetsAndResetLists(group, whitelist, blacklist); 182 } 183 184 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer) 185 { 186 soup_message_set_status(message, SOUP_STATUS_OK); 187 soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, kStyleSheetHTML, strlen(kStyleSheetHTML)); 188 soup_message_body_complete(message->response_body); 189 } 190 91 191 void beforeAll() 92 192 { 193 kServer = new WebKitTestServer(); 194 kServer->run(serverCallback); 195 93 196 Test::add("WebKitWebViewGroup", "default-group", testWebViewGroupDefault); 94 197 Test::add("WebKitWebViewGroup", "new-group", testWebViewGroupNewGroup); 95 198 Test::add("WebKitWebView", "new-with-group", testWebViewNewWithGroup); 96 199 Test::add("WebKitWebViewGroup", "settings", testWebViewGroupSettings); 200 WebViewTest::add("WebKitWebViewGroup", "injected-style-sheet", testWebViewGroupInjectedStyleSheet); 97 201 } 98 202 99 203 void afterAll() 100 204 { 101 102 } 205 delete kServer; 206 }
Note: See TracChangeset
for help on using the changeset viewer.