Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkNavigationHistoryInternal.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.internal;
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 XWalkViewInternal instance.
13  * It's not thread-safe and should be only called on UI thread.
14  */
15 @XWalkAPI(createInternally = true)
16 public class XWalkNavigationHistoryInternal implements Cloneable, Serializable {
17     private NavigationHistory mHistory;
18     private XWalkViewInternal mXWalkView;
19
20     // Never use this constructor.
21     // It is only used in XWalkNavigationHistoryBridge.
22     XWalkNavigationHistoryInternal() {
23         mXWalkView = null;
24         mHistory = null;
25     }
26
27     XWalkNavigationHistoryInternal(XWalkViewInternal view, NavigationHistory history) {
28         mXWalkView = view;
29         mHistory = history;
30     }
31
32     XWalkNavigationHistoryInternal(XWalkNavigationHistoryInternal history) {
33         mXWalkView = history.mXWalkView;
34         mHistory = history.mHistory;
35     }
36
37     /**
38      * Total size of navigation history for the XWalkViewInternal.
39      * @return the size of total navigation items.
40      * @since 1.0
41      */
42     @XWalkAPI
43     public int size() {
44         return mHistory.getEntryCount();
45     }
46
47     /**
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.
51      * @since 1.0
52      */
53     @XWalkAPI
54     public boolean hasItemAt(int index) {
55         return index >=0 && index <= size() - 1;
56     }
57
58     /**
59      * Get a specific item given by index.
60      * @param index the given index.
61      * @return the navigation item for the given index.
62      * @since 1.0
63      */
64     @XWalkAPI
65     public XWalkNavigationItemInternal getItemAt(int index) {
66         if (index < 0 || index >= size()) return null;
67         return new XWalkNavigationItemInternal(mHistory.getEntryAtIndex(index));
68     }
69
70     /**
71      * Get the current item which XWalkViewInternal displays.
72      * @return the current navigation item.
73      * @since 1.0
74      */
75     @XWalkAPI
76     public XWalkNavigationItemInternal getCurrentItem() {
77         return getItemAt(getCurrentIndex());
78     }
79
80     /**
81      * Test whether XWalkViewInternal can go back.
82      * @return true if it can go back.
83      * @since 1.0
84      */
85     @XWalkAPI
86     public boolean canGoBack() {
87         return mXWalkView.canGoBack();
88     }
89
90     /**
91      * Test whether XWalkViewInternal can go forward.
92      * @return true if it can go forward.
93      * @since 1.0
94      */
95     @XWalkAPI
96     public boolean canGoForward() {
97         return mXWalkView.canGoForward();
98     }
99
100     /**
101      * The direction for web page navigation.
102      * @since 1.0
103      */
104     @XWalkAPI
105     public enum DirectionInternal {
106         /** The backward direction for web page navigation. */
107         BACKWARD,
108         /** The forward direction for web page navigation. */
109         FORWARD
110     }
111
112     /**
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.
117      * @since 1.0
118      */
119     @XWalkAPI
120     public void navigate(DirectionInternal direction, int steps) {
121         switch(direction) {
122             case FORWARD:
123                 mXWalkView.navigateTo(steps);
124                 break;
125             case BACKWARD:
126                 mXWalkView.navigateTo(-steps);
127                 break;
128             default:
129                 break;
130         }
131     }
132
133     /**
134      * Get the index for current navigation item.
135      * @return current index in the navigation history.
136      * @since 1.0
137      */
138     @XWalkAPI
139     public int getCurrentIndex() {
140         return mHistory.getCurrentEntryIndex();
141     }
142
143     /**
144      * Clear all history owned by this XWalkViewInternal.
145      * @since 1.0
146      */
147     @XWalkAPI
148     public void clear() {
149         mXWalkView.clearHistory();
150     }
151
152     protected synchronized XWalkNavigationHistoryInternal clone() {
153         return new XWalkNavigationHistoryInternal(this);
154     }
155 }