288 lines
14 KiB
Text
288 lines
14 KiB
Text
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||
|
|
|
||
|
|
;; bjomarks-gui/src/browser.bjo — the module (bjomarks-gui browser): opens
|
||
|
|
;; an address in the user's browser.
|
||
|
|
;;
|
||
|
|
;; - With BJOMARKS_BROWSER set, that program is started with the address as
|
||
|
|
;; its one argument, with no shell between.
|
||
|
|
;; - In a distrobox container, host-spawn runs xdg-open on the host, with the
|
||
|
|
;; variables that tell it the display. distrobox's own xdg-open gives the
|
||
|
|
;; host none of them, and the host's xdg-open then finds no browser: "no
|
||
|
|
;; method available".
|
||
|
|
;; - Else the system opens it, as a click on a link does: xdg-open on Linux
|
||
|
|
;; and the BSDs, open on macOS, and the shell on Windows. That opens the
|
||
|
|
;; browser the user chose.
|
||
|
|
;; - The opener is waited for, up to 5 seconds. When it ends with an error,
|
||
|
|
;; the address was not opened, and the text says so. One that runs longer
|
||
|
|
;; has started a browser that it waits for: that is no error.
|
||
|
|
;; - Only http and https addresses are opened. A bookmark comes from the
|
||
|
|
;; files of other devices, and a `file:` address or one of a program's own
|
||
|
|
;; scheme can start a program on this one.
|
||
|
|
;;
|
||
|
|
;; A private window: the user's default browser, as `xdg-settings` names it,
|
||
|
|
;; is started as its .desktop file says, with its own flag for a private
|
||
|
|
;; window: --private-window for Firefox and those made from it, --incognito
|
||
|
|
;; for Chrome and those made from Chromium, and --inprivate for Edge. With
|
||
|
|
;; BJOMARKS_PRIVATE_BROWSER set, that program is started with the address
|
||
|
|
;; instead. Another browser, or another system than Linux, has no private
|
||
|
|
;; window here: `open-private-in-browser` says so, and the app copies the
|
||
|
|
;; address.
|
||
|
|
|
||
|
|
(export Opener opener-for distrobox-host-spawn open-in-browser browser-scheme?
|
||
|
|
private-flag exec-words private-command open-private-in-browser)
|
||
|
|
|
||
|
|
(import (std prelude))
|
||
|
|
(import (std interop))
|
||
|
|
(import (std os))
|
||
|
|
(import (std url))
|
||
|
|
|
||
|
|
(import/class
|
||
|
|
(ProcessStartInfo (: System.Diagnostics.ProcessStartInfo (-> string ProcessStartInfo)))
|
||
|
|
(Process (: System.Diagnostics.Process))
|
||
|
|
(Object (: System.Object)))
|
||
|
|
|
||
|
|
(import/extern
|
||
|
|
(shell-execute! (: System.Diagnostics.ProcessStartInfo.UseShellExecute (-> ProcessStartInfo bool void) #:set))
|
||
|
|
(redirect-output! (: System.Diagnostics.ProcessStartInfo.RedirectStandardOutput
|
||
|
|
(-> ProcessStartInfo bool void) #:set))
|
||
|
|
(output-of (: System.Diagnostics.Process.StandardOutput (-> Process System.IO.StreamReader) #:get))
|
||
|
|
(read-to-end (: System.IO.StreamReader.ReadToEnd (-> System.IO.StreamReader string)))
|
||
|
|
;; .NET strings, not Bjolang's.
|
||
|
|
(arguments-of (: System.Diagnostics.ProcessStartInfo.ArgumentList
|
||
|
|
(-> ProcessStartInfo (System.Collections.ObjectModel.Collection System.String)) #:get))
|
||
|
|
(add-argument! (: System.Collections.ObjectModel.Collection.Add
|
||
|
|
(-> (System.Collections.ObjectModel.Collection System.String) System.String void)))
|
||
|
|
(start-process (: System.Diagnostics.Process.Start (-> ProcessStartInfo Process)))
|
||
|
|
(wait-for-exit (: System.Diagnostics.Process.WaitForExit (-> Process int bool)))
|
||
|
|
(exit-code-of (: System.Diagnostics.Process.ExitCode (-> Process int) #:get))
|
||
|
|
(dispose-process (: System.Diagnostics.Process.Dispose (-> Process void))))
|
||
|
|
|
||
|
|
;; True for an http or https address, in any case.
|
||
|
|
(: (-> string bool))
|
||
|
|
(defun (browser-scheme? address)
|
||
|
|
(match (parse-url address)
|
||
|
|
((Ok u) (match (url-scheme u)
|
||
|
|
((Some s) (let ((scheme (string-downcase s))) (or (= scheme "http") (= scheme "https"))))
|
||
|
|
(None #f)))
|
||
|
|
((Err _) #f)))
|
||
|
|
|
||
|
|
;; How an address is opened: a program, with its arguments, or the shell of
|
||
|
|
;; Windows, which takes the address itself.
|
||
|
|
(type (: Opener (Union (: Run string (Vec string))
|
||
|
|
WindowsShell)))
|
||
|
|
|
||
|
|
;; The variables that host-spawn gives the host's xdg-open.
|
||
|
|
;; - With no display, xdg-open tries only browsers that run in a terminal.
|
||
|
|
;; - Not XDG_CURRENT_DESKTOP: with it, xdg-open uses the desktop's own
|
||
|
|
;; opener, which needs more of the session's variables, as KDE's needs
|
||
|
|
;; KDE_SESSION_VERSION. Without it, xdg-open opens the address with the
|
||
|
|
;; program that is the user's default for it, as xdg-mime says, on any
|
||
|
|
;; desktop.
|
||
|
|
(: string)
|
||
|
|
(def host-variables "DISPLAY,WAYLAND_DISPLAY,XDG_SESSION_TYPE")
|
||
|
|
|
||
|
|
;; The opener of `address`. `browser` is BJOMARKS_BROWSER, if it is set, and
|
||
|
|
;; `host-spawn` is host-spawn, when this program runs in a distrobox
|
||
|
|
;; container.
|
||
|
|
(: (-> string (Option string) (Option string) Opener))
|
||
|
|
(defun (opener-for address browser host-spawn)
|
||
|
|
(match (Tuple browser host-spawn)
|
||
|
|
((Tuple (Some program) _) (Run program [address]))
|
||
|
|
((Tuple None (Some spawn)) (Run spawn ["-no-pty" "-env" host-variables "xdg-open" address]))
|
||
|
|
(_ (cond
|
||
|
|
((windows?) WindowsShell)
|
||
|
|
((macos?) (Run "open" [address]))
|
||
|
|
(else (Run "xdg-open" [address]))))))
|
||
|
|
|
||
|
|
;; The first `name` on the PATH, if there is one.
|
||
|
|
(: (-> string (Option string)))
|
||
|
|
(defun (on-path name)
|
||
|
|
(match (get-environment-variable "PATH")
|
||
|
|
((Some path)
|
||
|
|
(def found (vec-filter (fun (dir) (and (not (string-empty? dir)) (file-exists? (path-combine dir name))))
|
||
|
|
(string-split path ":")))
|
||
|
|
(if (vec-empty? found) None (Some (path-combine (vec-ref found 0) name))))
|
||
|
|
(None None)))
|
||
|
|
|
||
|
|
;; host-spawn, when this program runs in a container that distrobox made:
|
||
|
|
;; the file that podman or docker puts in each container is there, and
|
||
|
|
;; host-spawn is on the PATH. `None` anywhere else.
|
||
|
|
(: (-> (Option string)))
|
||
|
|
(defun (distrobox-host-spawn)
|
||
|
|
(if (or (file-exists? "/run/.containerenv") (file-exists? "/.dockerenv"))
|
||
|
|
(on-path "host-spawn")
|
||
|
|
None))
|
||
|
|
|
||
|
|
;; How long the opener is waited for, in milliseconds.
|
||
|
|
(: int)
|
||
|
|
(def wait-ms 5000)
|
||
|
|
|
||
|
|
;; Why the opener `program` that ended with `code` did not open the address.
|
||
|
|
;; xdg-open ends with 3 when it found no program for the address, and
|
||
|
|
;; host-spawn ends as the host's xdg-open did.
|
||
|
|
(: (-> string int string))
|
||
|
|
(defun (opener-failure program code)
|
||
|
|
(def name (option-ref-or (path-filename program) program))
|
||
|
|
(if (and (= code 3) (or (= name "xdg-open") (= name "host-spawn")))
|
||
|
|
"xdg-open found no program to open it with."
|
||
|
|
(str name " ended with code " (int->string code) ".")))
|
||
|
|
|
||
|
|
;; Starts `program` with `args`, and waits for it a while.
|
||
|
|
(: (-> string (Vec string) string (Result string string)))
|
||
|
|
(defun (run-opener program args address)
|
||
|
|
(def info (ProcessStartInfo. program))
|
||
|
|
(shell-execute! info #f)
|
||
|
|
(loop (:for a args) (:do (add-argument! (arguments-of info) (string->clr-string a))))
|
||
|
|
(def p (start-process info))
|
||
|
|
(def ended (wait-for-exit p wait-ms))
|
||
|
|
(def code (if ended (exit-code-of p) 0))
|
||
|
|
(dispose-process p)
|
||
|
|
(if (= code 0)
|
||
|
|
(Ok (str "Opened " address))
|
||
|
|
(Err (str "Could not open " address ": " (opener-failure program code)))))
|
||
|
|
|
||
|
|
;; Gives `address` to the shell of Windows.
|
||
|
|
(: (-> string (Result string string)))
|
||
|
|
(defun (shell-open address)
|
||
|
|
(def info (ProcessStartInfo. address))
|
||
|
|
(shell-execute! info #t)
|
||
|
|
(def p (start-process info))
|
||
|
|
;; Start returns no process when the shell gave the address to one that
|
||
|
|
;; was already running.
|
||
|
|
(unless (eq? (cast Object p) (clr-default)) (dispose-process p))
|
||
|
|
(Ok (str "Opened " address)))
|
||
|
|
|
||
|
|
;; Opens `address`, and returns what was done, for the status line: the
|
||
|
|
;; text says it was opened, or why not. Raises what starting the program
|
||
|
|
;; raises, such as when BJOMARKS_BROWSER names no program. `#:host-spawn` is
|
||
|
|
;; host-spawn in a distrobox container: see `distrobox-host-spawn`.
|
||
|
|
(: (-> string (#:host-spawn (Option string)) (Result string string)))
|
||
|
|
(defun (open-in-browser address #:host-spawn (distrobox-host-spawn))
|
||
|
|
(if (not (browser-scheme? address))
|
||
|
|
(Err (str "Only http and https addresses are opened: " address))
|
||
|
|
(match (opener-for address (get-environment-variable "BJOMARKS_BROWSER") host-spawn)
|
||
|
|
((Run program args) (run-opener program args address))
|
||
|
|
(WindowsShell (shell-open address)))))
|
||
|
|
|
||
|
|
;; ---------------------------------------------------------------------------
|
||
|
|
;; A private window
|
||
|
|
;; ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
;; The flag that opens a private window in the browser of the .desktop file
|
||
|
|
;; `id`, such as "org.mozilla.firefox.desktop". None for one not known.
|
||
|
|
(: (-> string (Option string)))
|
||
|
|
(defun (private-flag id)
|
||
|
|
(def name (string-downcase id))
|
||
|
|
(defun (has? words) (not (vec-empty? (vec-filter (fun (w) (string-contains? name w)) words))))
|
||
|
|
(cond
|
||
|
|
((has? ["firefox" "librewolf" "waterfox" "floorp" "zen"]) (Some "--private-window"))
|
||
|
|
((has? ["edge"]) (Some "--inprivate"))
|
||
|
|
((has? ["chrome" "chromium" "brave" "vivaldi" "opera"]) (Some "--incognito"))
|
||
|
|
(else None)))
|
||
|
|
|
||
|
|
;; The words of the Exec line `exec` of a .desktop file, without its field
|
||
|
|
;; codes (%u, %U and the others) and flatpak's @@ marks. A word in double
|
||
|
|
;; quotes is one word.
|
||
|
|
(: (-> string (Vec string)))
|
||
|
|
(defun (exec-words exec)
|
||
|
|
(def chars (string->list exec))
|
||
|
|
(def words
|
||
|
|
(let go ((cs chars) (word "") (quoted #f) (started #f) (out []))
|
||
|
|
(match cs
|
||
|
|
((List c rest ...)
|
||
|
|
(cond
|
||
|
|
((char=? c #\") (go rest word (not quoted) #t out))
|
||
|
|
((and (char=? c #\space) (not quoted)) (if started (go rest "" #f #f (vec-add out word)) (go rest "" #f #f out)))
|
||
|
|
(else (go rest (str word (char->string c)) quoted #t out))))
|
||
|
|
(_ (if started (vec-add out word) out)))))
|
||
|
|
(vec-filter (fun (w) (not (or (string-starts-with? w "%") (string-starts-with? w "@@")))) words))
|
||
|
|
|
||
|
|
;; The program and its arguments that open `address` in a private window of
|
||
|
|
;; the browser of the .desktop file `id`, whose Exec line is `exec`.
|
||
|
|
(: (-> string string string (Option (Tuple string (Vec string)))))
|
||
|
|
(defun (private-command id exec address)
|
||
|
|
(def words (exec-words exec))
|
||
|
|
(match (private-flag id)
|
||
|
|
((Some flag) #:when (not (vec-empty? words))
|
||
|
|
(Some (Tuple (vec-ref words 0) (vec-append (vec-drop words 1) [flag address]))))
|
||
|
|
(_ None)))
|
||
|
|
|
||
|
|
;; What `program` with `args` writes, when it ends with 0 within the wait.
|
||
|
|
(: (-> string (Vec string) (Option string)))
|
||
|
|
(defun (output-of-program program args)
|
||
|
|
(: (-> (Option string)))
|
||
|
|
(defun (run)
|
||
|
|
(def info (ProcessStartInfo. program))
|
||
|
|
(shell-execute! info #f)
|
||
|
|
(redirect-output! info #t)
|
||
|
|
(loop (:for a args) (:do (add-argument! (arguments-of info) (string->clr-string a))))
|
||
|
|
(def p (start-process info))
|
||
|
|
(def text (read-to-end (output-of p)))
|
||
|
|
(def ended (wait-for-exit p wait-ms))
|
||
|
|
(def ok (and ended (= (exit-code-of p) 0)))
|
||
|
|
(dispose-process p)
|
||
|
|
(if ok (Some (string-trim text)) None))
|
||
|
|
(match (try (run) #:catch (System.Exception))
|
||
|
|
((Ok r) r)
|
||
|
|
((Err _) None)))
|
||
|
|
|
||
|
|
;; The folders of .desktop files: the user's, the system's, and flatpak's.
|
||
|
|
;; In a container, the host's system folders are under /run/host.
|
||
|
|
(: (-> bool (Vec string)))
|
||
|
|
(defun (application-folders in-container)
|
||
|
|
(def home (option-ref-or (get-environment-variable "XDG_DATA_HOME")
|
||
|
|
(path-combine (home-directory) ".local" "share")))
|
||
|
|
(def system (vec-filter (fun (d) (not (string-empty? d)))
|
||
|
|
(string-split (option-ref-or (get-environment-variable "XDG_DATA_DIRS")
|
||
|
|
"/usr/local/share:/usr/share")
|
||
|
|
":")))
|
||
|
|
(def systems (vec-append (if in-container
|
||
|
|
(vec-append ["/usr/local/share" "/usr/share"] system)
|
||
|
|
system)
|
||
|
|
["/var/lib/flatpak/exports/share"]))
|
||
|
|
(vec-append [(path-combine home "applications")
|
||
|
|
(path-combine home "flatpak" "exports" "share" "applications")]
|
||
|
|
(vec-map (fun (d) (path-combine (if in-container (str "/run/host" d) d) "applications")) systems)))
|
||
|
|
|
||
|
|
;; The first Exec line of the .desktop file `id`, in `folders`.
|
||
|
|
(: (-> string (Vec string) (Option string)))
|
||
|
|
(defun (desktop-exec id folders)
|
||
|
|
(def found (vec-filter file-exists? (vec-map (fun (d) (path-combine d id)) folders)))
|
||
|
|
(if (vec-empty? found)
|
||
|
|
None
|
||
|
|
(let ((lines (string-split (file-read-text (vec-ref found 0)) "\n")))
|
||
|
|
(def execs (vec-filter (fun (l) (string-starts-with? l "Exec=")) lines))
|
||
|
|
(if (vec-empty? execs) None (Some (string-drop (vec-ref execs 0) 5))))))
|
||
|
|
|
||
|
|
;; Opens `address` in a private window, and returns what was done, for the
|
||
|
|
;; status line. `Err` when there is no private window to open it in: see the
|
||
|
|
;; top of this module.
|
||
|
|
(: (-> string (#:host-spawn (Option string)) (Result string string)))
|
||
|
|
(defun (open-private-in-browser address #:host-spawn (distrobox-host-spawn))
|
||
|
|
(def none (str "No private window here: the browser is not one this app knows how to ask for one."))
|
||
|
|
(cond
|
||
|
|
((not (browser-scheme? address)) (Err (str "Only http and https addresses are opened: " address)))
|
||
|
|
(else
|
||
|
|
(match (get-environment-variable "BJOMARKS_PRIVATE_BROWSER")
|
||
|
|
((Some program) (run-opener program [address] address))
|
||
|
|
(None
|
||
|
|
(if (or (windows?) (macos?))
|
||
|
|
(Err none)
|
||
|
|
(let ((id (match host-spawn
|
||
|
|
((Some spawn) (output-of-program spawn ["-no-pty" "xdg-settings" "get" "default-web-browser"]))
|
||
|
|
(None (output-of-program "xdg-settings" ["get" "default-web-browser"])))))
|
||
|
|
(match (match id
|
||
|
|
((Some i) (match (desktop-exec i (application-folders (some? host-spawn)))
|
||
|
|
((Some exec) (private-command i exec address))
|
||
|
|
(None None)))
|
||
|
|
(None None))
|
||
|
|
((Some (Tuple program args))
|
||
|
|
(match host-spawn
|
||
|
|
((Some spawn)
|
||
|
|
(run-opener spawn (vec-append ["-no-pty" "-env" host-variables program] args) address))
|
||
|
|
(None (run-opener program args address))))
|
||
|
|
(None (Err none))))))))))
|
||
|
|
|