From f3266cc3604c956eed772815aa40dd3af8c708c4 Mon Sep 17 00:00:00 2001 From: "kkimlabs@chromium.org" Date: Thu, 14 Aug 2014 20:59:22 +0000 Subject: [PATCH] [Android] EnhancedBookmarksBridge for bookmark description read&write. Also moved BookmarksBridge#BookmarkItem to bookmarks component. BUG=386785 Review URL: https://codereview.chromium.org/459973004 Cr-Commit-Position: refs/heads/master@{#289695} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289695 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/android/java/DEPS | 1 + .../chromium/chrome/browser/BookmarksBridge.java | 133 +++------------------ chrome/browser/DEPS | 1 + .../browser/android/bookmarks/bookmarks_bridge.cc | 45 ++++--- .../browser/android/bookmarks/bookmarks_bridge.h | 1 + chrome/browser/android/chrome_jni_registrar.cc | 5 + chrome/chrome.gyp | 2 + chrome/chrome_browser.gypi | 1 + components/bookmarks.gypi | 55 +++++++++ components/bookmarks/DEPS | 1 + components/bookmarks/common/android/OWNERS | 3 + components/bookmarks/common/android/bookmark_id.cc | 25 ++++ components/bookmarks/common/android/bookmark_id.h | 24 ++++ .../bookmarks/common/android/bookmark_type.h | 18 +++ .../bookmarks/common/android/bookmark_type_list.h | 13 ++ .../common/android/component_jni_registrar.cc | 26 ++++ .../common/android/component_jni_registrar.h | 18 +++ .../chromium/components/bookmarks/BookmarkId.java | 117 ++++++++++++++++++ .../components/bookmarks/BookmarkType.template | 13 ++ components/enhanced_bookmarks.gypi | 39 ++++++ components/enhanced_bookmarks/DEPS | 1 + components/enhanced_bookmarks/android/OWNERS | 3 + .../android/component_jni_registrar.cc | 29 +++++ .../android/component_jni_registrar.h | 18 +++ .../android/enhanced_bookmarks_bridge.cc | 69 +++++++++++ .../android/enhanced_bookmarks_bridge.h | 43 +++++++ .../enhancedbookmarks/EnhancedBookmarksBridge.java | 45 +++++++ 27 files changed, 612 insertions(+), 137 deletions(-) create mode 100644 components/bookmarks/common/android/OWNERS create mode 100644 components/bookmarks/common/android/bookmark_id.cc create mode 100644 components/bookmarks/common/android/bookmark_id.h create mode 100644 components/bookmarks/common/android/bookmark_type.h create mode 100644 components/bookmarks/common/android/bookmark_type_list.h create mode 100644 components/bookmarks/common/android/component_jni_registrar.cc create mode 100644 components/bookmarks/common/android/component_jni_registrar.h create mode 100644 components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java create mode 100644 components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkType.template create mode 100644 components/enhanced_bookmarks/android/OWNERS create mode 100644 components/enhanced_bookmarks/android/component_jni_registrar.cc create mode 100644 components/enhanced_bookmarks/android/component_jni_registrar.h create mode 100644 components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc create mode 100644 components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h create mode 100644 components/enhanced_bookmarks/android/java/src/org/chromium/components/enhancedbookmarks/EnhancedBookmarksBridge.java diff --git a/chrome/android/java/DEPS b/chrome/android/java/DEPS index 1f00e3954d98..8ee643b7e2cb 100644 --- a/chrome/android/java/DEPS +++ b/chrome/android/java/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+components/web_contents_delegate_android", + "+components/bookmarks/common/android/java/src/org/chromium/components/bookmarks", "+components/dom_distiller/android/java/src/org/chromium/components/dom_distiller/core", "+content/public/android/java", "+sync/android/java/src/org/chromium/sync/internal_api/pub", diff --git a/chrome/android/java/src/org/chromium/chrome/browser/BookmarksBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/BookmarksBridge.java index 872cc8746d62..87b1061f9d94 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/BookmarksBridge.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/BookmarksBridge.java @@ -4,12 +4,10 @@ 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 org.chromium.components.bookmarks.BookmarkId; import java.util.ArrayList; import java.util.List; @@ -19,14 +17,6 @@ import java.util.List; * bookmark model stored in native. */ public class BookmarksBridge { - - // Should mirror constants in chrome/browser/android/bookmarks/bookmarks_bridge.cc - public static final int BOOKMARK_TYPE_NORMAL = 0; - public static final int BOOKMARK_TYPE_PARTNER = 1; - - 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; @@ -199,8 +189,8 @@ public class BookmarksBridge { assert mIsNativeBookmarkModelLoaded; List result = new ArrayList(); nativeGetChildIDs(mNativeBookmarksBridge, - id.mId, - id.mType, + id.getId(), + id.getType(), getFolders, getBookmarks, result); @@ -222,7 +212,7 @@ public class BookmarksBridge { */ public void setBookmarkTitle(BookmarkId id, String title) { assert mIsNativeBookmarkModelLoaded; - nativeSetBookmarkTitle(mNativeBookmarksBridge, id.mId, id.mType, title); + nativeSetBookmarkTitle(mNativeBookmarksBridge, id.getId(), id.getType(), title); } /** @@ -230,7 +220,7 @@ public class BookmarksBridge { */ public void setBookmarkUrl(BookmarkId id, String url) { assert mIsNativeBookmarkModelLoaded; - nativeSetBookmarkUrl(mNativeBookmarksBridge, id.mId, id.mType, url); + nativeSetBookmarkUrl(mNativeBookmarksBridge, id.getId(), id.getType(), url); } /** @@ -238,7 +228,7 @@ public class BookmarksBridge { */ public boolean doesBookmarkExist(BookmarkId id) { assert mIsNativeBookmarkModelLoaded; - return nativeDoesBookmarkExist(mNativeBookmarksBridge, id.mId, id.mType); + return nativeDoesBookmarkExist(mNativeBookmarksBridge, id.getId(), id.getType()); } /** @@ -307,6 +297,13 @@ public class BookmarksBridge { nativeMoveBookmark(mNativeBookmarksBridge, bookmarkId, newParentId, index); } + /** + * A bridge function to BookmarkModelFactory::GetForProfile. + */ + public static long getNativeBookmarkModel(Profile profile) { + return nativeGetNativeBookmarkModel(profile); + } + public static boolean isEditBookmarksEnabled() { return nativeIsEditBookmarksEnabled(); } @@ -434,113 +431,12 @@ public class BookmarksBridge { private native void nativeDeleteBookmark(long nativeBookmarksBridge, BookmarkId bookmarkId); private native void nativeMoveBookmark(long nativeBookmarksBridge, BookmarkId bookmarkId, BookmarkId newParentId, int index); + private static native long nativeGetNativeBookmarkModel(Profile profile); private native long nativeInit(Profile profile); private native void nativeDestroy(long nativeBookmarksBridge); private static native boolean nativeIsEditBookmarksEnabled(); /** - * Simple object representing the bookmark id. - */ - public static class BookmarkId { - private static final String LOG_TAG = "BookmarkId"; - 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; - } - - /** - * @param c The char representing the type. - * @return The Bookmark type from a char representing the type. - */ - private static int getBookmarkTypeFromChar(char c) { - switch (c) { - case TYPE_PARTNER: - return BOOKMARK_TYPE_PARTNER; - default: - return BOOKMARK_TYPE_NORMAL; - } - } - - /** - * @param c The char representing the bookmark type. - * @return Whether the char representing the bookmark type is a valid type. - */ - private static boolean isValidBookmarkTypeFromChar(char c) { - return c == TYPE_PARTNER; - } - - /** - * @param s The bookmark id string (Eg: p1 for partner bookmark id 1). - * @return the Bookmark id from the string which is a concatenation of bookmark type and - * the bookmark id. - */ - 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); - } - - /** - * @return The id of the bookmark. - */ - @CalledByNative("BookmarkId") - public long getId() { - return mId; - } - - /** - * @return The bookmark type. - */ - @CalledByNative("BookmarkId") - public int getType() { - return mType; - } - - private String getBookmarkTypeString() { - switch (mType) { - 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(); - } - } - - /** * Simple object representing the bookmark item. */ public static class BookmarkItem { @@ -553,7 +449,6 @@ public class BookmarksBridge { private final boolean mIsEditable; private final boolean mIsManaged; - private BookmarkItem(BookmarkId id, String title, String url, boolean isFolder, BookmarkId parentId, boolean isEditable, boolean isManaged) { mId = id; diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS index 474dba19d4ec..7bf93483b2d9 100644 --- a/chrome/browser/DEPS +++ b/chrome/browser/DEPS @@ -24,6 +24,7 @@ include_rules = [ "+components/data_reduction_proxy", "+components/dom_distiller", "+components/domain_reliability", + "+components/enhanced_bookmarks", "+components/favicon_base", "+components/feedback", "+components/gcm_driver", diff --git a/chrome/browser/android/bookmarks/bookmarks_bridge.cc b/chrome/browser/android/bookmarks/bookmarks_bridge.cc index 2f18e55089ec..7cd704af9ffb 100644 --- a/chrome/browser/android/bookmarks/bookmarks_bridge.cc +++ b/chrome/browser/android/bookmarks/bookmarks_bridge.cc @@ -17,6 +17,7 @@ #include "chrome/common/pref_names.h" #include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_utils.h" +#include "components/bookmarks/common/android/bookmark_type.h" #include "components/signin/core/browser/signin_manager.h" #include "content/public/browser/browser_thread.h" #include "jni/BookmarksBridge_jni.h" @@ -26,13 +27,13 @@ using base::android::ConvertUTF8ToJavaString; using base::android::ConvertUTF16ToJavaString; using base::android::ScopedJavaLocalRef; using base::android::ScopedJavaGlobalRef; +using bookmarks::android::JavaBookmarkIdGetId; +using bookmarks::android::JavaBookmarkIdGetType; +using bookmarks::BookmarkType; using content::BrowserThread; -// Should mirror constants in BookmarksBridge.java -static const int kBookmarkTypeNormal = 0; -static const int kBookmarkTypePartner = 1; - namespace { + class BookmarkNodeCreationTimeCompareFunctor { public: bool operator()(const BookmarkNode* lhs, const BookmarkNode* rhs) { @@ -90,6 +91,14 @@ static jlong Init(JNIEnv* env, jobject obj, jobject j_profile) { return reinterpret_cast(delegate); } +static jlong GetNativeBookmarkModel(JNIEnv* env, + jclass caller, + jobject j_profile) { + Profile *profile = ProfileAndroid::FromProfileAndroid(j_profile); + BookmarkModel *bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); + return reinterpret_cast(bookmark_model_); +} + static bool IsEditBookmarksEnabled() { return ProfileManager::GetLastUsedProfile()->GetPrefs()->GetBoolean( prefs::kEditBookmarksEnabled); @@ -169,7 +178,7 @@ void BookmarksBridge::GetChildIDs(JNIEnv* env, env, j_result_obj, partner_bookmarks_shim_->GetPartnerBookmarksRoot()->id(), - kBookmarkTypePartner); + BookmarkType::PARTNER); } } @@ -258,8 +267,8 @@ void BookmarksBridge::GetBookmarksForFolder(JNIEnv* env, jobject j_callback_obj, jobject j_result_obj) { DCHECK(IsLoaded()); - long folder_id = Java_BookmarkId_getId(env, j_folder_id_obj); - int type = Java_BookmarkId_getType(env, j_folder_id_obj); + long folder_id = JavaBookmarkIdGetId(env, j_folder_id_obj); + int type = JavaBookmarkIdGetType(env, j_folder_id_obj); const BookmarkNode* folder = GetFolderWithFallback(folder_id, type); if (!folder->is_folder() || !IsReachable(folder)) @@ -298,8 +307,8 @@ void BookmarksBridge::GetCurrentFolderHierarchy(JNIEnv* env, jobject j_callback_obj, jobject j_result_obj) { DCHECK(IsLoaded()); - long folder_id = Java_BookmarkId_getId(env, j_folder_id_obj); - int type = Java_BookmarkId_getType(env, j_folder_id_obj); + long folder_id = JavaBookmarkIdGetId(env, j_folder_id_obj); + int type = JavaBookmarkIdGetType(env, j_folder_id_obj); const BookmarkNode* folder = GetFolderWithFallback(folder_id, type); if (!folder->is_folder() || !IsReachable(folder)) @@ -328,8 +337,8 @@ void BookmarksBridge::DeleteBookmark(JNIEnv* env, DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(IsLoaded()); - long bookmark_id = Java_BookmarkId_getId(env, j_bookmark_id_obj); - int type = Java_BookmarkId_getType(env, j_bookmark_id_obj); + long bookmark_id = JavaBookmarkIdGetId(env, j_bookmark_id_obj); + int type = JavaBookmarkIdGetType(env, j_bookmark_id_obj); const BookmarkNode* node = GetNodeByID(bookmark_id, type); if (!IsEditable(node)) { NOTREACHED(); @@ -352,15 +361,15 @@ void BookmarksBridge::MoveBookmark(JNIEnv* env, DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(IsLoaded()); - long bookmark_id = Java_BookmarkId_getId(env, j_bookmark_id_obj); - int type = Java_BookmarkId_getType(env, j_bookmark_id_obj); + long bookmark_id = JavaBookmarkIdGetId(env, j_bookmark_id_obj); + int type = JavaBookmarkIdGetType(env, j_bookmark_id_obj); const BookmarkNode* node = GetNodeByID(bookmark_id, type); if (!IsEditable(node)) { NOTREACHED(); return; } - bookmark_id = Java_BookmarkId_getId(env, j_parent_id_obj); - type = Java_BookmarkId_getType(env, j_parent_id_obj); + bookmark_id = JavaBookmarkIdGetId(env, j_parent_id_obj); + type = JavaBookmarkIdGetType(env, j_parent_id_obj); const BookmarkNode* new_parent_node = GetNodeByID(bookmark_id, type); bookmark_model_->Move(node, new_parent_node, index); } @@ -400,7 +409,7 @@ void BookmarksBridge::ExtractBookmarkNodeInformation(const BookmarkNode* node, const BookmarkNode* BookmarksBridge::GetNodeByID(long node_id, int type) { const BookmarkNode* node; - if (type == kBookmarkTypePartner) { + if (type == BookmarkType::PARTNER) { node = partner_bookmarks_shim_->GetNodeByID( static_cast(node_id)); } else { @@ -450,9 +459,9 @@ const BookmarkNode* BookmarksBridge::GetParentNode(const BookmarkNode* node) { int BookmarksBridge::GetBookmarkType(const BookmarkNode* node) { if (partner_bookmarks_shim_->IsPartnerBookmark(node)) - return kBookmarkTypePartner; + return BookmarkType::PARTNER; else - return kBookmarkTypeNormal; + return BookmarkType::NORMAL; } base::string16 BookmarksBridge::GetTitle(const BookmarkNode* node) const { diff --git a/chrome/browser/android/bookmarks/bookmarks_bridge.h b/chrome/browser/android/bookmarks/bookmarks_bridge.h index 8100a9b65e8c..39c64f9475d2 100644 --- a/chrome/browser/android/bookmarks/bookmarks_bridge.h +++ b/chrome/browser/android/bookmarks/bookmarks_bridge.h @@ -14,6 +14,7 @@ #include "chrome/browser/android/bookmarks/partner_bookmarks_shim.h" #include "chrome/browser/bookmarks/chrome_bookmark_client.h" #include "components/bookmarks/browser/base_bookmark_model_observer.h" +#include "components/bookmarks/common/android/bookmark_id.h" class Profile; diff --git a/chrome/browser/android/chrome_jni_registrar.cc b/chrome/browser/android/chrome_jni_registrar.cc index 699b4118f38c..7d5ec74af0db 100644 --- a/chrome/browser/android/chrome_jni_registrar.cc +++ b/chrome/browser/android/chrome_jni_registrar.cc @@ -73,7 +73,9 @@ #include "chrome/browser/ui/android/toolbar/toolbar_model_android.h" #include "chrome/browser/ui/android/website_settings_popup_android.h" #include "components/autofill/core/browser/android/component_jni_registrar.h" +#include "components/bookmarks/common/android/component_jni_registrar.h" #include "components/dom_distiller/android/component_jni_registrar.h" +#include "components/enhanced_bookmarks/android/component_jni_registrar.h" #include "components/gcm_driver/android/component_jni_registrar.h" #include "components/navigation_interception/component_jni_registrar.h" #include "components/variations/android/component_jni_registrar.h" @@ -90,7 +92,10 @@ namespace android { static base::android::RegistrationMethod kChromeRegisteredMethods[] = { // Register JNI for components we depend on. + { "Bookmarks", bookmarks::android::RegisterBookmarks }, { "DomDistiller", dom_distiller::android::RegisterDomDistiller }, + { "EnhancedBookmarks", + enhanced_bookmarks::android::RegisterEnhancedBookmarks }, { "GCMDriver", gcm::android::RegisterGCMDriverJni }, { "NavigationInterception", navigation_interception::RegisterNavigationInterceptionJni }, diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 5307889024bd..e20ac139684a 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -638,7 +638,9 @@ 'tab_load_status_java', '../base/base.gyp:base', '../components/components.gyp:autofill_java', + '../components/components.gyp:bookmarks_java', '../components/components.gyp:dom_distiller_core_java', + '../components/components.gyp:enhanced_bookmarks_java', '../components/components.gyp:gcm_driver_java', '../components/components.gyp:navigation_interception_java', '../components/components.gyp:sessions', diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index bbc3575a8221..1039efb710fd 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -3282,6 +3282,7 @@ ['OS=="android"', { 'dependencies': [ '../components/components.gyp:cdm_browser', + '../components/components.gyp:enhanced_bookmarks', '../components/components.gyp:web_contents_delegate_android', '../third_party/android_opengl/etc1/etc1.gyp:etc1', 'chrome_browser_jni_headers', diff --git a/components/bookmarks.gypi b/components/bookmarks.gypi index c88c26e50432..9d70d3350c9a 100644 --- a/components/bookmarks.gypi +++ b/components/bookmarks.gypi @@ -57,6 +57,20 @@ 'bookmarks/browser/scoped_group_bookmark_actions.cc', 'bookmarks/browser/scoped_group_bookmark_actions.h', ], + 'conditions': [ + ['OS == "android"', { + 'dependencies': [ + 'bookmarks_jni_headers', + ], + 'sources' : [ + 'bookmarks/common/android/bookmark_id.cc', + 'bookmarks/common/android/bookmark_id.h', + 'bookmarks/common/android/bookmark_type_list.h', + 'bookmarks/common/android/component_jni_registrar.cc', + 'bookmarks/common/android/component_jni_registrar.h', + ], + }], + ], }, { 'target_name': 'bookmarks_common', @@ -104,4 +118,45 @@ ], }, ], + 'conditions' : [ + ['OS=="android"', { + 'targets': [ + { + 'target_name': 'bookmarks_java', + 'type': 'none', + 'dependencies': [ + '../base/base.gyp:base_java', + 'bookmark_type_java', + ], + 'variables': { + 'java_in_dir': 'bookmarks/common/android/java', + }, + 'includes': [ '../build/java.gypi' ], + }, + { + 'target_name': 'bookmarks_jni_headers', + 'type': 'none', + 'sources': [ + 'bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java', + ], + 'variables': { + 'jni_gen_package': 'components/bookmarks', + }, + 'includes': [ '../build/jni_generator.gypi' ], + }, + { + 'target_name': 'bookmark_type_java', + 'type': 'none', + 'sources': [ + 'bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkType.template', + ], + 'variables': { + 'package_name': 'org/chromium/components/bookmarks', + 'template_deps': ['bookmarks/common/android/bookmark_type_list.h'], + }, + 'includes': [ '../build/android/java_cpp_template.gypi' ], + }, + ], + }] + ], } diff --git a/components/bookmarks/DEPS b/components/bookmarks/DEPS index f5adbc8a301b..4aefd683a36b 100644 --- a/components/bookmarks/DEPS +++ b/components/bookmarks/DEPS @@ -5,6 +5,7 @@ include_rules = [ "+components/query_parser", "+components/startup_metric_utils", "+grit/components_strings.h", + "+jni", "+net/base", "+ui", ] diff --git a/components/bookmarks/common/android/OWNERS b/components/bookmarks/common/android/OWNERS new file mode 100644 index 000000000000..3df886e30ca6 --- /dev/null +++ b/components/bookmarks/common/android/OWNERS @@ -0,0 +1,3 @@ +yfriedman@chromium.org +tedchoc@chromium.org +kkimlabs@chromium.org diff --git a/components/bookmarks/common/android/bookmark_id.cc b/components/bookmarks/common/android/bookmark_id.cc new file mode 100644 index 000000000000..b3c5a9b4261b --- /dev/null +++ b/components/bookmarks/common/android/bookmark_id.cc @@ -0,0 +1,25 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/bookmarks/common/android/bookmark_id.h" + +#include "jni/BookmarkId_jni.h" + +namespace bookmarks { +namespace android { + +long JavaBookmarkIdGetId(JNIEnv* env, jobject obj) { + return Java_BookmarkId_getId(env, obj); +} + +int JavaBookmarkIdGetType(JNIEnv* env, jobject obj) { + return Java_BookmarkId_getType(env, obj); +} + +bool RegisterBookmarkId(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android +} // namespace bookmarks diff --git a/components/bookmarks/common/android/bookmark_id.h b/components/bookmarks/common/android/bookmark_id.h new file mode 100644 index 000000000000..61fd7a8c4242 --- /dev/null +++ b/components/bookmarks/common/android/bookmark_id.h @@ -0,0 +1,24 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_ID_H_ +#define COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_ID_H_ + +#include + +namespace bookmarks { +namespace android { + +// See BookmarkId#getId +long JavaBookmarkIdGetId(JNIEnv* env, jobject obj); + +// See BookmarkId#getType +int JavaBookmarkIdGetType(JNIEnv* env, jobject obj); + +bool RegisterBookmarkId(JNIEnv* env); + +} // namespace android +} // namespace bookmarks + +#endif // COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_ID_H_ diff --git a/components/bookmarks/common/android/bookmark_type.h b/components/bookmarks/common/android/bookmark_type.h new file mode 100644 index 000000000000..0c4c3e2af682 --- /dev/null +++ b/components/bookmarks/common/android/bookmark_type.h @@ -0,0 +1,18 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_TYPE_H_ +#define COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_TYPE_H_ + +namespace bookmarks { + +enum BookmarkType { +#define DEFINE_BOOKMARK_TYPE(name, value) name = value, +#include "components/bookmarks/common/android/bookmark_type_list.h" +#undef DEFINE_BOOKMARK_TYPE +}; + +} + +#endif // COMPONENTS_BOOKMARKS_COMMON_ANDROID_BOOKMARK_TYPE_H_ diff --git a/components/bookmarks/common/android/bookmark_type_list.h b/components/bookmarks/common/android/bookmark_type_list.h new file mode 100644 index 000000000000..e0a65948d025 --- /dev/null +++ b/components/bookmarks/common/android/bookmark_type_list.h @@ -0,0 +1,13 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file intentionally does not have header guards, it's included +// inside a macro to generate enum values. + +#ifndef DEFINE_BOOKMARK_TYPE +#error "DEFINE_BOOKMARK_TYPE should be defined before including this file" +#endif + +DEFINE_BOOKMARK_TYPE(NORMAL, 0) +DEFINE_BOOKMARK_TYPE(PARTNER, 1) diff --git a/components/bookmarks/common/android/component_jni_registrar.cc b/components/bookmarks/common/android/component_jni_registrar.cc new file mode 100644 index 000000000000..56a924aa6ced --- /dev/null +++ b/components/bookmarks/common/android/component_jni_registrar.cc @@ -0,0 +1,26 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/bookmarks/common/android/component_jni_registrar.h" + +#include "base/android/jni_android.h" +#include "base/android/jni_registrar.h" +#include "components/bookmarks/common/android/bookmark_id.h" + +namespace bookmarks { +namespace android { + +static base::android::RegistrationMethod kBookmarksRegisteredMethods[] = { + {"BookmarkId", RegisterBookmarkId}, +}; + +bool RegisterBookmarks(JNIEnv* env) { + return base::android::RegisterNativeMethods( + env, + kBookmarksRegisteredMethods, + arraysize(kBookmarksRegisteredMethods)); +} + +} // namespace android +} // namespace bookmarks diff --git a/components/bookmarks/common/android/component_jni_registrar.h b/components/bookmarks/common/android/component_jni_registrar.h new file mode 100644 index 000000000000..09b80600c32e --- /dev/null +++ b/components/bookmarks/common/android/component_jni_registrar.h @@ -0,0 +1,18 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_BOOKMARKS_COMMON_ANDROID_COMPONENT_JNI_REGISTRAR_H_ +#define COMPONENTS_BOOKMARKS_COMMON_ANDROID_COMPONENT_JNI_REGISTRAR_H_ + +#include + +namespace bookmarks { +namespace android { + +bool RegisterBookmarks(JNIEnv* env); + +} // namespace android +} // namespace bookmarks + +#endif // COMPONENTS_BOOKMARKS_COMMON_ANDROID_COMPONENT_JNI_REGISTRAR_H_ diff --git a/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java new file mode 100644 index 000000000000..88764febbbe5 --- /dev/null +++ b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkId.java @@ -0,0 +1,117 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.components.bookmarks; + +import android.text.TextUtils; +import android.util.Log; + +import org.chromium.base.CalledByNative; + +/** + * Simple object representing the bookmark id. + */ +public class BookmarkId { + public static final int INVALID_FOLDER_ID = -2; + public static final int ROOT_FOLDER_ID = -1; + + private static final String LOG_TAG = "BookmarkId"; + 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; + } + + /** + * @param c The char representing the type. + * @return The Bookmark type from a char representing the type. + */ + private static int getBookmarkTypeFromChar(char c) { + switch (c) { + case TYPE_PARTNER: + return BookmarkType.BOOKMARK_TYPE_PARTNER; + default: + return BookmarkType.BOOKMARK_TYPE_NORMAL; + } + } + + /** + * @param c The char representing the bookmark type. + * @return Whether the char representing the bookmark type is a valid type. + */ + private static boolean isValidBookmarkTypeFromChar(char c) { + return c == TYPE_PARTNER; + } + + /** + * @param s The bookmark id string (Eg: p1 for partner bookmark id 1). + * @return the Bookmark id from the string which is a concatenation of + * bookmark type and the bookmark id. + */ + public static BookmarkId getBookmarkIdFromString(String s) { + long id = ROOT_FOLDER_ID; + int type = BookmarkType.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); + } + + /** + * @return The id of the bookmark. + */ + @CalledByNative + public long getId() { + return mId; + } + + /** + * @return The bookmark type. + */ + @CalledByNative + public int getType() { + return mType; + } + + private String getBookmarkTypeString() { + switch (mType) { + case BookmarkType.BOOKMARK_TYPE_PARTNER: + return String.valueOf(TYPE_PARTNER); + case BookmarkType.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(); + } +} diff --git a/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkType.template b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkType.template new file mode 100644 index 000000000000..fd8915417aa9 --- /dev/null +++ b/components/bookmarks/common/android/java/src/org/chromium/components/bookmarks/BookmarkType.template @@ -0,0 +1,13 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.components.bookmarks; + +public class BookmarkType { + +#define DEFINE_BOOKMARK_TYPE(name, value) public static final int BOOKMARK_TYPE_##name = value; +#include "components/bookmarks/common/android/bookmark_type_list.h" +#undef DEFINE_BOOKMARK_TYPE + +} diff --git a/components/enhanced_bookmarks.gypi b/components/enhanced_bookmarks.gypi index 8f0be9e706c8..a26cc602391f 100644 --- a/components/enhanced_bookmarks.gypi +++ b/components/enhanced_bookmarks.gypi @@ -32,6 +32,17 @@ 'enhanced_bookmarks/persistent_image_store.h', ], 'conditions': [ + ['OS=="android"', { + 'sources': [ + 'enhanced_bookmarks/android/component_jni_registrar.cc', + 'enhanced_bookmarks/android/component_jni_registrar.h', + 'enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc', + 'enhanced_bookmarks/android/enhanced_bookmarks_bridge.h', + ], + 'dependencies': [ + 'enhanced_bookmarks_jni_headers', + ], + }], ['OS=="ios"', { 'sources!': [ 'enhanced_bookmarks/image_store_util.cc', @@ -67,4 +78,32 @@ 'includes': [ '../build/protoc.gypi' ], }, ], + 'conditions' : [ + ['OS=="android"', { + 'targets': [ + { + 'target_name': 'enhanced_bookmarks_java', + 'type': 'none', + 'dependencies': [ + 'components.gyp:bookmarks_java' + ], + 'variables': { + 'java_in_dir': 'enhanced_bookmarks/android/java', + }, + 'includes': [ '../build/java.gypi' ], + }, + { + 'target_name': 'enhanced_bookmarks_jni_headers', + 'type': 'none', + 'sources': [ + 'enhanced_bookmarks/android/java/src/org/chromium/components/enhancedbookmarks/EnhancedBookmarksBridge.java', + ], + 'variables': { + 'jni_gen_package': 'enhanced_bookmarks', + }, + 'includes': [ '../build/jni_generator.gypi' ], + }, + ], + }] + ], } diff --git a/components/enhanced_bookmarks/DEPS b/components/enhanced_bookmarks/DEPS index b3cce4858a85..de785aef16fd 100644 --- a/components/enhanced_bookmarks/DEPS +++ b/components/enhanced_bookmarks/DEPS @@ -1,5 +1,6 @@ include_rules = [ "+components/bookmarks", + "+jni", "+sql", "+ui", ] diff --git a/components/enhanced_bookmarks/android/OWNERS b/components/enhanced_bookmarks/android/OWNERS new file mode 100644 index 000000000000..3df886e30ca6 --- /dev/null +++ b/components/enhanced_bookmarks/android/OWNERS @@ -0,0 +1,3 @@ +yfriedman@chromium.org +tedchoc@chromium.org +kkimlabs@chromium.org diff --git a/components/enhanced_bookmarks/android/component_jni_registrar.cc b/components/enhanced_bookmarks/android/component_jni_registrar.cc new file mode 100644 index 000000000000..ab4440d5844c --- /dev/null +++ b/components/enhanced_bookmarks/android/component_jni_registrar.cc @@ -0,0 +1,29 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/enhanced_bookmarks/android/component_jni_registrar.h" + +#include "base/android/jni_android.h" +#include "base/android/jni_registrar.h" +#include "base/basictypes.h" +#include "components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h" + +using base::android::RegistrationMethod; + +namespace enhanced_bookmarks { +namespace android { + +static RegistrationMethod kEnhancedBookmarksRegisteredMethods[] = { + {"EnhancedBookmarksBridge", RegisterEnhancedBookmarksBridge}, +}; + +bool RegisterEnhancedBookmarks(JNIEnv* env) { + return base::android::RegisterNativeMethods( + env, + kEnhancedBookmarksRegisteredMethods, + arraysize(kEnhancedBookmarksRegisteredMethods)); +} + +} // namespace android +} // namespace enhanced_bookmarks diff --git a/components/enhanced_bookmarks/android/component_jni_registrar.h b/components/enhanced_bookmarks/android/component_jni_registrar.h new file mode 100644 index 000000000000..d02d626be1f5 --- /dev/null +++ b/components/enhanced_bookmarks/android/component_jni_registrar.h @@ -0,0 +1,18 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_COMPONENT_JNI_REGISTRAR_H_ +#define COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_COMPONENT_JNI_REGISTRAR_H_ + +#include + +namespace enhanced_bookmarks { +namespace android { + +bool RegisterEnhancedBookmarks(JNIEnv* env); + +} // namespace android +} // namespace enhanced_bookmarks + +#endif // COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_COMPONENT_JNI_REGISTRAR_H_ diff --git a/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc b/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc new file mode 100644 index 000000000000..445f746c41ca --- /dev/null +++ b/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.cc @@ -0,0 +1,69 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h" + +#include "base/android/jni_string.h" +#include "components/bookmarks/browser/bookmark_model.h" +#include "components/bookmarks/browser/bookmark_utils.h" +#include "components/bookmarks/common/android/bookmark_type.h" +#include "components/enhanced_bookmarks/metadata_accessor.h" +#include "jni/EnhancedBookmarksBridge_jni.h" + +using bookmarks::BookmarkType; + +namespace enhanced_bookmarks { +namespace android { + +EnhancedBookmarksBridge::EnhancedBookmarksBridge(JNIEnv* env, + jobject obj, + jlong bookmark_model_ptr) { + bookmark_model_ = reinterpret_cast(bookmark_model_ptr); +} + +void EnhancedBookmarksBridge::Destroy(JNIEnv*, jobject) { + delete this; +} + +ScopedJavaLocalRef EnhancedBookmarksBridge::GetBookmarkDescription( + JNIEnv* env, jobject obj, jlong id, jint type) { + DCHECK(bookmark_model_->loaded()); + DCHECK_EQ(type, BookmarkType::NORMAL); + + const BookmarkNode* node = bookmarks::GetBookmarkNodeByID( + bookmark_model_, static_cast(id)); + + return node ? + base::android::ConvertUTF8ToJavaString( + env, enhanced_bookmarks::DescriptionFromBookmark(node)) : + ScopedJavaLocalRef(); +} + +void EnhancedBookmarksBridge::SetBookmarkDescription(JNIEnv* env, + jobject obj, + jlong id, + jint type, + jstring description) { + DCHECK(bookmark_model_->loaded()); + DCHECK_EQ(type, BookmarkType::NORMAL); + + const BookmarkNode* node = bookmarks::GetBookmarkNodeByID( + bookmark_model_, static_cast(id)); + + enhanced_bookmarks::SetDescriptionForBookmark( + bookmark_model_, node, + base::android::ConvertJavaStringToUTF8(env, description)); +} + +static jlong Init(JNIEnv* env, jobject obj, jlong bookmark_model_ptr) { + return reinterpret_cast( + new EnhancedBookmarksBridge(env, obj, bookmark_model_ptr)); +} + +bool RegisterEnhancedBookmarksBridge(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android +} // namespace enhanced_bookmarks diff --git a/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h b/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h new file mode 100644 index 000000000000..1319686955ba --- /dev/null +++ b/components/enhanced_bookmarks/android/enhanced_bookmarks_bridge.h @@ -0,0 +1,43 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_ENHANCED_BOOKMARKS_BRIDGE_H_ +#define COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_ENHANCED_BOOKMARKS_BRIDGE_H_ + +#include "base/android/jni_android.h" + +class BookmarkModel; + +namespace enhanced_bookmarks { +namespace android { + +class EnhancedBookmarksBridge { + public: + EnhancedBookmarksBridge(JNIEnv* env, jobject obj, jlong bookmark_model_ptr); + void Destroy(JNIEnv*, jobject); + + base::android::ScopedJavaLocalRef GetBookmarkDescription( + JNIEnv* env, + jobject obj, + jlong id, + jint type); + + void SetBookmarkDescription(JNIEnv* env, + jobject obj, + jlong id, + jint type, + jstring description); + + private: + BookmarkModel* bookmark_model_; // weak + + DISALLOW_COPY_AND_ASSIGN(EnhancedBookmarksBridge); +}; + +bool RegisterEnhancedBookmarksBridge(JNIEnv* env); + +} // namespace android +} // namespace enhanced_bookmarks + +#endif // COMPONENTS_ENHANCED_BOOKMARKS_ANDROID_ENHANCED_BOOKMARKS_BRIDGE_H_ diff --git a/components/enhanced_bookmarks/android/java/src/org/chromium/components/enhancedbookmarks/EnhancedBookmarksBridge.java b/components/enhanced_bookmarks/android/java/src/org/chromium/components/enhancedbookmarks/EnhancedBookmarksBridge.java new file mode 100644 index 000000000000..c244cb5f4c8d --- /dev/null +++ b/components/enhanced_bookmarks/android/java/src/org/chromium/components/enhancedbookmarks/EnhancedBookmarksBridge.java @@ -0,0 +1,45 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.components.enhancedbookmarks; + +import org.chromium.base.JNINamespace; +import org.chromium.components.bookmarks.BookmarkId; + +/** + * Access gate to C++ side enhanced bookmarks functionalities. + */ +@JNINamespace("enhanced_bookmarks::android") +public final class EnhancedBookmarksBridge { + private long mNativeEnhancedBookmarksBridge; + + public EnhancedBookmarksBridge(long nativeBookmarkModel) { + mNativeEnhancedBookmarksBridge = nativeInit(nativeBookmarkModel); + } + + public void destroy() { + assert mNativeEnhancedBookmarksBridge != 0; + nativeDestroy(mNativeEnhancedBookmarksBridge); + mNativeEnhancedBookmarksBridge = 0; + } + + public String getBookmarkDescription(BookmarkId id) { + return nativeGetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.getId(), + id.getType()); + } + + public void setBookmarkDescription(BookmarkId id, String description) { + nativeSetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.getId(), id.getType(), + description); + } + + private native long nativeInit(long bookmarkModelPointer); + + private native void nativeDestroy(long nativeEnhancedBookmarksBridge); + + private native String nativeGetBookmarkDescription(long nativeEnhancedBookmarksBridge, long id, + int type); + private native void nativeSetBookmarkDescription(long nativeEnhancedBookmarksBridge, long id, + int type, String description); +} -- 2.11.4.GIT