Upstream version 8.36.161.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 public class XWalkNavigationHistoryInternal implements Cloneable, Serializable {
16     private NavigationHistory mHistory;
17     private XWalkViewInternal mXWalkView;
18
19     XWalkNavigationHistoryInternal(XWalkViewInternal view, NavigationHistory history) {
20         mXWalkView = view;
21         mHistory = history;
22     }
23
24     public XWalkNavigationHistoryInternal(XWalkNavigationHistoryInternal history) {
25         mXWalkView = history.mXWalkView;
26         mHistory = history.mHistory;
27     }
28
29     /**
30      * Total size of navigation history for the XWalkViewInternal.
31      * @return the size of total navigation items.
32      * @since 1.0
33      */
34     public int size() {
35         return mHistory.getEntryCount();
36     }
37
38     /**
39      * Test whether there is an item at a specific index.
40      * @param index the given index.
41      * @return true if there is an item at the specific index.
42      * @since 1.0
43      */
44     public boolean hasItemAt(int index) {
45         return index >=0 && index <= size() - 1;
46     }
47
48     /**
49      * Get a specific item given by index.
50      * @param index the given index.
51      * @return the navigation item for the given index.
52      * @since 1.0
53      */
54     public XWalkNavigationItemInternal getItemAt(int index) {
55         if (index < 0 || index >= size()) return null;
56         return new XWalkNavigationItemInternal(mHistory.getEntryAtIndex(index));
57     }
58
59     /**
60      * Get the current item which XWalkViewInternal displays.
61      * @return the current navigation item.
62      * @since 1.0
63      */
64     public XWalkNavigationItemInternal getCurrentItem() {
65         return getItemAt(getCurrentIndex());
66     }
67
68     /**
69      * Test whether XWalkViewInternal can go back.
70      * @return true if it can go back.
71      * @since 1.0
72      */
73     public boolean canGoBack() {
74         return mXWalkView.canGoBack();
75     }
76
77     /**
78      * Test whether XWalkViewInternal can go forward.
79      * @return true if it can go forward.
80      * @since 1.0
81      */
82     public boolean canGoForward() {
83         return mXWalkView.canGoForward();
84     }
85
86     /**
87      * The direction for web page navigation.
88      * @since 1.0
89      */
90     public enum DirectionInternal {
91         /** The backward direction for web page navigation. */
92         BACKWARD,
93         /** The forward direction for web page navigation. */
94         FORWARD
95     }
96
97     /**
98      * Navigates to the specified step from the current navigation item.
99      * Do nothing if the offset is out of bound.
100      * @param direction the direction of navigation.
101      * @param steps go back or foward with a given steps.
102      * @since 1.0
103      */
104     public void navigate(DirectionInternal direction, int steps) {
105         switch(direction) {
106             case FORWARD:
107                 mXWalkView.navigateTo(steps);
108                 break;
109             case BACKWARD:
110                 mXWalkView.navigateTo(-steps);
111                 break;
112             default:
113                 break;
114         }
115     }
116
117     /**
118      * Get the index for current navigation item.
119      * @return current index in the navigation history.
120      * @since 1.0
121      */
122     public int getCurrentIndex() {
123         return mHistory.getCurrentEntryIndex();
124     }
125
126     /**
127      * Clear all history owned by this XWalkViewInternal.
128      * @since 1.0
129      */
130     public void clear() {
131         mXWalkView.clearHistory();
132     }
133
134     protected synchronized XWalkNavigationHistoryInternal clone() {
135         return new XWalkNavigationHistoryInternal(this);
136     }
137 }