ephy-web-view.c 84.8 KB
Newer Older
1
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 ts=2 sts=2 et: */
3
/*
4
 *  Copyright © 2008, 2009 Gustavo Noronha Silva
5
 *  Copyright © 2009, 2010 Igalia S.L.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  This program 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 2, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
Xan Lopez's avatar
Xan Lopez committed
24
#include "ephy-web-view.h"
25

26
#include "ephy-about-handler.h"
27 28 29
#include "ephy-debug.h"
#include "ephy-embed-container.h"
#include "ephy-embed-prefs.h"
30
#include "ephy-embed-private.h"
31 32 33
#include "ephy-embed-shell.h"
#include "ephy-embed-type-builtins.h"
#include "ephy-embed-utils.h"
Xan Lopez's avatar
Xan Lopez committed
34
#include "ephy-embed.h"
35
#include "ephy-favicon-helpers.h"
36
#include "ephy-file-helpers.h"
37
#include "ephy-file-monitor.h"
38
#include "ephy-form-auth-data.h"
Xan Lopez's avatar
Xan Lopez committed
39
#include "ephy-history-service.h"
Xan Lopez's avatar
Xan Lopez committed
40
#include "ephy-prefs.h"
41
#include "ephy-settings.h"
42
#include "ephy-snapshot-service.h"
43
#include "ephy-string.h"
Xan Lopez's avatar
Xan Lopez committed
44
#include "ephy-web-app-utils.h"
45
#include "ephy-web-dom-utils.h"
46
#include "ephy-web-extension-proxy.h"
47 48
#include "ephy-zoom.h"

Xan Lopez's avatar
Xan Lopez committed
49 50 51 52
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
53
#include <libsoup/soup.h>
Xan Lopez's avatar
Xan Lopez committed
54

55 56 57 58 59 60 61 62
/**
 * SECTION:ephy-web-view
 * @short_description: Epiphany custom #WebkitWebView
 *
 * #EphyWebView wraps #WebkitWebView implementing custom functionality on top of
 * it.
 */

63 64
#define MAX_HIDDEN_POPUPS       5

65 66
#define EPHY_PAGE_TEMPLATE_ERROR         "/org/gnome/epiphany/page-templates/error.html"

67 68 69 70 71 72 73
struct _EphyWebViewPrivate {
  EphyWebViewSecurityLevel security_level;
  EphyWebViewDocumentType document_type;
  EphyWebViewNavigationFlags nav_flags;

  /* Flags */
  guint is_blank : 1;
74
  guint is_setting_zoom : 1;
75
  guint load_failed : 1;
76
  guint history_frozen : 1;
77
  guint ever_committed : 1;
78 79 80

  char *address;
  char *typed_address;
81
  char *loading_message;
82 83 84
  char *link_message;
  GdkPixbuf *icon;

85 86
  /* Local file watch. */
  EphyFileMonitor *file_monitor;
87

88 89
  /* Regex to figure out if we're dealing with a wanna-be URI */
  GRegex *non_search_regex;
90
  GRegex *domain_regex;
91

92 93
  GSList *hidden_popups;
  GSList *shown_popups;
94 95

  GtkWidget *password_info_bar;
96

97
  EphyHistoryService *history_service;
98 99
  GCancellable *history_service_cancellable;

100
  guint snapshot_timeout_id;
101

102
  EphyHistoryPageVisitType visit_type;
103 104

  gulong do_not_track_handler;
105 106 107 108

  /* TLS information. */
  GTlsCertificate *certificate;
  GTlsCertificateFlags tls_errors;
109 110 111

  /* Web Extension */
  EphyWebExtensionProxy *web_extension;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
};

typedef struct {
  char *url;
  char *name;
  char *features;
} PopupInfo;

enum {
  PROP_0,
  PROP_ADDRESS,
  PROP_DOCUMENT_TYPE,
  PROP_HIDDEN_POPUP_COUNT,
  PROP_ICON,
  PROP_LINK_MESSAGE,
  PROP_NAVIGATION,
  PROP_POPUPS_ALLOWED,
  PROP_SECURITY,
  PROP_STATUS_MESSAGE,
  PROP_TYPED_ADDRESS,
132
  PROP_IS_BLANK,
133 134 135 136
};

#define EPHY_WEB_VIEW_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_WEB_VIEW, EphyWebViewPrivate))

137 138
G_DEFINE_TYPE (EphyWebView, ephy_web_view, WEBKIT_TYPE_WEB_VIEW)

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static guint
popup_blocker_n_hidden (EphyWebView *view)
{
  return g_slist_length (view->priv->hidden_popups);
}

static void
popups_manager_free_info (PopupInfo *popup)
{
  g_free (popup->url);
  g_free (popup->name);
  g_free (popup->features);
  g_slice_free (PopupInfo, popup);
}

static void
popups_manager_show (PopupInfo *popup,
                     EphyWebView *view)
{
  /* Only show popup with non NULL url */
  if (popup->url != NULL) {
160 161 162
    /* FIXME: we need a way of opening windows in here. This used to
     * be implemented in EphyEmbedSingle open_window method, but it's
     * been a no-op for a while. */
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  }
  popups_manager_free_info (popup);
}

static void
popups_manager_show_all (EphyWebView *view)
{
  LOG ("popup_blocker_show_all: view %p", view);

  g_slist_foreach (view->priv->hidden_popups,
                   (GFunc)popups_manager_show, view);
  g_slist_free (view->priv->hidden_popups);
  view->priv->hidden_popups = NULL;

  g_object_notify (G_OBJECT (view), "hidden-popup-count");
}

static char *
popups_manager_new_window_info (EphyEmbedContainer *container)
{
  EphyEmbed *embed;
184
  GtkAllocation allocation;
185 186 187
  gboolean is_popup;
  char *features;

188
  g_object_get (container, "is-popup", &is_popup, NULL);
189 190 191 192 193
  g_return_val_if_fail (is_popup, g_strdup (""));

  embed = ephy_embed_container_get_active_child (container);
  g_return_val_if_fail (embed != NULL, g_strdup (""));

194 195
  gtk_widget_get_allocation (GTK_WIDGET (embed), &allocation);

196
  features = g_strdup_printf
197
             ("width=%d,height=%d,toolbar=%d",
198 199
              allocation.width,
              allocation.height,
200
              1);
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

  return features;
}

static void
popups_manager_add (EphyWebView *view,
                    const char *url,
                    const char *name,
                    const char *features)
{
  EphyWebViewPrivate *priv = view->priv;
  PopupInfo *popup;

  LOG ("popups_manager_add: view %p, url %s, features %s",
       view, url, features);

  popup = g_slice_new (PopupInfo);

  popup->url = g_strdup (url);
  popup->name = g_strdup (name);
  popup->features = g_strdup (features);

  priv->hidden_popups = g_slist_prepend (priv->hidden_popups, popup);

  if (popup_blocker_n_hidden (view) > MAX_HIDDEN_POPUPS) {/* bug #160863 */
    /* Remove the oldest popup */
    GSList *l = view->priv->hidden_popups;

    while (l->next->next != NULL) {
      l = l->next;
    }

    popup = (PopupInfo *)l->next->data;
    popups_manager_free_info (popup);

    l->next = NULL;
  } else {
    g_object_notify (G_OBJECT (view), "hidden-popup-count");
  }
}

static void
popups_manager_hide (EphyEmbedContainer *container,
                     EphyWebView *parent_view)
{
  EphyEmbed *embed;
247
  const char *location;
248 249 250 251 252
  char *features;

  embed = ephy_embed_container_get_active_child (container);
  g_return_if_fail (EPHY_IS_EMBED (embed));

253
  location = ephy_web_view_get_address (ephy_embed_get_web_view (embed));
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
  if (location == NULL) return;

  features = popups_manager_new_window_info (container);

  popups_manager_add (parent_view, location, "" /* FIXME? maybe _blank? */, features);

  gtk_widget_destroy (GTK_WIDGET (container));

  g_free (features);
}

static void
popups_manager_hide_all (EphyWebView *view)
{
  LOG ("popup_blocker_hide_all: view %p", view);

  g_slist_foreach (view->priv->shown_popups,
                   (GFunc)popups_manager_hide, view);
  g_slist_free (view->priv->shown_popups);
  view->priv->shown_popups = NULL;
}

static void
ephy_web_view_set_popups_allowed (EphyWebView *view,
                                  gboolean allowed)
{
  if (allowed) {
    popups_manager_show_all (view);
  } else {
    popups_manager_hide_all (view);
  }
}

static gboolean
ephy_web_view_get_popups_allowed (EphyWebView *view)
{
290
  const char *location;
291 292
  gboolean allow;

293
  location = ephy_web_view_get_address (view);
294 295
  if (location == NULL) return FALSE;/* FALSE, TRUE… same thing */

Xan Lopez's avatar
Xan Lopez committed
296 297
  allow = g_settings_get_boolean (EPHY_SETTINGS_WEB,
                                  EPHY_PREFS_WEB_ENABLE_POPUPS);
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 324 325 326 327 328 329 330 331
  return allow;
}

static gboolean
popups_manager_remove_window (EphyWebView *view,
                              EphyEmbedContainer *container)
{
  view->priv->shown_popups = g_slist_remove (view->priv->shown_popups,
                                              container);

  return FALSE;
}

static void
popups_manager_add_window (EphyWebView *view,
                           EphyEmbedContainer *container)
{
  LOG ("popups_manager_add_window: view %p, container %p", view, container);

  view->priv->shown_popups = g_slist_prepend (view->priv->shown_popups, container);

  g_signal_connect_swapped (container, "destroy",
                            G_CALLBACK (popups_manager_remove_window),
                            view);
}

static void
disconnect_popup (EphyEmbedContainer *container,
                  EphyWebView *view)
{
  g_signal_handlers_disconnect_by_func
  (container, G_CALLBACK (popups_manager_remove_window), view);
}

332 333 334 335 336 337
/**
 * ephy_web_view_popups_manager_reset:
 * @view: an #EphyWebView
 *
 * Resets the state of the popups manager in @view.
 **/
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
void
ephy_web_view_popups_manager_reset (EphyWebView *view)
{
  g_slist_foreach (view->priv->hidden_popups,
                   (GFunc)popups_manager_free_info, NULL);
  g_slist_free (view->priv->hidden_popups);
  view->priv->hidden_popups = NULL;

  g_slist_foreach (view->priv->shown_popups,
                   (GFunc)disconnect_popup, view);
  g_slist_free (view->priv->shown_popups);
  view->priv->shown_popups = NULL;

  g_object_notify (G_OBJECT (view), "hidden-popup-count");
  g_object_notify (G_OBJECT (view), "popups-allowed");
}

static void
ephy_web_view_get_property (GObject *object,
                            guint prop_id,
                            GValue *value,
                            GParamSpec *pspec)
{
  EphyWebViewPrivate *priv = EPHY_WEB_VIEW (object)->priv;

  switch (prop_id) {
    case PROP_ADDRESS:
      g_value_set_string (value, priv->address);
      break;
    case PROP_TYPED_ADDRESS:
      g_value_set_string (value, priv->typed_address);
      break;
    case PROP_DOCUMENT_TYPE:
      g_value_set_enum (value, priv->document_type);
      break;
    case PROP_HIDDEN_POPUP_COUNT:
      g_value_set_int (value, popup_blocker_n_hidden
                       (EPHY_WEB_VIEW (object)));
      break;
    case PROP_ICON:
      g_value_set_object (value, priv->icon);
      break;
    case PROP_LINK_MESSAGE:
      g_value_set_string (value, priv->link_message);
      break;
    case PROP_NAVIGATION:
      g_value_set_flags (value, priv->nav_flags);
      break;
    case PROP_POPUPS_ALLOWED:
      g_value_set_boolean (value, ephy_web_view_get_popups_allowed
                           (EPHY_WEB_VIEW (object)));
      break;
    case PROP_SECURITY:
      g_value_set_enum (value, priv->security_level);
      break;
    case PROP_STATUS_MESSAGE:
394
      g_value_set_string (value, ephy_web_view_get_status_message (EPHY_WEB_VIEW (object)));
395
      break;
396 397 398
    case PROP_IS_BLANK:
      g_value_set_boolean (value, priv->is_blank);
      break;
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    default:
      break;
  }
}

static void
ephy_web_view_set_property (GObject *object,
                            guint prop_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
  switch (prop_id) {
    case PROP_POPUPS_ALLOWED:
      ephy_web_view_set_popups_allowed (EPHY_WEB_VIEW (object), g_value_get_boolean (value));
      break;
    case PROP_TYPED_ADDRESS:
415
      ephy_web_view_set_typed_address (EPHY_WEB_VIEW (object), g_value_get_string (value));
416 417 418 419 420 421 422 423 424 425
      break;
      break;
    case PROP_ADDRESS:
    case PROP_DOCUMENT_TYPE:
    case PROP_HIDDEN_POPUP_COUNT:
    case PROP_ICON:
    case PROP_LINK_MESSAGE:
    case PROP_NAVIGATION:
    case PROP_SECURITY:
    case PROP_STATUS_MESSAGE:
426
    case PROP_IS_BLANK:
427 428 429 430 431 432 433
      /* read only */
      break;
    default:
      break;
  }
}

434 435 436
static gboolean
ephy_web_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
{
437 438
  EphyWebView *web_view = EPHY_WEB_VIEW (widget);
  gboolean key_handled = FALSE;
439

440
  key_handled = GTK_WIDGET_CLASS (ephy_web_view_parent_class)->key_press_event (widget, event);
441

442 443
  if (key_handled)
    return TRUE;
444

445
  g_signal_emit_by_name (web_view, "search-key-press", event, &key_handled);
446

447
  return key_handled;
448 449
}

450 451 452
static gboolean
ephy_web_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
{
453 454 455 456 457
  /* This are the special cases WebkitWebView doesn't handle but we have an
   * interest in handling. */

  /* We always show the browser context menu on control-rightclick. */
  if (event->button == 3 && event->state == GDK_CONTROL_MASK)
458
    return FALSE;
459

460 461 462 463 464 465 466 467 468 469 470
  /* Handle typical back/forward mouse buttons. */
  if (event->button == 8) {
    webkit_web_view_go_back (WEBKIT_WEB_VIEW (widget));
    return TRUE;
  }

  if (event->button == 9) {
    webkit_web_view_go_forward (WEBKIT_WEB_VIEW (widget));
    return TRUE;
  }

471
  /* Let WebKitWebView handle this. */
472
  return GTK_WIDGET_CLASS (ephy_web_view_parent_class)->button_press_event (widget, event);
473 474
}

475 476 477 478 479 480 481 482 483 484 485 486 487
static GtkWidget *
ephy_web_view_create_form_auth_save_confirmation_info_bar (EphyWebView *web_view,
                                                           const char *hostname,
                                                           const char *username)
{
  GtkWidget *info_bar;
  GtkWidget *action_area;
  GtkWidget *content_area;
  GtkWidget *label;
  char *message;

  LOG ("Going to show infobar about %s", webkit_web_view_get_uri (WEBKIT_WEB_VIEW (web_view)));

488
  info_bar = gtk_info_bar_new_with_buttons (_("Save"), GTK_RESPONSE_YES,
489
                                            NULL);
490
  gtk_info_bar_set_show_close_button (GTK_INFO_BAR (info_bar), TRUE);
491 492 493 494 495 496

  action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
  gtk_orientable_set_orientation (GTK_ORIENTABLE (action_area),
                                  GTK_ORIENTATION_HORIZONTAL);

  label = gtk_label_new (NULL);
497 498
  /* Translators: The %s the hostname where this is happening. 
   * Example: mail.google.com.
499
   */
500 501
  message = g_markup_printf_escaped (_("Do you want to save your password for “%s”?"),
                                     hostname);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  gtk_label_set_markup (GTK_LABEL (label), message);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  g_free (message);

  content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
  gtk_container_add (GTK_CONTAINER (content_area), label);
  gtk_widget_show (label);

  ephy_embed_add_top_widget (EPHY_GET_EMBED_FROM_EPHY_WEB_VIEW (web_view),
                             info_bar, FALSE);

  /* We track the info_bar, so we only ever show one */
  if (web_view->priv->password_info_bar)
    gtk_widget_destroy (web_view->priv->password_info_bar);

  web_view->priv->password_info_bar = info_bar;
  g_object_add_weak_pointer (G_OBJECT (info_bar),
                             (gpointer *)&web_view->priv->password_info_bar);

  return info_bar;
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static void
update_navigation_flags (EphyWebView *view)
{
  EphyWebViewPrivate *priv = view->priv;
  guint flags = 0;
  WebKitWebView *web_view = WEBKIT_WEB_VIEW (view);

  if (webkit_web_view_can_go_back (web_view))
    flags |= EPHY_WEB_VIEW_NAV_BACK;

  if (webkit_web_view_can_go_forward (web_view))
    flags |= EPHY_WEB_VIEW_NAV_FORWARD;

  if (priv->nav_flags != (EphyWebViewNavigationFlags)flags) {
    priv->nav_flags = (EphyWebViewNavigationFlags)flags;

    g_object_notify (G_OBJECT (view), "navigation");
  }
}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static void
ephy_web_view_freeze_history (EphyWebView *view)
{
  view->priv->history_frozen = TRUE;
}

static void
ephy_web_view_thaw_history (EphyWebView *view)
{
  view->priv->history_frozen = FALSE;
}

static gboolean
ephy_web_view_is_history_frozen (EphyWebView *view)
{
  return view->priv->history_frozen;
}


563 564 565
static void
ephy_web_view_clear_history (EphyWebView *view)
{
566
  /* TODO: WebKitBackForwardList is read-only in WebKit2 */
567 568
}

569 570 571 572 573 574 575
static void
ephy_web_view_history_cleared_cb (EphyHistoryService *history_service,
                                  EphyWebView *view)
{
  ephy_web_view_clear_history (view);
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
typedef struct {
  char *url;
  time_t mtime;
} GetSnapshotPathAsyncData;

static void
got_snapshot_path_cb (EphySnapshotService *service,
                      GAsyncResult *result,
                      GetSnapshotPathAsyncData *data)
{
  char *snapshot;

  snapshot = ephy_snapshot_service_get_snapshot_path_finish (service, result, NULL);
  if (snapshot) {
    ephy_embed_shell_set_thumbanil_path (ephy_embed_shell_get_default (), data->url, data->mtime, snapshot);
    g_free (snapshot);
  }
  g_free (data->url);
  g_free (data);
}

static gboolean
web_view_check_snapshot (WebKitWebView *web_view)
{
  EphyWebView* view = EPHY_WEB_VIEW (web_view);
  EphySnapshotService *service = ephy_snapshot_service_get_default ();
  const char *url = webkit_web_view_get_uri (web_view);
  GetSnapshotPathAsyncData *data;

  view->priv->snapshot_timeout_id = 0;

  if (ephy_snapshot_service_lookup_snapshot_path (service, url))
    return FALSE;

  data = g_new (GetSnapshotPathAsyncData, 1);
  data->url = g_strdup (url);
  data->mtime = time (NULL);
  ephy_snapshot_service_get_snapshot_path_async (service, web_view, data->mtime, NULL,
                                                 (GAsyncReadyCallback)got_snapshot_path_cb,
                                                 data);

  return FALSE;
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
static void
_ephy_web_view_update_icon (EphyWebView *view)
{
  EphyWebViewPrivate *priv = view->priv;

  if (priv->icon != NULL) {
    g_object_unref (priv->icon);
    priv->icon = NULL;
  }

  if (priv->address) {
    cairo_surface_t *icon_surface = webkit_web_view_get_favicon (WEBKIT_WEB_VIEW (view));
    if (icon_surface)
      priv->icon = ephy_pixbuf_get_from_surface_scaled (icon_surface, FAVICON_SIZE, FAVICON_SIZE);
  }

  g_object_notify (G_OBJECT (view), "icon");
}

static void
icon_changed_cb (EphyWebView *view,
                 GParamSpec *pspec,
                 gpointer user_data)
{
644 645 646 647 648
  if (view->priv->snapshot_timeout_id == 0) {
    view->priv->snapshot_timeout_id = g_timeout_add_full (G_PRIORITY_LOW, 0,
                                                          (GSourceFunc)web_view_check_snapshot,
                                                          view, NULL);
  }
649 650 651
  _ephy_web_view_update_icon (view);
}

652 653 654 655 656
typedef struct {
  EphyWebView *web_view;
  guint request_id;
} FormAuthRequestData;

657 658 659
static void
form_auth_data_save_confirmation_response (GtkInfoBar *info_bar,
                                           gint response_id,
660
                                           FormAuthRequestData *data)
661 662 663
{
  gtk_widget_destroy (GTK_WIDGET (info_bar));

664 665 666 667 668
  if (data->web_view->priv->web_extension) {
    ephy_web_extension_proxy_form_auth_data_save_confirmation_response (data->web_view->priv->web_extension,
                                                                        data->request_id,
                                                                        response_id == GTK_RESPONSE_YES);
  }
669

670
  g_slice_free (FormAuthRequestData, data);
671 672 673
}

static void
674
form_auth_data_save_requested (EphyWebExtensionProxy *web_extension,
675 676 677 678
                               guint request_id,
                               guint64 page_id,
                               const char *hostname,
                               const char *username,
679
                               EphyWebView *web_view)
680 681
{
  GtkWidget *info_bar;
682
  FormAuthRequestData *data;
683

684
  g_assert (webkit_web_view_get_page_id (WEBKIT_WEB_VIEW (web_view)) == page_id);
685

686 687 688 689
  info_bar = ephy_web_view_create_form_auth_save_confirmation_info_bar (web_view, hostname, username);
  data = g_slice_new (FormAuthRequestData);
  data->web_view = web_view;
  data->request_id = request_id;
690 691
  g_signal_connect (info_bar, "response",
                    G_CALLBACK (form_auth_data_save_confirmation_response),
692
                    data);
693 694 695 696

  gtk_widget_show (info_bar);
}

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
static void
page_created_cb (EphyEmbedShell *shell,
                 guint64 page_id,
                 EphyWebExtensionProxy *web_extension,
                 EphyWebView *web_view)
{
  EphyWebViewPrivate *priv = web_view->priv;

  if (webkit_web_view_get_page_id (WEBKIT_WEB_VIEW (web_view)) != page_id)
    return;

  priv->web_extension = web_extension;
  g_object_add_weak_pointer (G_OBJECT (priv->web_extension), (gpointer *)&priv->web_extension);

  g_signal_connect (priv->web_extension, "form-auth-data-save-requested",
                    G_CALLBACK (form_auth_data_save_requested),
                    web_view);
}

716
static void
717
ephy_web_view_dispose (GObject *object)
718 719 720
{
  EphyWebViewPrivate *priv = EPHY_WEB_VIEW (object)->priv;

721 722 723 724
  g_signal_handlers_disconnect_by_func (priv->history_service,
                                        ephy_web_view_history_cleared_cb,
                                        EPHY_WEB_VIEW (object));

725
  g_signal_handlers_disconnect_by_func (object, icon_changed_cb, NULL);
726 727

  g_signal_handlers_disconnect_by_func (ephy_embed_shell_get_default (), form_auth_data_save_requested, object);
728

729 730 731 732 733 734 735 736 737
  g_clear_object (&priv->file_monitor);

  g_clear_object (&priv->icon);

  if (priv->history_service_cancellable) {
    g_cancellable_cancel (priv->history_service_cancellable);
    g_clear_object (&priv->history_service_cancellable);
  }

738 739 740
  if (priv->snapshot_timeout_id) {
    g_source_remove (priv->snapshot_timeout_id);
    priv->snapshot_timeout_id = 0;
741 742 743 744 745 746 747 748 749 750 751 752
  }

  g_clear_object(&priv->certificate);

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

static void
ephy_web_view_finalize (GObject *object)
{
  EphyWebViewPrivate *priv = EPHY_WEB_VIEW (object)->priv;

753 754 755 756 757
  ephy_web_view_popups_manager_reset (EPHY_WEB_VIEW (object));

  g_free (priv->address);
  g_free (priv->typed_address);
  g_free (priv->link_message);
758
  g_free (priv->loading_message);
759 760 761 762

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

763 764 765 766 767 768 769 770 771 772 773 774
static void
_ephy_web_view_set_is_blank (EphyWebView *view,
                             gboolean is_blank)
{
  EphyWebViewPrivate *priv = view->priv;

  if (priv->is_blank != is_blank) {
    priv->is_blank = is_blank;
    g_object_notify (G_OBJECT (view), "is-blank");
  }
}

775 776 777 778 779 780
static void
title_changed_cb (WebKitWebView *web_view,
                  GParamSpec *spec,
                  gpointer data)
{
  const char *uri;
781 782
  const char *title;
  char *title_from_address = NULL;
783 784
  EphyWebView *webview  = EPHY_WEB_VIEW (web_view);
  EphyHistoryService *history = webview->priv->history_service;
785

786
  uri = webkit_web_view_get_uri (web_view);
787
  title = webkit_web_view_get_title (web_view);
788

789
  if (!title && uri)
790
    title = title_from_address = ephy_embed_utils_get_title_from_address (uri);
791

792
  if (uri && title && !ephy_web_view_is_history_frozen (webview))
793
    ephy_history_service_set_url_title (history, uri, title, NULL, NULL, NULL);
794

795
  g_free (title_from_address);
796 797
}

798
/*
799
 * Sets the view location to be address. Note that this function might
800
 * also set the typed-address property to NULL.
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
 */
static void
ephy_web_view_set_address (EphyWebView *view,
                           const char *address)
{
  EphyWebViewPrivate *priv = view->priv;
  GObject *object = G_OBJECT (view);
  gboolean is_blank;

  g_free (priv->address);
  priv->address = g_strdup (address);

  is_blank = address == NULL ||
             strcmp (address, "about:blank") == 0;
  _ephy_web_view_set_is_blank (view, is_blank);

  if (ephy_web_view_is_loading (view) && priv->typed_address != NULL)
    ephy_web_view_set_typed_address (view, NULL);

  g_object_notify (object, "address");
}

823 824 825 826 827 828 829 830
static void
uri_changed_cb (WebKitWebView *web_view,
                GParamSpec *spec,
                gpointer data)
{
  char *uri;
  const char *current_address;

831 832 833 834 835
  /* We already update the URI manually while loading, so only
   * update the URI when it changes after the page has been loaded
   * which is usually the result of navigation within the same page action.
   */
  if (webkit_web_view_is_loading (web_view))
836 837
    return;

838 839 840
  /* We need to check if we get URI notifications without going
     through the usual load process, as this can happen when changing
     location within a page */
841 842 843
  current_address = ephy_web_view_get_address (EPHY_WEB_VIEW (web_view));
  g_object_get (web_view, "uri", &uri, NULL);

844 845 846 847 848 849
  if (g_str_equal (uri, current_address) == FALSE)
    ephy_web_view_set_address (EPHY_WEB_VIEW (web_view), uri);

  g_free (uri);
}

850 851 852 853 854 855 856 857 858 859 860 861 862
static void
mouse_target_changed_cb (EphyWebView *web_view,
                         WebKitHitTestResult *hit_test_result,
                         guint modifiers,
                         gpointer data)
{
  const char *message = NULL;

  if (webkit_hit_test_result_context_is_link (hit_test_result))
    message = webkit_hit_test_result_get_link_uri (hit_test_result);

  ephy_web_view_set_link_message (web_view, message);
}
863 864

static void
865
process_crashed_cb (EphyWebView *web_view, gpointer user_data)
866
{
867 868
  if (ephy_embed_has_load_pending (EPHY_GET_EMBED_FROM_EPHY_WEB_VIEW (web_view)))
    return;
869

870 871
  ephy_web_view_load_error_page (web_view, ephy_web_view_get_address (web_view),
                                 EPHY_WEB_VIEW_ERROR_PROCESS_CRASH, NULL);
872 873
}

874 875 876
static void
ephy_web_view_constructed (GObject *object)
{
877 878 879
  if (G_OBJECT_CLASS (ephy_web_view_parent_class)->constructed)
    G_OBJECT_CLASS (ephy_web_view_parent_class)->constructed (object);

880 881 882 883 884
  /* Use full content zooming by default */
  /* FIXME: we could make this configurable through GSettings, or have
   * different keys for text and full content zooming. AFAIK you can
   * have both enabled at the same time in WebKit now (although our
   * API does not reflect this atm). See r67274 in WebKit. */
885
  g_signal_emit_by_name (ephy_embed_shell_get_default (), "web-view-created", object);
886 887 888

  g_signal_connect (object, "web-process-crashed",
                    G_CALLBACK (process_crashed_cb), object);
889 890
}

891 892 893
static void
impl_loading_homepage (EphyWebView *view)
{
894
  ephy_web_view_freeze_history (view);
895 896
}

897 898 899
static void
ephy_web_view_class_init (EphyWebViewClass *klass)
{
900
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
901
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
902 903 904 905 906

  gobject_class->dispose = ephy_web_view_dispose;
  gobject_class->finalize = ephy_web_view_finalize;
  gobject_class->get_property = ephy_web_view_get_property;
  gobject_class->set_property = ephy_web_view_set_property;
907
  gobject_class->constructed = ephy_web_view_constructed;
908

909
  widget_class->button_press_event = ephy_web_view_button_press_event;
910 911
  widget_class->key_press_event = ephy_web_view_key_press_event;

912 913
  klass->loading_homepage = impl_loading_homepage;

914 915 916 917 918
/**
 * EphyWebView:address:
 *
 * View's current address.
 **/
919 920 921 922 923 924 925
  g_object_class_install_property (gobject_class,
                                   PROP_ADDRESS,
                                   g_param_spec_string ("address",
                                                        "Address",
                                                        "The view's address",
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
926 927 928 929 930 931

/**
 * EphyWebView:typed-address:
 *
 * User typed address for the current view.
 **/
932 933 934 935 936 937 938
  g_object_class_install_property (gobject_class,
                                   PROP_TYPED_ADDRESS,
                                   g_param_spec_string ("typed-address",
                                                        "Typed Address",
                                                        "The typed address",
                                                        "",
                                                        G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
939 940 941 942 943 944

/**
 * EphyWebView:security-level:
 *
 * One of #EphyWebViewSecurityLevel, determining view's current security level.
 **/
945 946 947 948 949 950 951 952
  g_object_class_install_property (gobject_class,
                                   PROP_SECURITY,
                                   g_param_spec_enum ("security-level",
                                                      "Security Level",
                                                      "The view's security level",
                                                      EPHY_TYPE_WEB_VIEW_SECURITY_LEVEL,
                                                      EPHY_WEB_VIEW_STATE_IS_UNKNOWN,
                                                      G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
953 954 955 956 957 958

/**
 * EphyWebView:document-type:
 *
 * Document type determined for the view.
 **/
959 960 961 962
  g_object_class_install_property (gobject_class,
                                   PROP_DOCUMENT_TYPE,
                                   g_param_spec_enum ("document-type",
                                                      "Document Type",
Xan Lopez's avatar
Xan Lopez committed
963
                                                      "The view's document type",
964 965 966 967
                                                      EPHY_TYPE_WEB_VIEW_DOCUMENT_TYPE,
                                                      EPHY_WEB_VIEW_DOCUMENT_HTML,
                                                      G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

968 969 970 971 972
/**
 * EphyWebView:navigation:
 *
 * View's navigation flags as #EphyWebViewNavigationFlags.
 **/
973 974 975 976 977 978 979 980
  g_object_class_install_property (gobject_class,
                                   PROP_NAVIGATION,
                                   g_param_spec_flags ("navigation",
                                                       "Navigation flags",
                                                       "The view's navigation flags",
                                                       EPHY_TYPE_WEB_VIEW_NAVIGATION_FLAGS,
                                                       0,
                                                       G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
981 982 983 984 985 986

/**
 * EphyWebView:status-message:
 *
 * Statusbar message corresponding to this view.
 **/
987 988 989 990 991 992 993
  g_object_class_install_property (gobject_class,
                                   PROP_STATUS_MESSAGE,
                                   g_param_spec_string ("status-message",
                                                        "Status Message",
                                                        "The view's statusbar message",
                                                        NULL,
                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
994 995 996 997 998 999

/**
 * EphyWebView:link-message:
 *
 * ???
 **/
1000 1001 1002 1003 1004 1005 1006
  g_object_class_install_property (gobject_class,
                                   PROP_LINK_MESSAGE,
                                   g_param_spec_string ("link-message",
                                                        "Link Message",
                                                        "The view's link message",
                                                        NULL,
                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1007 1008 1009 1010 1011 1012

/**
 * EphyWebView:icon:
 *
 * View's favicon set by the loaded site.
 **/
1013 1014 1015 1016 1017 1018 1019 1020
  g_object_class_install_property (gobject_class,
                                   PROP_ICON,
                                   g_param_spec_object ("icon",
                                                        "Icon",
                                                        "The view icon's",
                                                        GDK_TYPE_PIXBUF,
                                                        G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

1021 1022 1023 1024 1025
/**
 * EphyWebView:hidden-popup-count:
 *
 * Number of hidden (blocked) popup windows.
 **/
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  g_object_class_install_property (gobject_class,
                                   PROP_HIDDEN_POPUP_COUNT,
                                   g_param_spec_int ("hidden-popup-count",
                                                     "Number of Blocked Popups",
                                                     "The view's number of blocked popup windows",
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

1036 1037 1038 1039 1040
/**
 * EphyWebView:popups-allowed:
 *
 * If popup windows from this view are to be displayed.
 **/
1041 1042 1043 1044 1045 1046 1047 1048
  g_object_class_install_property (gobject_class,
                                   PROP_POPUPS_ALLOWED,
                                   g_param_spec_boolean ("popups-allowed",
                                                         "Popups Allowed",
                                                         "Whether popup windows are to be displayed",
                                                         FALSE,
                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
/**
 * EphyWebView:is-blank:
 *
 * Whether the view is showing the blank address.
 **/
  g_object_class_install_property (gobject_class,
                                   PROP_IS_BLANK,
                                   g_param_spec_boolean ("is-blank",
                                                         "Is blank",
                                                         "If the EphyWebView is blank",
                                                         FALSE,
                                                         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));

1062
/**
1063
 * EphyWebView::new-window:
1064
 * @view: the #EphyWebView that received the signal
1065 1066
 * @new_view: the newly opened #EphyWebView
 *
1067
 * The ::new-window signal is emitted after a new window has been opened by
1068 1069
 * the view. For example, when a JavaScript popup window is opened.
 **/
1070
    g_signal_new ("new-window",
1071 1072 1073 1074 1075 1076 1077 1078
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (EphyWebViewClass, new_window),
            NULL, NULL,
            g_cclosure_marshal_VOID__OBJECT,
            G_TYPE_NONE,
            1,
            GTK_TYPE_WIDGET);
1079 1080
/**
 * EphyWebView::ge-popup-blocked:
1081
 * @view: the #EphyWebView that received the signal
1082 1083 1084 1085 1086 1087 1088
 * @address: The requested URL
 * @target: The requested window name, e.g. "_blank"
 * @features: The requested features: for example, "height=400,width=200"
 *
 * The ::ge_popup_blocked signal is emitted when the viewed web page requests
 * a popup window (with javascript:open()) but popup windows are not allowed.
 **/
1089 1090 1091 1092 1093
    g_signal_new ("ge_popup_blocked",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_FIRST,
            G_STRUCT_OFFSET (EphyWebViewClass, popup_blocked),
            NULL, NULL,
1094
            g_cclosure_marshal_generic,
1095 1096 1097 1098 1099
            G_TYPE_NONE,
            3,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
1100

1101 1102
/**
 * EphyWebView::ge-search-link:
1103
 * @view: the #EphyWebView that received the signal
1104 1105 1106 1107
 * @type: the mime-type of the search description
 * @title: the title of the news feed
 * @address: the URL to @embed's web site's search description
 *
1108 1109
 * The ::ge-search-link signal is emitted when @embed discovers that a
 * search description is available for the site it is visiting.
1110
 **/
1111 1112 1113 1114 1115
    g_signal_new ("ge_search_link",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_FIRST,
            G_STRUCT_OFFSET (EphyWebViewClass, search_link),
            NULL, NULL,
1116
            g_cclosure_marshal_generic,
1117 1118 1119 1120 1121
            G_TYPE_NONE,
            3,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
1122 1123 1124

/**
 * EphyWebView::ge-feed-link:
1125
 * @view: the #EphyWebView that received the signal
1126 1127 1128 1129
 * @type: the mime-type of the news feed
 * @title: the title of the news feed
 * @address: the URL to @embed's web site's news feed
 *
1130 1131
 * The ::ge-feed-link signal is emitted when @embed discovers that a
 * news feed is available for the site it is visiting.
1132
 **/
1133 1134 1135 1136 1137
    g_signal_new ("ge_feed_link",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_FIRST,
            G_STRUCT_OFFSET (EphyWebViewClass, feed_link),
            NULL, NULL,
1138
            g_cclosure_marshal_generic,
1139 1140 1141 1142 1143
            G_TYPE_NONE,
            3,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
1144 1145
/**
 * EphyWebView::ge-modal-alert:
1146
 * @view: the #EphyWebView that received the signal
1147 1148 1149 1150 1151 1152
 *
 * The ::ge-modal-alert signal is emitted when a DOM event will open a
 * modal alert.
 *
 * Return %TRUE to prevent the dialog from being opened.
 **/
1153 1154 1155 1156 1157
    g_signal_new ("ge_modal_alert",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (EphyWebViewClass, modal_alert),
            g_signal_accumulator_true_handled, NULL,
1158
            g_cclosure_marshal_generic,
1159 1160
            G_TYPE_BOOLEAN,
            0);
1161 1162
/**
 * EphyWebView::ge-modal-alert-closed:
1163
 * @view: the #EphyWebView that received the signal
1164 1165 1166 1167
 *
 * The ::ge-modal-alert-closed signal is emitted when a modal alert put up by a
 * DOM event was closed.
 **/
1168 1169 1170 1171 1172 1173 1174 1175
    g_signal_new ("ge_modal_alert_closed",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (EphyWebViewClass, modal_alert_closed),
            NULL, NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE,
            0);
1176 1177

/**
1178
 * EphyWebView::search-key-press:
1179
 * @view: the #EphyWebView that received the signal
1180 1181
 * @event: the #GdkEventKey which triggered this signal
 *
1182
 * The ::search-key-press signal is emitted for keypresses which
1183 1184
 * should be used for find implementations.
 **/
1185
    g_signal_new ("search-key-press",
1186 1187 1188 1189
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (EphyWebViewClass, search_key_press),
            g_signal_accumulator_true_handled, NULL,
1190
            g_cclosure_marshal_generic,
1191 1192 1193
            G_TYPE_BOOLEAN,
            1,
            GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1194 1195 1196

/**
 * EphyWebView::content-blocked:
1197
 * @view: the #EphyWebView that received the signal
Xan Lopez's avatar
Xan Lopez committed
1198
 * @uri: blocked URI
1199 1200 1201
 *
 * The ::content-blocked signal is emitted when an url has been blocked.
 **/
1202 1203 1204 1205 1206 1207 1208 1209 1210
    g_signal_new ("content-blocked",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (EphyWebViewClass, content_blocked),
            NULL, NULL,
            g_cclosure_marshal_VOID__STRING,
            G_TYPE_NONE,
            1,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
1211 1212
/**
 * EphyWebView::new-document-now:
1213
 * @view: the #EphyWebView that received the signal
1214 1215 1216 1217 1218 1219 1220
 * @uri: URI of the new content
 *
 * The ::new-document-now signal is emitted when a new page content
 * is being loaded into the browser. It's a good place to do view
 * related changes, for example to restore the zoom level of a page
 * or to set an user style sheet.
 **/
1221 1222 1223 1224 1225 1226 1227 1228 1229
    g_signal_new ("new-document-now",
            EPHY_TYPE_WEB_VIEW,
            G_SIGNAL_RUN_FIRST,
            G_STRUCT_OFFSET (EphyWebViewClass, new_document_now),
            NULL, NULL,
            g_cclosure_marshal_VOID__STRING,
            G_TYPE_NONE,
            1,
            G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
1230

1231
/**
1232
 * EphyWebView::loading-homepage:
1233 1234 1235 1236 1237 1238
 * @view: the #EphyWebView that received the signal
 *
 * The ::loading-homepage signal is emitted when the @view is about to
 * load the homepage set by the user.
 **/
    g_signal_new ("loading-homepage",
1239 1240 1241 1242 1243 1244 1245
                  EPHY_TYPE_WEB_VIEW,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EphyWebViewClass, loading_homepage),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);
1246

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
/**
 * EphyWebView::donload-only-load:
 * @view: the #EphyWebView that received the signal
 *
 * The ::download-only-load signal is emitted when the @view has its main load
 * replaced by a download, and that is the only reason why the @view has been created.
 **/
    g_signal_new ("download-only-load",
                  EPHY_TYPE_WEB_VIEW,
                  G_SIGNAL_RUN_FIRST,
                  0,
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE,
                  0);

1263 1264 1265 1266
  g_type_class_add_private (gobject_class, sizeof (EphyWebViewPrivate));
}

static void
1267 1268 1269
new_window_cb (EphyWebView *view,
               EphyWebView *new_view,
               gpointer user_data)
1270 1271 1272 1273 1274 1275
{
  EphyEmbedContainer *container;

  g_return_if_fail (new_view != NULL);

  container = EPHY_EMBED_CONTAINER (gtk_widget_get_toplevel (GTK_WIDGET (new_view)));
1276
  g_return_if_fail (container != NULL || !gtk_widget_is_toplevel (GTK_WIDGET (container)));
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

  popups_manager_add_window (view, container);
}

static void
ge_popup_blocked_cb (EphyWebView *view,
                     const char *url,
                     const char *name,
                     const char *features,
                     gpointer user_data)
{
  popups_manager_add (view, url, name, features);
1289 1290
}

1291 1292 1293 1294 1295 1296 1297 1298
static gboolean
decide_policy_cb (WebKitWebView *web_view,
                  WebKitPolicyDecision *decision,
                  WebKitPolicyDecisionType decision_type,
                  gpointer user_data)
{
  WebKitResponsePolicyDecision *response_decision;
  WebKitURIResponse *response;
1299
  WebKitURIRequest *request;
1300
  WebKitWebResource *main_resource;
1301
  EphyWebViewDocumentType type;
1302
  const char *mime_type;
1303
  const char *request_uri;
1304 1305 1306 1307 1308 1309 1310 1311

  if (decision_type != WEBKIT_POLICY_DECISION_TYPE_RESPONSE)
    return FALSE;

  response_decision = WEBKIT_RESPONSE_POLICY_DECISION (decision);
  response = webkit_response_policy_decision_get_response (response_decision);
  mime_type = webkit_uri_response_get_mime_type (response);

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  /* If WebKit can't handle the mime type start the download
     process */
  if (webkit_response_policy_decision_is_mime_type_supported (response_decision))
    return FALSE;

  /* If it's not the main resource we don't need to set the document type. */
  request = webkit_response_policy_decision_get_request (response_decision);
  request_uri = webkit_uri_request_get_uri (request);
  main_resource = webkit_web_view_get_main_resource (web_view);
  if (g_strcmp0 (webkit_web_resource_get_uri (main_resource), request_uri) != 0)
    return FALSE;

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
  type = EPHY_WEB_VIEW_DOCUMENT_OTHER;
  if (!strcmp (mime_type, "text/html") || !strcmp (mime_type, "text/plain"))
    type = EPHY_WEB_VIEW_DOCUMENT_HTML;
  else if (!strcmp (mime_type, "application/xhtml+xml"))
    type = EPHY_WEB_VIEW_DOCUMENT_XML;
  else if (!strncmp (mime_type, "image/", 6))
    type = EPHY_WEB_VIEW_DOCUMENT_IMAGE;

  /* FIXME: maybe it makes more sense to have an API to query the mime
   * type when the load of a page starts than doing this here.
   */
  if (EPHY_WEB_VIEW (web_view)->priv->document_type != type) {
    EPHY_WEB_VIEW (web_view)->priv->document_type = type;

    g_object_notify (G_OBJECT (web_view), "document-type");
  }

1341
  webkit_policy_decision_download (decision);
1342

1343
  return TRUE;
1344
}
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
static void
decide_on_geolocation_policy_request (GtkWidget *info_bar,
                                      int response,
                                      WebKitPermissionRequest *request)
{
  gtk_widget_destroy (info_bar);

  switch (response) {
  case GTK_RESPONSE_YES:
    webkit_permission_request_allow (request);
    break;
  default:
    webkit_permission_request_deny (request);
    break;
  }

  gtk_widget_destroy (info_bar);
  g_object_unref (request);
}
1365

1366 1367 1368
static gboolean
permission_request_cb (WebKitWebView           *web_view,
                       WebKitPermissionRequest *decision)
1369 1370 1371 1372 1373 1374
{
  GtkWidget *info_bar;
  GtkWidget *action_area;
  GtkWidget *content_area;
  GtkWidget *label;
  char *message;
1375
  char *host;
1376

1377 1378 1379
  if (!WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST (decision))
    return FALSE;

1380 1381 1382
  info_bar = gtk_info_bar_new_with_buttons (_("Deny"), GTK_RESPONSE_NO,
                                            _("Allow"), GTK_RESPONSE_YES,
                                            NULL);
1383 1384 1385

  action_area = gtk_info_bar_get_action_area (GTK_INFO_BAR (info_bar));
  /* Translators: Geolocation policy for a specific site. */
1386 1387
  gtk_orientable_set_orientation (GTK_ORIENTABLE (action_area),
                                  GTK_ORIENTATION_HORIZONTAL);
1388 1389

  /* Label */
1390
  host = ephy_string_get_host_name (webkit_web_view_get_uri (web_view));
1391
  message = g_markup_printf_escaped (_("The page at <b>%s</b> wants to know your location."),
1392 1393
                                     host);
  g_free (host);
1394

1395 1396 1397
  label = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (label), message);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1398 1399 1400 1401 1402 1403 1404 1405

  g_free (message);

  content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
  gtk_container_add (GTK_CONTAINER (content_area), label);

  gtk_widget_show_all (info_bar);

1406
  /* Ref the decision, to keep it alive while we decide */
1407 1408
  g_signal_connect (info_bar, "response",
                    G_CALLBACK (decide_on_geolocation_policy_request),
1409
                    g_object_ref (decision));
1410 1411 1412 1413 1414 1415 1416

  ephy_embed_add_top_widget (EPHY_GET_EMBED_FROM_EPHY_WEB_VIEW (web_view),
                             info_bar, TRUE);

  return TRUE;
}

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
static void
get_host_for_url_cb (gpointer service,
                     gboolean success,
                     gpointer result_data,
                     gpointer user_data)
{
  EphyHistoryHost *host;
  EphyWebView *view;
  double current_zoom;

  if (success == FALSE)
    return;

  view = EPHY_WEB_VIEW (user_data);
  host = (EphyHistoryHost *)result_data;

1433
  current_zoom = webkit_web_view_get_zoom_level (WEBKIT_WEB_VIEW (view));
1434 1435 1436

  if (host->zoom_level != current_zoom) {
    view->priv->is_setting_zoom = TRUE;
1437
    webkit_web_view_set_zoom_level (WEBKIT_WEB_VIEW (view), host->zoom_level);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
    view->priv->is_setting_zoom = FALSE;
  }

  ephy_history_host_free (host);
}

static void
restore_zoom_level (EphyWebView *view,
                    const char *address)
{
  if (ephy_embed_utils_address_has_web_scheme (address))
    ephy_history_service_get_host_for_url (view->priv->history_service,
                                           address, view->priv->history_service_cancellable,
                                           (EphyHistoryJobCallback)get_host_for_url_cb, view);
}

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
/**
 * ephy_web_view_set_loading_message:
 * @view: an #EphyWebView
 * @address: the loading address
 *
 * Update @view's loading message
 **/
static void
ephy_web_view_set_loading_message (EphyWebView *view,
                                   const char *address)
{
  EphyWebViewPrivate *priv = view->priv;

  g_clear_pointer (&priv->loading_message, g_free);
  if (address) {
    char *title;

1471
    title = ephy_embed_utils_get_title_from_address (address);
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481

    if (title != NULL && title[0] != '\0') {
      /* translators: %s here is the address of the web page */
      priv->loading_message = g_strdup_printf (_ ("Loading “%s”…"), title);
    } else {
      priv->loading_message = g_strdup (_("Loading…"));
    }

    g_free (title);
  }
1482 1483

  g_object_notify (G_OBJECT (view), "status-message");
1484 1485
}

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static void
ephy_web_view_location_changed (EphyWebView *view,
                                const char *location)
{
  GObject *object = G_OBJECT (view);
  EphyWebViewPrivate *priv = view->priv;

  g_object_freeze_notify (object);

  /* Do this up here so we still have the old address around. */
  ephy_file_monitor_update_location (priv->file_monitor, location);

1498
  if (location == NULL || location[0] == '\0') {
1499
    ephy_web_view_set_address (view, NULL);
1500 1501 1502
  } else if (g_str_has_prefix (location, EPHY_ABOUT_SCHEME":applications")) {
    SoupURI *uri = soup_uri_new (location);
    char *new_address;
1503

1504 1505 1506 1507
    /* Strip the query from the URL for about:applications. */
    soup_uri_set_query (uri, NULL);
    new_address = soup_uri_to_string (uri, FALSE);
    soup_uri_free (uri);
1508

1509 1510 1511 1512 1513
    ephy_web_view_set_address (view, new_address);
    g_free (new_address);
  } else {
    /* We do this to get rid of an eventual password in the URL. */
    ephy_web_view_set_address (view, location);
1514
    ephy_web_view_set_loading_message (view, location);
1515 1516 1517
  }

  ephy_web_view_set_link_message (view, NULL);
1518 1519

  _ephy_web_view_update_icon (view);
1520 1521 1522 1523 1524 1525

  update_navigation_flags (view);

  g_object_thaw_notify (object);
}

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
static void
load_changed_cb (WebKitWebView *web_view,
                 WebKitLoadEvent load_event,
                 gpointer user_data)
{
  EphyWebView *view = EPHY_WEB_VIEW (web_view);
  EphyWebViewPrivate *priv = view->priv;
  GObject *object = G_OBJECT (web_view);

  g_object_freeze_notify (object);

  switch (load_event) {
  case WEBKIT_LOAD_STARTED: {
1539
    const char *loading_uri = NULL;
1540 1541 1542

    priv->load_failed = FALSE;

1543 1544 1545 1546 1547
    if (priv->snapshot_timeout_id) {
      g_source_remove (priv->snapshot_timeout_id);
      priv->snapshot_timeout_id = 0;
    }

1548 1549 1550
    loading_uri = webkit_web_view_get_uri (web_view);
    g_signal_emit_by_name (view, "new-document-now", loading_uri);

1551 1552 1553
    if (ephy_embed_utils_is_no_show_address (loading_uri))
      ephy_web_view_freeze_history (view);

1554
    if (priv->address == NULL || priv->address[0] == '\0')
1555 1556
      ephy_web_view_set_address (view, loading_uri);

1557
    ephy_web_view_set_loading_message (view, loading_uri);
1558

1559 1560 1561
    /* Zoom level. */
    restore_zoom_level (view, loading_uri);

1562 1563 1564 1565 1566 1567
    break;
  }
  case WEBKIT_LOAD_REDIRECTED:
    /* TODO: Update the loading uri */
    break;
  case WEBKIT_LOAD_COMMITTED: {
1568
    const char* uri;
1569 1570
    EphyWebViewSecurityLevel security_level = EPHY_WEB_VIEW_STATE_IS_UNKNOWN;

1571 1572
    priv->ever_committed = TRUE;

1573 1574 1575 1576 1577
    /* Title and location. */
    uri = webkit_web_view_get_uri (web_view);
    ephy_web_view_location_changed (view, uri);

    /* Security status. */
1578
    g_clear_object (&priv->certificate);
1579
    if (webkit_web_view_get_tls_info (web_view, &priv->certificate, &priv->tls_errors)) {
1580
      g_object_ref (priv->certificate);
1581 1582
      security_level = priv->tls_errors == 0 ?
        EPHY_WEB_VIEW_STATE_IS_SECURE_HIGH : EPHY_WEB_VIEW_STATE_IS_BROKEN;
1583 1584 1585 1586 1587
    }

    ephy_web_view_set_security_level (EPHY_WEB_VIEW (web_view), security_level);

    /* History. */
1588
    if (!ephy_web_view_is_history_frozen (view)) {
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
      char *history_uri = NULL;

      /* TODO: move the normalization down to the history service? */
      if (g_str_has_prefix (uri, EPHY_ABOUT_SCHEME))
          history_uri = g_strdup_printf ("about:%s", uri + EPHY_ABOUT_SCHEME_LEN + 1);
      else
        history_uri = g_strdup (uri);

      ephy_history_service_visit_url (priv->history_service,
                                      history_uri,
                                      priv->visit_type);

      g_free (history_uri);
    }
1603

1604 1605
    break;
  }
1606
  case WEBKIT_LOAD_FINISHED:
1607
    ephy_web_view_set_loading_message (view, NULL);
1608

1609 1610 1611
    /* Ensure we load the icon for this web view, if available. */
    _ephy_web_view_update_icon (view);

1612 1613 1614
    /* Reset visit type. */
    priv->visit_type = EPHY_PAGE_VISIT_NONE;

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
    if (!ephy_web_view_is_history_frozen (view) &&
        ephy_embed_shell_get_mode (ephy_embed_shell_get_default ()) != EPHY_EMBED_SHELL_MODE_INCOGNITO) {
      if (!ephy_snapshot_service_lookup_snapshot_path (ephy_snapshot_service_get_default (), webkit_web_view_get_uri (web_view))) {
        /* If the snapshot check hasn't been scheduled already by the favicon callback,
         * check the snapshot if we don't get a favicon in 1 second
         */
        if (priv->snapshot_timeout_id == 0) {
          priv->snapshot_timeout_id = g_timeout_add_seconds_full (G_PRIORITY_LOW, 1,
                                                                  (GSourceFunc)web_view_check_snapshot,
                                                                  web_view, NULL);
        }
      }
1627 1628
    }

1629 1630
    ephy_web_view_thaw_history (view);

1631 1632 1633 1634 1635
    break;
  }

  g_object_thaw_notify (object);
}
1636

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
/**
 * ephy_web_view_set_placeholder:
 * @view: an #EphyWebView
 * @uri: uri that will eventually be loaded
 * @title: last-known title of the page that will eventually be loaded
 *
 * Makes the #EphyWebView pretend a page that will eventually be loaded is
 * already there.
 *
 **/
void
ephy_web_view_set_placeholder (EphyWebView *view,
                               const char *uri,
                               const char *title)
{
  char *html;

  g_return_if_fail (EPHY_IS_WEB_VIEW (view));

  /* We want only the actual load to be the one recorded in history, but
   * doing a load here is the simplest way to replace the loading
   * spinner with the favicon. */
  ephy_web_view_freeze_history (view);

  html = g_markup_printf_escaped ("<head><title>%s</title></head>", title);

  webkit_web_view_load_alternate_html (WEBKIT_WEB_VIEW (view), html, uri, NULL);

  g_free (html);

  ephy_web_view_set_address (view, uri);
}

1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686

static char *
get_style_sheet (void)
{
  const gchar *file;
  GError *error = NULL;
  char *sheet;

  file = ephy_file ("error.css");
  if (file && !g_file_get_contents (file, &sheet, NULL, &error)) {
    g_debug ("Unable to load error style sheet: %s", error->message);
    g_error_free (error);
  }

  return sheet;
}

1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
/**
 * ephy_web_view_load_error_page:
 * @view: an #EphyWebView
 * @uri: uri that caused the failure
 * @page: one of #EphyWebViewErrorPage
 * @error: a GError to inspect, or %NULL
 *
 * Loads an error page appropiate for @page in @view.
 *
 **/
void
ephy_web_view_load_error_page (EphyWebView *view,
                               const char *uri,
                               EphyWebViewErrorPage page,
                               GError *error)
{
  GString *html = g_string_new ("");
  const char *reason;
  char *hostname;
  char *lang;
  char *page_title;
  char *msg_title;
  char *msg;
  char *button_label;
1711
  char *stylesheet;
1712
  const char *custom_class;
1713
  GBytes *html_file;
1714 1715 1716 1717 1718 1719 1720

  if (error)
    reason = error->message;
  else
    reason = _("None specified");

  hostname = ephy_string_get_host_name (uri);
1721 1722
  if (hostname == NULL)
    hostname = g_strdup (uri);
1723 1724 1725 1726

  lang = g_strdup (pango_language_to_string (gtk_get_default_language ()));
  g_strdelimit (lang, "_-@", '\0');

1727 1728
  html_file = g_resources_lookup_data (EPHY_PAGE_TEMPLATE_ERROR, 0, NULL);

1729 1730
  switch (page) {
    case EPHY_WEB_VIEW_ERROR_PAGE_NETWORK_ERROR:
1731
      page_title = g_strdup_printf (_("Problem loading “%s”"), hostname);
1732

1733 1734
      msg_title = g_strdup (_("Oops! Unable to display this website."));
      msg = g_strdup_printf (_("<p>The site at “%s” seems "
1735
                               "to be unavailable. The precise error was:</p>"
1736 1737 1738 1739
                               "<p><code>%s</code></p>"
                               "<p>It may be "
                               "temporarily unavailable or moved to a new "
                               "address. You may wish to verify that your "
1740
                               "internet connection is working correctly.</p>"),
1741
                             uri, reason);
1742 1743
      button_label = g_strdup (_("Try again"));
      custom_class = "network-error";
Diego Escalante Urrelo's avatar