Bug 1097098 : Get tab history calculation offloaded to JS . r=bnicholson

This commit is contained in:
vivek 2014-12-05 17:59:45 -08:00
Родитель e966106d94
Коммит 297384e6ce
3 изменённых файлов: 79 добавлений и 60 удалений

Просмотреть файл

@ -96,7 +96,6 @@ public class Tab {
public static final int LOAD_PROGRESS_STOP = 100;
private static final int DEFAULT_BACKGROUND_COLOR = Color.WHITE;
public static final int MAX_HISTORY_LIST_SIZE = 50;
public enum ErrorType {
CERT_ERROR, // Pages with certificate problems

Просмотреть файл

@ -34,55 +34,14 @@ public class TabHistoryController {
this.showTabHistoryListener = showTabHistoryListener;
}
public boolean showTabHistory(final Tab tab, final HistoryAction action) {
int historyIndex = tab.getHistoryIndex();
int historySize = tab.getHistorySize();
switch(action) {
case BACK:
if (!tab.canDoBack()) {
return false;
}
return showHistory(Math.max(historyIndex - Tab.MAX_HISTORY_LIST_SIZE, 0), historyIndex, historyIndex);
case FORWARD:
if (!tab.canDoForward()) {
return false;
}
return showHistory(historyIndex, Math.min(historySize - 1, historyIndex + Tab.MAX_HISTORY_LIST_SIZE), historyIndex);
case ALL:
if (!tab.canDoForward() && !tab.canDoBack()) {
return false;
}
int min = historyIndex - Tab.MAX_HISTORY_LIST_SIZE / 2;
int max = historyIndex + Tab.MAX_HISTORY_LIST_SIZE / 2;
if (min < 0) {
max -= min;
}
if (max > historySize - 1) {
min -= max - (historySize - 1);
max = historySize - 1;
}
min = Math.max(min, 0);
return showHistory(min, max, historyIndex);
default:
return false;
}
}
/**
* This method will show the history starting on fromIndex until toIndex of the history.
* This method will show the history for the current tab.
*/
private boolean showHistory(final int fromIndex, final int toIndex, final int selIndex) {
public boolean showTabHistory(final Tab tab, final HistoryAction action) {
JSONObject json = new JSONObject();
try {
json.put("fromIndex", fromIndex);
json.put("toIndex", toIndex);
json.put("selIndex", selIndex);
json.put("action", action.name());
json.put("tabId", tab.getId());
} catch (JSONException e) {
Log.e(LOGTAG, "JSON error", e);
}
@ -92,17 +51,26 @@ public class TabHistoryController {
public void onResponse(NativeJSObject nativeJSObject) {
/*
* The response from gecko request is of the form
* "urls" : [
* {
* "title": "google",
* "url": "google.com",
* "selected": false
* }
* ]
* {
* "historyItems" : [
* {
* "title": "google",
* "url": "google.com",
* "selected": false
* }
* ],
* toIndex = 1
* }
*/
final NativeJSObject[] historyItems = nativeJSObject.getObjectArray("historyItems");
if (historyItems.length == 0) {
// Empty history, return without showing the popup.
return;
}
final List<TabHistoryPage> historyPageList = new ArrayList<>(historyItems.length);
final int toIndex = nativeJSObject.getInt("toIndex");
for (NativeJSObject obj : historyItems) {
final String title = obj.getString("title");

Просмотреть файл

@ -293,6 +293,7 @@ Strings.init();
const kFormHelperModeDisabled = 0;
const kFormHelperModeEnabled = 1;
const kFormHelperModeDynamic = 2; // disabled on tablets
const kMaxHistoryListSize = 50;
var BrowserApp = {
_tabs: [],
@ -1544,6 +1545,17 @@ var BrowserApp = {
case "Session:Navigate":
let index = JSON.parse(aData);
let webNav = BrowserApp.selectedTab.window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
let historySize = webNav.sessionHistory.count;
if (index < 0) {
index = 0;
Log.e("Browser", "Negative index truncated to zero");
} else if (index >= historySize) {
Log.e("Browser", "Incorrect index " + index + " truncated to " + historySize - 1);
index = historySize - 1;
}
browser.gotoIndex(index);
break;
@ -1961,15 +1973,52 @@ var BrowserApp = {
this._prefObservers = newPrefObservers;
},
// This method will return a list of history items from fromIndex to toIndex, optionally
// selecting selIndex(if fromIndex<=selIndex<=toIndex)
// This method will return a list of history items and toIndex based on the action provided from the fromIndex to toIndex,
// optionally selecting selIndex (if fromIndex <= selIndex <= toIndex)
getHistory: function(data) {
let fromIndex = data.fromIndex;
let toIndex = data.toIndex;
let selIndex = data.selIndex;
let action = data.action;
let webNav = BrowserApp.getTabForId(data.tabId).window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation);
let historyIndex = webNav.sessionHistory.index;
let historySize = webNav.sessionHistory.count;
let canGoBack = webNav.canGoBack;
let canGoForward = webNav.canGoForward;
let listitems = [];
let fromIndex = 0;
let toIndex = historySize - 1;
let selIndex = historyIndex;
if (action == "BACK" && canGoBack) {
fromIndex = Math.max(historyIndex - kMaxHistoryListSize, 0);
toIndex = historyIndex;
selIndex = historyIndex;
} else if (action == "FORWARD" && canGoForward) {
fromIndex = historyIndex;
toIndex = Math.min(historySize - 1, historyIndex + kMaxHistoryListSize);
selIndex = historyIndex;
} else if (action == "ALL" && (canGoBack || canGoForward)){
fromIndex = historyIndex - kMaxHistoryListSize / 2;
toIndex = historyIndex + kMaxHistoryListSize / 2;
if (fromIndex < 0) {
toIndex -= fromIndex;
}
if (toIndex > historySize - 1) {
fromIndex -= toIndex - (historySize - 1);
toIndex = historySize - 1;
}
fromIndex = Math.max(fromIndex, 0);
selIndex = historyIndex;
} else {
// return empty list immediately.
return {
"historyItems": listitems,
"toIndex": toIndex
};
}
let browser = this.selectedBrowser;
let hist = browser.sessionHistory;
let listitems = [];
for (let i = toIndex; i >= fromIndex; i--) {
let entry = hist.getEntryAtIndex(i, false);
let item = {
@ -1980,7 +2029,10 @@ var BrowserApp = {
listitems.push(item);
}
return { "historyItems" : listitems };
return {
"historyItems": listitems,
"toIndex": toIndex
};
},
};