88022881cdacc9538b06119b266f151f6b176e96
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkNavigationHistory.java
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.
4
5 package org.xwalk.core;
6
7 import java.io.Serializable;
8
9 import org.chromium.content.browser.NavigationHistory;
10
11 /**
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.
14  */
15 public final class XWalkNavigationHistory implements Cloneable, Serializable {
16     private NavigationHistory mHistory;
17     private XWalkView mXWalkView;
18
19     XWalkNavigationHistory(XWalkView view, NavigationHistory history) {
20         mXWalkView = view;
21         mHistory = history;
22     }
23
24     XWalkNavigationHistory(XWalkNavigationHistory history) {
25         mXWalkView = history.mXWalkView;
26         mHistory = history.mHistory;
27     }
28
29     /**
30      * Total size of navigation history for the XWalkView.
31      * @return the size of total navigation items.
32      */
33     public int size() {
34         return mHistory.getEntryCount();
35     }
36
37     /**
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.
41      */
42     public boolean hasItemAt(int index) {
43         return index >=0 && index <= size() - 1;
44     }
45
46     /**
47      * Get a specific item given by index.
48      * @param index the given index.
49      * @return the navigation item for the given index.
50      */
51     public XWalkNavigationItem getItemAt(int index) {
52         return new XWalkNavigationItem(mHistory.getEntryAtIndex(index));
53     }
54
55     /**
56      * Get the current item which XWalkView displays.
57      * @return the current navigation item.
58      */
59     public XWalkNavigationItem getCurrentItem() {
60         return getItemAt(getCurrentIndex());
61     }
62
63     /**
64      * Test whether XWalkView can go back.
65      * @return true if it can go back.
66      */
67     public boolean canGoBack() {
68         return mXWalkView.canGoBack();
69     }
70
71     /**
72      * Test whether XWalkView can go forward.
73      * @return true if it can go forward.
74      */
75     public boolean canGoForward() {
76         return mXWalkView.canGoForward();
77     }
78
79     /**
80      * The direction for web page navigation.
81      */
82     public enum Direction {
83         /** The backward direction for web page navigation. */
84         BACKWARD,
85         /** The forward direction for web page navigation. */
86         FORWARD
87     }
88
89     /**
90      * Navigates to the specified step from the current navigation item.
91      * Do nothing if the offset is out of bound.
92      * @param direction the direction of navigation.
93      * @param steps go back or foward with a given steps.
94      */
95     public void navigate(Direction direction, int steps) {
96         switch(direction) {
97             case FORWARD:
98                 mXWalkView.navigateTo(steps);
99                 break;
100             case BACKWARD:
101                 mXWalkView.navigateTo(-steps);
102                 break;
103             default:
104                 break;
105         }
106     }
107
108     /**
109      * Get the index for current navigation item.
110      * @return current index in the navigation history.
111      */
112     public int getCurrentIndex() {
113         return mHistory.getCurrentEntryIndex();
114     }
115
116     /**
117      * Clear all history owned by this XWalkView.
118      */
119     public void clear() {
120         mXWalkView.clearHistory();
121     }
122
123     protected synchronized XWalkNavigationHistory clone() {
124         return new XWalkNavigationHistory(this);
125     }
126 }