Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java

Issue 563843002: Refactor ShortcutHelper (Java/C++) to allow asynchronous init. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@manifest_manager_content
Patch Set: review comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
index 9762af96c1a8ccab4b72e70771067bab3da9eda8..c2ac5f8863abb09f06fd81d546c3291407118b7a 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java
@@ -38,19 +38,78 @@ public class ShortcutHelper {
}
/**
+ * Callback to be passed to the initialized() method.
+ */
+ public interface OnInitialized {
+ public void onInitialized(String title);
+ }
+
+ private final Context mAppContext;
+ private final Tab mTab;
+
+ private OnInitialized mCallback;
+ private boolean mIsInitialized = false;
gone 2014/09/11 20:32:21 defaults to false
mlamouri (slow - plz ping) 2014/09/12 10:15:16 Interesting.
+ private long mNativeShortcutHelper = 0;
gone 2014/09/11 20:32:21 defaults to 0
mlamouri (slow - plz ping) 2014/09/12 10:15:16 Done.
+
+ public ShortcutHelper(Context appContext, Tab tab) {
+ mAppContext = appContext;
+ mTab = tab;
+ }
+
+ /**
+ * Gets all the information required to initialize the UI, the passed
+ * callback will be run when those information will be available.
+ * @param callback Callback to be run when initialized.
+ */
+ public void initialize(OnInitialized callback) {
+ mCallback = callback;
+ mNativeShortcutHelper = nativeInitialize(mTab.getNativePtr());
+ }
+
+ /**
+ * Returns whether the object is initialized.
+ */
+ public boolean isInitialized() {
+ return mIsInitialized;
+ }
+
+ /**
+ * Puts the object in a state where it is safe to be destroyed.
+ */
+ public void tearDown() {
+ nativeTearDown(mNativeShortcutHelper);
+
+ // Make sure the callback isn't run if the tear down happens before
+ // onInitialized() is called.
+ mCallback = null;
+ mNativeShortcutHelper = 0;
+ }
+
+ @CalledByNative
+ private void onInitialized(String title) {
+ mIsInitialized = true;
+
+ if (mCallback != null) {
+ mCallback.onInitialized(title);
+ }
+ }
+
+ /**
* Adds a shortcut for the current Tab.
- * @param appContext The application context.
- * @param tab Tab to create a shortcut for.
* @param userRequestedTitle Updated title for the shortcut.
*/
- public static void addShortcut(Context appContext, Tab tab, String userRequestedTitle) {
+ public void addShortcut(String userRequestedTitle) {
if (TextUtils.isEmpty(sFullScreenAction)) {
Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting.");
return;
}
- ActivityManager am = (ActivityManager) appContext.getSystemService(
+ ActivityManager am = (ActivityManager) mAppContext.getSystemService(
Context.ACTIVITY_SERVICE);
- nativeAddShortcut(tab.getNativePtr(), userRequestedTitle, am.getLauncherLargeIconSize());
+ nativeAddShortcut(mNativeShortcutHelper, userRequestedTitle, am.getLauncherLargeIconSize());
+
+ // The C++ instance is no longer owned by the Java object.
+ mCallback = null;
+ mNativeShortcutHelper = 0;
}
/**
@@ -106,6 +165,8 @@ public class ShortcutHelper {
context.startActivity(homeIntent);
}
- private static native void nativeAddShortcut(long tabAndroidPtr, String userRequestedTitle,
+ private native long nativeInitialize(long tabAndroidPtr);
+ private native void nativeAddShortcut(long nativeShortcutHelper, String userRequestedTitle,
int launcherLargeIconSize);
+ private native void nativeTearDown(long nativeShortcutHelper);
}

Powered by Google App Engine
This is Rietveld 408576698