Bug 699786 - Cancel pending favicon loads when location changes (r=blassey)
authorLucas Rocha <lucasr@mozilla.com>
Thu, 17 Nov 2011 21:35:19 +0000
changeset 81900 9999a423d8abf7eb27e8fe3b96993a6f67d18f2e
parent 81899 e20ccbf697a12e9dc9231835add5fab108482bc5
child 81901 02f6f69e8e2fc20e1cdb4d51c0c30000a4093f6c
push idunknown
push userunknown
push dateunknown
reviewersblassey
bugs699786
milestone11.0a1
Bug 699786 - Cancel pending favicon loads when location changes (r=blassey)
embedding/android/GeckoApp.java
embedding/android/Tab.java
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -545,19 +545,34 @@ abstract public class GeckoApp
         event.mType = GeckoEvent.SAVE_STATE;
         event.mCharacters = getStartupBitmapFilePath();
         if (sync)
             GeckoAppShell.sendEventToGeckoSync(event);
         else
             GeckoAppShell.sendEventToGecko(event);
     }
 
+    private void maybeCancelFaviconLoad(Tab tab) {
+        long faviconLoadId = tab.getFaviconLoadId();
+
+        if (faviconLoadId == Favicons.NOT_LOADING)
+            return;
+
+        // Cancel pending favicon load task
+        mFavicons.cancelFaviconLoad(faviconLoadId);
+
+        // Reset favicon load state
+        tab.setFaviconLoadId(Favicons.NOT_LOADING);
+    }
+
     private void loadFavicon(final Tab tab) {
-        mFavicons.loadFavicon(tab.getURL(), tab.getFaviconURL(),
-                new Favicons.OnFaviconLoadedListener() {
+        maybeCancelFaviconLoad(tab);
+
+        long id = mFavicons.loadFavicon(tab.getURL(), tab.getFaviconURL(),
+                        new Favicons.OnFaviconLoadedListener() {
 
             public void onFaviconLoaded(String pageUrl, Drawable favicon) {
                 // Leave favicon UI untouched if we failed to load the image
                 // for some reason.
                 if (favicon == null)
                     return;
 
                 Log.i(LOGTAG, "Favicon successfully loaded for URL = " + pageUrl);
@@ -565,23 +580,26 @@ abstract public class GeckoApp
                 // The tab might be pointing to another URL by the time the
                 // favicon is finally loaded, in which case we simply ignore it.
                 if (!tab.getURL().equals(pageUrl))
                     return;
 
                 Log.i(LOGTAG, "Favicon is for current URL = " + pageUrl);
 
                 tab.updateFavicon(favicon);
+                tab.setFaviconLoadId(Favicons.NOT_LOADING);
 
                 if (Tabs.getInstance().isSelectedTab(tab))
                     mBrowserToolbar.setFavicon(tab.getFavicon());
 
                 onTabsChanged(tab);
             }
         });
+
+        tab.setFaviconLoadId(id);
     }
 
     void handleLocationChange(final int tabId, final String uri) {
         final Tab tab = Tabs.getInstance().getTab(tabId);
         if (tab == null)
             return;
         
         String oldBaseURI = tab.getURL();
@@ -597,16 +615,18 @@ abstract public class GeckoApp
         if (baseURI.equals(oldBaseURI))
             return;
 
         tab.updateFavicon(null);
         tab.updateFaviconURL(null);
         tab.updateSecurityMode("unknown");
         tab.removeTransientDoorHangers();
 
+        maybeCancelFaviconLoad(tab);
+
         mMainHandler.post(new Runnable() {
             public void run() {
                 if (Tabs.getInstance().isSelectedTab(tab)) {
                     mBrowserToolbar.setTitle(uri);
                     mBrowserToolbar.setFavicon(null);
                     mBrowserToolbar.setSecurityMode("unknown");
                     mDoorHangerPopup.updatePopup();
                 }
--- a/embedding/android/Tab.java
+++ b/embedding/android/Tab.java
@@ -66,16 +66,17 @@ public class Tab {
     private String mFaviconUrl;
     private String mSecurityMode;
     private Drawable mThumbnail;
     private List<HistoryEntry> mHistory;
     private int mHistoryIndex;
     private boolean mLoading;
     private boolean mBookmark;
     private HashMap<String, DoorHanger> mDoorHangers;
+    private long mFaviconLoadId;
 
     static class HistoryEntry {
         public final String mUri;
         public String mTitle;
 
         public HistoryEntry(String uri, String title) {
             mUri = uri;
             mTitle = title;
@@ -93,16 +94,17 @@ public class Tab {
         mFavicon = null;
         mFaviconUrl = null;
         mSecurityMode = "unknown";
         mThumbnail = null;
         mHistory = new ArrayList<HistoryEntry>();
         mHistoryIndex = -1;
         mBookmark = false;
         mDoorHangers = new HashMap<String, DoorHanger>();
+        mFaviconLoadId = 0;
     }
 
     public int getId() {
         return mId;
     }
 
     public String getURL() {
         return mUrl;
@@ -173,16 +175,24 @@ public class Tab {
     public void setLoading(boolean loading) {
         mLoading = loading;
     }
 
     private void setBookmark(boolean bookmark) {
         mBookmark = bookmark;
     }
 
+    public void setFaviconLoadId(long faviconLoadId) {
+        mFaviconLoadId = faviconLoadId;
+    }
+
+    public long getFaviconLoadId() {
+        return mFaviconLoadId;
+    }
+
     public HistoryEntry getLastHistoryEntry() {
         if (mHistory.isEmpty())
             return null;
         return mHistory.get(mHistoryIndex);
     }
 
     public void updateFavicon(Drawable favicon) {
         mFavicon = favicon;