[epiphany/pgriffis/web-extension/downloads] WebExtensions: Consistently use api-utils
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/downloads] WebExtensions: Consistently use api-utils
- Date: Mon, 20 Jun 2022 01:08:05 +0000 (UTC)
commit 1f4834ced286cd7f51b5b2f3233cd91ca536a8c7
Author: Patrick Griffis <pgriffis igalia com>
Date: Sun Jun 19 20:08:00 2022 -0500
WebExtensions: Consistently use api-utils
src/webextension/api/cookies.c | 78 ++++++++++--------------------
src/webextension/api/notifications.c | 17 ++-----
src/webextension/api/tabs.c | 93 +++++++-----------------------------
3 files changed, 46 insertions(+), 142 deletions(-)
---
diff --git a/src/webextension/api/cookies.c b/src/webextension/api/cookies.c
index b62d57a22..faff077c1 100644
--- a/src/webextension/api/cookies.c
+++ b/src/webextension/api/cookies.c
@@ -19,6 +19,7 @@
#include "config.h"
+#include "api-utils.h"
#include "ephy-shell.h"
#include "cookies.h"
@@ -30,37 +31,6 @@ get_cookie_manager (void)
return webkit_website_data_manager_get_cookie_manager (data_manager);
}
-static char *
-get_string_property (JSCValue *obj,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (obj, name);
- if (!jsc_value_is_string (value))
- return NULL;
- return jsc_value_to_string (value);
-}
-
-static gint32
-get_int_property (JSCValue *args,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (args, name);
-
- if (jsc_value_is_undefined (value))
- return -1;
-
- return jsc_value_to_int32 (value);
-}
-
-static gboolean
-get_boolean_property (JSCValue *obj,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (obj, name);
- return jsc_value_to_boolean (value);
-}
-
-
static const char *
samesite_to_string (SoupSameSitePolicy policy)
{
@@ -253,8 +223,8 @@ cookies_handler_get (EphyWebExtension *self,
return;
}
- cookie_name = get_string_property (details, "name");
- url = get_string_property (details, "url");
+ cookie_name = api_utils_get_string_property (details, "name", NULL);
+ url = api_utils_get_string_property (details, "url", NULL);
if (!url || !cookie_name) {
g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"cookies.get(): details missing url or name");
@@ -321,15 +291,15 @@ cookies_handler_set (EphyWebExtension *self,
return;
}
- url = get_string_property (details, "url");
- domain = get_string_property (details, "domain");
- cookie_name = get_string_property (details, "name");
- value = get_string_property (details, "value");
- path = get_string_property (details, "path");
- same_site_str = get_string_property (details, "sameSite");
- expiration = get_int_property (details, "expirationDate");
- secure = get_boolean_property (details, "secure");
- http_only = get_boolean_property (details, "httpOnline");
+ url = api_utils_get_string_property (details, "url", NULL);
+ domain = api_utils_get_string_property (details, "domain", NULL);
+ cookie_name = api_utils_get_string_property (details, "name", NULL);
+ value = api_utils_get_string_property (details, "value", NULL);
+ path = api_utils_get_string_property (details, "path", NULL);
+ same_site_str = api_utils_get_string_property (details, "sameSite", NULL);
+ expiration = api_utils_get_int32_property (details, "expirationDate", -1);
+ secure = api_utils_get_boolean_property (details, "secure", FALSE);
+ http_only = api_utils_get_boolean_property (details, "httpOnline", FALSE);
if (!url) {
g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"cookies.set(): Missing url property");
@@ -381,8 +351,8 @@ cookies_handler_remove (EphyWebExtension *self,
return;
}
- url = get_string_property (details, "url");
- cookie_name = get_string_property (details, "name");
+ url = api_utils_get_string_property (details, "url", NULL);
+ cookie_name = api_utils_get_string_property (details, "name", NULL);
if (!url || !name) {
g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"cookies.remove(): Missing url or name property");
@@ -407,8 +377,8 @@ typedef struct {
char *domain;
char *name;
char *path;
- int secure;
- int session;
+ ApiTriStateValue secure;
+ ApiTriStateValue session;
} GetAllCookiesCallbackData;
static void
@@ -430,9 +400,9 @@ cookie_matches_filter (SoupCookie *cookie,
return FALSE;
if (data->path && strcmp (soup_cookie_get_path (cookie), data->path) != 0)
return FALSE;
- if (data->secure != -1 && soup_cookie_get_secure (cookie) != data->secure)
+ if (data->secure != API_VALUE_UNSET && soup_cookie_get_secure (cookie) != data->secure)
return FALSE;
- if (data->session != -1) {
+ if (data->session != API_VALUE_UNSET) {
gpointer expires = soup_cookie_get_expires (cookie);
if (data->session && expires)
return FALSE;
@@ -505,7 +475,7 @@ cookies_handler_get_all (EphyWebExtension *self,
return;
}
- url = get_string_property (details, "url");
+ url = api_utils_get_string_property (details, "url", NULL);
/* TODO: We can handle the case of no url by using webkit_website_data_manager_fetch() to list all domains
and then get all cookies
* for all domains, but this is rather an ugly amount of work compared to libsoup directly. */
@@ -521,11 +491,11 @@ cookies_handler_get_all (EphyWebExtension *self,
callback_data = g_new0 (GetAllCookiesCallbackData, 1);
callback_data->task = task;
- callback_data->name = get_string_property (details, "name");
- callback_data->domain = get_string_property (details, "domain");
- callback_data->path = get_string_property (details, "path");
- callback_data->secure = get_int_property (details, "secure");
- callback_data->session = get_int_property (details, "session");
+ callback_data->name = api_utils_get_string_property (details, "name", NULL);
+ callback_data->domain = api_utils_get_string_property (details, "domain", NULL);
+ callback_data->path = api_utils_get_string_property (details, "path", NULL);
+ callback_data->secure = api_utils_get_tri_state_value_property (details, "secure");
+ callback_data->session = api_utils_get_tri_state_value_property (details, "session");
/* FIXME: The WebKit API doesn't expose details like first-party URLs to better filter this. The
underlying libsoup API does so
* this just requires additions to WebKitGTK. */
diff --git a/src/webextension/api/notifications.c b/src/webextension/api/notifications.c
index c0ee0bff1..9cae6e8d3 100644
--- a/src/webextension/api/notifications.c
+++ b/src/webextension/api/notifications.c
@@ -23,18 +23,9 @@
#include "ephy-shell.h"
#include "ephy-web-extension.h"
+#include "api-utils.h"
#include "notifications.h"
-static char *
-get_string_property (JSCValue *obj,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (obj, name);
- if (!jsc_value_is_string (value))
- return NULL;
- return jsc_value_to_string (value);
-}
-
static char *
create_extension_notification_id (EphyWebExtension *web_extension,
const char *id)
@@ -79,8 +70,8 @@ notifications_handler_create (EphyWebExtension *self,
return NULL;
}
- title = get_string_property (value, "title");
- message = get_string_property (value, "message");
+ title = api_utils_get_string_property (value, "title", NULL);
+ message = api_utils_get_string_property (value, "message", NULL);
if (!title || !message) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "notifications.%s():
title and message are required", name);
@@ -95,7 +86,7 @@ notifications_handler_create (EphyWebExtension *self,
if (jsc_value_is_array (button_array)) {
for (guint i = 0; i < 2; i++) {
g_autoptr (JSCValue) button = jsc_value_object_get_property_at_index (button_array, i);
- g_autofree char *button_title = get_string_property (button, "title");
+ g_autofree char *button_title = api_utils_get_string_property (button, "title", NULL);
if (button_title)
g_notification_add_button_with_target (notification, button_title, "app.webextension-notification",
"(ssi)", extension_guid, id, i);
}
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index e295cea7f..1f1ef3d30 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -25,13 +25,12 @@
#include "ephy-window.h"
#include "ephy-reader-handler.h"
+#include "api-utils.h"
#include "tabs.h"
/* Matches Firefox. */
static const int WINDOW_ID_CURRENT = -2;
-static const int VALUE_UNSET = -1;
-
static WebKitWebView *
get_web_view_for_tab_id (EphyShell *shell,
gint32 tab_id,
@@ -141,62 +140,6 @@ ephy_web_extension_api_tabs_create_tab_object (EphyWebExtension *self,
return json_builder_get_root (builder);
}
-typedef enum {
- TAB_QUERY_UNSET = -1,
- TAB_QUERY_DONT_MATCH = 0,
- TAB_QUERY_MATCH = 1,
-} TabQuery;
-
-static TabQuery
-get_tab_query_property (JSCValue *args,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (args, name);
-
- if (!value || jsc_value_is_undefined (value))
- return TAB_QUERY_UNSET;
-
- return jsc_value_to_boolean (value);
-}
-
-static gint32
-get_number_property (JSCValue *args,
- const char *name)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (args, name);
-
- if (!value || jsc_value_is_undefined (value))
- return -1;
-
- return jsc_value_to_int32 (value);
-}
-
-static gboolean
-get_boolean_property (JSCValue *obj,
- const char *name,
- gboolean default_value)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (obj, name);
-
- if (jsc_value_is_undefined (value))
- return default_value;
-
- return jsc_value_to_boolean (value);
-}
-
-static char *
-get_string_property (JSCValue *obj,
- const char *name,
- const char *default_value)
-{
- g_autoptr (JSCValue) value = jsc_value_object_get_property (obj, name);
-
- if (!jsc_value_is_string (value))
- return g_strdup (default_value);
-
- return jsc_value_to_string (value);
-}
-
static EphyWindow *
get_window_by_id (EphyShell *shell,
gint64 window_id)
@@ -227,18 +170,18 @@ tabs_handler_query (EphyWebExtension *self,
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
GList *windows;
EphyWindow *active_window;
- TabQuery current_window;
- TabQuery active;
+ ApiTriStateValue current_window;
+ ApiTriStateValue active;
gint32 window_id;
gint32 tab_index;
if (!jsc_value_is_object (value))
return NULL;
- active = get_tab_query_property (value, "active");
- current_window = get_tab_query_property (value, "currentWindow");
- window_id = get_number_property (value, "windowId");
- tab_index = get_number_property (value, "index");
+ active = api_utils_get_tri_state_value_property (value, "active");
+ current_window = api_utils_get_tri_state_value_property (value, "currentWindow");
+ window_id = api_utils_get_int32_property (value, "windowId", -1);
+ tab_index = api_utils_get_int32_property (value, "index", -1);
if (window_id == WINDOW_ID_CURRENT) {
current_window = TRUE;
@@ -262,9 +205,9 @@ tabs_handler_query (EphyWebExtension *self,
if (window_id != -1 && (guint64)window_id != ephy_window_get_uid (window))
continue;
- if (current_window == TAB_QUERY_MATCH && window != active_window)
+ if (current_window == API_VALUE_TRUE && window != active_window)
continue;
- else if (current_window == TAB_QUERY_DONT_MATCH && window == active_window)
+ else if (current_window == API_VALUE_FALSE && window == active_window)
continue;
tab_view = ephy_window_get_tab_view (window);
@@ -276,9 +219,9 @@ tabs_handler_query (EphyWebExtension *self,
continue;
web_view = ephy_embed_get_web_view (EPHY_EMBED (ephy_tab_view_get_nth_page (tab_view, i)));
- if (active == TAB_QUERY_MATCH && web_view != active_web_view)
+ if (active == API_VALUE_TRUE && web_view != active_web_view)
continue;
- else if (active == TAB_QUERY_DONT_MATCH && web_view == active_web_view)
+ else if (active == API_VALUE_FALSE && web_view == active_web_view)
continue;
add_web_view_to_json (self, builder, window, web_view);
@@ -630,20 +573,20 @@ tabs_handler_create (EphyWebExtension *self,
return NULL;
}
- url = resolve_to_absolute_url (self, get_string_property (create_properties, "url", NULL));
+ url = resolve_to_absolute_url (self, api_utils_get_string_property (create_properties, "url", NULL));
if (!ephy_web_extension_api_tabs_url_is_unprivileged (url)) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.create(): URL '%s'
is not allowed", url);
return NULL;
}
- if (get_boolean_property (create_properties, "active", FALSE))
+ if (api_utils_get_boolean_property (create_properties, "active", FALSE))
new_tab_flags |= EPHY_NEW_TAB_JUMP;
- parent_window = get_window_by_id (shell, get_number_property (create_properties, "windowId"));
+ parent_window = get_window_by_id (shell, api_utils_get_int32_property (create_properties, "windowId", -1));
embed = ephy_shell_new_tab (shell, parent_window, NULL, new_tab_flags);
new_web_view = ephy_embed_get_web_view (embed);
- if (url && get_boolean_property (create_properties, "openInReaderMode", FALSE)) {
+ if (url && api_utils_get_boolean_property (create_properties, "openInReaderMode", FALSE)) {
char *reader_url = g_strconcat (EPHY_READER_SCHEME, ":", url, NULL);
g_free (url);
url = reader_url;
@@ -705,14 +648,14 @@ tabs_handler_update (EphyWebExtension *self,
return NULL;
}
- new_url = resolve_to_absolute_url (self, get_string_property (update_properties, "url", NULL));
+ new_url = resolve_to_absolute_url (self, api_utils_get_string_property (update_properties, "url", NULL));
if (!ephy_web_extension_api_tabs_url_is_unprivileged (new_url)) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.update(): URL '%s'
is not allowed", new_url);
return NULL;
}
- muted = get_boolean_property (update_properties, "muted", VALUE_UNSET);
- if (muted != VALUE_UNSET)
+ muted = api_utils_get_tri_state_value_property (update_properties, "muted");
+ if (muted != API_VALUE_UNSET)
webkit_web_view_set_is_muted (target_web_view, muted);
if (new_url)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]