| 1 | /* |
| 2 | * Copyright (C) 2011 Igalia S.L. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "WebKitBackForwardListItem.h" |
| 22 | |
| 23 | #include "WebKitBackForwardListPrivate.h" |
| 24 | #include <wtf/HashMap.h> |
| 25 | #include <wtf/NeverDestroyed.h> |
| 26 | #include <wtf/glib/GRefPtr.h> |
| 27 | #include <wtf/glib/WTFGType.h> |
| 28 | #include <wtf/text/CString.h> |
| 29 | |
| 30 | using namespace WebKit; |
| 31 | |
| 32 | /** |
| 33 | * SECTION: WebKitBackForwardListItem |
| 34 | * @Short_description: One item of the #WebKitBackForwardList |
| 35 | * @Title: WebKitBackForwardListItem |
| 36 | * @See_also: #WebKitBackForwardList |
| 37 | * |
| 38 | * A history item is part of the #WebKitBackForwardList and consists |
| 39 | * out of a title and a URI. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | struct _WebKitBackForwardListItemPrivate { |
| 44 | RefPtr<WebBackForwardListItem> webListItem; |
| 45 | CString uri; |
| 46 | CString title; |
| 47 | CString originalURI; |
| 48 | }; |
| 49 | |
| 50 | WEBKIT_DEFINE_TYPE(WebKitBackForwardListItem, webkit_back_forward_list_item, G_TYPE_INITIALLY_UNOWNED) |
| 51 | |
| 52 | static void webkit_back_forward_list_item_class_init(WebKitBackForwardListItemClass*) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | typedef HashMap<WebBackForwardListItem*, WebKitBackForwardListItem*> HistoryItemsMap; |
| 57 | |
| 58 | static HistoryItemsMap& historyItemsMap() |
| 59 | { |
| 60 | static NeverDestroyed<HistoryItemsMap> itemsMap; |
| 61 | return itemsMap; |
| 62 | } |
| 63 | |
| 64 | static void webkitBackForwardListItemFinalized(gpointer webListItem, GObject* finalizedListItem) |
| 65 | { |
| 66 | ASSERT_UNUSED(finalizedListItem, G_OBJECT(historyItemsMap().get(static_cast<WebBackForwardListItem*>(webListItem))) == finalizedListItem); |
| 67 | historyItemsMap().remove(static_cast<WebBackForwardListItem*>(webListItem)); |
| 68 | } |
| 69 | |
| 70 | WebKitBackForwardListItem* webkitBackForwardListItemGetOrCreate(WebBackForwardListItem* webListItem) |
| 71 | { |
| 72 | if (!webListItem) |
| 73 | return 0; |
| 74 | |
| 75 | WebKitBackForwardListItem* listItem = historyItemsMap().get(webListItem); |
| 76 | if (listItem) |
| 77 | return listItem; |
| 78 | |
| 79 | listItem = WEBKIT_BACK_FORWARD_LIST_ITEM(g_object_new(WEBKIT_TYPE_BACK_FORWARD_LIST_ITEM, NULL)); |
| 80 | listItem->priv->webListItem = webListItem; |
| 81 | |
| 82 | g_object_weak_ref(G_OBJECT(listItem), webkitBackForwardListItemFinalized, webListItem); |
| 83 | historyItemsMap().set(webListItem, listItem); |
| 84 | |
| 85 | return listItem; |
| 86 | } |
| 87 | |
| 88 | WebBackForwardListItem* webkitBackForwardListItemGetItem(WebKitBackForwardListItem* listItem) |
| 89 | { |
| 90 | return listItem->priv->webListItem.get(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * webkit_back_forward_list_item_get_uri: |
| 95 | * @list_item: a #WebKitBackForwardListItem |
| 96 | * |
| 97 | * This URI may differ from the original URI if the page was, |
| 98 | * for example, redirected to a new location. |
| 99 | * See also webkit_back_forward_list_item_get_original_uri(). |
| 100 | * |
| 101 | * Returns: the URI of @list_item or %NULL |
| 102 | * when the URI is empty. |
| 103 | */ |
| 104 | const gchar* webkit_back_forward_list_item_get_uri(WebKitBackForwardListItem* listItem) |
| 105 | { |
| 106 | g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0); |
| 107 | |
| 108 | WebKitBackForwardListItemPrivate* priv = listItem->priv; |
| 109 | String url = priv->webListItem->url(); |
| 110 | if (url.isEmpty()) |
| 111 | return 0; |
| 112 | |
| 113 | priv->uri = url.utf8(); |
| 114 | return priv->uri.data(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * webkit_back_forward_list_item_get_title: |
| 119 | * @list_item: a #WebKitBackForwardListItem |
| 120 | * |
| 121 | * Returns: the page title of @list_item or %NULL |
| 122 | * when the title is empty. |
| 123 | */ |
| 124 | const gchar* webkit_back_forward_list_item_get_title(WebKitBackForwardListItem* listItem) |
| 125 | { |
| 126 | g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0); |
| 127 | |
| 128 | WebKitBackForwardListItemPrivate* priv = listItem->priv; |
| 129 | String title = priv->webListItem->title(); |
| 130 | if (title.isEmpty()) |
| 131 | return 0; |
| 132 | |
| 133 | priv->title = title.utf8(); |
| 134 | return priv->title.data(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * webkit_back_forward_list_item_get_original_uri: |
| 139 | * @list_item: a #WebKitBackForwardListItem |
| 140 | * |
| 141 | * See also webkit_back_forward_list_item_get_uri(). |
| 142 | * |
| 143 | * Returns: the original URI of @list_item or %NULL |
| 144 | * when the original URI is empty. |
| 145 | */ |
| 146 | const gchar* webkit_back_forward_list_item_get_original_uri(WebKitBackForwardListItem* listItem) |
| 147 | { |
| 148 | g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0); |
| 149 | |
| 150 | WebKitBackForwardListItemPrivate* priv = listItem->priv; |
| 151 | String originalURL = priv->webListItem->originalURL(); |
| 152 | if (originalURL.isEmpty()) |
| 153 | return 0; |
| 154 | |
| 155 | priv->originalURI = originalURL.utf8(); |
| 156 | return priv->originalURI.data(); |
| 157 | } |
| 158 | |