Bug 843234 - Move default favicon creation to separate thread. r=margaret
authorWes Johnston <wjohnston@mozilla.com>
Fri, 22 Feb 2013 16:05:35 -0800
changeset 122699 c34d5490c0ad11404ac7d014d78c5f852f1feb98
parent 122698 6271f9482edecc9e6a30ea6a1c7ba257863f1da0
child 122700 f46e28183c028a70d7664f3d0eb197588097d7c6
push id24356
push usergszorc@mozilla.com
push dateSun, 24 Feb 2013 01:00:12 +0000
treeherdermozilla-central@195e706140d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs843234
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 843234 - Move default favicon creation to separate thread. r=margaret
mobile/android/base/db/BrowserProvider.java.in
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -1000,100 +1000,117 @@ public class BrowserProvider extends Con
             if (bookmark.has(lang)) {
                 return bookmark.getString(lang);
             }
             // Default to the non-localized property name
             return bookmark.getString(property);
         }
 
         // Returns the number of bookmarks inserted in the db
-        private int createDistributionBookmarks(SQLiteDatabase db) {
+        private int createDistributionBookmarks(final SQLiteDatabase db) {
             JSONArray bookmarks = Distribution.getBookmarks(mContext);
             if (bookmarks == null) {
                 return 0;
             }
 
             Locale locale = Locale.getDefault();
             int pos = 0;
             for (int i = 0; i < bookmarks.length(); i++) {
                 try {
-                    JSONObject bookmark = bookmarks.getJSONObject(i);
+                    final JSONObject bookmark = bookmarks.getJSONObject(i);
 
                     String title = getLocalizedProperty(bookmark, "title", locale);
-                    String url = getLocalizedProperty(bookmark, "url", locale);
-
-                    // Look for an optional icon data URI
-                    Bitmap icon = null;
-                    if (bookmark.has("icon")) {
-                        String iconData = bookmark.getString("icon");
-                        icon = BitmapUtils.getBitmapFromDataURI(iconData);
+                    final String url = getLocalizedProperty(bookmark, "url", locale);
+                    createBookmark(db, title, url, pos);
+                    pos++;
+
+                    // return early if there is no icon for this bookmark
+                    if (!bookmark.has("icon")) {
+                        continue;
                     }
 
-                    createBookmark(db, title, url, pos, icon);
-                    pos++;
+                    // create icons in a separate thread to avoid blocking about:home on startup
+                    GeckoBackgroundThread.getHandler().post(new Runnable() {
+                        public void run() {
+                            try {
+                                String iconData = bookmark.getString("icon");
+                                Bitmap icon = BitmapUtils.getBitmapFromDataURI(iconData);
+                                if (icon != null) {
+                                    createFavicon(db, url, icon);
+                                }
+                            } catch (JSONException e) {
+                                Log.e(LOGTAG, "Error creating distribution bookmark icon", e);
+                            }
+                        }
+                    });
                 } catch (JSONException e) {
                     Log.e(LOGTAG, "Error creating distribution bookmark", e);
                 }
             }
             return pos;
         }
 
         // Inserts default bookmarks, starting at a specified position
-        private void createDefaultBookmarks(SQLiteDatabase db, int pos) {
+        private void createDefaultBookmarks(final SQLiteDatabase db, int pos) {
             Class<?> stringsClass = R.string.class;
             Field[] fields = stringsClass.getFields();
             Pattern p = Pattern.compile("^bookmarkdefaults_title_");
 
             for (int i = 0; i < fields.length; i++) {
-                String name = fields[i].getName();
+                final String name = fields[i].getName();
                 Matcher m = p.matcher(name);
                 if (!m.find()) {
                     continue;
                 }
                 try {
                     int titleid = fields[i].getInt(null);
                     String title = mContext.getString(titleid);
 
                     Field urlField = stringsClass.getField(name.replace("_title_", "_url_"));
                     int urlId = urlField.getInt(null);
-                    String url = mContext.getString(urlId);
-
-                    Bitmap icon = getDefaultFaviconFromPath(name);
-                    if (icon == null) {
-                        icon = getDefaultFaviconFromDrawable(name);
-                    }
-                    createBookmark(db, title, url, pos, icon);
+                    final String url = mContext.getString(urlId);
+                    createBookmark(db, title, url, pos);
+
+                    // create icons in a separate thread to avoid blocking about:home on startup
+                    GeckoBackgroundThread.getHandler().post(new Runnable() {
+                        public void run() {
+                            Bitmap icon = getDefaultFaviconFromPath(name);
+                            if (icon == null) {
+                                icon = getDefaultFaviconFromDrawable(name);
+                            }
+                            if (icon != null) {
+                                createFavicon(db, url, icon);
+                            }
+                        }
+                    });
                     pos++;
                 } catch (java.lang.IllegalAccessException ex) {
                     Log.e(LOGTAG, "Can't create bookmark " + name, ex);
                 } catch (java.lang.NoSuchFieldException ex) {
                     Log.e(LOGTAG, "Can't create bookmark " + name, ex);
                 }
             }
         }
 
-        private void createBookmark(SQLiteDatabase db, String title, String url, int pos, Bitmap icon) {
+        private void createBookmark(SQLiteDatabase db, String title, String url, int pos) {
             ContentValues bookmarkValues = new ContentValues();
             bookmarkValues.put(Bookmarks.PARENT, guidToID(db, Bookmarks.MOBILE_FOLDER_GUID));
 
             long now = System.currentTimeMillis();
             bookmarkValues.put(Bookmarks.DATE_CREATED, now);
             bookmarkValues.put(Bookmarks.DATE_MODIFIED, now);
 
             bookmarkValues.put(Bookmarks.TITLE, title);
             bookmarkValues.put(Bookmarks.URL, url);
             bookmarkValues.put(Bookmarks.GUID, Utils.generateGuid());
             bookmarkValues.put(Bookmarks.POSITION, pos);
             db.insertOrThrow(TABLE_BOOKMARKS, Bookmarks.TITLE, bookmarkValues);
-
-            // Return early if there's no icon to set
-            if (icon == null) {
-                return;
-            }
-
+        }
+
+        private void createFavicon(SQLiteDatabase db, String url, Bitmap icon) {
             ByteArrayOutputStream stream = new ByteArrayOutputStream();
             icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
 
             ContentValues iconValues = new ContentValues();
             iconValues.put(Favicons.DATA, stream.toByteArray());
             iconValues.put(Favicons.PAGE_URL, url);
             insertFavicon(db, iconValues);
         }