Bug 840574 - Add applicationId to tabs; use it to open new urls in the correct tabs. r=margaret

Note that I'm not actually sure where delayLoad and selected are used by
Gecko and thus the side effects of changing them (but I did set the flags
as I'd expect for them to work).
This commit is contained in:
Michael Comella
2015-05-01 15:04:07 -07:00
parent 1e50dff8be
commit d632ee27c0
4 changed files with 122 additions and 45 deletions

View File

@@ -343,6 +343,20 @@ public class Tabs implements GeckoEventListener {
return mTabs.get(id);
}
public synchronized Tab getTabForApplicationId(final String applicationId) {
if (applicationId == null) {
return null;
}
for (final Tab tab : mOrder) {
if (applicationId.equals(tab.getApplicationId())) {
return tab;
}
}
return null;
}
/** Close tab and then select the default next tab */
@RobocopTarget
public synchronized void closeTab(Tab tab) {
@@ -796,23 +810,33 @@ public class Tabs implements GeckoEventListener {
* @return the Tab if a new one was created; null otherwise
*/
public Tab loadUrl(String url, int flags) {
return loadUrl(url, null, -1, flags);
return loadUrl(url, null, -1, null, flags);
}
public Tab loadUrl(final String url, final String applicationId, final int flags) {
return loadUrl(url, null, -1, applicationId, flags);
}
public Tab loadUrl(final String url, final String searchEngine, final int parentId, final int flags) {
return loadUrl(url, searchEngine, parentId, null, flags);
}
/**
* Loads a tab with the given URL.
*
* @param url URL of page to load, or search term used if searchEngine is given
* @param searchEngine if given, the search engine with this name is used
* to search for the url string; if null, the URL is loaded directly
* @param parentId ID of this tab's parent, or -1 if it has no parent
* @param flags flags used to load tab
* @param url URL of page to load, or search term used if searchEngine is given
* @param searchEngine if given, the search engine with this name is used
* to search for the url string; if null, the URL is loaded directly
* @param parentId ID of this tab's parent, or -1 if it has no parent
* @param applicationId Identity of the calling application
* @param flags flags used to load tab
*
* @return the Tab if a new one was created; null otherwise
* @return the Tab if a new one was created; null otherwise
*/
public Tab loadUrl(String url, String searchEngine, int parentId, int flags) {
public Tab loadUrl(final String url, final String searchEngine, final int parentId,
final String applicationId, final int flags) {
JSONObject args = new JSONObject();
Tab added = null;
Tab tabToSelect = null;
boolean delayLoad = (flags & LOADURL_DELAY_LOAD) != 0;
// delayLoad implies background tab
@@ -828,14 +852,42 @@ public class Tabs implements GeckoEventListener {
args.put("engine", searchEngine);
args.put("parentId", parentId);
args.put("userEntered", userEntered);
args.put("newTab", (flags & LOADURL_NEW_TAB) != 0);
args.put("isPrivate", isPrivate);
args.put("pinned", (flags & LOADURL_PINNED) != 0);
args.put("delayLoad", delayLoad);
args.put("desktopMode", desktopMode);
final boolean needsNewTab;
if (applicationId == null) {
needsNewTab = (flags & LOADURL_NEW_TAB) != 0;
} else {
final Tab applicationTab = getTabForApplicationId(applicationId);
if (applicationTab == null) {
needsNewTab = true;
} else {
needsNewTab = false;
delayLoad = false;
background = false;
tabToSelect = applicationTab;
final int tabToSelectId = tabToSelect.getId();
args.put("tabID", tabToSelectId);
// This must be called before the "Tab:Load" event is sent. I think addTab gets
// away with it because having "newTab" == true causes the selected tab to be
// updated in JS for the "Tab:Load" event but "newTab" is false in our case.
// This makes me think the other selectTab is not necessary (bug 1160673).
//
// Note: that makes the later call redundant but selectTab exits early so I'm
// fine not adding the complex logic to avoid calling it again.
selectTab(tabToSelect.getId());
}
}
args.put("newTab", needsNewTab);
args.put("delayLoad", delayLoad);
args.put("selected", !background);
if ((flags & LOADURL_NEW_TAB) != 0) {
if (needsNewTab) {
int tabId = getNextTabId();
args.put("tabID", tabId);
@@ -847,8 +899,9 @@ public class Tabs implements GeckoEventListener {
// Add the new tab to the end of the tab order.
final int tabIndex = -1;
added = addTab(tabId, tabUrl, external, parentId, url, isPrivate, tabIndex);
added.setDesktopMode(desktopMode);
tabToSelect = addTab(tabId, tabUrl, external, parentId, url, isPrivate, tabIndex);
tabToSelect.setDesktopMode(desktopMode);
tabToSelect.setApplicationId(applicationId);
}
} catch (Exception e) {
Log.w(LOGTAG, "Error building JSON arguments for loadUrl.", e);
@@ -856,22 +909,22 @@ public class Tabs implements GeckoEventListener {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Load", args.toString()));
if (added == null) {
if (tabToSelect == null) {
return null;
}
if (!delayLoad && !background) {
selectTab(added.getId());
selectTab(tabToSelect.getId());
}
// TODO: surely we could just fetch *any* cached icon?
if (AboutPages.isBuiltinIconPage(url)) {
Log.d(LOGTAG, "Setting about: tab favicon inline.");
added.addFavicon(url, Favicons.browserToolbarFaviconSize, "");
added.loadFavicon();
tabToSelect.addFavicon(url, Favicons.browserToolbarFaviconSize, "");
tabToSelect.loadFavicon();
}
return added;
return tabToSelect;
}
public Tab addTab() {