~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit/efl/ewk/ewk_history.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2009-2010 ProFUSION embedded systems
 
3
    Copyright (C) 2009-2010 Samsung Electronics
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public License
 
16
    along with this library; see the file COPYING.LIB.  If not, write to
 
17
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
    Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
#include "config.h"
 
22
#include "ewk_history.h"
 
23
 
 
24
#include "BackForwardListImpl.h"
 
25
#include "CairoUtilitiesEfl.h"
 
26
#include "HistoryItem.h"
 
27
#include "IconDatabaseBase.h"
 
28
#include "Image.h"
 
29
#include "IntSize.h"
 
30
#include "Page.h"
 
31
#include "PageGroup.h"
 
32
#include "ewk_history_private.h"
 
33
#include "ewk_private.h"
 
34
#include <Eina.h>
 
35
#include <eina_safety_checks.h>
 
36
#include <wtf/text/CString.h>
 
37
 
 
38
struct _Ewk_History {
 
39
    WebCore::BackForwardListImpl* core;
 
40
};
 
41
 
 
42
#define EWK_HISTORY_CORE_GET_OR_RETURN(history, core_, ...)      \
 
43
    if (!(history)) {                                            \
 
44
        CRITICAL("history is NULL.");                            \
 
45
        return __VA_ARGS__;                                      \
 
46
    }                                                            \
 
47
    if (!(history)->core) {                                      \
 
48
        CRITICAL("history->core is NULL.");                      \
 
49
        return __VA_ARGS__;                                      \
 
50
    }                                                            \
 
51
    if (!(history)->core->enabled()) {                           \
 
52
        ERR("history->core is disabled!.");                      \
 
53
        return __VA_ARGS__;                                      \
 
54
    }                                                            \
 
55
    WebCore::BackForwardListImpl* core_ = (history)->core
 
56
 
 
57
 
 
58
struct _Ewk_History_Item {
 
59
    WebCore::HistoryItem* core;
 
60
 
 
61
    const char* title;
 
62
    const char* alternateTitle;
 
63
    const char* uri;
 
64
    const char* originalUri;
 
65
};
 
66
 
 
67
#define EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core_, ...) \
 
68
    if (!(item)) {                                            \
 
69
        CRITICAL("item is NULL.");                            \
 
70
        return __VA_ARGS__;                                   \
 
71
    }                                                         \
 
72
    if (!(item)->core) {                                      \
 
73
        CRITICAL("item->core is NULL.");                      \
 
74
        return __VA_ARGS__;                                   \
 
75
    }                                                         \
 
76
    WebCore::HistoryItem* core_ = (item)->core
 
77
 
 
78
static inline Eina_List* _ewk_history_item_list_get(const WebCore::HistoryItemVector& coreItems)
 
79
{
 
80
    Eina_List* result = 0;
 
81
    unsigned int size;
 
82
 
 
83
    size = coreItems.size();
 
84
    for (unsigned int i = 0; i < size; i++) {
 
85
        Ewk_History_Item* item = ewk_history_item_new_from_core(coreItems[i].get());
 
86
        if (item)
 
87
            result = eina_list_append(result, item);
 
88
    }
 
89
 
 
90
    return result;
 
91
}
 
92
 
 
93
Eina_Bool ewk_history_clear(Ewk_History* history)
 
94
{
 
95
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, false);
 
96
 
 
97
    WebCore::Page* page = core->page();
 
98
    if (page && page->groupPtr())
 
99
        page->groupPtr()->removeVisitedLinks();
 
100
 
 
101
    const int limit = ewk_history_limit_get(history);
 
102
    ewk_history_limit_set(history, 0);
 
103
    ewk_history_limit_set(history, limit);
 
104
 
 
105
    return true;
 
106
}
 
107
 
 
108
Eina_Bool ewk_history_forward(Ewk_History* history)
 
109
{
 
110
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, false);
 
111
    if (core->forwardListCount() < 1)
 
112
        return false;
 
113
    core->goForward();
 
114
    return true;
 
115
}
 
116
 
 
117
Eina_Bool ewk_history_back(Ewk_History* history)
 
118
{
 
119
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, false);
 
120
    if (core->backListCount() < 1)
 
121
        return false;
 
122
    core->goBack();
 
123
    return true;
 
124
}
 
125
 
 
126
Eina_Bool ewk_history_history_item_add(Ewk_History* history, const Ewk_History_Item* item)
 
127
{
 
128
    EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, false);
 
129
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, false);
 
130
    history_core->addItem(item_core);
 
131
    return true;
 
132
}
 
133
 
 
134
Eina_Bool ewk_history_history_item_set(Ewk_History* history, const Ewk_History_Item* item)
 
135
{
 
136
    EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, false);
 
137
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, false);
 
138
    history_core->goToItem(item_core);
 
139
    return true;
 
140
}
 
141
 
 
142
Ewk_History_Item* ewk_history_history_item_back_get(const Ewk_History* history)
 
143
{
 
144
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
145
    return ewk_history_item_new_from_core(core->backItem());
 
146
}
 
147
 
 
148
Ewk_History_Item* ewk_history_history_item_current_get(const Ewk_History* history)
 
149
{
 
150
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
151
    WebCore::HistoryItem* currentItem = core->currentItem();
 
152
    if (currentItem)
 
153
        return ewk_history_item_new_from_core(currentItem);
 
154
    return 0;
 
155
}
 
156
 
 
157
Ewk_History_Item* ewk_history_history_item_forward_get(const Ewk_History* history)
 
158
{
 
159
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
160
    return ewk_history_item_new_from_core(core->forwardItem());
 
161
}
 
162
 
 
163
Ewk_History_Item* ewk_history_history_item_nth_get(const Ewk_History* history, int index)
 
164
{
 
165
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
166
    return ewk_history_item_new_from_core(core->itemAtIndex(index));
 
167
}
 
168
 
 
169
Eina_Bool ewk_history_history_item_contains(const Ewk_History* history, const Ewk_History_Item* item)
 
170
{
 
171
    EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, false);
 
172
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, false);
 
173
    return history_core->containsItem(item_core);
 
174
}
 
175
 
 
176
Eina_List* ewk_history_forward_list_get(const Ewk_History* history)
 
177
{
 
178
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
179
    WebCore::HistoryItemVector items;
 
180
    int limit = core->forwardListCount();
 
181
    core->forwardListWithLimit(limit, items);
 
182
    return _ewk_history_item_list_get(items);
 
183
}
 
184
 
 
185
Eina_List* ewk_history_forward_list_get_with_limit(const Ewk_History* history, int limit)
 
186
{
 
187
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
188
    WebCore::HistoryItemVector items;
 
189
    core->forwardListWithLimit(limit, items);
 
190
    return _ewk_history_item_list_get(items);
 
191
}
 
192
 
 
193
int ewk_history_forward_list_length(const Ewk_History* history)
 
194
{
 
195
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
196
    return core->forwardListCount();
 
197
}
 
198
 
 
199
Eina_List* ewk_history_back_list_get(const Ewk_History* history)
 
200
{
 
201
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
202
    WebCore::HistoryItemVector items;
 
203
    int limit = core->backListCount();
 
204
    core->backListWithLimit(limit, items);
 
205
    return _ewk_history_item_list_get(items);
 
206
}
 
207
 
 
208
Eina_List* ewk_history_back_list_get_with_limit(const Ewk_History* history, int limit)
 
209
{
 
210
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
211
    WebCore::HistoryItemVector items;
 
212
    core->backListWithLimit(limit, items);
 
213
    return _ewk_history_item_list_get(items);
 
214
}
 
215
 
 
216
int ewk_history_back_list_length(const Ewk_History* history)
 
217
{
 
218
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
219
    return core->backListCount();
 
220
}
 
221
 
 
222
int ewk_history_limit_get(Ewk_History* history)
 
223
{
 
224
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0);
 
225
    return core->capacity();
 
226
}
 
227
 
 
228
Eina_Bool ewk_history_limit_set(const Ewk_History* history, int limit)
 
229
{
 
230
    EWK_HISTORY_CORE_GET_OR_RETURN(history, core, false);
 
231
    core->setCapacity(limit);
 
232
    return true;
 
233
}
 
234
 
 
235
Ewk_History_Item* ewk_history_item_new_from_core(WebCore::HistoryItem* core)
 
236
{
 
237
    Ewk_History_Item* item;
 
238
 
 
239
    if (!core) {
 
240
        ERR("WebCore::HistoryItem is NULL.");
 
241
        return 0;
 
242
    }
 
243
 
 
244
    core->ref();
 
245
 
 
246
    item = new Ewk_History_Item;
 
247
    memset(item, 0, sizeof(*item));
 
248
    item->core = core;
 
249
 
 
250
    return item;
 
251
}
 
252
 
 
253
Ewk_History_Item* ewk_history_item_new(const char* uri, const char* title)
 
254
{
 
255
    WTF::String historyUri = WTF::String::fromUTF8(uri);
 
256
    WTF::String historyTitle = WTF::String::fromUTF8(title);
 
257
    WTF::RefPtr<WebCore::HistoryItem> core = WebCore::HistoryItem::create(historyUri, historyTitle, 0);
 
258
    Ewk_History_Item* item = ewk_history_item_new_from_core(core.release().leakRef());
 
259
    return item;
 
260
}
 
261
 
 
262
static inline void _ewk_history_item_free(Ewk_History_Item* item, WebCore::HistoryItem* core)
 
263
{
 
264
    core->deref();
 
265
    delete item;
 
266
}
 
267
 
 
268
void ewk_history_item_free(Ewk_History_Item* item)
 
269
{
 
270
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core);
 
271
    _ewk_history_item_free(item, core);
 
272
}
 
273
 
 
274
void ewk_history_item_list_free(Eina_List* history_items)
 
275
{
 
276
    void* deleteItem;
 
277
    EINA_LIST_FREE(history_items, deleteItem) {
 
278
        Ewk_History_Item* item = (Ewk_History_Item*)deleteItem;
 
279
        _ewk_history_item_free(item, item->core);
 
280
    }
 
281
}
 
282
 
 
283
const char* ewk_history_item_title_get(const Ewk_History_Item* item)
 
284
{
 
285
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
286
    // hide the following optimzation from outside
 
287
    Ewk_History_Item* historyItem = const_cast<Ewk_History_Item*>(item);
 
288
    eina_stringshare_replace(&historyItem->title, core->title().utf8().data());
 
289
    return historyItem->title;
 
290
}
 
291
 
 
292
const char* ewk_history_item_title_alternate_get(const Ewk_History_Item* item)
 
293
{
 
294
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
295
    // hide the following optimzation from outside
 
296
    Ewk_History_Item* historyItem = const_cast<Ewk_History_Item*>(item);
 
297
    eina_stringshare_replace(&historyItem->alternateTitle,
 
298
                             core->alternateTitle().utf8().data());
 
299
    return historyItem->alternateTitle;
 
300
}
 
301
 
 
302
void ewk_history_item_title_alternate_set(Ewk_History_Item* item, const char* title)
 
303
{
 
304
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core);
 
305
    if (!eina_stringshare_replace(&item->alternateTitle, title))
 
306
        return;
 
307
    core->setAlternateTitle(WTF::String::fromUTF8(title));
 
308
}
 
309
 
 
310
const char* ewk_history_item_uri_get(const Ewk_History_Item* item)
 
311
{
 
312
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
313
    // hide the following optimzation from outside
 
314
    Ewk_History_Item* historyItem = const_cast<Ewk_History_Item*>((item));
 
315
    eina_stringshare_replace(&historyItem->uri, core->urlString().utf8().data());
 
316
    return historyItem->uri;
 
317
}
 
318
 
 
319
const char* ewk_history_item_uri_original_get(const Ewk_History_Item* item)
 
320
{
 
321
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
322
    // hide the following optimzation from outside
 
323
    Ewk_History_Item* historyItem = const_cast<Ewk_History_Item*>(item);
 
324
    eina_stringshare_replace(&historyItem->originalUri,
 
325
                             core->originalURLString().utf8().data());
 
326
    return historyItem->originalUri;
 
327
}
 
328
 
 
329
double ewk_history_item_time_last_visited_get(const Ewk_History_Item* item)
 
330
{
 
331
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0.0);
 
332
    return core->lastVisitedTime();
 
333
}
 
334
 
 
335
cairo_surface_t* ewk_history_item_icon_surface_get(const Ewk_History_Item* item)
 
336
{
 
337
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
338
 
 
339
    WebCore::NativeImagePtr icon = WebCore::iconDatabase().synchronousNativeIconForPageURL(core->url(), WebCore::IntSize(16, 16));
 
340
    if (!icon)
 
341
        ERR("icon is NULL.");
 
342
 
 
343
    return icon ? icon->surface() : 0;
 
344
}
 
345
 
 
346
Evas_Object* ewk_history_item_icon_object_add(const Ewk_History_Item* item, Evas* canvas)
 
347
{
 
348
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
349
    EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0);
 
350
 
 
351
    WebCore::NativeImagePtr icon = WebCore::iconDatabase().synchronousNativeIconForPageURL(core->url(), WebCore::IntSize(16, 16));
 
352
    if (!icon) {
 
353
        ERR("icon is NULL.");
 
354
        return 0;
 
355
    }
 
356
 
 
357
    cairo_surface_t* surface = icon->surface();
 
358
    return surface ? WebCore::evasObjectFromCairoImageSurface(canvas, surface).leakRef() : 0;
 
359
}
 
360
 
 
361
Eina_Bool ewk_history_item_page_cache_exists(const Ewk_History_Item* item)
 
362
{
 
363
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, false);
 
364
    return core->isInPageCache();
 
365
}
 
366
 
 
367
int ewk_history_item_visit_count(const Ewk_History_Item* item)
 
368
{
 
369
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0);
 
370
    return core->visitCount();
 
371
}
 
372
 
 
373
Eina_Bool ewk_history_item_visit_last_failed(const Ewk_History_Item* item)
 
374
{
 
375
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, true);
 
376
    return core->lastVisitWasFailure();
 
377
}
 
378
 
 
379
 
 
380
/* internal methods ****************************************************/
 
381
/**
 
382
 * @internal
 
383
 *
 
384
 * Creates history for given view. Called internally by ewk_view and
 
385
 * should never be called from outside.
 
386
 *
 
387
 * @param core WebCore::BackForwardListImpl instance to use internally.
 
388
 *
 
389
 * @return newly allocated history instance or @c NULL on errors.
 
390
 */
 
391
Ewk_History* ewk_history_new(WebCore::BackForwardListImpl* core)
 
392
{
 
393
    Ewk_History* history;
 
394
    EINA_SAFETY_ON_NULL_RETURN_VAL(core, 0);
 
395
    DBG("core=%p", core);
 
396
 
 
397
    history = new Ewk_History;
 
398
    history->core = core;
 
399
    core->ref();
 
400
 
 
401
    return history;
 
402
}
 
403
 
 
404
/**
 
405
 * @internal
 
406
 *
 
407
 * Destroys previously allocated history instance. This is called
 
408
 * automatically by ewk_view and should never be called from outside.
 
409
 *
 
410
 * @param history instance to free
 
411
 */
 
412
void ewk_history_free(Ewk_History* history)
 
413
{
 
414
    DBG("history=%p", history);
 
415
    history->core->deref();
 
416
    delete history;
 
417
}
 
418
 
 
419
namespace EWKPrivate {
 
420
 
 
421
WebCore::HistoryItem* coreHistoryItem(const Ewk_History_Item* ewkHistoryItem)
 
422
{
 
423
    EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(ewkHistoryItem, core, 0);
 
424
    return core;
 
425
}
 
426
 
 
427
} // namespace EWKPrivate