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 "EWebKit.h" 26 #include "HistoryItem.h" 27 #include "IconDatabaseBase.h" 28 #include "Image.h" 29 #include "IntSize.h" 30 #include "ewk_private.h" 31 #include <wtf/text/CString.h> 32 33 #include <Eina.h> 34 #include <eina_safety_checks.h> 35 36 struct _Ewk_History { 37 WebCore::BackForwardListImpl *core; 38 }; 39 40 #define EWK_HISTORY_CORE_GET_OR_RETURN(history, core_, ...) \ 41 if (!(history)) { \ 42 CRITICAL("history is NULL."); \ 43 return __VA_ARGS__; \ 44 } \ 45 if (!(history)->core) { \ 46 CRITICAL("history->core is NULL."); \ 47 return __VA_ARGS__; \ 48 } \ 49 if (!(history)->core->enabled()) { \ 50 ERR("history->core is disabled!."); \ 51 return __VA_ARGS__; \ 52 } \ 53 WebCore::BackForwardListImpl *core_ = (history)->core 54 55 56 struct _Ewk_History_Item { 57 WebCore::HistoryItem *core; 58 59 const char *title; 60 const char *alternate_title; 61 const char *uri; 62 const char *original_uri; 63 }; 64 65 #define EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core_, ...) \ 66 if (!(item)) { \ 67 CRITICAL("item is NULL."); \ 68 return __VA_ARGS__; \ 69 } \ 70 if (!(item)->core) { \ 71 CRITICAL("item->core is NULL."); \ 72 return __VA_ARGS__; \ 73 } \ 74 WebCore::HistoryItem *core_ = (item)->core 75 76 77 static inline Ewk_History_Item *_ewk_history_item_new(WebCore::HistoryItem *core) 78 { 79 Ewk_History_Item* item; 80 81 if (!core) { 82 ERR("WebCore::HistoryItem is NULL."); 83 return 0; 84 } 85 86 item = (Ewk_History_Item *)calloc(1, sizeof(Ewk_History_Item)); 87 if (!item) { 88 CRITICAL("Could not allocate item memory."); 89 return 0; 90 } 91 92 core->ref(); 93 item->core = core; 94 95 return item; 96 } 97 98 static inline Eina_List *_ewk_history_item_list_get(const WebCore::HistoryItemVector &core_items) 99 { 100 Eina_List* ret = 0; 101 unsigned int i, size; 102 103 size = core_items.size(); 104 for (i = 0; i < size; i++) { 105 Ewk_History_Item* item = _ewk_history_item_new(core_items[i].get()); 106 if (item) 107 ret = eina_list_append(ret, item); 108 } 109 110 return ret; 111 } 112 113 /** 114 * Go forward in history one item, if possible. 115 * 116 * @param history which history instance to modify. 117 * 118 * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. 119 */ 120 Eina_Bool ewk_history_forward(Ewk_History* history) 121 { 122 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, EINA_FALSE); 123 if (core->forwardListCount() < 1) 124 return EINA_FALSE; 125 core->goForward(); 126 return EINA_TRUE; 127 } 128 129 /** 130 * Go back in history one item, if possible. 131 * 132 * @param history which history instance to modify. 133 * 134 * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. 135 */ 136 Eina_Bool ewk_history_back(Ewk_History* history) 137 { 138 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, EINA_FALSE); 139 if (core->backListCount() < 1) 140 return EINA_FALSE; 141 core->goBack(); 142 return EINA_TRUE; 143 } 144 145 /** 146 * Adds the given item to history. 147 * 148 * Memory handling: This will not modify or even take references to 149 * given item (Ewk_History_Item), so you should still handle it with 150 * ewk_history_item_free(). 151 * 152 * @param history which history instance to modify. 153 * @param item reference to add to history. Unmodified. Must @b not be @c NULL. 154 * 155 * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. 156 */ 157 Eina_Bool ewk_history_history_item_add(Ewk_History* history, const Ewk_History_Item* item) 158 { 159 EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, EINA_FALSE); 160 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, EINA_FALSE); 161 history_core->addItem(item_core); 162 return EINA_TRUE; 163 } 164 165 /** 166 * Sets the given item as current in history (go to item). 167 * 168 * Memory handling: This will not modify or even take references to 169 * given item (Ewk_History_Item), so you should still handle it with 170 * ewk_history_item_free(). 171 * 172 * @param history which history instance to modify. 173 * @param item reference to go to history. Unmodified. Must @b not be @c NULL. 174 * 175 * @return @c EINA_TRUE on success, @c EINA_FALSE on failure. 176 */ 177 Eina_Bool ewk_history_history_item_set(Ewk_History* history, const Ewk_History_Item* item) 178 { 179 EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, EINA_FALSE); 180 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, EINA_FALSE); 181 history_core->goToItem(item_core); 182 return EINA_TRUE; 183 } 184 185 /** 186 * Get the first item from back list, if any. 187 * 188 * @param history which history instance to query. 189 * 190 * @return the @b newly allocated item instance. This memory must be 191 * released with ewk_history_item_free() after use. 192 */ 193 Ewk_History_Item* ewk_history_history_item_back_get(const Ewk_History* history) 194 { 195 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 196 return _ewk_history_item_new(core->backItem()); 197 } 198 199 /** 200 * Get the current item in history, if any. 201 * 202 * @param history which history instance to query. 203 * 204 * @return the @b newly allocated item instance. This memory must be 205 * released with ewk_history_item_free() after use. 206 */ 207 Ewk_History_Item* ewk_history_history_item_current_get(const Ewk_History* history) 208 { 209 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 210 return _ewk_history_item_new(core->currentItem()); 211 } 212 213 /** 214 * Get the first item from forward list, if any. 215 * 216 * @param history which history instance to query. 217 * 218 * @return the @b newly allocated item instance. This memory must be 219 * released with ewk_history_item_free() after use. 220 */ 221 Ewk_History_Item* ewk_history_history_item_forward_get(const Ewk_History* history) 222 { 223 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 224 return _ewk_history_item_new(core->forwardItem()); 225 } 226 227 /** 228 * Get item at given position, if any at that index. 229 * 230 * @param history which history instance to query. 231 * @param index position of item to get. 232 * 233 * @return the @b newly allocated item instance. This memory must be 234 * released with ewk_history_item_free() after use. 235 */ 236 Ewk_History_Item* ewk_history_history_item_nth_get(const Ewk_History* history, int index) 237 { 238 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 239 return _ewk_history_item_new(core->itemAtIndex(index)); 240 } 241 242 /** 243 * Queries if given item is in history. 244 * 245 * Memory handling: This will not modify or even take references to 246 * given item (Ewk_History_Item), so you should still handle it with 247 * ewk_history_item_free(). 248 * 249 * @param history which history instance to modify. 250 * @param item reference to check in history. Must @b not be @c NULL. 251 * 252 * @return @c EINA_TRUE if in history, @c EINA_FALSE if not or failure. 253 */ 254 Eina_Bool ewk_history_history_item_contains(const Ewk_History* history, const Ewk_History_Item* item) 255 { 256 EWK_HISTORY_CORE_GET_OR_RETURN(history, history_core, EINA_FALSE); 257 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, item_core, EINA_FALSE); 258 return history_core->containsItem(item_core); 259 } 260 261 /** 262 * Get the whole forward list. 263 * 264 * @param history which history instance to query. 265 * 266 * @return a newly allocated list of @b newly allocated item 267 * instance. This memory of each item must be released with 268 * ewk_history_item_free() after use. use 269 * ewk_history_item_list_free() for convenience. 270 * 271 * @see ewk_history_item_list_free() 272 * @see ewk_history_forward_list_get_with_limit() 273 */ 274 Eina_List* ewk_history_forward_list_get(const Ewk_History* history) 275 { 276 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 277 WebCore::HistoryItemVector items; 278 int limit = core->forwardListCount(); 279 core->forwardListWithLimit(limit, items); 280 return _ewk_history_item_list_get(items); 281 } 282 283 /** 284 * Get the forward list within the given limit. 285 * 286 * @param history which history instance to query. 287 * @param limit the maximum number of items to return. 288 * 289 * @return a newly allocated list of @b newly allocated item 290 * instance. This memory of each item must be released with 291 * ewk_history_item_free() after use. use 292 * ewk_history_item_list_free() for convenience. 293 * 294 * @see ewk_history_item_list_free() 295 * @see ewk_history_forward_list_length() 296 * @see ewk_history_forward_list_get() 297 */ 298 Eina_List* ewk_history_forward_list_get_with_limit(const Ewk_History* history, int limit) 299 { 300 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 301 WebCore::HistoryItemVector items; 302 core->forwardListWithLimit(limit, items); 303 return _ewk_history_item_list_get(items); 304 } 305 306 /** 307 * Get the whole size of forward list. 308 * 309 * @param history which history instance to query. 310 * 311 * @return number of elements in whole list. 312 * 313 * @see ewk_history_forward_list_get_with_limit() 314 */ 315 int ewk_history_forward_list_length(const Ewk_History* history) 316 { 317 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 318 return core->forwardListCount(); 319 } 320 321 /** 322 * Get the whole back list. 323 * 324 * @param history which history instance to query. 325 * 326 * @return a newly allocated list of @b newly allocated item 327 * instance. This memory of each item must be released with 328 * ewk_history_item_free() after use. use 329 * ewk_history_item_list_free() for convenience. 330 * 331 * @see ewk_history_item_list_free() 332 * @see ewk_history_back_list_get_with_limit() 333 */ 334 Eina_List* ewk_history_back_list_get(const Ewk_History* history) 335 { 336 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 337 WebCore::HistoryItemVector items; 338 int limit = core->backListCount(); 339 core->backListWithLimit(limit, items); 340 return _ewk_history_item_list_get(items); 341 } 342 343 /** 344 * Get the back list within the given limit. 345 * 346 * @param history which history instance to query. 347 * @param limit the maximum number of items to return. 348 * 349 * @return a newly allocated list of @b newly allocated item 350 * instance. This memory of each item must be released with 351 * ewk_history_item_free() after use. use 352 * ewk_history_item_list_free() for convenience. 353 * 354 * @see ewk_history_item_list_free() 355 * @see ewk_history_back_list_length() 356 * @see ewk_history_back_list_get() 357 */ 358 Eina_List* ewk_history_back_list_get_with_limit(const Ewk_History* history, int limit) 359 { 360 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 361 WebCore::HistoryItemVector items; 362 core->backListWithLimit(limit, items); 363 return _ewk_history_item_list_get(items); 364 } 365 366 /** 367 * Get the whole size of back list. 368 * 369 * @param history which history instance to query. 370 * 371 * @return number of elements in whole list. 372 * 373 * @see ewk_history_back_list_get_with_limit() 374 */ 375 int ewk_history_back_list_length(const Ewk_History* history) 376 { 377 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 378 return core->backListCount(); 379 } 380 381 /** 382 * Get maximum capacity of given history. 383 * 384 * @param history which history instance to query. 385 * 386 * @return maximum number of entries this history will hold. 387 */ 388 int ewk_history_limit_get(Ewk_History* history) 389 { 390 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, 0); 391 return core->capacity(); 392 } 393 394 /** 395 * Set maximum capacity of given history. 396 * 397 * @param history which history instance to modify. 398 * @param limit maximum size to allow. 399 * 400 * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise. 401 */ 402 Eina_Bool ewk_history_limit_set(const Ewk_History* history, int limit) 403 { 404 EWK_HISTORY_CORE_GET_OR_RETURN(history, core, EINA_FALSE); 405 core->setCapacity(limit); 406 return EINA_TRUE; 407 } 408 409 /** 410 * Create a new history item with given URI and title. 411 * 412 * @param uri where this resource is located. 413 * @param title resource title. 414 * 415 * @return newly allocated history item or @c NULL on errors. You must 416 * free this item with ewk_history_item_free(). 417 */ 418 Ewk_History_Item* ewk_history_item_new(const char* uri, const char* title) 419 { 420 WTF::String u = WTF::String::fromUTF8(uri); 421 WTF::String t = WTF::String::fromUTF8(title); 422 WTF::RefPtr<WebCore::HistoryItem> core = WebCore::HistoryItem::create(u, t, 0); 423 Ewk_History_Item* item = _ewk_history_item_new(core.release().releaseRef()); 424 return item; 425 } 426 427 static inline void _ewk_history_item_free(Ewk_History_Item* item, WebCore::HistoryItem* core) 428 { 429 core->deref(); 430 free(item); 431 } 432 433 /** 434 * Free given history item instance. 435 * 436 * @param item what to free. 437 */ 438 void ewk_history_item_free(Ewk_History_Item* item) 439 { 440 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core); 441 _ewk_history_item_free(item, core); 442 } 443 444 /** 445 * Free given list and associated history items instances. 446 * 447 * @param history_items list of items to free (both list nodes and 448 * item instances). 449 */ 450 void ewk_history_item_list_free(Eina_List* history_items) 451 { 452 void* d; 453 EINA_LIST_FREE(history_items, d) { 454 Ewk_History_Item* item = (Ewk_History_Item*)d; 455 _ewk_history_item_free(item, item->core); 456 } 457 } 458 459 /** 460 * Query title for given history item. 461 * 462 * @param item history item to query. 463 * 464 * @return the title pointer, that may be @c NULL. This pointer is 465 * guaranteed to be eina_stringshare, so whenever possible 466 * save yourself some cpu cycles and use 467 * eina_stringshare_ref() instead of eina_stringshare_add() or 468 * strdup(). 469 */ 470 const char* ewk_history_item_title_get(const Ewk_History_Item* item) 471 { 472 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 473 // hide the following optimzation from outside 474 Ewk_History_Item* i = (Ewk_History_Item*)item; 475 eina_stringshare_replace(&i->title, core->title().utf8().data()); 476 return i->title; 477 } 478 479 /** 480 * Query alternate title for given history item. 481 * 482 * @param item history item to query. 483 * 484 * @return the alternate title pointer, that may be @c NULL. This 485 * pointer is guaranteed to be eina_stringshare, so whenever 486 * possible save yourself some cpu cycles and use 487 * eina_stringshare_ref() instead of eina_stringshare_add() or 488 * strdup(). 489 */ 490 const char* ewk_history_item_title_alternate_get(const Ewk_History_Item* item) 491 { 492 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 493 // hide the following optimzation from outside 494 Ewk_History_Item* i = (Ewk_History_Item*)item; 495 eina_stringshare_replace(&i->alternate_title, 496 core->alternateTitle().utf8().data()); 497 return i->alternate_title; 498 } 499 500 /** 501 * Set alternate title for given history item. 502 * 503 * @param item history item to query. 504 * @param title new alternate title to use for given item. No 505 * references are kept after this function returns. 506 */ 507 void ewk_history_item_title_alternate_set(Ewk_History_Item* item, const char* title) 508 { 509 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core); 510 if (!eina_stringshare_replace(&item->alternate_title, title)) 511 return; 512 core->setAlternateTitle(WTF::String::fromUTF8(title)); 513 } 514 515 /** 516 * Query URI for given history item. 517 * 518 * @param item history item to query. 519 * 520 * @return the URI pointer, that may be @c NULL. This pointer is 521 * guaranteed to be eina_stringshare, so whenever possible 522 * save yourself some cpu cycles and use 523 * eina_stringshare_ref() instead of eina_stringshare_add() or 524 * strdup(). 525 */ 526 const char* ewk_history_item_uri_get(const Ewk_History_Item* item) 527 { 528 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 529 // hide the following optimzation from outside 530 Ewk_History_Item* i = (Ewk_History_Item*)item; 531 eina_stringshare_replace(&i->uri, core->urlString().utf8().data()); 532 return i->uri; 533 } 534 535 /** 536 * Query original URI for given history item. 537 * 538 * @param item history item to query. 539 * 540 * @return the original URI pointer, that may be @c NULL. This pointer 541 * is guaranteed to be eina_stringshare, so whenever possible 542 * save yourself some cpu cycles and use 543 * eina_stringshare_ref() instead of eina_stringshare_add() or 544 * strdup(). 545 */ 546 const char* ewk_history_item_uri_original_get(const Ewk_History_Item* item) 547 { 548 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 549 // hide the following optimzation from outside 550 Ewk_History_Item* i = (Ewk_History_Item*)item; 551 eina_stringshare_replace(&i->original_uri, 552 core->originalURLString().utf8().data()); 553 return i->original_uri; 554 } 555 556 /** 557 * Query last visited time for given history item. 558 * 559 * @param item history item to query. 560 * 561 * @return the time in seconds this item was visited. 562 */ 563 double ewk_history_item_time_last_visited_get(const Ewk_History_Item* item) 564 { 565 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0.0); 566 return core->lastVisitedTime(); 567 } 568 569 /** 570 * Get the icon (aka favicon) associated with this history item. 571 * 572 * @note in order to have this working, one must open icon database 573 * with ewk_settings_icon_database_path_set(). 574 * 575 * @param item history item to query. 576 * 577 * @return the surface reference or @c NULL on errors. Note that the 578 * reference may be to a standard fallback icon. 579 */ 580 cairo_surface_t* ewk_history_item_icon_surface_get(const Ewk_History_Item* item) 581 { 582 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 583 584 WebCore::Image* icon = WebCore::iconDatabase().synchronousIconForPageURL(core->url(), WebCore::IntSize(16, 16)); 585 if (!icon) { 586 ERR("icon is NULL."); 587 return 0; 588 } 589 return icon->nativeImageForCurrentFrame(); 590 } 591 592 /** 593 * Add an Evas_Object of type 'image' to given canvas with history item icon. 594 * 595 * This is an utility function that creates an Evas_Object of type 596 * image set to have fill always match object size 597 * (evas_object_image_filled_add()), saving some code to use it from Evas. 598 * 599 * @note in order to have this working, one must open icon database 600 * with ewk_settings_icon_database_path_set(). 601 * 602 * @param item history item to query. 603 * @param canvas evas instance where to add resulting object. 604 * 605 * @return newly allocated Evas_Object instance or @c NULL on 606 * errors. Delete the object with evas_object_del(). 607 */ 608 Evas_Object* ewk_history_item_icon_object_add(const Ewk_History_Item* item, Evas* canvas) 609 { 610 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 611 EINA_SAFETY_ON_NULL_RETURN_VAL(canvas, 0); 612 WebCore::Image* icon = WebCore::iconDatabase().synchronousIconForPageURL(core->url(), WebCore::IntSize(16, 16)); 613 cairo_surface_t* surface; 614 615 if (!icon) { 616 ERR("icon is NULL."); 617 return 0; 618 } 619 620 surface = icon->nativeImageForCurrentFrame(); 621 return ewk_util_image_from_cairo_surface_add(canvas, surface); 622 } 623 624 /** 625 * Query if given item is still in page cache. 626 * 627 * @param item history item to query. 628 * 629 * @return @c EINA_TRUE if in cache, @c EINA_FALSE otherwise. 630 */ 631 Eina_Bool ewk_history_item_page_cache_exists(const Ewk_History_Item* item) 632 { 633 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, EINA_FALSE); 634 return core->isInPageCache(); 635 } 636 637 /** 638 * Query number of times item was visited. 639 * 640 * @param item history item to query. 641 * 642 * @return number of visits. 643 */ 644 int ewk_history_item_visit_count(const Ewk_History_Item* item) 645 { 646 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, 0); 647 return core->visitCount(); 648 } 649 650 /** 651 * Query if last visit to item was failure or not. 652 * 653 * @param item history item to query. 654 * 655 * @return @c EINA_TRUE if last visit was failure, @c EINA_FALSE if it 656 * was fine. 657 */ 658 Eina_Bool ewk_history_item_visit_last_failed(const Ewk_History_Item* item) 659 { 660 EWK_HISTORY_ITEM_CORE_GET_OR_RETURN(item, core, EINA_TRUE); 661 return core->lastVisitWasFailure(); 662 } 663 664 665 /* internal methods ****************************************************/ 666 /** 667 * @internal 668 * 669 * Creates history for given view. Called internally by ewk_view and 670 * should never be called from outside. 671 * 672 * @param core WebCore::BackForwardListImpl instance to use internally. 673 * 674 * @return newly allocated history instance or @c NULL on errors. 675 */ 676 Ewk_History* ewk_history_new(WebCore::BackForwardListImpl* core) 677 { 678 Ewk_History* history; 679 EINA_SAFETY_ON_NULL_RETURN_VAL(core, 0); 680 DBG("core=%p", core); 681 682 history = (Ewk_History*)malloc(sizeof(Ewk_History)); 683 if (!history) { 684 CRITICAL("Could not allocate history memory."); 685 return 0; 686 } 687 688 core->ref(); 689 history->core = core; 690 691 return history; 692 } 693 694 /** 695 * @internal 696 * 697 * Destroys previously allocated history instance. This is called 698 * automatically by ewk_view and should never be called from outside. 699 * 700 * @param history instance to free 701 */ 702 void ewk_history_free(Ewk_History* history) 703 { 704 DBG("history=%p", history); 705 history->core->deref(); 706 free(history); 707 } 708