Bug 1317632 - If the bookmark to add is a reader mode page, defer db action till it cache completely and save the same url in bookmarks and uriannotations table to avoid URL chagne between two http requests. r=sebastian
authorcnevinc <cnevinc@livemail.tw>
Sat, 14 Jan 2017 12:05:48 +0800
changeset 375590 e7c232eb0efa4076d3402a919671edeff7e9de28
parent 375589 8ac365ab3cfb611c5e85f24f533ac6399cdf0240
child 375591 b67585d1f51838b66cbc0854f74210c8120affbd
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1317632
milestone53.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 1317632 - If the bookmark to add is a reader mode page, defer db action till it cache completely and save the same url in bookmarks and uriannotations table to avoid URL chagne between two http requests. r=sebastian MozReview-Commit-ID: 8ZwteTOKe1G
mobile/android/base/java/org/mozilla/gecko/Tab.java
mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
--- a/mobile/android/base/java/org/mozilla/gecko/Tab.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tab.java
@@ -526,26 +526,28 @@ public class Tab {
     public void addBookmark() {
         final String url = getURL();
         if (url == null) {
             return;
         }
 
         final String pageUrl = ReaderModeUtils.stripAboutReaderUrl(getURL());
 
-        ThreadUtils.postToBackgroundThread(new Runnable() {
-            @Override
-            public void run() {
-                mDB.addBookmark(getContentResolver(), mTitle, pageUrl);
-                Tabs.getInstance().notifyListeners(Tab.this, Tabs.TabEvents.BOOKMARK_ADDED);
-            }
-        });
-
         if (AboutPages.isAboutReader(url)) {
             ReadingListHelper.cacheReaderItem(pageUrl, mId, mAppContext);
+            // defer bookmarking after completely added to cache.
+        } else {
+            ThreadUtils.postToBackgroundThread(new Runnable() {
+                @Override
+                public void run() {
+                    mDB.addBookmark(getContentResolver(), mTitle, pageUrl);
+                    Tabs.getInstance().notifyListeners(Tab.this, Tabs.TabEvents.BOOKMARK_ADDED);
+                }
+            });
+
         }
     }
 
     public void removeBookmark() {
         final String url = getURL();
         if (url == null) {
             return;
         }
--- a/mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
@@ -1,22 +1,25 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.reader;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.util.Log;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.GeckoProfile;
+import org.mozilla.gecko.Tab;
+import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.annotation.RobocopTarget;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.UrlAnnotations;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
@@ -141,18 +144,23 @@ public class SavedReaderViewHelper {
             Log.w(LOG_TAG, "Item insertion failed:", e);
             // This should never happen, absent any errors in our own implementation
             throw new IllegalStateException("Failure inserting into SavedReaderViewHelper json");
         }
 
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
-                UrlAnnotations annotations = BrowserDB.from(mContext).getUrlAnnotations();
-                annotations.insertReaderViewUrl(mContext.getContentResolver(), pageURL);
+                final UrlAnnotations annotations = BrowserDB.from(mContext).getUrlAnnotations();
+                final ContentResolver contentResolver = mContext.getContentResolver();
+                final Tab selectedTab = Tabs.getInstance().getSelectedTab();
+
+                annotations.insertReaderViewUrl(contentResolver, pageURL);
+                BrowserDB.from(mContext).addBookmark(contentResolver, selectedTab.getTitle(), pageURL);
+                Tabs.getInstance().notifyListeners(selectedTab, Tabs.TabEvents.BOOKMARK_ADDED);
 
                 commit();
             }
         });
     }
 
     protected synchronized void remove(@NonNull final String pageURL) {
         assertItemsLoaded();