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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 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 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* Copyright © 2000-2003 Marco Pesenti Gritti
* Copyright © 2011 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/>.
*/
#include <config.h>
#include "ephy-embed-shell.h"
#include "ephy-about-handler.h"
#include "ephy-dbus-util.h"
#include "ephy-debug.h"
#include "ephy-downloads-manager.h"
#include "ephy-embed-container.h"
#include "ephy-embed-prefs.h"
#include "ephy-embed-type-builtins.h"
#include "ephy-embed-utils.h"
#include "ephy-encodings.h"
#include "ephy-file-helpers.h"
#include "ephy-filters-manager.h"
#include "ephy-flatpak-utils.h"
#include "ephy-history-service.h"
#include "ephy-password-manager.h"
#include "ephy-profile-utils.h"
#include "ephy-settings.h"
#include "ephy-snapshot-service.h"
#include "ephy-tabs-catalog.h"
#include "ephy-uri-helpers.h"
#include "ephy-uri-tester-shared.h"
#include "ephy-view-source-handler.h"
#include "ephy-web-app-utils.h"
#include "ephy-web-extension-proxy.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#define PAGE_SETUP_FILENAME "page-setup-gtk.ini"
#define PRINT_SETTINGS_FILENAME "print-settings.ini"
typedef struct {
WebKitWebContext *web_context;
EphyHistoryService *global_history_service;
EphyGSBService *global_gsb_service;
EphyEncodings *encodings;
GtkPageSetup *page_setup;
GtkPrintSettings *print_settings;
EphyEmbedShellMode mode;
WebKitUserContentManager *user_content;
EphyDownloadsManager *downloads_manager;
EphyPermissionsManager *permissions_manager;
EphyPasswordManager *password_manager;
EphyAboutHandler *about_handler;
EphyViewSourceHandler *source_handler;
char *guid;
GDBusServer *dbus_server;
GList *web_extensions;
EphyFiltersManager *filters_manager;
EphySearchEngineManager *search_engine_manager;
GCancellable *cancellable;
} EphyEmbedShellPrivate;
enum {
RESTORED_WINDOW,
WEB_VIEW_CREATED,
PAGE_CREATED,
ALLOW_TLS_CERTIFICATE,
ALLOW_UNSAFE_BROWSING,
SENSITIVE_FORM_FOCUSED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
enum {
PROP_0,
PROP_MODE,
N_PROPERTIES
};
static GParamSpec *object_properties[N_PROPERTIES] = { NULL, };
static EphyEmbedShell *embed_shell = NULL;
static void ephy_embed_shell_tabs_catalog_iface_init (EphyTabsCatalogInterface *iface);
G_DEFINE_TYPE_WITH_CODE (EphyEmbedShell, ephy_embed_shell, DZL_TYPE_APPLICATION,
G_ADD_PRIVATE (EphyEmbedShell)
G_IMPLEMENT_INTERFACE (EPHY_TYPE_TABS_CATALOG,
ephy_embed_shell_tabs_catalog_iface_init))
static EphyWebView *
ephy_embed_shell_get_view_for_page_id (EphyEmbedShell *self,
guint64 page_id,
const char *origin)
{
GList *windows = gtk_application_get_windows (GTK_APPLICATION (self));
for (GList *l = windows; l && l->data; l = l->next) {
g_autoptr(GList) tabs = ephy_embed_container_get_children (l->data);
for (GList *t = tabs; t && t->data; t = t->next) {
EphyWebView *ephy_view = ephy_embed_get_web_view (t->data);
WebKitWebView *web_view = WEBKIT_WEB_VIEW (ephy_view);
if (webkit_web_view_get_page_id (web_view) != page_id)
continue;
g_autofree char *real_origin = ephy_uri_to_security_origin (webkit_web_view_get_uri (web_view));
if (g_strcmp0 (real_origin, origin)) {
g_debug ("Extension's origin '%s' doesn't match real origin '%s'", origin, real_origin);
return NULL;
}
return ephy_view;
}
}
return NULL;
}
static EphyWebExtensionProxy *
ephy_embed_shell_get_extension_proxy_for_page_id (EphyEmbedShell *self,
guint64 page_id,
const char *origin)
{
EphyWebView *view = ephy_embed_shell_get_view_for_page_id (self, page_id, origin);
return view ? ephy_web_view_get_web_extension_proxy (view) : NULL;
}
static GList *
tabs_catalog_get_tabs_info (EphyTabsCatalog *catalog)
{
EphyEmbedShell *embed_shell = EPHY_EMBED_SHELL (catalog);
WebKitFaviconDatabase *database;
GList *windows;
GList *tabs;
GList *tabs_info = NULL;
const char *title;
const char *url;
char *favicon;
windows = gtk_application_get_windows (GTK_APPLICATION (embed_shell));
database = webkit_web_context_get_favicon_database (ephy_embed_shell_get_web_context (embed_shell));
for (GList *l = windows; l && l->data; l = l->next) {
tabs = ephy_embed_container_get_children (l->data);
for (GList *t = tabs; t && t->data; t = t->next) {
title = ephy_embed_get_title (t->data);
if (!g_strcmp0 (title, _(BLANK_PAGE_TITLE)) || !g_strcmp0 (title, _(OVERVIEW_PAGE_TITLE)))
continue;
url = ephy_web_view_get_display_address (ephy_embed_get_web_view (t->data));
favicon = webkit_favicon_database_get_favicon_uri (database, url);
tabs_info = g_list_prepend (tabs_info,
ephy_tab_info_new (title, url, favicon));
g_free (favicon);
}
g_list_free (tabs);
}
return tabs_info;
}
static void
ephy_embed_shell_tabs_catalog_iface_init (EphyTabsCatalogInterface *iface)
{
iface->get_tabs_info = tabs_catalog_get_tabs_info;
}
static void
ephy_embed_shell_dispose (GObject *object)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (EPHY_EMBED_SHELL (object));
if (priv->cancellable) {
g_cancellable_cancel (priv->cancellable);
g_clear_object (&priv->cancellable);
}
g_clear_object (&priv->encodings);
g_clear_object (&priv->page_setup);
g_clear_object (&priv->print_settings);
g_clear_object (&priv->global_history_service);
g_clear_object (&priv->global_gsb_service);
g_clear_object (&priv->about_handler);
g_clear_object (&priv->source_handler);
g_clear_object (&priv->user_content);
g_clear_object (&priv->downloads_manager);
g_clear_object (&priv->password_manager);
g_clear_object (&priv->permissions_manager);
g_clear_object (&priv->web_context);
g_clear_pointer (&priv->guid, g_free);
g_clear_object (&priv->dbus_server);
g_clear_object (&priv->filters_manager);
g_clear_object (&priv->search_engine_manager);
G_OBJECT_CLASS (ephy_embed_shell_parent_class)->dispose (object);
}
static void
web_extension_sensitive_form_focused_message_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
guint64 page_id;
gboolean insecure_action;
GVariant *variant;
char *message_str;
message_str = jsc_value_to_string (webkit_javascript_result_get_js_value (message));
variant = g_variant_parse (G_VARIANT_TYPE ("(tb)"), message_str, NULL, NULL, NULL);
g_free (message_str);
g_variant_get (variant, "(tb)", &page_id, &insecure_action);
g_signal_emit (shell, signals[SENSITIVE_FORM_FOCUSED], 0,
page_id, insecure_action);
g_variant_unref (variant);
}
static void
history_service_query_urls_cb (EphyHistoryService *service,
gboolean success,
GList *urls,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
if (!success)
return;
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
ephy_web_extension_proxy_history_set_urls (web_extension, urls);
}
for (l = urls; l; l = g_list_next (l))
ephy_embed_shell_schedule_thumbnail_update (shell, (EphyHistoryURL *)l->data);
}
static void
ephy_embed_shell_update_overview_urls (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
EphyHistoryQuery *query;
query = ephy_history_query_new_for_overview ();
ephy_history_service_query_urls (priv->global_history_service, query, NULL,
(EphyHistoryJobCallback)history_service_query_urls_cb,
shell);
ephy_history_query_free (query);
}
static void
history_service_urls_visited_cb (EphyHistoryService *history,
EphyEmbedShell *shell)
{
ephy_embed_shell_update_overview_urls (shell);
}
static void
history_set_url_hidden_cb (EphyHistoryService *service,
gboolean success,
gpointer result_data,
EphyEmbedShell *shell)
{
if (!success)
return;
ephy_embed_shell_update_overview_urls (shell);
}
static void
web_extension_overview_message_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
char *url_to_remove;
url_to_remove = jsc_value_to_string (webkit_javascript_result_get_js_value (message));
ephy_history_service_set_url_hidden (priv->global_history_service,
url_to_remove, TRUE, NULL,
(EphyHistoryJobCallback)history_set_url_hidden_cb,
shell);
g_free (url_to_remove);
}
static void
web_extension_tls_error_page_message_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
guint64 page_id;
page_id = jsc_value_to_double (webkit_javascript_result_get_js_value (message));
g_signal_emit (shell, signals[ALLOW_TLS_CERTIFICATE], 0, page_id);
}
static void
web_extension_unsafe_browsing_error_page_message_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
guint64 page_id;
page_id = jsc_value_to_double (webkit_javascript_result_get_js_value (message));
g_signal_emit (shell, signals[ALLOW_UNSAFE_BROWSING], 0, page_id);
}
static void
web_extension_about_apps_message_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
char *app_id;
app_id = jsc_value_to_string (webkit_javascript_result_get_js_value (message));
ephy_web_application_delete (app_id);
g_free (app_id);
}
typedef struct {
EphyEmbedShell *shell;
char *origin;
gint32 promise_id;
guint64 page_id;
} PasswordManagerData;
static void
password_manager_query_finished_cb (GList *records,
PasswordManagerData *data)
{
EphyPasswordRecord *record;
const char *username = NULL;
const char *password = NULL;
record = records && records->data ? EPHY_PASSWORD_RECORD (records->data) : NULL;
if (record) {
username = ephy_password_record_get_username (record);
password = ephy_password_record_get_password (record);
}
EphyWebExtensionProxy *proxy = ephy_embed_shell_get_extension_proxy_for_page_id (data->shell,
data->page_id,
data->origin);
if (proxy)
ephy_web_extension_proxy_password_query_response (proxy, username, password, data->promise_id, data->page_id);
g_object_unref (data->shell);
g_free (data->origin);
g_free (data);
}
static char *
property_to_string_or_null (JSCValue *value,
const char *name)
{
g_autoptr(JSCValue) prop = jsc_value_object_get_property (value, name);
if (jsc_value_is_null (prop) || jsc_value_is_undefined (prop))
return NULL;
return jsc_value_to_string (prop);
}
static int
property_to_int32 (JSCValue *value,
const char *name)
{
g_autoptr(JSCValue) prop = jsc_value_object_get_property (value, name);
return jsc_value_to_int32 (prop);
}
static int
property_to_uint64 (JSCValue *value,
const char *name)
{
g_autoptr(JSCValue) prop = jsc_value_object_get_property (value, name);
return (guint64)jsc_value_to_double (prop);
}
static void
web_extension_password_manager_query_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
JSCValue *value = webkit_javascript_result_get_js_value (message);
g_autofree char *origin = property_to_string_or_null (value, "origin");
g_autofree char *target_origin = property_to_string_or_null (value, "targetOrigin");
g_autofree char *username = property_to_string_or_null (value, "username");
g_autofree char *username_field = property_to_string_or_null (value, "usernameField");
g_autofree char *password_field = property_to_string_or_null (value, "passwordField");
gint32 promise_id = property_to_int32 (value, "promiseID");
guint64 page_id = property_to_uint64 (value, "pageID");
PasswordManagerData *data = g_new (PasswordManagerData, 1);
data->shell = g_object_ref (shell);
data->promise_id = promise_id;
data->page_id = page_id;
data->origin = g_steal_pointer (&origin);
ephy_password_manager_query (priv->password_manager,
NULL,
origin,
target_origin,
username,
username_field,
password_field,
(EphyPasswordManagerQueryCallback)password_manager_query_finished_cb,
data);
}
typedef struct {
EphyPasswordManager *password_manager;
EphyPermissionsManager *permissions_manager;
char *origin;
char *target_origin;
char *username;
char *password;
char *username_field;
char *password_field;
gboolean is_new;
} SaveAuthRequest;
static void
save_auth_request_free (SaveAuthRequest *request)
{
g_object_unref (request->password_manager);
g_object_unref (request->permissions_manager);
g_free (request->origin);
g_free (request->target_origin);
g_free (request->username);
g_free (request->password);
g_free (request->username_field);
g_free (request->password_field);
g_free (request);
}
static void
save_auth_request_response_cb (gint response_id,
SaveAuthRequest *data)
{
if (response_id == GTK_RESPONSE_REJECT) {
ephy_permissions_manager_set_permission (data->permissions_manager,
EPHY_PERMISSION_TYPE_SAVE_PASSWORD,
data->origin,
EPHY_PERMISSION_DENY);
} else if (response_id == GTK_RESPONSE_YES) {
ephy_password_manager_save (data->password_manager, data->origin, data->target_origin,
data->username, data->password, data->username_field,
data->password_field, data->is_new);
}
}
static void
web_extension_password_manager_save_real (EphyEmbedShell *shell,
JSCValue *value,
gboolean is_request)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_autofree char *origin = property_to_string_or_null (value, "origin");
g_autofree char *target_origin = property_to_string_or_null (value, "targetOrigin");
g_autofree char *username = property_to_string_or_null (value, "username");
g_autofree char *password = property_to_string_or_null (value, "password");
g_autofree char *username_field = property_to_string_or_null (value, "usernameField");
g_autofree char *password_field = property_to_string_or_null (value, "passwordField");
g_autoptr(JSCValue) is_new_prop = jsc_value_object_get_property (value, "isNew");
gboolean is_new = jsc_value_to_boolean (is_new_prop);
guint64 page_id = property_to_uint64 (value, "pageID");
EphyWebView *view;
/* Both password and password field are required. */
if (password == NULL || password_field == NULL)
return;
/* The username field is required if username is present. */
if (username && !username_field)
g_clear_pointer (&username, g_free);
/* The username is required if username field is present. */
if (!username && username_field)
g_clear_pointer (&username_field, g_free);
/* This also sanity checks that a page isn't saving websites for
* other origins. Remember the request comes from the untrusted web
* process and we have to make sure it's not being evil here.
*/
view = ephy_embed_shell_get_view_for_page_id (shell, page_id, origin);
if (view == NULL)
return;
if (!is_request) {
ephy_password_manager_save (priv->password_manager, origin, target_origin, username,
password, username_field, password_field, is_new);
return;
}
SaveAuthRequest *request = g_new (SaveAuthRequest, 1);
request->password_manager = g_object_ref (priv->password_manager);
request->permissions_manager = g_object_ref (priv->permissions_manager);
request->origin = g_steal_pointer (&origin);
request->target_origin = g_steal_pointer (&target_origin);
request->username = g_steal_pointer (&username);
request->password = g_steal_pointer (&password);
request->username_field = g_steal_pointer (&username_field);
request->password_field = g_steal_pointer (&password_field);
request->is_new = is_new;
ephy_web_view_show_auth_form_save_request (view, request->origin, request->username,
(EphyPasswordSaveRequestCallback)save_auth_request_response_cb,
request, (GDestroyNotify)save_auth_request_free);
}
static void
web_extension_password_manager_save_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
JSCValue *value = webkit_javascript_result_get_js_value (message);
web_extension_password_manager_save_real (shell, value, FALSE);
}
static void
web_extension_password_manager_request_save_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
JSCValue *value = webkit_javascript_result_get_js_value (message);
web_extension_password_manager_save_real (shell, value, TRUE);
}
static void
web_extension_password_manager_cached_users_received_cb (WebKitUserContentManager *manager,
WebKitJavascriptResult *message,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
JSCValue *value = webkit_javascript_result_get_js_value (message);
g_autofree char *origin = property_to_string_or_null (value, "origin");
gint32 promise_id = property_to_int32 (value, "promiseID");
guint64 page_id = property_to_uint64 (value, "pageID");
GList *cached_users;
cached_users = ephy_password_manager_get_cached_users (priv->password_manager, origin);
EphyWebExtensionProxy *proxy = ephy_embed_shell_get_extension_proxy_for_page_id (
shell, page_id, origin);
if (proxy)
ephy_web_extension_proxy_password_cached_users_response (proxy, cached_users, promise_id, page_id);
}
static void
history_service_url_title_changed_cb (EphyHistoryService *service,
const char *url,
const char *title,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
ephy_web_extension_proxy_history_set_url_title (web_extension, url, title);
}
}
static void
history_service_url_deleted_cb (EphyHistoryService *service,
EphyHistoryURL *url,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
ephy_web_extension_proxy_history_delete_url (web_extension, url->url);
}
}
static void
history_service_host_deleted_cb (EphyHistoryService *service,
const char *deleted_url,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
SoupURI *deleted_uri;
deleted_uri = soup_uri_new (deleted_url);
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
ephy_web_extension_proxy_history_delete_host (web_extension, soup_uri_get_host (deleted_uri));
}
soup_uri_free (deleted_uri);
}
static void
history_service_cleared_cb (EphyHistoryService *service,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
ephy_web_extension_proxy_history_clear (web_extension);
}
}
typedef struct {
EphyWebExtensionProxy *extension;
char *url;
char *path;
} DelayedThumbnailUpdateData;
static DelayedThumbnailUpdateData *
delayed_thumbnail_update_data_new (EphyWebExtensionProxy *extension,
const char *url,
const char *path)
{
DelayedThumbnailUpdateData *data = g_new (DelayedThumbnailUpdateData, 1);
data->extension = extension;
data->url = g_strdup (url);
data->path = g_strdup (path);
g_object_add_weak_pointer (G_OBJECT (extension), (gpointer *)&data->extension);
return data;
}
static void
delayed_thumbnail_update_data_free (DelayedThumbnailUpdateData *data)
{
if (data->extension)
g_object_remove_weak_pointer (G_OBJECT (data->extension), (gpointer *)&data->extension);
g_free (data->url);
g_free (data->path);
g_free (data);
}
static gboolean
delayed_thumbnail_update_cb (DelayedThumbnailUpdateData *data)
{
if (!data->extension) {
delayed_thumbnail_update_data_free (data);
return G_SOURCE_REMOVE;
}
if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data->extension), "initialized"))) {
ephy_web_extension_proxy_history_set_url_thumbnail (data->extension, data->url, data->path);
delayed_thumbnail_update_data_free (data);
return G_SOURCE_REMOVE;
}
/* Web extension is not initialized yet, try again later.... */
return G_SOURCE_CONTINUE;
}
void
ephy_embed_shell_set_thumbnail_path (EphyEmbedShell *shell,
const char *url,
const char *path)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *l;
for (l = priv->web_extensions; l; l = g_list_next (l)) {
EphyWebExtensionProxy *web_extension = (EphyWebExtensionProxy *)l->data;
if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (web_extension), "initialized"))) {
ephy_web_extension_proxy_history_set_url_thumbnail (web_extension, url, path);
} else {
DelayedThumbnailUpdateData *data = delayed_thumbnail_update_data_new (web_extension, url, path);
g_timeout_add (50, (GSourceFunc)delayed_thumbnail_update_cb, data);
}
}
}
static void
got_snapshot_path_for_url_cb (EphySnapshotService *service,
GAsyncResult *result,
char *url)
{
char *snapshot;
GError *error = NULL;
snapshot = ephy_snapshot_service_get_snapshot_path_for_url_finish (service, result, &error);
if (snapshot) {
ephy_embed_shell_set_thumbnail_path (ephy_embed_shell_get_default (), url, snapshot);
g_free (snapshot);
} else {
/* Bad luck, not something to warn about. */
g_info ("Failed to get snapshot for URL %s: %s", url, error->message);
g_error_free (error);
}
g_free (url);
}
void
ephy_embed_shell_schedule_thumbnail_update (EphyEmbedShell *shell,
EphyHistoryURL *url)
{
EphySnapshotService *service;
const char *snapshot;
service = ephy_snapshot_service_get_default ();
snapshot = ephy_snapshot_service_lookup_cached_snapshot_path (service, url->url);
if (snapshot) {
ephy_embed_shell_set_thumbnail_path (shell, url->url, snapshot);
} else {
ephy_snapshot_service_get_snapshot_path_for_url_async (service,
url->url,
NULL,
(GAsyncReadyCallback)got_snapshot_path_for_url_cb,
g_strdup (url->url));
}
}
/**
* ephy_embed_shell_get_global_history_service:
* @shell: the #EphyEmbedShell
*
* Return value: (transfer none): the global #EphyHistoryService
**/
EphyHistoryService *
ephy_embed_shell_get_global_history_service (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (priv->global_history_service == NULL) {
char *filename;
EphySQLiteConnectionMode mode;
if (priv->mode == EPHY_EMBED_SHELL_MODE_INCOGNITO ||
priv->mode == EPHY_EMBED_SHELL_MODE_AUTOMATION ||
priv->mode == EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER)
mode = EPHY_SQLITE_CONNECTION_MODE_READ_ONLY;
else
mode = EPHY_SQLITE_CONNECTION_MODE_READWRITE;
filename = g_build_filename (ephy_profile_dir (), EPHY_HISTORY_FILE, NULL);
priv->global_history_service = ephy_history_service_new (filename, mode);
g_free (filename);
g_assert (priv->global_history_service);
g_signal_connect_object (priv->global_history_service, "urls-visited",
G_CALLBACK (history_service_urls_visited_cb),
shell, 0);
g_signal_connect_object (priv->global_history_service, "url-title-changed",
G_CALLBACK (history_service_url_title_changed_cb),
shell, 0);
g_signal_connect_object (priv->global_history_service, "url-deleted",
G_CALLBACK (history_service_url_deleted_cb),
shell, 0);
g_signal_connect_object (priv->global_history_service, "host-deleted",
G_CALLBACK (history_service_host_deleted_cb),
shell, 0);
g_signal_connect_object (priv->global_history_service, "cleared",
G_CALLBACK (history_service_cleared_cb),
shell, 0);
}
return priv->global_history_service;
}
/**
* ephy_embed_shell_get_global_gsb_service:
* @shell: the #EphyEmbedShell
*
* Return value: (transfer none): the global #EphyGSBService
**/
EphyGSBService *
ephy_embed_shell_get_global_gsb_service (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_return_val_if_fail (EPHY_IS_EMBED_SHELL (shell), NULL);
if (priv->global_gsb_service == NULL) {
char *api_key;
char *db_path;
api_key = g_settings_get_string (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_GSB_API_KEY);
db_path = g_build_filename (ephy_default_cache_dir (), EPHY_GSB_FILE, NULL);
priv->global_gsb_service = ephy_gsb_service_new (api_key, db_path);
g_free (api_key);
g_free (db_path);
}
return priv->global_gsb_service;
}
/**
* ephy_embed_shell_get_encodings:
* @shell: the #EphyEmbedShell
*
* Return value: (transfer none):
**/
EphyEncodings *
ephy_embed_shell_get_encodings (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (priv->encodings == NULL)
priv->encodings = ephy_encodings_new ();
return priv->encodings;
}
void
ephy_embed_shell_restored_window (EphyEmbedShell *shell)
{
g_signal_emit (shell, signals[RESTORED_WINDOW], 0);
}
static void
about_request_cb (WebKitURISchemeRequest *request,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
ephy_about_handler_handle_request (priv->about_handler, request);
}
static void
source_request_cb (WebKitURISchemeRequest *request,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
ephy_view_source_handler_handle_request (priv->source_handler, request);
}
static void
ephy_resource_request_cb (WebKitURISchemeRequest *request)
{
const char *path;
GInputStream *stream;
gsize size;
GError *error = NULL;
path = webkit_uri_scheme_request_get_path (request);
if (!g_resources_get_info (path, 0, &size, NULL, &error)) {
webkit_uri_scheme_request_finish_error (request, error);
g_error_free (error);
return;
}
stream = g_resources_open_stream (path, 0, &error);
if (stream) {
webkit_uri_scheme_request_finish (request, stream, size, NULL);
g_object_unref (stream);
} else {
webkit_uri_scheme_request_finish_error (request, error);
g_error_free (error);
}
}
static void
ftp_request_cb (WebKitURISchemeRequest *request)
{
GDesktopAppInfo *app_info;
const char *uri;
GList *list = NULL;
GError *error = NULL;
uri = webkit_uri_scheme_request_get_uri (request);
g_app_info_launch_default_for_uri (uri, NULL, &error);
if (!error) {
g_signal_emit_by_name (webkit_uri_scheme_request_get_web_view (request), "close", NULL);
return;
}
/* Default URI handler didn't work. Try nautilus before giving up. */
app_info = g_desktop_app_info_new ("org.gnome.Nautilus.desktop");
list = g_list_append (list, (gpointer)uri);
if (app_info && g_app_info_launch_uris (G_APP_INFO (app_info), list, NULL, NULL))
g_signal_emit_by_name (webkit_uri_scheme_request_get_web_view (request), "close", NULL);
else
webkit_uri_scheme_request_finish_error (request, error);
g_list_free (list);
g_error_free (error);
g_object_unref (app_info);
}
static void
web_extension_destroyed (EphyEmbedShell *shell,
GObject *web_extension)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
priv->web_extensions = g_list_remove (priv->web_extensions, web_extension);
}
static void
ephy_embed_shell_watch_web_extension (EphyEmbedShell *shell,
EphyWebExtensionProxy *web_extension)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
priv->web_extensions = g_list_prepend (priv->web_extensions, web_extension);
g_object_weak_ref (G_OBJECT (web_extension), (GWeakNotify)web_extension_destroyed, shell);
}
static void
ephy_embed_shell_unwatch_web_extension (EphyWebExtensionProxy *web_extension,
EphyEmbedShell *shell)
{
g_object_weak_unref (G_OBJECT (web_extension), (GWeakNotify)web_extension_destroyed, shell);
}
static void
initialize_web_extensions (WebKitWebContext *web_context,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GVariant *user_data;
gboolean private_profile;
gboolean browser_mode;
const char *address;
#if DEVELOPER_MODE
webkit_web_context_set_web_extensions_directory (web_context, BUILD_ROOT "/embed/web-extension");
#else
webkit_web_context_set_web_extensions_directory (web_context, EPHY_WEB_EXTENSIONS_DIR);
#endif
address = priv->dbus_server ? g_dbus_server_get_client_address (priv->dbus_server) : NULL;
private_profile = priv->mode == EPHY_EMBED_SHELL_MODE_PRIVATE || priv->mode == EPHY_EMBED_SHELL_MODE_INCOGNITO || priv->mode == EPHY_EMBED_SHELL_MODE_AUTOMATION;
browser_mode = priv->mode == EPHY_EMBED_SHELL_MODE_BROWSER;
user_data = g_variant_new ("(smsmssbb)",
priv->guid,
address,
ephy_profile_dir_is_default () ? NULL : ephy_profile_dir (),
ephy_filters_manager_get_adblock_filters_dir (priv->filters_manager),
private_profile,
browser_mode);
webkit_web_context_set_web_extensions_initialization_user_data (web_context, user_data);
}
static void
initialize_notification_permissions (WebKitWebContext *web_context,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GList *permitted_origins;
GList *denied_origins;
permitted_origins = ephy_permissions_manager_get_permitted_origins (priv->permissions_manager,
EPHY_PERMISSION_TYPE_SHOW_NOTIFICATIONS);
denied_origins = ephy_permissions_manager_get_denied_origins (priv->permissions_manager,
EPHY_PERMISSION_TYPE_SHOW_NOTIFICATIONS);
webkit_web_context_initialize_notification_permissions (web_context, permitted_origins, denied_origins);
}
static void
web_extension_page_created (EphyWebExtensionProxy *extension,
guint64 page_id,
EphyEmbedShell *shell)
{
g_object_set_data (G_OBJECT (extension), "initialized", GINT_TO_POINTER (TRUE));
g_signal_emit (shell, signals[PAGE_CREATED], 0, page_id, extension);
}
static gboolean
new_connection_cb (GDBusServer *server,
GDBusConnection *connection,
EphyEmbedShell *shell)
{
EphyWebExtensionProxy *extension;
extension = ephy_web_extension_proxy_new (connection);
ephy_embed_shell_watch_web_extension (shell, extension);
g_signal_connect_object (extension, "page-created",
G_CALLBACK (web_extension_page_created), shell, 0);
return TRUE;
}
static gboolean
authorize_authenticated_peer_cb (GDBusAuthObserver *observer,
GIOStream *stream,
GCredentials *credentials,
EphyEmbedShell *shell)
{
return ephy_dbus_peer_is_authorized (credentials);
}
static void
ephy_embed_shell_setup_web_extensions_server (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
GDBusAuthObserver *observer;
char *address;
GError *error = NULL;
address = g_strdup_printf ("unix:tmpdir=%s", g_get_tmp_dir ());
observer = g_dbus_auth_observer_new ();
g_signal_connect_object (observer, "authorize-authenticated-peer",
G_CALLBACK (authorize_authenticated_peer_cb), shell, 0);
/* Why sync?
*
* (a) The server must be started before web extensions try to connect.
* (b) Gio actually has no async version. Don't know why.
*/
priv->dbus_server = g_dbus_server_new_sync (address,
G_DBUS_SERVER_FLAGS_NONE,
priv->guid,
observer,
NULL,
&error);
if (error) {
g_warning ("Failed to start web extension server on %s: %s", address, error->message);
g_error_free (error);
goto out;
}
g_signal_connect_object (priv->dbus_server, "new-connection",
G_CALLBACK (new_connection_cb), shell, 0);
g_dbus_server_start (priv->dbus_server);
out:
g_free (address);
g_object_unref (observer);
}
static void
ephy_embed_shell_setup_process_model (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
EphyPrefsProcessModel process_model;
guint max_processes;
if (ephy_embed_shell_get_mode (shell) == EPHY_EMBED_SHELL_MODE_APPLICATION)
process_model = EPHY_PREFS_PROCESS_MODEL_SHARED_SECONDARY_PROCESS;
else
process_model = g_settings_get_enum (EPHY_SETTINGS_MAIN, EPHY_PREFS_PROCESS_MODEL);
switch (process_model) {
case EPHY_PREFS_PROCESS_MODEL_SHARED_SECONDARY_PROCESS:
max_processes = 1;
break;
case EPHY_PREFS_PROCESS_MODEL_ONE_SECONDARY_PROCESS_PER_WEB_VIEW:
max_processes = g_settings_get_uint (EPHY_SETTINGS_MAIN, EPHY_PREFS_MAX_PROCESSES);
break;
default:
g_assert_not_reached ();
}
webkit_web_context_set_process_model (priv->web_context, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
webkit_web_context_set_web_process_count_limit (priv->web_context, max_processes);
}
static void
ephy_embed_shell_create_web_context (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
WebKitWebsiteDataManager *manager;
if (priv->mode == EPHY_EMBED_SHELL_MODE_INCOGNITO) {
priv->web_context = webkit_web_context_new_ephemeral ();
return;
}
if (priv->mode == EPHY_EMBED_SHELL_MODE_AUTOMATION) {
priv->web_context = webkit_web_context_new_ephemeral ();
webkit_web_context_set_automation_allowed (priv->web_context, TRUE);
return;
}
manager = webkit_website_data_manager_new ("base-data-directory", ephy_profile_dir (),
"base-cache-directory", ephy_cache_dir (),
NULL);
priv->web_context = webkit_web_context_new_with_website_data_manager (manager);
g_object_unref (manager);
}
static char *
adblock_filters_dir (EphyEmbedShell *shell)
{
return g_build_filename (ephy_cache_dir (), "adblock", NULL);
}
static void
download_started_cb (WebKitWebContext *web_context,
WebKitDownload *download,
EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
EphyDownload *ephy_download;
gboolean ephy_download_set;
/* Is download locked down? */
if (g_settings_get_boolean (EPHY_SETTINGS_LOCKDOWN,
EPHY_PREFS_LOCKDOWN_SAVE_TO_DISK)) {
webkit_download_cancel (download);
return;
}
/* Only create an EphyDownload for the WebKitDownload if it doesn't exist yet.
* This can happen when the download has been started automatically by WebKit,
* due to a context menu action or policy checker decision. Downloads started
* explicitly by Epiphany are marked with ephy-download-set GObject data.
*/
ephy_download_set = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (download), "ephy-download-set"));
if (ephy_download_set)
return;
ephy_download = ephy_download_new (download);
ephy_downloads_manager_add_download (priv->downloads_manager, ephy_download);
g_object_unref (ephy_download);
}
static void
ephy_embed_shell_startup (GApplication *application)
{
EphyEmbedShell *shell = EPHY_EMBED_SHELL (application);
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
char *favicon_db_path;
WebKitCookieManager *cookie_manager;
char *filename;
char *cookie_policy;
char *filters_dir;
G_APPLICATION_CLASS (ephy_embed_shell_parent_class)->startup (application);
ephy_embed_shell_create_web_context (shell);
ephy_embed_shell_setup_web_extensions_server (shell);
/* User content manager */
if (priv->mode != EPHY_EMBED_SHELL_MODE_TEST)
priv->user_content = webkit_user_content_manager_new ();
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"overview",
priv->guid);
g_signal_connect_object (priv->user_content, "script-message-received::overview",
G_CALLBACK (web_extension_overview_message_received_cb),
shell, 0);
webkit_user_content_manager_register_script_message_handler (priv->user_content,
"tlsErrorPage");
g_signal_connect_object (priv->user_content, "script-message-received::tlsErrorPage",
G_CALLBACK (web_extension_tls_error_page_message_received_cb),
shell, 0);
webkit_user_content_manager_register_script_message_handler (priv->user_content,
"unsafeBrowsingErrorPage");
g_signal_connect_object (priv->user_content, "script-message-received::unsafeBrowsingErrorPage",
G_CALLBACK (web_extension_unsafe_browsing_error_page_message_received_cb),
shell, 0);
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"sensitiveFormFocused",
priv->guid);
g_signal_connect_object (priv->user_content, "script-message-received::sensitiveFormFocused",
G_CALLBACK (web_extension_sensitive_form_focused_message_received_cb),
shell, 0);
webkit_user_content_manager_register_script_message_handler (priv->user_content,
"aboutApps");
g_signal_connect_object (priv->user_content, "script-message-received::aboutApps",
G_CALLBACK (web_extension_about_apps_message_received_cb),
shell, 0);
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"passwordManagerQuery",
priv->guid);
g_signal_connect (priv->user_content, "script-message-received::passwordManagerQuery",
G_CALLBACK (web_extension_password_manager_query_received_cb),
shell);
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"passwordManagerQueryUsernames",
priv->guid);
g_signal_connect (priv->user_content, "script-message-received::passwordManagerQueryUsernames",
G_CALLBACK (web_extension_password_manager_cached_users_received_cb),
shell);
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"passwordManagerSave",
priv->guid);
g_signal_connect (priv->user_content, "script-message-received::passwordManagerSave",
G_CALLBACK (web_extension_password_manager_save_received_cb),
shell);
webkit_user_content_manager_register_script_message_handler_in_world (priv->user_content,
"passwordManagerRequestSave",
priv->guid);
g_signal_connect (priv->user_content, "script-message-received::passwordManagerRequestSave",
G_CALLBACK (web_extension_password_manager_request_save_received_cb),
shell);
ephy_embed_shell_setup_process_model (shell);
g_signal_connect_object (priv->web_context, "initialize-web-extensions",
G_CALLBACK (initialize_web_extensions),
shell, 0);
priv->permissions_manager = ephy_permissions_manager_new ();
g_signal_connect_object (priv->web_context, "initialize-notification-permissions",
G_CALLBACK (initialize_notification_permissions),
shell, 0);
priv->password_manager = ephy_password_manager_new ();
/* Do not cache favicons in automation mode. Don't change the TLS policy either, since that's
* handled by session capabilities in automation mode.
*/
if (priv->mode != EPHY_EMBED_SHELL_MODE_AUTOMATION) {
/* Favicon Database */
favicon_db_path = g_build_filename (ephy_cache_dir (), "icondatabase", NULL);
webkit_web_context_set_favicon_database_directory (priv->web_context, favicon_db_path);
g_free (favicon_db_path);
/* Do not ignore TLS errors. */
webkit_web_context_set_tls_errors_policy (priv->web_context, WEBKIT_TLS_ERRORS_POLICY_FAIL);
}
/* about: URIs handler */
priv->about_handler = ephy_about_handler_new ();
webkit_web_context_register_uri_scheme (priv->web_context,
EPHY_ABOUT_SCHEME,
(WebKitURISchemeRequestCallback)about_request_cb,
shell, NULL);
/* Register about scheme as local so that it can contain file resources */
webkit_security_manager_register_uri_scheme_as_local (webkit_web_context_get_security_manager (priv->web_context),
EPHY_ABOUT_SCHEME);
/* view source handler */
priv->source_handler = ephy_view_source_handler_new ();
webkit_web_context_register_uri_scheme (priv->web_context, EPHY_VIEW_SOURCE_SCHEME,
(WebKitURISchemeRequestCallback)source_request_cb,
shell, NULL);
/* ephy-resource handler */
webkit_web_context_register_uri_scheme (priv->web_context, "ephy-resource",
(WebKitURISchemeRequestCallback)ephy_resource_request_cb,
NULL, NULL);
/* No support for FTP, try to open in nautilus instead of failing */
webkit_web_context_register_uri_scheme (priv->web_context, "ftp",
(WebKitURISchemeRequestCallback)ftp_request_cb,
NULL, NULL);
/* Store cookies in moz-compatible SQLite format */
cookie_manager = webkit_web_context_get_cookie_manager (priv->web_context);
if (!webkit_web_context_is_ephemeral (priv->web_context)) {
filename = g_build_filename (ephy_profile_dir (), "cookies.sqlite", NULL);
webkit_cookie_manager_set_persistent_storage (cookie_manager, filename,
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
g_free (filename);
}
cookie_policy = g_settings_get_string (EPHY_SETTINGS_WEB,
EPHY_PREFS_WEB_COOKIES_POLICY);
ephy_embed_prefs_set_cookie_accept_policy (cookie_manager, cookie_policy);
g_free (cookie_policy);
filters_dir = adblock_filters_dir (shell);
priv->filters_manager = ephy_filters_manager_new (filters_dir);
g_free (filters_dir);
g_signal_connect (priv->web_context, "download-started",
G_CALLBACK (download_started_cb), shell);
}
static void
ephy_embed_shell_shutdown (GApplication *application)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (EPHY_EMBED_SHELL (application));
G_APPLICATION_CLASS (ephy_embed_shell_parent_class)->shutdown (application);
if (priv->dbus_server)
g_dbus_server_stop (priv->dbus_server);
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"overview",
priv->guid);
webkit_user_content_manager_unregister_script_message_handler (priv->user_content,
"tlsErrorPage");
webkit_user_content_manager_unregister_script_message_handler (priv->user_content,
"unsafeBrowsingErrorPage");
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"passwordManagerRequestSave",
priv->guid);
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"sensitiveFormFocused",
priv->guid);
webkit_user_content_manager_unregister_script_message_handler (priv->user_content, "aboutApps");
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"passwordManagerQuery",
priv->guid);
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"passwordManagerSave",
priv->guid);
webkit_user_content_manager_unregister_script_message_handler_in_world (priv->user_content,
"passwordManagerQueryUsernames",
priv->guid);
g_list_foreach (priv->web_extensions, (GFunc)ephy_embed_shell_unwatch_web_extension, application);
g_object_unref (ephy_embed_prefs_get_settings ());
ephy_embed_utils_shutdown ();
}
static void
ephy_embed_shell_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (EPHY_EMBED_SHELL (object));
switch (prop_id) {
case PROP_MODE:
priv->mode = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ephy_embed_shell_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (EPHY_EMBED_SHELL (object));
switch (prop_id) {
case PROP_MODE:
g_value_set_enum (value, priv->mode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
ephy_embed_shell_constructed (GObject *object)
{
EphyEmbedShell *shell;
EphyEmbedShellPrivate *priv;
EphyEmbedShellMode mode;
G_OBJECT_CLASS (ephy_embed_shell_parent_class)->constructed (object);
shell = EPHY_EMBED_SHELL (object);
priv = ephy_embed_shell_get_instance_private (shell);
priv->guid = g_dbus_generate_guid ();
mode = ephy_embed_shell_get_mode (shell);
/* These do not run the EmbedShell application instance, so make sure that
there is a web context and a user content manager for them. */
if (mode == EPHY_EMBED_SHELL_MODE_TEST ||
mode == EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER) {
ephy_embed_shell_create_web_context (shell);
priv->user_content = webkit_user_content_manager_new ();
}
}
static void
ephy_embed_shell_init (EphyEmbedShell *shell)
{
/* globally accessible singleton */
g_assert (embed_shell == NULL);
embed_shell = shell;
}
static void
ephy_embed_shell_class_init (EphyEmbedShellClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
object_class->dispose = ephy_embed_shell_dispose;
object_class->set_property = ephy_embed_shell_set_property;
object_class->get_property = ephy_embed_shell_get_property;
object_class->constructed = ephy_embed_shell_constructed;
application_class->startup = ephy_embed_shell_startup;
application_class->shutdown = ephy_embed_shell_shutdown;
object_properties[PROP_MODE] =
g_param_spec_enum ("mode",
"Mode",
"The global mode for this instance.",
EPHY_TYPE_EMBED_SHELL_MODE,
EPHY_EMBED_SHELL_MODE_BROWSER,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPERTIES,
object_properties);
/**
* EphyEmbedShell::finished-restoring-window:
* @shell: the #EphyEmbedShell
*
* The ::finished-restoring-window signal is emitted when the
* session finishes restoring a window.
**/
signals[RESTORED_WINDOW] =
g_signal_new ("window-restored",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EphyEmbedShellClass, restored_window),
NULL, NULL, NULL,
G_TYPE_NONE,
0);
/**
* EphyEmbedShell::web-view-created:
* @shell: the #EphyEmbedShell
* @view: the newly created #EphyWebView
*
* The ::web-view-created signal will be emitted every time a new
* #EphyWebView is created.
*
**/
signals[WEB_VIEW_CREATED] =
g_signal_new ("web-view-created",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_LAST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 1,
EPHY_TYPE_WEB_VIEW);
/**
* EphyEmbedShell::page-created:
* @shell: the #EphyEmbedShell
* @page_id: the identifier of the web page created
* @web_extension: the #EphyWebExtensionProxy
*
* Emitted when a web page is created in the web process.
*/
signals[PAGE_CREATED] =
g_signal_new ("page-created",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 2,
G_TYPE_UINT64,
EPHY_TYPE_WEB_EXTENSION_PROXY);
/**
* EphyEmbedShell::allow-tls-certificate:
* @shell: the #EphyEmbedShell
* @page_id: the identifier of the web page
*
* Emitted when the web extension requests an exception be
* permitted for the invalid TLS certificate on the given page
*/
signals[ALLOW_TLS_CERTIFICATE] =
g_signal_new ("allow-tls-certificate",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 1,
G_TYPE_UINT64);
/**
* EphyEmbedShell::allow-unsafe-browsing:
* @shell: the #EphyEmbedShell
* @page_id: the identifier of the web page
*
* Emitted when the web extension requests an exception be
* permitted for the unsafe browsing warning on the given page
*/
signals[ALLOW_UNSAFE_BROWSING] =
g_signal_new ("allow-unsafe-browsing",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 1,
G_TYPE_UINT64);
/**
* EphyEmbedShell::sensitive-form-focused
* @shell: the #EphyEmbedShell
* @page_id: the identifier of the web page
* @insecure_action: whether the target of the form is http://
*
* Emitted when a form in a web page gains focus.
*/
signals[SENSITIVE_FORM_FOCUSED] =
g_signal_new ("sensitive-form-focused",
EPHY_TYPE_EMBED_SHELL,
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 2,
G_TYPE_UINT64,
G_TYPE_BOOLEAN);
}
/**
* ephy_embed_shell_get_default:
*
* Retrieves the default #EphyEmbedShell object
*
* Return value: (transfer none): the default #EphyEmbedShell
**/
EphyEmbedShell *
ephy_embed_shell_get_default (void)
{
return embed_shell;
}
void
ephy_embed_shell_set_page_setup (EphyEmbedShell *shell,
GtkPageSetup *page_setup)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
char *path;
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (page_setup != NULL)
g_object_ref (page_setup);
else
page_setup = gtk_page_setup_new ();
if (priv->page_setup != NULL)
g_object_unref (priv->page_setup);
priv->page_setup = page_setup;
path = g_build_filename (ephy_profile_dir (), PAGE_SETUP_FILENAME, NULL);
gtk_page_setup_to_file (page_setup, path, NULL);
g_free (path);
}
/**
* ephy_embed_shell_get_page_setup:
*
* Return value: (transfer none):
**/
GtkPageSetup *
ephy_embed_shell_get_page_setup (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (priv->page_setup == NULL) {
GError *error = NULL;
char *path;
path = g_build_filename (ephy_profile_dir (), PAGE_SETUP_FILENAME, NULL);
priv->page_setup = gtk_page_setup_new_from_file (path, &error);
g_free (path);
if (error)
g_error_free (error);
/* If that still didn't work, create a new, empty one */
if (priv->page_setup == NULL)
priv->page_setup = gtk_page_setup_new ();
}
return priv->page_setup;
}
/**
* ephy_embed_shell_set_print_settings:
* @shell: the #EphyEmbedShell
* @settings: the new #GtkPrintSettings object
*
* Sets the global #GtkPrintSettings object.
*
**/
void
ephy_embed_shell_set_print_settings (EphyEmbedShell *shell,
GtkPrintSettings *settings)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
char *path;
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (settings != NULL)
g_object_ref (settings);
if (priv->print_settings != NULL)
g_object_unref (priv->print_settings);
priv->print_settings = settings ? settings : gtk_print_settings_new ();
path = g_build_filename (ephy_profile_dir (), PRINT_SETTINGS_FILENAME, NULL);
gtk_print_settings_to_file (settings, path, NULL);
g_free (path);
}
/**
* ephy_embed_shell_get_print_settings:
* @shell: the #EphyEmbedShell
*
* Gets the global #GtkPrintSettings object.
*
* Returns: (transfer none): a #GtkPrintSettings object
**/
GtkPrintSettings *
ephy_embed_shell_get_print_settings (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_assert (EPHY_IS_EMBED_SHELL (shell));
if (priv->print_settings == NULL) {
GError *error = NULL;
char *path;
path = g_build_filename (ephy_profile_dir (), PRINT_SETTINGS_FILENAME, NULL);
priv->print_settings = gtk_print_settings_new_from_file (path, &error);
g_free (path);
/* Note: the gtk print settings file format is the same as our
* legacy one, so no need to migrate here.
*/
if (priv->print_settings == NULL)
priv->print_settings = gtk_print_settings_new ();
}
return priv->print_settings;
}
/**
* ephy_embed_shell_get_mode:
* @shell: an #EphyEmbedShell
*
* Returns: the global mode of the @shell
**/
EphyEmbedShellMode
ephy_embed_shell_get_mode (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
g_assert (EPHY_IS_EMBED_SHELL (shell));
return priv->mode;
}
/**
* ephy_embed_shell_clear_cache:
* @shell: an #EphyEmbedShell
*
* Clears the HTTP cache (temporarily saved web pages).
**/
void
ephy_embed_shell_clear_cache (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
webkit_web_context_clear_cache (priv->web_context);
}
WebKitUserContentManager *
ephy_embed_shell_get_user_content_manager (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
return priv->user_content;
}
const char *
ephy_embed_shell_get_guid (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
return priv->guid;
}
WebKitWebContext *
ephy_embed_shell_get_web_context (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
return priv->web_context;
}
EphyDownloadsManager *
ephy_embed_shell_get_downloads_manager (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
if (!priv->downloads_manager)
priv->downloads_manager = EPHY_DOWNLOADS_MANAGER (g_object_new (EPHY_TYPE_DOWNLOADS_MANAGER, NULL));
return priv->downloads_manager;
}
EphyPermissionsManager *
ephy_embed_shell_get_permissions_manager (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
return priv->permissions_manager;
}
EphySearchEngineManager *
ephy_embed_shell_get_search_engine_manager (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
if (!priv->search_engine_manager)
priv->search_engine_manager = ephy_search_engine_manager_new ();
return priv->search_engine_manager;
}
EphyPasswordManager *
ephy_embed_shell_get_password_manager (EphyEmbedShell *shell)
{
EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
return priv->password_manager;
}
|