diff options
Diffstat (limited to 'components/bookmarks/common/android')
9 files changed, 257 insertions, 0 deletions
diff --git a/components/bookmarks/common/android/OWNERS b/components/bookmarks/common/android/OWNERS new file mode 100644 index 0000000..3df886e --- /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 0000000..b3c5a9b4 --- /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 0000000..61fd7a8 --- /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 <jni.h> + +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 0000000..0c4c3e2 --- /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 0000000..e0a6594 --- /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 0000000..56a924a --- /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 0000000..09b8060 --- /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 <jni.h> + +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 0000000..88764fe --- /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 0000000..fd89154 --- /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 + +} |