X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=4f7b691cc9ba5a3a83736ce77a9973284b864fd2;hb=bce814b30ae026f97067bdd3c43a3b08f78c33bf;hp=22a3e8a736bee5ea85beda66b6d30098744a8a4b;hpb=b9530ad5d1cd29e176c9f16b92f1983993b22049;p=surf.git diff --git a/surf.c b/surf.c index 22a3e8a..4f7b691 100644 --- a/surf.c +++ b/surf.c @@ -59,7 +59,8 @@ typedef struct Client { Window xid; WebKitWebView *view; WebKitWebInspector *inspector; - const char *title, *linkhover; + WebKitHitTestResult *mousepos; + const char *title, *targeturi; const char *needle; gint progress; struct Client *next; @@ -113,14 +114,13 @@ static WebKitCookieAcceptPolicy cookiepolicy_get(void); static char cookiepolicy_set(const WebKitCookieAcceptPolicy p); static char *copystr(char **str, const char *src); -static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, - Client *c); -static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, - WebKitNetworkRequest *r, gchar *m, - WebKitWebPolicyDecision *p, Client *c); -static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, - WebKitNetworkRequest *r, WebKitWebNavigationAction - *n, WebKitWebPolicyDecision *p, Client *c); +static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a, + Client *c); +static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d, + WebKitPolicyDecisionType dt, Client *c); +static void decidenavigation(WebKitPolicyDecision *d, Client *c); +static void decidenewwindow(WebKitPolicyDecision *d, Client *c); +static void decideresource(WebKitPolicyDecision *d, Client *c); static gboolean deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c); static void destroyclient(Client *c); @@ -129,8 +129,8 @@ static void die(const char *errstr, ...); static void eval(Client *c, const Arg *arg); static void find(Client *c, const Arg *arg); static void fullscreen(Client *c, const Arg *arg); -static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f, - WebKitGeolocationPolicyDecision *d, Client *c); +static gboolean permissionrequested(WebKitWebView *v, + WebKitPermissionRequest *r, Client *c); static const char *getatom(Client *c, int a); static void gettogglestat(Client *c); static void getpagestat(Client *c); @@ -151,8 +151,8 @@ static void inspector_finished(WebKitWebInspector *i, Client *c); static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods, Client *c); -static void linkhover(WebKitWebView *v, const char* t, const char* l, - Client *c); +static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, + guint modifiers, Client *c); static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c); static void loaduri(Client *c, const Arg *arg); @@ -426,39 +426,131 @@ copystr(char **str, const char *src) return tmp; } -WebKitWebView * -createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) +GtkWidget * +createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c) { - Client *n = newclient(); - return n->view; + Client *n; + + switch (webkit_navigation_action_get_navigation_type(a)) { + case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */ + /* + * popup windows of type “other” are almost always triggered + * by user gesture, so inverse the logic here + */ +/* instead of this, compare destination uri to mouse-over uri for validating window */ + if (webkit_navigation_action_is_user_gesture(a)) { + return NULL; + break; + } + case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: + n = newclient(c); + break; + default: + return NULL; + break; + } + + return GTK_WIDGET(n->view); } gboolean -decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, - gchar *m, WebKitWebPolicyDecision *p, Client *c) +decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d, + WebKitPolicyDecisionType dt, Client *c) { - if (!webkit_web_view_can_show_mime_type(v, m)) { - webkit_web_policy_decision_download(p); - return TRUE; + switch (dt) { + case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION: + decidenavigation(d, c); + break; + case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION: + decidenewwindow(d, c); + break; + case WEBKIT_POLICY_DECISION_TYPE_RESPONSE: + decideresource(d, c); + break; + default: + webkit_policy_decision_ignore(d); + break; } - return FALSE; + return TRUE; } -gboolean -decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, - WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, - Client *c) +void +decidenavigation(WebKitPolicyDecision *d, Client *c) +{ + WebKitNavigationAction *a; + + a = webkit_navigation_policy_decision_get_navigation_action( + WEBKIT_NAVIGATION_POLICY_DECISION(d)); + + switch (webkit_navigation_action_get_navigation_type(a)) { + case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: + case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */ + default: + /* Do not navigate to links with a "_blank" target (popup) */ + if (webkit_navigation_policy_decision_get_frame_name( + WEBKIT_NAVIGATION_POLICY_DECISION(d))) { + webkit_policy_decision_ignore(d); + } else { + /* Filter out navigation to different domain ? */ + /* get action→urirequest, copy and load in new window+view + * on Ctrl+Click ? */ + webkit_policy_decision_use(d); + } + break; + } +} + +void +decidenewwindow(WebKitPolicyDecision *d, Client *c) { + WebKitNavigationAction *a; Arg arg; - if (webkit_web_navigation_action_get_reason(n) - == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) { - webkit_web_policy_decision_ignore(p); - arg.v = (void *)webkit_network_request_get_uri(r); - newwindow(NULL, &arg, 0); - return TRUE; + a = webkit_navigation_policy_decision_get_navigation_action( + WEBKIT_NAVIGATION_POLICY_DECISION(d)); + + switch (webkit_navigation_action_get_navigation_type(a)) { + case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */ + case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: + /* Filter domains here */ +/* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event. + * test for link clicked but no button ? */ + arg.v = webkit_uri_request_get_uri( + webkit_navigation_action_get_request(a)); + newwindow(c, &arg, 0); + break; + case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */ + default: + break; + } + + webkit_policy_decision_ignore(d); +} + +void +decideresource(WebKitPolicyDecision *d, Client *c) +{ + WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d); + WebKitURIResponse *res; + + if (webkit_response_policy_decision_is_mime_type_supported(r)) { + webkit_policy_decision_use(d); + } else { +res = webkit_response_policy_decision_get_response(r); + webkit_policy_decision_ignore(d); + download(c, res); } - return FALSE; } gboolean @@ -526,14 +618,18 @@ fullscreen(Client *c, const Arg *arg) c->fullscreen = !c->fullscreen; } -void -geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f, - WebKitGeolocationPolicyDecision *d, Client *c) +gboolean +permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c) { - if (allowgeolocation) - webkit_geolocation_policy_allow(d); - else - webkit_geolocation_policy_deny(d); + if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) { + if (allowgeolocation) + webkit_permission_request_allow(r); + else + webkit_permission_request_deny(r); + return TRUE; + } + + return FALSE; } const char * @@ -691,14 +787,24 @@ keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods, } void -linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) +mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers, + Client *c) { - if (l) { - c->linkhover = copystr(&c->linkhover, l); - } else if (c->linkhover) { - free(c->linkhover); - c->linkhover = NULL; - } + WebKitHitTestResultContext hc; + + /* Keep the hit test to know where is the pointer on the next click */ + c->mousepos = h; + + hc = webkit_hit_test_result_get_context(h); + + if (hc & OnLink) + c->targeturi = webkit_hit_test_result_get_link_uri(h); + else if (hc & OnImg) + c->targeturi = webkit_hit_test_result_get_image_uri(h); + else if (hc & OnMedia) + c->targeturi = webkit_hit_test_result_get_media_uri(h); + else + c->targeturi = NULL; updatetitle(c); } @@ -869,22 +975,19 @@ newview(Client *c, WebKitWebView *rv) "notify::title", G_CALLBACK(titlechanged), c); g_signal_connect(G_OBJECT(v), - "hovering-over-link", - G_CALLBACK(linkhover), c); + "mouse-target-changed", + G_CALLBACK(mousetargetchanged), c); g_signal_connect(G_OBJECT(v), - "geolocation-policy-decision-requested", - G_CALLBACK(geopolicyrequested), c); + "permission-request", + G_CALLBACK(permissionrequested), c); g_signal_connect(G_OBJECT(v), - "create-web-view", - G_CALLBACK(createwindow), c); + "create", + G_CALLBACK(createview), c); g_signal_connect(G_OBJECT(v), "ready-to-show", G_CALLBACK(showview), c); g_signal_connect(G_OBJECT(v), - "new-window-policy-decision-requested", - G_CALLBACK(decidewindow), c); - g_signal_connect(G_OBJECT(v), - "mime-type-policy-decision-requested", - G_CALLBACK(decidedownload), c); + "decide-policy", + G_CALLBACK(decidepolicy), c); g_signal_connect(G_OBJECT(v), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);