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..39e075b1f9d07f60a90663fff6ec2d257fd5078a 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); |
|
newt (away)
2016/03/30 19:03:50
This is a much cleaner public interface :)
|
| } |
| /** |
| @@ -29,28 +31,39 @@ 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() { |
| + public void destroy() { |
|
newt (away)
2016/03/30 19:03:50
javadoc
dgn
2016/03/31 00:46:45
Done.
|
| assert mNativeSnippetsBridge != 0; |
| nativeDestroy(mNativeSnippetsBridge); |
| mNativeSnippetsBridge = 0; |
| } |
| + public void setObserver(SnippetsObserver observer) { |
|
newt (away)
2016/03/30 19:03:50
javadoc. Worth mentioning that this should only be
dgn
2016/03/31 00:46:45
Done.
|
| + 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; |
| + |
| + 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); |
|
newt (away)
2016/03/30 19:03:50
mObserver could be null here. (at least with the c
dgn
2016/03/31 00:46:45
The method can be called from the native side only
|
| + } |
| + |
| private native long nativeInit(Profile profile); |
| private native void nativeDestroy(long nativeNTPSnippetsBridge); |
|
newt (away)
2016/03/30 19:03:50
nit: s/NTP/Ntp/
In Java code, even acronyms shoul
dgn
2016/03/31 00:46:45
That refers to the c++ class name, can't be modifi
|
| - private native void nativeSetObserver(long nativeNTPSnippetsBridge, SnippetsObserver observer); |
| + private native void nativeSetObserver(long nativeNTPSnippetsBridge, SnippetsBridge bridge); |
| } |