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.internal;
7 import java.io.Serializable;
9 import org.chromium.content.browser.NavigationHistory;
12 * This class represents a navigation history for a XWalkViewInternal instance.
13 * It's not thread-safe and should be only called on UI thread.
15 @XWalkAPI(createInternally = true)
16 public class XWalkNavigationHistoryInternal implements Cloneable, Serializable {
17 private NavigationHistory mHistory;
18 private XWalkViewInternal mXWalkView;
20 // Never use this constructor.
21 // It is only used in XWalkNavigationHistoryBridge.
22 XWalkNavigationHistoryInternal() {
27 XWalkNavigationHistoryInternal(XWalkViewInternal view, NavigationHistory history) {
32 XWalkNavigationHistoryInternal(XWalkNavigationHistoryInternal history) {
33 mXWalkView = history.mXWalkView;
34 mHistory = history.mHistory;
38 * Total size of navigation history for the XWalkViewInternal.
39 * @return the size of total navigation items.
44 return mHistory.getEntryCount();
48 * Test whether there is an item at a specific index.
49 * @param index the given index.
50 * @return true if there is an item at the specific index.
54 public boolean hasItemAt(int index) {
55 return index >=0 && index <= size() - 1;
59 * Get a specific item given by index.
60 * @param index the given index.
61 * @return the navigation item for the given index.
65 public XWalkNavigationItemInternal getItemAt(int index) {
66 if (index < 0 || index >= size()) return null;
67 return new XWalkNavigationItemInternal(mHistory.getEntryAtIndex(index));
71 * Get the current item which XWalkViewInternal displays.
72 * @return the current navigation item.
76 public XWalkNavigationItemInternal getCurrentItem() {
77 return getItemAt(getCurrentIndex());
81 * Test whether XWalkViewInternal can go back.
82 * @return true if it can go back.
86 public boolean canGoBack() {
87 return mXWalkView.canGoBack();
91 * Test whether XWalkViewInternal can go forward.
92 * @return true if it can go forward.
96 public boolean canGoForward() {
97 return mXWalkView.canGoForward();
101 * The direction for web page navigation.
105 public enum DirectionInternal {
106 /** The backward direction for web page navigation. */
108 /** The forward direction for web page navigation. */
113 * Navigates to the specified step from the current navigation item.
114 * Do nothing if the offset is out of bound.
115 * @param direction the direction of navigation.
116 * @param steps go back or foward with a given steps.
120 public void navigate(DirectionInternal direction, int steps) {
123 mXWalkView.navigateTo(steps);
126 mXWalkView.navigateTo(-steps);
134 * Get the index for current navigation item.
135 * @return current index in the navigation history.
139 public int getCurrentIndex() {
140 return mHistory.getCurrentEntryIndex();
144 * Clear all history owned by this XWalkViewInternal.
148 public void clear() {
149 mXWalkView.clearHistory();
152 protected synchronized XWalkNavigationHistoryInternal clone() {
153 return new XWalkNavigationHistoryInternal(this);