File: ephy-flatpak-utils.c

package info (click to toggle)
epiphany-browser 3.38.2-1%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,476 kB
  • sloc: ansic: 65,045; javascript: 65,029; xml: 1,000; python: 163; sh: 82; makefile: 14
file content (125 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2017 Igalia S.L.
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Epiphany is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

/* For O_PATH */
#define _GNU_SOURCE

#include <config.h>
#include "ephy-flatpak-utils.h"

#include <errno.h>
#include <fcntl.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#if USE_LIBPORTAL
#include <libportal/portal-gtk3.h>
#endif
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

static gboolean is_web_process = FALSE;

void
ephy_flatpak_utils_set_is_web_process_extension (void)
{
  g_assert (!is_web_process);

  is_web_process = TRUE;
}

gboolean
ephy_is_running_inside_flatpak (void)
{
  static _Thread_local gboolean decided = FALSE;
  static _Thread_local gboolean under_flatpak = FALSE;

  if (decided)
    return under_flatpak;

  /* This function cannot be used in the web process extension, because WebKit
   * creates a .flatpak-info in its web process sandbox even when we are not
   * running under flatpak. It would always return TRUE.
   */
  g_assert (!is_web_process);

  under_flatpak = g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
  decided = TRUE;
  return under_flatpak;
}

#if USE_LIBPORTAL
static void
opened_uri (GObject      *object,
            GAsyncResult *result,
            gpointer      data)
{
  g_autoptr (XdpPortal) portal = XDP_PORTAL (object);
  g_autoptr (GError) error = NULL;
  gboolean open_dir = GPOINTER_TO_INT (data);
  gboolean res;

  if (open_dir)
    res = xdp_portal_open_directory_finish (portal, result, &error);
  else
    res = xdp_portal_open_uri_finish (portal, result, &error);

  if (!res)
    g_warning ("%s", error->message);
}
#endif

static void
ephy_open_uri (const char *uri,
               gboolean    is_dir)
{
#if USE_LIBPORTAL
  GApplication *application;
  GtkWindow *window;
  XdpParent *parent;
  g_autoptr (XdpPortal) portal = xdp_portal_new ();

  application = g_application_get_default ();
  window = gtk_application_get_active_window (GTK_APPLICATION (application));
  parent = xdp_parent_new_gtk (window);

  if (is_dir)
    xdp_portal_open_directory (g_steal_pointer (&portal), parent, uri, XDP_OPEN_URI_FLAG_ASK, NULL, opened_uri, GINT_TO_POINTER (TRUE));
  else
    xdp_portal_open_uri (g_steal_pointer (&portal), parent, uri, XDP_OPEN_URI_FLAG_ASK, NULL, opened_uri, GINT_TO_POINTER (FALSE));

  xdp_parent_free (parent);
#else
  g_warning ("Flatpak portal support disabled at compile time, cannot open %s",
             uri);
#endif
}

void
ephy_open_directory_via_flatpak_portal (const char *uri)
{
  ephy_open_uri (uri, TRUE);
}

void
ephy_open_uri_via_flatpak_portal (const char *uri)
{
  ephy_open_uri (uri, FALSE);
}