github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/usersession/xdgopenproxy/xdgopenproxy.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  // Package xdgopenproxy provides a client for snap userd's xdg-open D-Bus proxy
    21  package xdgopenproxy
    22  
    23  import (
    24  	"net/url"
    25  
    26  	"github.com/godbus/dbus"
    27  	"golang.org/x/xerrors"
    28  )
    29  
    30  type responseError struct {
    31  	msg string
    32  }
    33  
    34  func (u *responseError) Error() string { return u.msg }
    35  
    36  func (u *responseError) Is(err error) bool {
    37  	_, ok := err.(*responseError)
    38  	return ok
    39  }
    40  
    41  type desktopLauncher interface {
    42  	OpenFile(bus *dbus.Conn, path string) error
    43  	OpenURI(bus *dbus.Conn, url string) error
    44  }
    45  
    46  var availableLaunchers = []desktopLauncher{
    47  	&portalLauncher{},
    48  	&userdLauncher{},
    49  }
    50  
    51  // Run attempts to open given file or URL using one of available launchers
    52  func Run(urlOrFile string) error {
    53  	bus, err := dbus.SessionBus()
    54  	if err != nil {
    55  		return err
    56  	}
    57  	defer bus.Close()
    58  
    59  	return launch(bus, availableLaunchers, urlOrFile)
    60  }
    61  
    62  func launchWithOne(bus *dbus.Conn, l desktopLauncher, urlOrFile string) error {
    63  	if u, err := url.Parse(urlOrFile); err == nil {
    64  		if u.Scheme == "file" {
    65  			return l.OpenFile(bus, u.Path)
    66  		} else if u.Scheme != "" {
    67  			return l.OpenURI(bus, urlOrFile)
    68  		}
    69  	}
    70  	return l.OpenFile(bus, urlOrFile)
    71  }
    72  
    73  func launch(bus *dbus.Conn, launchers []desktopLauncher, urlOrFile string) error {
    74  	var err error
    75  	for _, l := range launchers {
    76  		err = launchWithOne(bus, l, urlOrFile)
    77  		if err == nil {
    78  			break
    79  		}
    80  		if xerrors.Is(err, &responseError{}) {
    81  			// got a response which indicates the action was either
    82  			// explicitly rejected by the user or abandoned due to
    83  			// other reasons eg. timeout waiting for user to respond
    84  			break
    85  		}
    86  	}
    87  	return err
    88  }