Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Allow only one bookmark to be added for multiple fast starring
Browse files Browse the repository at this point in the history
A quick user might be able to save duplicated bookmarks when he clicks
the star button before the bookmark model is loaded yet. This CL forces
that only one operation can be done before the model is loaded.

BUG=516658

Review URL: https://codereview.chromium.org/1263223005

Cr-Commit-Position: refs/heads/master@{#341976}

[email protected]

Review URL: https://codereview.chromium.org/1277663002 .

Cr-Commit-Position: refs/branch-heads/2454@{#241}
Cr-Branched-From: 12bfc33-refs/heads/master@{#338390}
  • Loading branch information
jollycopper committed Aug 5, 2015
1 parent 1b83485 commit 153ab39
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.chrome.browser.webapps.AddToHomescreenDialog;
import org.chromium.chrome.browser.widget.ControlContainer;
import org.chromium.components.bookmarks.BookmarkId;
import org.chromium.components.bookmarks.BookmarkType;
import org.chromium.content.browser.ContentReadbackHandler;
import org.chromium.content.browser.ContentReadbackHandler.GetBitmapCallback;
import org.chromium.content.browser.ContentViewCore;
Expand Down Expand Up @@ -193,6 +191,7 @@ public abstract class ChromeActivity extends AsyncInitializationActivity
private ChromeAppMenuPropertiesDelegate mAppMenuPropertiesDelegate;
private AppMenuHandler mAppMenuHandler;
private ToolbarManager mToolbarManager;
private BookmarkModelObserver mBookmarkObserver;

// Time in ms that it took took us to inflate the initial layout
private long mInflateInitialLayoutDurationMs;
Expand Down Expand Up @@ -914,28 +913,22 @@ public void addOrEditBookmark(final Tab tabToBookmark) {

if (EnhancedBookmarkUtils.isEnhancedBookmarkEnabled(tabToBookmark.getProfile())) {
final EnhancedBookmarksModel bookmarkModel = new EnhancedBookmarksModel();
if (bookmarkModel.isBookmarkModelLoaded()) {
EnhancedBookmarkUtils.addOrEditBookmark(bookmarkId, bookmarkModel,
tabToBookmark, getSnackbarManager(), ChromeActivity.this);
} else if (mBookmarkObserver == null) {
mBookmarkObserver = new BookmarkModelObserver() {
@Override
public void bookmarkModelChanged() {}

BookmarkModelObserver modelObserver = new BookmarkModelObserver() {
@Override
public void bookmarkModelChanged() {}

@Override
public void bookmarkModelLoaded() {
if (bookmarkId == ChromeBrowserProviderClient.INVALID_BOOKMARK_ID) {
EnhancedBookmarkUtils.addBookmarkAndShowSnackbar(bookmarkModel,
@Override
public void bookmarkModelLoaded() {
EnhancedBookmarkUtils.addOrEditBookmark(bookmarkId, bookmarkModel,
tabToBookmark, getSnackbarManager(), ChromeActivity.this);
} else {
EnhancedBookmarkUtils.startEditActivity(ChromeActivity.this,
new BookmarkId(bookmarkId, BookmarkType.NORMAL));
bookmarkModel.removeModelObserver(this);
}
bookmarkModel.removeModelObserver(this);
}
};

if (bookmarkModel.isBookmarkModelLoaded()) {
modelObserver.bookmarkModelLoaded();
} else {
bookmarkModel.addModelObserver(modelObserver);
};
bookmarkModel.addModelObserver(mBookmarkObserver);
}
} else {
Intent intent = new Intent(this, ManageBookmarkActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BookmarksBridge;
import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem;
import org.chromium.chrome.browser.ChromeBrowserProviderClient;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.Tab;
import org.chromium.chrome.browser.UrlConstants;
Expand All @@ -30,6 +31,7 @@
import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.chrome.browser.util.MathUtils;
import org.chromium.components.bookmarks.BookmarkId;
import org.chromium.components.bookmarks.BookmarkType;
import org.chromium.ui.base.DeviceFormFactor;

/**
Expand Down Expand Up @@ -57,14 +59,18 @@ public static boolean isEnhancedBookmarkEnabled(Profile profile) {
}

/**
* Static method used for activities to show snackbar that notifies user that the bookmark has
* been added successfully. Note this method also starts fetching salient image in background.
* If the tab has already been bookmarked, start {@link EnhancedBookmarkEditActivity} for the
* bookmark. If not, add the bookmark to bookmarkmodel, and show a snackbar notifying the user.
*/
public static void addBookmarkAndShowSnackbar(EnhancedBookmarksModel bookmarkModel, Tab tab,
final SnackbarManager snackbarManager, final Activity activity) {
// TODO(ianwen): remove activity from argument list.
final BookmarkId enhancedId = bookmarkModel.addBookmark(bookmarkModel.getDefaultFolder(),
0, tab.getTitle(), tab.getUrl());
public static void addOrEditBookmark(long idToAdd, EnhancedBookmarksModel bookmarkModel,
Tab tab, final SnackbarManager snackbarManager, final Activity activity) {
if (idToAdd != ChromeBrowserProviderClient.INVALID_BOOKMARK_ID) {
startEditActivity(activity, new BookmarkId(idToAdd, BookmarkType.NORMAL));
return;
}

final BookmarkId enhancedId = bookmarkModel
.addBookmark(bookmarkModel.getDefaultFolder(), 0, tab.getTitle(), tab.getUrl());

Pair<EnhancedBookmarksModel, BookmarkId> pair = Pair.create(bookmarkModel, enhancedId);

Expand Down Expand Up @@ -112,6 +118,9 @@ public static boolean showEnhancedBookmarkIfEnabled(Activity activity) {
return true;
}

/**
* Starts an {@link EnhancedBookmarkEditActivity} for the given {@link BookmarkId}.
*/
public static void startEditActivity(Context context, BookmarkId bookmarkId) {
Intent intent = new Intent(context, EnhancedBookmarkEditActivity.class);
intent.putExtra(EnhancedBookmarkEditActivity.INTENT_BOOKMARK_ID, bookmarkId.toString());
Expand Down

0 comments on commit 153ab39

Please sign in to comment.