[EFL] Fix compile warning
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / ewk_back_forward_list.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ewk_back_forward_list.h"
28
29 #include "WKAPICast.h"
30 #include "WKArray.h"
31 #include "WKBackForwardList.h"
32 #include "WKRetainPtr.h"
33 #include "ewk_back_forward_list_item_private.h"
34 #include <wtf/text/CString.h>
35
36 using namespace WebKit;
37
38 typedef HashMap<WKBackForwardListItemRef, Ewk_Back_Forward_List_Item*> ItemsMap;
39
40 /**
41  * \struct  _Ewk_Back_Forward_List
42  * @brief   Contains the Back Forward List data.
43  */
44 struct _Ewk_Back_Forward_List {
45     WKRetainPtr<WKBackForwardListRef> wkList;
46     mutable ItemsMap wrapperCache;
47
48     _Ewk_Back_Forward_List(WKBackForwardListRef listRef)
49         : wkList(listRef)
50     { }
51
52     ~_Ewk_Back_Forward_List()
53     {
54         ItemsMap::iterator it = wrapperCache.begin();
55         ItemsMap::iterator end = wrapperCache.end();
56         for (; it != end; ++it)
57             ewk_back_forward_list_item_unref(it->second);
58     }
59 };
60
61 #define EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList_, ...)  \
62     if (!(list)) {                                             \
63         EINA_LOG_CRIT("list is NULL.");                        \
64         return __VA_ARGS__;                                    \
65     }                                                          \
66     if (!(list)->wkList) {                                     \
67         EINA_LOG_CRIT("list->wkList is NULL.");                \
68         return __VA_ARGS__;                                    \
69     }                                                          \
70     WKBackForwardListRef wkList_ = (list)->wkList.get()
71
72
73 static inline Ewk_Back_Forward_List_Item* addItemToWrapperCache(const Ewk_Back_Forward_List* list, WKBackForwardListItemRef wkItem)
74 {
75     EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
76     EINA_SAFETY_ON_NULL_RETURN_VAL(wkItem, 0);
77
78     Ewk_Back_Forward_List_Item* item = list->wrapperCache.get(wkItem);
79     if (!item) {
80         item = ewk_back_forward_list_item_new(wkItem);
81         list->wrapperCache.set(wkItem, item);
82     }
83
84     return item;
85 }
86
87 static inline Eina_List* createEinaList(const Ewk_Back_Forward_List* list, WKArrayRef wkList)
88 {
89     if (!wkList)
90         return 0;
91
92     Eina_List* result = 0;
93
94     const size_t count = WKArrayGetSize(wkList);
95     for (size_t i = 0; i < count; ++i) {
96         WKBackForwardListItemRef wkItem = static_cast<WKBackForwardListItemRef>(WKArrayGetItemAtIndex(wkList, i));
97         Ewk_Back_Forward_List_Item* item = addItemToWrapperCache(list, wkItem);
98         ewk_back_forward_list_item_ref(item);
99         result = eina_list_append(result, item);
100     }
101
102     return result;
103 }
104
105 Ewk_Back_Forward_List_Item* ewk_back_forward_list_current_item_get(const Ewk_Back_Forward_List* list)
106 {
107     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
108
109     return addItemToWrapperCache(list, WKBackForwardListGetCurrentItem(wkList));
110 }
111
112 Ewk_Back_Forward_List_Item* ewk_back_forward_list_previous_item_get(const Ewk_Back_Forward_List* list)
113 {
114     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
115
116     return addItemToWrapperCache(list, WKBackForwardListGetBackItem(wkList));
117 }
118
119 Ewk_Back_Forward_List_Item* ewk_back_forward_list_next_item_get(const Ewk_Back_Forward_List* list)
120 {
121     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
122
123     return addItemToWrapperCache(list, WKBackForwardListGetForwardItem(wkList));
124 }
125
126 Ewk_Back_Forward_List_Item* ewk_back_forward_list_item_at_index_get(const Ewk_Back_Forward_List* list, int index)
127 {
128     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
129
130     return addItemToWrapperCache(list, WKBackForwardListGetItemAtIndex(wkList, index));
131 }
132
133 unsigned ewk_back_forward_list_count(Ewk_Back_Forward_List* list)
134 {
135     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
136
137     const unsigned currentItem = ewk_back_forward_list_current_item_get(list) ? 1 : 0;
138
139     return WKBackForwardListGetBackListCount(wkList) + WKBackForwardListGetForwardListCount(wkList) + currentItem;
140 }
141
142 Eina_List* ewk_back_forward_list_n_back_items_copy(const Ewk_Back_Forward_List* list, int limit)
143 {
144     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
145
146     if (limit == -1)
147         limit = WKBackForwardListGetBackListCount(wkList);
148     EINA_SAFETY_ON_FALSE_RETURN_VAL(limit >= 0, 0);
149     WKRetainPtr<WKArrayRef> backList(AdoptWK, WKBackForwardListCopyBackListWithLimit(wkList, limit));
150
151     return createEinaList(list, backList.get());
152 }
153
154 Eina_List* ewk_back_forward_list_n_forward_items_copy(const Ewk_Back_Forward_List* list, int limit)
155 {
156     EWK_BACK_FORWARD_LIST_WK_GET_OR_RETURN(list, wkList, 0);
157
158     if (limit == -1)
159         limit = WKBackForwardListGetForwardListCount(wkList);
160     EINA_SAFETY_ON_FALSE_RETURN_VAL(limit >= 0, 0);
161     WKRetainPtr<WKArrayRef> forwardList(AdoptWK, WKBackForwardListCopyForwardListWithLimit(wkList, limit));
162
163     return createEinaList(list, forwardList.get());
164 }
165
166 /**
167  * @internal
168  * Updates items cache.
169  */
170 void ewk_back_forward_list_changed(Ewk_Back_Forward_List* list, WKBackForwardListItemRef wkAddedItem, WKArrayRef wkRemovedItems)
171 {
172     if (wkAddedItem) // Checking also here to avoid EINA_SAFETY_ON_NULL_RETURN_VAL warnings.
173         addItemToWrapperCache(list, wkAddedItem); // Puts new item to the cache.
174
175     const size_t removedItemsSize = wkRemovedItems ? WKArrayGetSize(wkRemovedItems) : 0;
176     for (size_t i = 0; i < removedItemsSize; ++i) {
177         WKBackForwardListItemRef wkItem = static_cast<WKBackForwardListItemRef>(WKArrayGetItemAtIndex(wkRemovedItems, i));
178         if (Ewk_Back_Forward_List_Item* item = list->wrapperCache.take(wkItem))
179             ewk_back_forward_list_item_unref(item);
180     }
181 }
182
183 /**
184  * @internal
185  * Constructs a Ewk_Back_Forward_List from a WKBackForwardListRef.
186  */
187 Ewk_Back_Forward_List* ewk_back_forward_list_new(WKBackForwardListRef wkBackForwardListRef)
188 {
189     EINA_SAFETY_ON_NULL_RETURN_VAL(wkBackForwardListRef, 0);
190
191     return new Ewk_Back_Forward_List(wkBackForwardListRef);
192 }
193
194 /**
195  * @internal
196  * Frees a Ewk_Back_Forward_List object.
197  */
198 void ewk_back_forward_list_free(Ewk_Back_Forward_List* list)
199 {
200     EINA_SAFETY_ON_NULL_RETURN(list);
201
202     delete list;
203 }