This source file includes following definitions.
- CalledByNative
- onBookmarksAvailable
- CalledByNative
- onBookmarksFolderHierarchyAvailable
- bookmarkNodeMoved
- bookmarkNodeAdded
- bookmarkNodeRemoved
- bookmarkNodeChanged
- bookmarkNodeChildrenReordered
- extensiveBookmarkChangesBeginning
- extensiveBookmarkChangesEnded
- bookmarkModelChanged
- destroy
- addObserver
- removeObserver
- getBookmarksForFolder
- getCurrentFolderHierarchy
- deleteBookmark
- moveBookmark
- isEditBookmarksEnabled
- bookmarkModelLoaded
- bookmarkModelDeleted
- bookmarkNodeMoved
- bookmarkNodeAdded
- bookmarkNodeRemoved
- bookmarkNodeChanged
- bookmarkNodeChildrenReordered
- extensiveBookmarkChangesBeginning
- extensiveBookmarkChangesEnded
- bookmarkModelChanged
- createBookmarkItem
- addToList
- createBookmarkId
- nativeGetBookmarksForFolder
- nativeGetCurrentFolderHierarchy
- nativeDeleteBookmark
- nativeMoveBookmark
- nativeInit
- nativeDestroy
- nativeIsEditBookmarksEnabled
- getBookmarkTypeFromChar
- isValidBookmarkTypeFromChar
- getBookmarkIdFromString
- CalledByNative
- getId
- CalledByNative
- getType
- getBookmarkTypeString
- toString
- equals
- hashCode
- getTitle
- getUrl
- getId
- isFolder
- getParentId
- isEditable
- callCallbackMethod
package org.chromium.chrome.browser;
import android.text.TextUtils;
import android.util.Log;
import org.chromium.base.CalledByNative;
import org.chromium.base.ObserverList;
import org.chromium.chrome.browser.profiles.Profile;
import java.util.ArrayList;
import java.util.List;
public class BookmarksBridge {
public static final int BOOKMARK_TYPE_NORMAL = 0;
public static final int BOOKMARK_TYPE_MANAGED = 1;
public static final int BOOKMARK_TYPE_PARTNER = 2;
public static final int INVALID_FOLDER_ID = -2;
public static final int ROOT_FOLDER_ID = -1;
private final Profile mProfile;
private long mNativeBookmarksBridge;
private boolean mIsNativeBookmarkModelLoaded;
private final List<DelayedBookmarkCallback> mDelayedBookmarkCallbacks =
new ArrayList<DelayedBookmarkCallback>();
private final ObserverList<BookmarkModelObserver> mObservers =
new ObserverList<BookmarkModelObserver>();
public interface BookmarksCallback {
@CalledByNative("BookmarksCallback")
void onBookmarksAvailable(BookmarkId folderId, List<BookmarkItem> bookmarksList);
@CalledByNative("BookmarksCallback")
void onBookmarksFolderHierarchyAvailable(BookmarkId folderId,
List<BookmarkItem> bookmarksList);
}
public interface BookmarkModelObserver {
void bookmarkNodeMoved(
BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, int newIndex);
void bookmarkNodeAdded(BookmarkItem parent, int index);
void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, BookmarkItem node);
void bookmarkNodeChanged(BookmarkItem node);
void bookmarkNodeChildrenReordered(BookmarkItem node);
void extensiveBookmarkChangesBeginning();
void extensiveBookmarkChangesEnded();
void bookmarkModelChanged();
}
public BookmarksBridge(Profile profile) {
mProfile = profile;
mNativeBookmarksBridge = nativeInit(profile);
}
public void destroy() {
if (mNativeBookmarksBridge != 0) {
nativeDestroy(mNativeBookmarksBridge);
mNativeBookmarksBridge = 0;
mIsNativeBookmarkModelLoaded = false;
mDelayedBookmarkCallbacks.clear();
}
mObservers.clear();
}
public void addObserver(BookmarkModelObserver observer) {
mObservers.addObserver(observer);
}
public void removeObserver(BookmarkModelObserver observer) {
mObservers.removeObserver(observer);
}
public void getBookmarksForFolder(BookmarkId folderId, BookmarksCallback callback) {
if (mIsNativeBookmarkModelLoaded) {
nativeGetBookmarksForFolder(mNativeBookmarksBridge, folderId, callback,
new ArrayList<BookmarkItem>());
} else {
mDelayedBookmarkCallbacks.add(new DelayedBookmarkCallback(folderId, callback,
DelayedBookmarkCallback.GET_BOOKMARKS_FOR_FOLDER, this));
}
}
public void getCurrentFolderHierarchy(BookmarkId folderId, BookmarksCallback callback) {
if (mIsNativeBookmarkModelLoaded) {
nativeGetCurrentFolderHierarchy(mNativeBookmarksBridge, folderId, callback,
new ArrayList<BookmarkItem>());
} else {
mDelayedBookmarkCallbacks.add(new DelayedBookmarkCallback(folderId, callback,
DelayedBookmarkCallback.GET_CURRENT_FOLDER_HIERARCHY, this));
}
}
public void deleteBookmark(BookmarkId bookmarkId) {
nativeDeleteBookmark(mNativeBookmarksBridge, bookmarkId);
}
public void moveBookmark(BookmarkId bookmarkId, BookmarkId newParentId, int index) {
nativeMoveBookmark(mNativeBookmarksBridge, bookmarkId, newParentId, index);
}
public static boolean isEditBookmarksEnabled() {
return nativeIsEditBookmarksEnabled();
}
@CalledByNative
private void bookmarkModelLoaded() {
mIsNativeBookmarkModelLoaded = true;
if (!mDelayedBookmarkCallbacks.isEmpty()) {
for (int i = 0; i < mDelayedBookmarkCallbacks.size(); i++) {
mDelayedBookmarkCallbacks.get(i).callCallbackMethod();
}
mDelayedBookmarkCallbacks.clear();
}
}
@CalledByNative
private void bookmarkModelDeleted() {
destroy();
}
@CalledByNative
private void bookmarkNodeMoved(
BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, int newIndex) {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkNodeMoved(oldParent, oldIndex, newParent, newIndex);
}
}
@CalledByNative
private void bookmarkNodeAdded(BookmarkItem parent, int index) {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkNodeAdded(parent, index);
}
}
@CalledByNative
private void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, BookmarkItem node) {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkNodeRemoved(parent, oldIndex, node);
}
}
@CalledByNative
private void bookmarkNodeChanged(BookmarkItem node) {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkNodeChanged(node);
}
}
@CalledByNative
private void bookmarkNodeChildrenReordered(BookmarkItem node) {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkNodeChildrenReordered(node);
}
}
@CalledByNative
private void extensiveBookmarkChangesBeginning() {
for (BookmarkModelObserver observer : mObservers) {
observer.extensiveBookmarkChangesBeginning();
}
}
@CalledByNative
private void extensiveBookmarkChangesEnded() {
for (BookmarkModelObserver observer : mObservers) {
observer.extensiveBookmarkChangesEnded();
}
}
@CalledByNative
private void bookmarkModelChanged() {
for (BookmarkModelObserver observer : mObservers) {
observer.bookmarkModelChanged();
}
}
@CalledByNative
private static BookmarkItem createBookmarkItem(long id, int type, String title, String url,
boolean isFolder, long parentId, int parentIdType, boolean isEditable) {
return new BookmarkItem(new BookmarkId(id, type), title, url, isFolder,
new BookmarkId(parentId, parentIdType), isEditable);
}
@CalledByNative
private static void addToList(List<BookmarkItem> bookmarksList, BookmarkItem bookmark) {
bookmarksList.add(bookmark);
}
@CalledByNative
private static BookmarkId createBookmarkId(long id, int type) {
return new BookmarkId(id, type);
}
private native void nativeGetBookmarksForFolder(long nativeBookmarksBridge,
BookmarkId folderId, BookmarksCallback callback,
List<BookmarkItem> bookmarksList);
private native void nativeGetCurrentFolderHierarchy(long nativeBookmarksBridge,
BookmarkId folderId, BookmarksCallback callback,
List<BookmarkItem> bookmarksList);
private native void nativeDeleteBookmark(long nativeBookmarksBridge, BookmarkId bookmarkId);
private native void nativeMoveBookmark(long nativeBookmarksBridge, BookmarkId bookmarkId,
BookmarkId newParentId, int index);
private native long nativeInit(Profile profile);
private native void nativeDestroy(long nativeBookmarksBridge);
private static native boolean nativeIsEditBookmarksEnabled();
public static class BookmarkId {
private static final String LOG_TAG = "BookmarkId";
private static final char TYPE_MANAGED = 'm';
private static final char TYPE_PARTNER = 'p';
private final long mId;
private final int mType;
public BookmarkId(long id, int type) {
mId = id;
mType = type;
}
private static int getBookmarkTypeFromChar(char c) {
switch (c) {
case TYPE_MANAGED:
return BOOKMARK_TYPE_MANAGED;
case TYPE_PARTNER:
return BOOKMARK_TYPE_PARTNER;
default:
return BOOKMARK_TYPE_NORMAL;
}
}
private static boolean isValidBookmarkTypeFromChar(char c) {
return (c == TYPE_MANAGED || c == TYPE_PARTNER);
}
public static BookmarkId getBookmarkIdFromString(String s) {
long id = ROOT_FOLDER_ID;
int type = BOOKMARK_TYPE_NORMAL;
if (TextUtils.isEmpty(s)) return new BookmarkId(id, type);
char folderTypeChar = s.charAt(0);
if (isValidBookmarkTypeFromChar(folderTypeChar)) {
type = getBookmarkTypeFromChar(folderTypeChar);
s = s.substring(1);
}
try {
id = Long.parseLong(s);
} catch (NumberFormatException exception) {
Log.e(LOG_TAG, "Error parsing url to extract the bookmark folder id.", exception);
}
return new BookmarkId(id, type);
}
@CalledByNative("BookmarkId")
public long getId() {
return mId;
}
@CalledByNative("BookmarkId")
public int getType() {
return mType;
}
private String getBookmarkTypeString() {
switch (mType) {
case BOOKMARK_TYPE_MANAGED:
return String.valueOf(TYPE_MANAGED);
case BOOKMARK_TYPE_PARTNER:
return String.valueOf(TYPE_PARTNER);
case BOOKMARK_TYPE_NORMAL:
default:
return "";
}
}
@Override
public String toString() {
return getBookmarkTypeString() + mId;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof BookmarkId)) return false;
BookmarkId item = (BookmarkId) o;
return (item.mId == mId && item.mType == mType);
}
@Override
public int hashCode() {
return toString().hashCode();
}
}
public static class BookmarkItem {
private final String mTitle;
private final String mUrl;
private final BookmarkId mId;
private final boolean mIsFolder;
private final BookmarkId mParentId;
private final boolean mIsEditable;
private BookmarkItem(BookmarkId id, String title, String url, boolean isFolder,
BookmarkId parentId, boolean isEditable) {
mId = id;
mTitle = title;
mUrl = url;
mIsFolder = isFolder;
mParentId = parentId;
mIsEditable = isEditable;
}
public String getTitle() {
return mTitle;
}
public String getUrl() {
return mUrl;
}
public BookmarkId getId() {
return mId;
}
public boolean isFolder() {
return mIsFolder;
}
public BookmarkId getParentId() {
return mParentId;
}
public boolean isEditable() {
return mIsEditable;
}
}
private static class DelayedBookmarkCallback {
private static final int GET_BOOKMARKS_FOR_FOLDER = 0;
private static final int GET_CURRENT_FOLDER_HIERARCHY = 1;
private final BookmarksCallback mCallback;
private final BookmarkId mFolderId;
private final int mCallbackMethod;
private final BookmarksBridge mHandler;
private DelayedBookmarkCallback(BookmarkId folderId, BookmarksCallback callback,
int method, BookmarksBridge handler) {
mFolderId = folderId;
mCallback = callback;
mCallbackMethod = method;
mHandler = handler;
}
private void callCallbackMethod() {
switch (mCallbackMethod) {
case GET_BOOKMARKS_FOR_FOLDER:
mHandler.getBookmarksForFolder(mFolderId, mCallback);
break;
case GET_CURRENT_FOLDER_HIERARCHY:
mHandler.getCurrentFolderHierarchy(mFolderId, mCallback);
break;
default:
assert false;
break;
}
}
}
}