2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "ewk_back_forward_list.h"
29 #include "WKAPICast.h"
31 #include "WKBackForwardList.h"
32 #include "ewk_back_forward_list_private.h"
33 #include <wtf/text/CString.h>
35 using namespace WebKit;
37 Ewk_Back_Forward_List::Ewk_Back_Forward_List(WKBackForwardListRef listRef)
41 Ewk_Back_Forward_List_Item* Ewk_Back_Forward_List::nextItem() const
43 return getFromCacheOrCreate(WKBackForwardListGetForwardItem(m_wkList.get()));
46 Ewk_Back_Forward_List_Item* Ewk_Back_Forward_List::previousItem() const
48 return getFromCacheOrCreate(WKBackForwardListGetBackItem(m_wkList.get()));
51 Ewk_Back_Forward_List_Item* Ewk_Back_Forward_List::currentItem() const
53 return getFromCacheOrCreate(WKBackForwardListGetCurrentItem(m_wkList.get()));
56 Ewk_Back_Forward_List_Item* Ewk_Back_Forward_List::itemAt(int index) const
58 return getFromCacheOrCreate(WKBackForwardListGetItemAtIndex(m_wkList.get(), index));
61 unsigned Ewk_Back_Forward_List::size() const
63 const unsigned currentItem = WKBackForwardListGetCurrentItem(m_wkList.get()) ? 1 : 0;
65 return WKBackForwardListGetBackListCount(m_wkList.get()) + WKBackForwardListGetForwardListCount(m_wkList.get()) + currentItem;
68 WKRetainPtr<WKArrayRef> Ewk_Back_Forward_List::backList(int limit) const
71 limit = WKBackForwardListGetBackListCount(m_wkList.get());
74 return adoptWK(WKBackForwardListCopyBackListWithLimit(m_wkList.get(), limit));
77 WKRetainPtr<WKArrayRef> Ewk_Back_Forward_List::forwardList(int limit) const
80 limit = WKBackForwardListGetForwardListCount(m_wkList.get());
83 return adoptWK(WKBackForwardListCopyForwardListWithLimit(m_wkList.get(), limit));
86 Ewk_Back_Forward_List_Item* Ewk_Back_Forward_List::getFromCacheOrCreate(WKBackForwardListItemRef wkItem) const
91 RefPtr<Ewk_Back_Forward_List_Item> item = m_wrapperCache.get(wkItem);
93 item = Ewk_Back_Forward_List_Item::create(wkItem);
94 m_wrapperCache.set(wkItem, item);
100 Eina_List* Ewk_Back_Forward_List::createEinaList(WKArrayRef wkList) const
105 Eina_List* result = 0;
107 const size_t count = WKArrayGetSize(wkList);
108 for (size_t i = 0; i < count; ++i) {
109 WKBackForwardListItemRef wkItem = static_cast<WKBackForwardListItemRef>(WKArrayGetItemAtIndex(wkList, i));
110 Ewk_Back_Forward_List_Item* item = getFromCacheOrCreate(wkItem);
111 result = eina_list_append(result, ewk_back_forward_list_item_ref(item));
119 * Updates items cache.
121 void Ewk_Back_Forward_List::update(WKBackForwardListItemRef wkAddedItem, WKArrayRef wkRemovedItems)
123 if (wkAddedItem) // Checking also here to avoid EINA_SAFETY_ON_NULL_RETURN_VAL warnings.
124 getFromCacheOrCreate(wkAddedItem); // Puts new item to the cache.
126 const size_t removedItemsSize = wkRemovedItems ? WKArrayGetSize(wkRemovedItems) : 0;
127 for (size_t i = 0; i < removedItemsSize; ++i) {
128 WKBackForwardListItemRef wkItem = static_cast<WKBackForwardListItemRef>(WKArrayGetItemAtIndex(wkRemovedItems, i));
129 m_wrapperCache.remove(wkItem);
133 Ewk_Back_Forward_List_Item* ewk_back_forward_list_current_item_get(const Ewk_Back_Forward_List* list)
135 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
137 return list->currentItem();
140 Ewk_Back_Forward_List_Item* ewk_back_forward_list_previous_item_get(const Ewk_Back_Forward_List* list)
142 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
144 return list->previousItem();
147 Ewk_Back_Forward_List_Item* ewk_back_forward_list_next_item_get(const Ewk_Back_Forward_List* list)
149 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
151 return list->nextItem();
154 Ewk_Back_Forward_List_Item* ewk_back_forward_list_item_at_index_get(const Ewk_Back_Forward_List* list, int index)
156 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
158 return list->itemAt(index);
161 unsigned ewk_back_forward_list_count(Ewk_Back_Forward_List* list)
163 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
168 Eina_List* ewk_back_forward_list_n_back_items_copy(const Ewk_Back_Forward_List* list, int limit)
170 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
171 EINA_SAFETY_ON_FALSE_RETURN_VAL(limit == -1 || limit > 0, 0);
173 WKRetainPtr<WKArrayRef> backList = list->backList(limit);
175 return list->createEinaList(backList.get());
178 Eina_List* ewk_back_forward_list_n_forward_items_copy(const Ewk_Back_Forward_List* list, int limit)
180 EINA_SAFETY_ON_NULL_RETURN_VAL(list, 0);
181 EINA_SAFETY_ON_FALSE_RETURN_VAL(limit == -1 || limit > 0, 0);
183 WKRetainPtr<WKArrayRef> forwardList = list->forwardList(limit);
185 return list->createEinaList(forwardList.get());