Upstream version 7.36.151.0
[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         if (index < 0 || index >= size()) return null;
53         return new XWalkNavigationItem(mHistory.getEntryAtIndex(index));
54     }
55
56     /**
57      * Get the current item which XWalkView displays.
58      * @return the current navigation item.
59      */
60     public XWalkNavigationItem getCurrentItem() {
61         return getItemAt(getCurrentIndex());
62     }
63
64     /**
65      * Test whether XWalkView can go back.
66      * @return true if it can go back.
67      */
68     public boolean canGoBack() {
69         return mXWalkView.canGoBack();
70     }
71
72     /**
73      * Test whether XWalkView can go forward.
74      * @return true if it can go forward.
75      */
76     public boolean canGoForward() {
77         return mXWalkView.canGoForward();
78     }
79
80     /**
81      * The direction for web page navigation.
82      */
83     public enum Direction {
84         /** The backward direction for web page navigation. */
85         BACKWARD,
86         /** The forward direction for web page navigation. */
87         FORWARD
88     }
89
90     /**
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.
95      */
96     public void navigate(Direction direction, int steps) {
97         switch(direction) {
98             case FORWARD:
99                 mXWalkView.navigateTo(steps);
100                 break;
101             case BACKWARD:
102                 mXWalkView.navigateTo(-steps);
103                 break;
104             default:
105                 break;
106         }
107     }
108
109     /**
110      * Get the index for current navigation item.
111      * @return current index in the navigation history.
112      */
113     public int getCurrentIndex() {
114         return mHistory.getCurrentEntryIndex();
115     }
116
117     /**
118      * Clear all history owned by this XWalkView.
119      */
120     public void clear() {
121         mXWalkView.clearHistory();
122     }
123
124     protected synchronized XWalkNavigationHistory clone() {
125         return new XWalkNavigationHistory(this);
126     }
127 }