summaryrefslogtreecommitdiff
path: root/src/ephy-permission-popover.c
blob: 6068f243955b28daa939bcdd827eddaf1d645a91 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2023 Jamie Murphy <hello@itsjamie.dev>
 *
 *  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/>.
 */

#include "config.h"

#include "ephy-embed-shell.h"
#include "ephy-lib-type-builtins.h"
#include "ephy-permission-popover.h"

#include <glib/gi18n.h>

struct _EphyPermissionPopover {
  GtkPopover parent_instance;

  GtkLabel *permission_title;
  GtkLabel *permission_description;

  EphyPermissionType permission_type;
  WebKitPermissionRequest *permission_request;
  char *origin;
};

G_DEFINE_FINAL_TYPE (EphyPermissionPopover, ephy_permission_popover, GTK_TYPE_POPOVER)

enum {
  PROP_0,
  PROP_PERMISSION_TYPE,
  PROP_PERMISSION_REQUEST,
  PROP_ORIGIN,
  LAST_PROP
};

static GParamSpec *obj_properties[LAST_PROP];

enum {
  ALLOW,
  DENY,
  LAST_SIGNAL
};

static gint signals[LAST_SIGNAL] = { 0 };

static void
on_permission_deny (EphyPermissionPopover *self)
{
  gtk_popover_popdown (GTK_POPOVER (self));
  g_signal_emit (self, signals[DENY], 0);
}

static void
on_permission_allow (EphyPermissionPopover *self)
{
  gtk_popover_popdown (GTK_POPOVER (self));
  g_signal_emit (self, signals[ALLOW], 0);
}

static void
ephy_permission_popover_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  EphyPermissionPopover *self = EPHY_PERMISSION_POPOVER (object);

  switch (prop_id) {
    case PROP_PERMISSION_TYPE:
      g_value_set_enum (value, self->permission_type);
      break;
    case PROP_PERMISSION_REQUEST:
      g_value_set_object (value, self->permission_request);
      break;
    case PROP_ORIGIN:
      g_value_set_string (value, self->origin);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
ephy_permission_popover_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  EphyPermissionPopover *self = EPHY_PERMISSION_POPOVER (object);

  switch (prop_id) {
    case PROP_PERMISSION_TYPE:
      self->permission_type = g_value_get_enum (value);
      break;
    case PROP_PERMISSION_REQUEST:
      self->permission_request = g_object_ref (g_value_get_object (value));
      break;
    case PROP_ORIGIN:
      self->origin = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
ephy_permission_popover_constructed (GObject *object)
{
  EphyPermissionPopover *self = EPHY_PERMISSION_POPOVER (object);
  g_autofree char *title = NULL;
  g_autofree char *message = NULL;

  ephy_permission_popover_get_text (self, &title, &message);

  gtk_label_set_label (self->permission_title, title);
  gtk_label_set_label (self->permission_description, message);
}

static void
ephy_permission_popover_dispose (GObject *object)
{
  EphyPermissionPopover *self = EPHY_PERMISSION_POPOVER (object);

  g_clear_object (&self->permission_request);

  G_OBJECT_CLASS (ephy_permission_popover_parent_class)->dispose (object);
}

static void
ephy_permission_popover_finalize (GObject *object)
{
  EphyPermissionPopover *self = EPHY_PERMISSION_POPOVER (object);

  g_free (self->origin);

  G_OBJECT_CLASS (ephy_permission_popover_parent_class)->finalize (object);
}

static void
ephy_permission_popover_class_init (EphyPermissionPopoverClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->get_property = ephy_permission_popover_get_property;
  object_class->set_property = ephy_permission_popover_set_property;
  object_class->constructed = ephy_permission_popover_constructed;
  object_class->dispose = ephy_permission_popover_dispose;
  object_class->finalize = ephy_permission_popover_finalize;

  obj_properties[PROP_PERMISSION_TYPE] =
    g_param_spec_enum ("permission-type", "", "", EPHY_TYPE_PERMISSION_TYPE,
                       EPHY_PERMISSION_TYPE_SHOW_NOTIFICATIONS,
                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_PERMISSION_REQUEST] =
    g_param_spec_object ("permission-request", "", "", WEBKIT_TYPE_PERMISSION_REQUEST,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_ORIGIN] =
    g_param_spec_string ("origin", "", "", "",
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, LAST_PROP, obj_properties);

  /**
   * EphyPermissionPopover::allow:
   * @popover: the object on which the signal is emitted
   *
   * Emitted when the user presses "Allow" on the popover prompt
   *
   */
  signals[ALLOW] = g_signal_new ("allow", G_OBJECT_CLASS_TYPE (klass),
                                 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
                                 0, NULL, NULL, NULL,
                                 G_TYPE_NONE,
                                 0);

  /**
   * EphyPermissionPopover::deny:
   * @popover: the object on which the signal is emitted
   *
   * Emitted when the user presses "Deny" on the popover prompt
   *
   */
  signals[DENY] = g_signal_new ("deny", G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
                                0, NULL, NULL, NULL,
                                G_TYPE_NONE,
                                0);

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/epiphany/gtk/permission-popover.ui");
  gtk_widget_class_bind_template_child (widget_class, EphyPermissionPopover, permission_title);
  gtk_widget_class_bind_template_child (widget_class, EphyPermissionPopover, permission_description);

  gtk_widget_class_bind_template_callback (widget_class, on_permission_deny);
  gtk_widget_class_bind_template_callback (widget_class, on_permission_allow);
}

static void
ephy_permission_popover_init (EphyPermissionPopover *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}

EphyPermissionPopover *
ephy_permission_popover_new (EphyPermissionType       permission_type,
                             WebKitPermissionRequest *permission_request,
                             const char              *origin)
{
  return g_object_new (EPHY_TYPE_PERMISSION_POPOVER,
                       "permission-type", permission_type,
                       "permission-request", permission_request,
                       "origin", origin,
                       NULL);
}

EphyPermissionType
ephy_permission_popover_get_permission_type (EphyPermissionPopover *self)
{
  g_assert (EPHY_IS_PERMISSION_POPOVER (self));

  return self->permission_type;
}

WebKitPermissionRequest *
ephy_permission_popover_get_permission_request (EphyPermissionPopover *self)
{
  g_assert (EPHY_IS_PERMISSION_POPOVER (self));

  return self->permission_request;
}

const char *
ephy_permission_popover_get_origin (EphyPermissionPopover *self)
{
  g_assert (EPHY_IS_PERMISSION_POPOVER (self));

  return g_strdup (self->origin);
}

void
ephy_permission_popover_get_text (EphyPermissionPopover  *self,
                                  char                  **title,
                                  char                  **message)
{
  const char *requesting_domain = NULL;
  const char *current_domain = NULL;
  g_autofree char *bold_origin = NULL;

  g_assert (EPHY_IS_PERMISSION_POPOVER (self));

  bold_origin = g_markup_printf_escaped ("<b>%s</b>", self->origin);
  switch (self->permission_type) {
    case EPHY_PERMISSION_TYPE_SHOW_NOTIFICATIONS:
      /* Translators: Notification policy for a specific site. */
      *title = g_strdup (_("Notification Request"));
      /* Translators: Notification policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to send you notifications"),
                                  bold_origin);
      break;
    case EPHY_PERMISSION_TYPE_ACCESS_LOCATION:
      /* Translators: Geolocation policy for a specific site. */
      *title = g_strdup (_("Location Access Request"));
      /* Translators: Geolocation policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to know your location"),
                                  bold_origin);
      break;
    case EPHY_PERMISSION_TYPE_ACCESS_MICROPHONE:
      /* Translators: Microphone policy for a specific site. */
      *title = g_strdup (_("Microphone Access Request"));
      /* Translators: Microphone policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to use your microphone"),
                                  bold_origin);
      break;
    case EPHY_PERMISSION_TYPE_ACCESS_WEBCAM:
      /* Translators: Webcam policy for a specific site. */
      *title = g_strdup (_("Webcam Access Request"));
      /* Translators: Webcam policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to use your webcam"),
                                  bold_origin);
      break;
    case EPHY_PERMISSION_TYPE_ACCESS_WEBCAM_AND_MICROPHONE:
      /* Translators: Webcam and microphone policy for a specific site. */
      *title = g_strdup (_("Webcam and Microphone Access Request"));
      /* Translators: Webcam and microphone policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to use your webcam and microphone"),
                                  bold_origin);
      break;
    case EPHY_PERMISSION_TYPE_COOKIES:
      requesting_domain = webkit_website_data_access_permission_request_get_requesting_domain (WEBKIT_WEBSITE_DATA_ACCESS_PERMISSION_REQUEST (self->permission_request));
      current_domain = webkit_website_data_access_permission_request_get_current_domain (WEBKIT_WEBSITE_DATA_ACCESS_PERMISSION_REQUEST (self->permission_request));
      /* Translators: Storage access policy for a specific site. */
      *title = g_strdup (_("Website Data Access Request"));
      /* Translators: Storage access policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to access its own data (including cookies) while browsing “%s”. This will allow “%s” to track your activity on “%s”."),
                                  requesting_domain, current_domain, requesting_domain, current_domain);
      break;
    case EPHY_PERMISSION_TYPE_CLIPBOARD:
      /* Translators: Clipboard policy for a specific site. */
      *title = g_strdup (_("Clipboard Access Request"));
      /* Translators: Clipboard policy for a specific site. */
      *message = g_strdup_printf (_("The page at “%s” would like to access your clipboard"),
                                  bold_origin);
      break;
    default:
      g_assert_not_reached ();
  }
}