Chromium Code Reviews| 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..cf110d09bf2d904611ad965ab7ebbd2f06fdf6c4 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 |
| @@ -4,9 +4,13 @@ |
| package org.chromium.chrome.browser.ntp.snippets; |
| +import org.chromium.base.Log; |
| 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 +18,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 +32,63 @@ 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); |
| + } |
| + |
| + /** |
| + * Unregisters the observer. Safe to call even if the bridge has been destroyed. |
| + */ |
| + public void removeObserver() { |
| + mObserver = null; |
| + } |
| + |
| + @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; |
| + if (mObserver == null) { |
| + Log.w(TAG, "Snippets have been received while there is no observer to notify."); |
|
newt (away)
2016/03/31 04:16:22
Keep comments as short as reasonable. The log buff
dgn
2016/03/31 13:38:32
Removed the comment and just used an assert. The o
|
| + return; |
| + } |
| + |
| + 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); |
| } |