Skip to content

Commit

Permalink
Add utility functions and new xwidget commands
Browse files Browse the repository at this point in the history
Co-authored-by: Jaesup Kwak <veshboo@gmail.com>

* lisp/xwidget.el (xwidget-webkit-callback): Add case for
'response-callback' event.
(xwidget-webkit-download-dir): New variable.
(xwidget-webkit-save-as-file): New function.
* src/nsxwidget.m (XwWebView::decidePolicyForNavigationResponse):
Store download event.
* src/xwidget.c src/xwidget.h (store_xwidget_download_callback_event):
New function.
  • Loading branch information
goranmoomin authored and larsmagne committed Aug 12, 2020
1 parent 9e1af82 commit c25321e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions etc/NEWS
Expand Up @@ -986,6 +986,9 @@ If Emacs was built with xwidget support, you can access the embedded
webkit browser with 'M-x xwidget-webkit-browse-url'. Viewing two
instances of xwidget webkit is not supported.

*** Downloading files from xwidget-webkit is now supported.
The new variable 'xwidget-webkit-download-dir' says where to download to.

*** New functions for xwidget-webkit mode
'xwidget-webkit-clone-and-split-below',
'xwidget-webkit-clone-and-split-right'.
Expand Down
32 changes: 32 additions & 0 deletions lisp/xwidget.el
Expand Up @@ -288,6 +288,12 @@ XWIDGET instance, XWIDGET-EVENT-TYPE depends on the originating xwidget."
(xwidget-webkit-show-id-or-named-element
xwidget
(match-string 1 strarg)))))
;; TODO: Response handling other than download.
((eq xwidget-event-type 'download-callback)
(let ((url (nth 3 last-input-event))
(mime-type (nth 4 last-input-event))
(file-name (nth 5 last-input-event)))
(xwidget-webkit-save-as-file url mime-type file-name)))
((eq xwidget-event-type 'javascript-callback)
(let ((proc (nth 3 last-input-event))
(arg (nth 4 last-input-event)))
Expand All @@ -308,6 +314,32 @@ If non-nil, plugins are enabled. Otherwise, disabled."))
;; Keep track of [vh]scroll when switching buffers
(image-mode-setup-winprops))

;;; Download, save as file.

(defcustom xwidget-webkit-download-dir "~/Downloads/"
"Directory where download file saved."
:version "27.1"
:type 'file)

(defun xwidget-webkit-save-as-file (url mime-type file-name)
"For XWIDGET webkit, save URL of MIME-TYPE to location specified by user.
FILE-NAME combined with `xwidget-webkit-download-dir' is the default file name
of the prompt when reading. When the file name the user specified is a
directory, URL is saved at the specified directory as FILE-NAME."
(let ((save-name (read-file-name
(format "Save URL `%s' of type `%s' in file/directory: "
url mime-type)
xwidget-webkit-download-dir
(when file-name
(expand-file-name
file-name
xwidget-webkit-download-dir)))))
(if (file-directory-p save-name)
(setq save-name
(expand-file-name (file-name-nondirectory file-name) save-name)))
(setq xwidget-webkit-download-dir (file-name-directory save-name))
(url-copy-file url save-name t)))

;;; Bookmarks integration

(defcustom xwidget-webkit-bookmark-jump-new-session nil
Expand Down
12 changes: 12 additions & 0 deletions src/nsxwidget.m
Expand Up @@ -121,6 +121,18 @@ - (void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
if (!navigationResponse.canShowMIMEType)
{
NSString *url = navigationResponse.response.URL.absoluteString;
NSString *mimetype = navigationResponse.response.MIMEType;
NSString *filename = navigationResponse.response.suggestedFilename;
decisionHandler (WKNavigationResponsePolicyCancel);
store_xwidget_download_callback_event (self.xw,
url.UTF8String,
mimetype.UTF8String,
filename.UTF8String);
return;
}
decisionHandler (WKNavigationResponsePolicyAllow);

self.urlScriptBlocked[navigationResponse.response.URL] =
Expand Down
20 changes: 20 additions & 0 deletions src/xwidget.c
Expand Up @@ -258,6 +258,26 @@ store_xwidget_event_string (struct xwidget *xw, const char *eventname,
kbd_buffer_store_event (&event);
}

void
store_xwidget_download_callback_event (struct xwidget *xw,
const char *url,
const char *mimetype,
const char *filename)
{
struct input_event event;
Lisp_Object xwl;
XSETXWIDGET (xwl, xw);
EVENT_INIT (event);
event.kind = XWIDGET_EVENT;
event.frame_or_window = Qnil;
event.arg = list5 (intern ("download-callback"),
xwl,
build_string (url),
build_string (mimetype),
build_string (filename));
kbd_buffer_store_event (&event);
}

void
store_xwidget_js_callback_event (struct xwidget *xw,
Lisp_Object proc,
Expand Down
5 changes: 5 additions & 0 deletions src/xwidget.h
Expand Up @@ -154,6 +154,11 @@ void store_xwidget_event_string (struct xwidget *xw,
const char *eventname,
const char *eventstr);

void store_xwidget_download_callback_event (struct xwidget *xw,
const char *url,
const char *mimetype,
const char *filename);

void store_xwidget_js_callback_event (struct xwidget *xw,
Lisp_Object proc,
Lisp_Object argument);
Expand Down

0 comments on commit c25321e

Please sign in to comment.