Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsBridge.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsBridge.java |
index 64df7d0bf5ecc2e80aad60215441f03e8382f19b..be48eada9030f57f6b80265127e060974874d924 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsBridge.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsBridge.java |
@@ -7,6 +7,9 @@ |
import org.chromium.base.annotations.CalledByNative; |
import org.chromium.chrome.browser.profiles.Profile; |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
/** |
* Provides access to the snippets to display on the NTP using the C++ NTP Snippets Service |
*/ |
@@ -14,14 +17,13 @@ |
private static final String TAG = "SnippetsBridge"; |
private long mNativeSnippetsBridge; |
+ private SnippetsObserver mObserver; |
/** |
* An observer for notifying when new snippets are loaded |
*/ |
public interface SnippetsObserver { |
- @CalledByNative("SnippetsObserver") |
- public void onSnippetsAvailable( |
- String[] titles, String[] urls, String[] thumbnailUrls, String[] previewText); |
+ public void onSnippetsReceived(List<SnippetArticle> snippets); |
} |
/** |
@@ -29,28 +31,53 @@ public void onSnippetsAvailable( |
* |
* @param profile Profile of the user that we will retrieve snippets for. |
*/ |
- public SnippetsBridge(Profile profile, final SnippetsObserver observer) { |
+ public SnippetsBridge(Profile profile) { |
mNativeSnippetsBridge = nativeInit(profile); |
- SnippetsObserver wrappedObserver = new SnippetsObserver() { |
- @Override |
- public void onSnippetsAvailable( |
- String[] titles, String[] urls, String[] thumbnailUrls, String[] previewText) { |
- // Don't notify observer if we've already been destroyed. |
- if (mNativeSnippetsBridge != 0) { |
- observer.onSnippetsAvailable(titles, urls, thumbnailUrls, previewText); |
- } |
- } |
- }; |
- nativeSetObserver(mNativeSnippetsBridge, wrappedObserver); |
} |
- void destroy() { |
+ /** |
+ * Destroys the native service and unregisters observers. This object can't be reused to |
+ * communicate with any native service and should be discarded. |
+ */ |
+ public void destroy() { |
assert mNativeSnippetsBridge != 0; |
nativeDestroy(mNativeSnippetsBridge); |
mNativeSnippetsBridge = 0; |
+ mObserver = null; |
+ } |
+ |
+ /** |
+ * Sets the recipient for the fetched snippets. This method should be called only once. |
+ * |
+ * Before the observer is set, the native code will not attempt to transmit them to java. Upon |
+ * registration, the observer will be notified of already fetched snippets. |
+ * |
+ * @param observer object to notify when snippets are received. |
+ */ |
+ public void setObserver(SnippetsObserver observer) { |
+ assert mObserver == null; |
+ |
+ mObserver = observer; |
+ nativeSetObserver(mNativeSnippetsBridge, this); |
+ } |
+ |
+ @CalledByNative |
+ private void onSnippetsAvailable(String[] titles, String[] urls, String[] thumbnailUrls, |
+ String[] previewText, long[] timestamps) { |
+ // Don't notify observer if we've already been destroyed. |
+ if (mNativeSnippetsBridge == 0) return; |
+ assert mObserver != null; |
+ |
+ List<SnippetArticle> newSnippets = new ArrayList<>(titles.length); |
+ for (int i = 0; i < titles.length; i++) { |
+ newSnippets.add(new SnippetArticle( |
+ titles[i], "", previewText[i], urls[i], thumbnailUrls[i], timestamps[i], i)); |
+ } |
+ |
+ mObserver.onSnippetsReceived(newSnippets); |
} |
private native long nativeInit(Profile profile); |
private native void nativeDestroy(long nativeNTPSnippetsBridge); |
- private native void nativeSetObserver(long nativeNTPSnippetsBridge, SnippetsObserver observer); |
+ private native void nativeSetObserver(long nativeNTPSnippetsBridge, SnippetsBridge bridge); |
} |