1 // Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import java.io.Serializable;
9 import org.chromium.content.browser.NavigationHistory;
12 * This class represents a navigation history for a XWalkView instance.
13 * It's not thread-safe and should be only called on UI thread.
15 public final class XWalkNavigationHistory implements Cloneable, Serializable {
16 private NavigationHistory mHistory;
17 private XWalkView mXWalkView;
19 XWalkNavigationHistory(XWalkView view, NavigationHistory history) {
24 XWalkNavigationHistory(XWalkNavigationHistory history) {
25 mXWalkView = history.mXWalkView;
26 mHistory = history.mHistory;
30 * Total size of navigation history for the XWalkView.
31 * @return the size of total navigation items.
34 return mHistory.getEntryCount();
38 * Test whether there is an item at a specific index.
39 * @param index the given index.
40 * @return true if there is an item at the specific index.
42 public boolean hasItemAt(int index) {
43 return index >=0 && index <= size() - 1;
47 * Get a specific item given by index.
48 * @param index the given index.
49 * @return the navigation item for the given index.
51 public XWalkNavigationItem getItemAt(int index) {
52 if (index < 0 || index >= size()) return null;
53 return new XWalkNavigationItem(mHistory.getEntryAtIndex(index));
57 * Get the current item which XWalkView displays.
58 * @return the current navigation item.
60 public XWalkNavigationItem getCurrentItem() {
61 return getItemAt(getCurrentIndex());
65 * Test whether XWalkView can go back.
66 * @return true if it can go back.
68 public boolean canGoBack() {
69 return mXWalkView.canGoBack();
73 * Test whether XWalkView can go forward.
74 * @return true if it can go forward.
76 public boolean canGoForward() {
77 return mXWalkView.canGoForward();
81 * The direction for web page navigation.
83 public enum Direction {
84 /** The backward direction for web page navigation. */
86 /** The forward direction for web page navigation. */
91 * Navigates to the specified step from the current navigation item.
92 * Do nothing if the offset is out of bound.
93 * @param direction the direction of navigation.
94 * @param steps go back or foward with a given steps.
96 public void navigate(Direction direction, int steps) {
99 mXWalkView.navigateTo(steps);
102 mXWalkView.navigateTo(-steps);
110 * Get the index for current navigation item.
111 * @return current index in the navigation history.
113 public int getCurrentIndex() {
114 return mHistory.getCurrentEntryIndex();
118 * Clear all history owned by this XWalkView.
120 public void clear() {
121 mXWalkView.clearHistory();
124 protected synchronized XWalkNavigationHistory clone() {
125 return new XWalkNavigationHistory(this);