1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.webview.chromium;
18
19import org.chromium.content_public.browser.NavigationHistory;
20
21import android.webkit.WebBackForwardList;
22import android.webkit.WebHistoryItem;
23
24import java.util.ArrayList;
25import java.util.List;
26
27/**
28 * WebView Chromium implementation of WebBackForwardList. Simple immutable
29 * wrapper around NavigationHistory.
30 */
31public class WebBackForwardListChromium extends WebBackForwardList {
32    private final List<WebHistoryItemChromium> mHistroryItemList;
33    private final int mCurrentIndex;
34
35    /* package */ WebBackForwardListChromium(NavigationHistory nav_history) {
36        mCurrentIndex = nav_history.getCurrentEntryIndex();
37        mHistroryItemList = new ArrayList<WebHistoryItemChromium>(nav_history.getEntryCount());
38        for (int i = 0; i < nav_history.getEntryCount(); ++i) {
39            mHistroryItemList.add(
40                    new WebHistoryItemChromium(nav_history.getEntryAtIndex(i)));
41        }
42    }
43
44    /**
45     * See {@link android.webkit.WebBackForwardList#getCurrentItem}.
46     */
47    @Override
48    public synchronized WebHistoryItem getCurrentItem() {
49        if (getSize() == 0) {
50            return null;
51        } else {
52            return getItemAtIndex(getCurrentIndex());
53        }
54    }
55
56    /**
57     * See {@link android.webkit.WebBackForwardList#getCurrentIndex}.
58     */
59    @Override
60    public synchronized int getCurrentIndex() {
61        return mCurrentIndex;
62    }
63
64    /**
65     * See {@link android.webkit.WebBackForwardList#getItemAtIndex}.
66     */
67    @Override
68    public synchronized WebHistoryItem getItemAtIndex(int index) {
69        if (index < 0 || index >= getSize()) {
70            return null;
71        } else {
72            return mHistroryItemList.get(index);
73        }
74    }
75
76    /**
77     * See {@link android.webkit.WebBackForwardList#getSize}.
78     */
79    @Override
80    public synchronized int getSize() {
81        return mHistroryItemList.size();
82    }
83
84    // Clone constructor.
85    private WebBackForwardListChromium(List<WebHistoryItemChromium> list,
86                                       int currentIndex) {
87        mHistroryItemList = list;
88        mCurrentIndex = currentIndex;
89    }
90
91    /**
92     * See {@link android.webkit.WebBackForwardList#clone}.
93     */
94    @Override
95    protected synchronized WebBackForwardListChromium clone() {
96        List<WebHistoryItemChromium> list =
97                new ArrayList<WebHistoryItemChromium>(getSize());
98        for (int i = 0; i < getSize(); ++i) {
99            list.add(mHistroryItemList.get(i).clone());
100        }
101        return new WebBackForwardListChromium(list, mCurrentIndex);
102    }
103}
104