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 "WebKitPrivate.h"
25#include <wtf/HashMap.h>
26#include <wtf/gobject/GRefPtr.h>
27#include <wtf/text/CString.h>
28
29using namespace WebKit;
30
31/**
32 * SECTION: WebKitBackForwardListItem
33 * @Short_description: One item of the #WebKitBackForwardList
34 * @Title: WebKitBackForwardListItem
35 * @See_also: #WebKitBackForwardList
36 *
37 * A history item is part of the #WebKitBackForwardList and consists
38 * out of a title and a URI.
39 *
40 */
41
42struct _WebKitBackForwardListItemPrivate {
43    RefPtr<WebBackForwardListItem> webListItem;
44    CString uri;
45    CString title;
46    CString originalURI;
47};
48
49WEBKIT_DEFINE_TYPE(WebKitBackForwardListItem, webkit_back_forward_list_item, G_TYPE_INITIALLY_UNOWNED)
50
51static void webkit_back_forward_list_item_class_init(WebKitBackForwardListItemClass* listItemClass)
52{
53}
54
55typedef HashMap<WebBackForwardListItem*, WebKitBackForwardListItem*> HistoryItemsMap;
56
57static HistoryItemsMap& historyItemsMap()
58{
59    DEFINE_STATIC_LOCAL(HistoryItemsMap, itemsMap, ());
60    return itemsMap;
61}
62
63static void webkitBackForwardListItemFinalized(gpointer webListItem, GObject* finalizedListItem)
64{
65    ASSERT(G_OBJECT(historyItemsMap().get(static_cast<WebBackForwardListItem*>(webListItem))) == finalizedListItem);
66    historyItemsMap().remove(static_cast<WebBackForwardListItem*>(webListItem));
67}
68
69WebKitBackForwardListItem* webkitBackForwardListItemGetOrCreate(WebBackForwardListItem* webListItem)
70{
71    if (!webListItem)
72        return 0;
73
74    WebKitBackForwardListItem* listItem = historyItemsMap().get(webListItem);
75    if (listItem)
76        return listItem;
77
78    listItem = WEBKIT_BACK_FORWARD_LIST_ITEM(g_object_new(WEBKIT_TYPE_BACK_FORWARD_LIST_ITEM, NULL));
79    listItem->priv->webListItem = webListItem;
80
81    g_object_weak_ref(G_OBJECT(listItem), webkitBackForwardListItemFinalized, webListItem);
82    historyItemsMap().set(webListItem, listItem);
83
84    return listItem;
85}
86
87WebBackForwardListItem* webkitBackForwardListItemGetItem(WebKitBackForwardListItem* listItem)
88{
89    return listItem->priv->webListItem.get();
90}
91
92/**
93 * webkit_back_forward_list_item_get_uri:
94 * @list_item: a #WebKitBackForwardListItem
95 *
96 * This URI may differ from the original URI if the page was,
97 * for example, redirected to a new location.
98 * See also webkit_back_forward_list_item_get_original_uri().
99 *
100 * Returns: the URI of @list_item or %NULL
101 *    when the URI is empty.
102 */
103const gchar* webkit_back_forward_list_item_get_uri(WebKitBackForwardListItem* listItem)
104{
105    g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0);
106
107    WebKitBackForwardListItemPrivate* priv = listItem->priv;
108    String url = priv->webListItem->url();
109    if (url.isEmpty())
110        return 0;
111
112    priv->uri = url.utf8();
113    return priv->uri.data();
114}
115
116/**
117 * webkit_back_forward_list_item_get_title:
118 * @list_item: a #WebKitBackForwardListItem
119 *
120 * Returns: the page title of @list_item or %NULL
121 *    when the title is empty.
122 */
123const gchar* webkit_back_forward_list_item_get_title(WebKitBackForwardListItem* listItem)
124{
125    g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0);
126
127    WebKitBackForwardListItemPrivate* priv = listItem->priv;
128    String title = priv->webListItem->title();
129    if (title.isEmpty())
130        return 0;
131
132    priv->title = title.utf8();
133    return priv->title.data();
134}
135
136/**
137 * webkit_back_forward_list_item_get_original_uri:
138 * @list_item: a #WebKitBackForwardListItem
139 *
140 * See also webkit_back_forward_list_item_get_uri().
141 *
142 * Returns: the original URI of @list_item or %NULL
143 *    when the original URI is empty.
144 */
145const gchar* webkit_back_forward_list_item_get_original_uri(WebKitBackForwardListItem* listItem)
146{
147    g_return_val_if_fail(WEBKIT_IS_BACK_FORWARD_LIST_ITEM(listItem), 0);
148
149    WebKitBackForwardListItemPrivate* priv = listItem->priv;
150    String originalURL = priv->webListItem->originalURL();
151    if (originalURL.isEmpty())
152        return 0;
153
154    priv->originalURI = originalURL.utf8();
155    return priv->originalURI.data();
156}
157