Changeset 103928 in webkit
- Timestamp:
- Jan 3, 2012 12:10:30 AM (12 years ago)
- Location:
- trunk/Tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Tools/ChangeLog
r103927 r103928 1 2011-12-04 Philippe Normand <pnormand@igalia.com> 2 3 [WK2][GTK] WebSettings support in MiniBrowser 4 https://bugs.webkit.org/show_bug.cgi?id=73773 5 6 Reviewed by Martin Robinson. 7 8 Patch heavily inspired from the WebKit GtkLauncher to support 9 command-line WebSettings. Example: --enable-fullscreen=TRUE. 10 The full list of options can be consulted with --help-websettings. 11 12 * MiniBrowser/gtk/main.c: 13 (loadURI): 14 (parseOptionEntryCallback): 15 (isValidParameterType): 16 (getOptionEntriesFromWebKitSettings): 17 (addSettingsGroupToContext): 18 (main): 19 1 20 2012-01-02 Philippe Normand <pnormand@igalia.com> 2 21 -
trunk/Tools/MiniBrowser/gtk/main.c
r101710 r103928 27 27 28 28 #include "BrowserWindow.h" 29 #include <errno.h> 30 #include <gtk/gtk.h> 31 #include <string.h> 29 32 #include <webkit2/webkit2.h> 30 #include <gtk/gtk.h>31 33 32 34 static const gchar **uriArguments = NULL; … … 41 43 } 42 44 43 static void loadURI(const gchar *uri)45 static void createBrowserWindow(const gchar *uri, WebKitSettings *webkitSettings) 44 46 { 45 47 GtkWidget *webView = webkit_web_view_new(); 46 48 GtkWidget *mainWindow = browser_window_new(WEBKIT_WEB_VIEW(webView)); 47 49 gchar *url = argumentToURL(uri); 50 51 if (webkitSettings) { 52 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webView), webkitSettings); 53 g_object_unref(webkitSettings); 54 } 55 48 56 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webView), url); 49 57 g_free(url); … … 59 67 }; 60 68 69 static gboolean parseOptionEntryCallback(const gchar *optionNameFull, const gchar *value, WebKitSettings *webSettings, GError **error) 70 { 71 if (strlen(optionNameFull) <= 2) { 72 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Invalid option %s", optionNameFull); 73 return FALSE; 74 } 75 76 /* We have two -- in option name so remove them. */ 77 const gchar *optionName = optionNameFull + 2; 78 GParamSpec *spec = g_object_class_find_property(G_OBJECT_GET_CLASS(webSettings), optionName); 79 if (!spec) { 80 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Cannot find web settings for option %s", optionNameFull); 81 return FALSE; 82 } 83 84 switch (G_PARAM_SPEC_VALUE_TYPE(spec)) { 85 case G_TYPE_BOOLEAN: { 86 gboolean propertyValue = !(value && g_ascii_strcasecmp(value, "true") && strcmp(value, "1")); 87 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL); 88 break; 89 } 90 case G_TYPE_STRING: 91 g_object_set(G_OBJECT(webSettings), optionName, value, NULL); 92 break; 93 case G_TYPE_INT: { 94 glong propertyValue; 95 gchar *end; 96 97 errno = 0; 98 propertyValue = g_ascii_strtoll(value, &end, 0); 99 if (errno == ERANGE || propertyValue > G_MAXINT || propertyValue < G_MININT) { 100 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Integer value '%s' for %s out of range", value, optionNameFull); 101 return FALSE; 102 } 103 if (errno || value == end) { 104 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse integer value '%s' for %s", value, optionNameFull); 105 return FALSE; 106 } 107 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL); 108 break; 109 } 110 case G_TYPE_FLOAT: { 111 gdouble propertyValue; 112 gchar *end; 113 114 errno = 0; 115 propertyValue = g_ascii_strtod(value, &end); 116 if (errno == ERANGE || propertyValue > G_MAXFLOAT || propertyValue < G_MINFLOAT) { 117 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Float value '%s' for %s out of range", value, optionNameFull); 118 return FALSE; 119 } 120 if (errno || value == end) { 121 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, "Cannot parse float value '%s' for %s", value, optionNameFull); 122 return FALSE; 123 } 124 g_object_set(G_OBJECT(webSettings), optionName, propertyValue, NULL); 125 break; 126 } 127 default: 128 g_assert_not_reached(); 129 } 130 131 return TRUE; 132 } 133 134 static gboolean isValidParameterType(GType gParamType) 135 { 136 return (gParamType == G_TYPE_BOOLEAN || gParamType == G_TYPE_STRING || gParamType == G_TYPE_INT 137 || gParamType == G_TYPE_FLOAT); 138 } 139 140 static GOptionEntry* getOptionEntriesFromWebKitSettings(WebKitSettings *webSettings) 141 { 142 GParamSpec **propertySpecs; 143 GOptionEntry *optionEntries; 144 guint numProperties, numEntries, i; 145 146 propertySpecs = g_object_class_list_properties(G_OBJECT_GET_CLASS(webSettings), &numProperties); 147 if (!propertySpecs) 148 return NULL; 149 150 optionEntries = g_new0(GOptionEntry, numProperties + 1); 151 numEntries = 0; 152 for (i = 0; i < numProperties; i++) { 153 GParamSpec *param = propertySpecs[i]; 154 155 /* Fill in structures only for writable and not construct-only properties. */ 156 if (!param || !(param->flags & G_PARAM_WRITABLE) || (param->flags & G_PARAM_CONSTRUCT_ONLY)) 157 continue; 158 159 GType gParamType = G_PARAM_SPEC_VALUE_TYPE(param); 160 if (!isValidParameterType(gParamType)) 161 continue; 162 163 GOptionEntry *optionEntry = &optionEntries[numEntries++]; 164 optionEntry->long_name = g_param_spec_get_name(param); 165 166 /* There is no easy way to figure our short name for generated option entries. 167 optionEntry.short_name=*/ 168 /* For bool arguments "enable" type make option argument not required. */ 169 if (gParamType == G_TYPE_BOOLEAN && (strstr(optionEntry->long_name, "enable"))) 170 optionEntry->flags = G_OPTION_FLAG_OPTIONAL_ARG; 171 optionEntry->arg = G_OPTION_ARG_CALLBACK; 172 optionEntry->arg_data = parseOptionEntryCallback; 173 optionEntry->description = g_param_spec_get_blurb(param); 174 optionEntry->arg_description = g_type_name(gParamType); 175 } 176 g_free(propertySpecs); 177 178 return optionEntries; 179 } 180 181 static gboolean addSettingsGroupToContext(GOptionContext *context, WebKitSettings* webkitSettings) 182 { 183 GOptionEntry *optionEntries = getOptionEntriesFromWebKitSettings(webkitSettings); 184 if (!optionEntries) 185 return FALSE; 186 187 GOptionGroup *webSettingsGroup = g_option_group_new("websettings", 188 "WebKitSettings writable properties for default WebKitWebView", 189 "WebKitSettings properties", 190 webkitSettings, 191 NULL); 192 g_option_group_add_entries(webSettingsGroup, optionEntries); 193 g_free(optionEntries); 194 195 /* Option context takes ownership of the group. */ 196 g_option_context_add_group(context, webSettingsGroup); 197 198 return TRUE; 199 } 200 61 201 int main(int argc, char *argv[]) 62 202 { … … 66 206 g_option_context_add_main_entries(context, commandLineOptions, 0); 67 207 g_option_context_add_group(context, gtk_get_option_group(TRUE)); 208 209 WebKitSettings *webkitSettings = webkit_settings_new(); 210 if (!addSettingsGroupToContext(context, webkitSettings)) { 211 g_object_unref(webkitSettings); 212 webkitSettings = 0; 213 } 68 214 69 215 GError *error = 0; … … 81 227 82 228 for (i = 0; uriArguments[i]; i++) 83 loadURI(uriArguments[i]);229 createBrowserWindow(uriArguments[i], webkitSettings); 84 230 } else 85 loadURI("http://www.webkitgtk.org/");231 createBrowserWindow("http://www.webkitgtk.org/", webkitSettings); 86 232 87 233 gtk_main();
Note: See TracChangeset
for help on using the changeset viewer.