OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.offline_items_collection; | 5 package org.chromium.components.offline_items_collection; |
6 | 6 |
7 import android.os.Handler; | |
8 | |
7 import org.chromium.base.ObserverList; | 9 import org.chromium.base.ObserverList; |
8 import org.chromium.base.annotations.CalledByNative; | 10 import org.chromium.base.annotations.CalledByNative; |
9 import org.chromium.base.annotations.JNINamespace; | 11 import org.chromium.base.annotations.JNINamespace; |
12 import org.chromium.components.offline_items_collection.OfflineContentProvider; | |
10 | 13 |
11 import java.util.ArrayList; | 14 import java.util.ArrayList; |
12 | 15 |
13 /** | 16 /** |
14 * A helper class responsible for exposing the C++ OfflineContentAggregator | 17 * A helper class responsible for exposing the C++ OfflineContentAggregator |
15 * (components/offline_items_collection/core/offline_content_aggregator.h) class to Java. This | 18 * (components/offline_items_collection/core/offline_content_aggregator.h) class to Java. This |
16 * class is created and owned by it's C++ counterpart OfflineContentAggregatorBr idge | 19 * class is created and owned by it's C++ counterpart OfflineContentAggregatorBr idge |
17 * (components/offline_items_collection/core/android/offline_content_aggregator_ bridge.h). | 20 * (components/offline_items_collection/core/android/offline_content_aggregator_ bridge.h). |
18 */ | 21 */ |
19 @JNINamespace("offline_items_collection::android") | 22 @JNINamespace("offline_items_collection::android") |
20 public class OfflineContentAggregatorBridge implements OfflineContentProvider { | 23 public class OfflineContentAggregatorBridge implements OfflineContentProvider { |
24 private final Handler mHandler = new Handler(); | |
25 | |
21 private long mNativeOfflineContentAggregatorBridge; | 26 private long mNativeOfflineContentAggregatorBridge; |
22 private ObserverList<OfflineContentProvider.Observer> mObservers; | 27 private ObserverList<OfflineContentProvider.Observer> mObservers; |
23 private boolean mItemsAvailable; | 28 private boolean mItemsAvailable; |
24 | 29 |
25 /** | 30 /** |
26 * A private constructor meant to be called by the C++ OfflineContentAggrega torBridge. | 31 * A private constructor meant to be called by the C++ OfflineContentAggrega torBridge. |
27 * @param nativeOfflineContentAggregatorBridge A C++ pointer to the | 32 * @param nativeOfflineContentAggregatorBridge A C++ pointer to the |
28 * OfflineContentAggregatorBridge. | 33 * OfflineContentAggregatorBridge. |
29 */ | 34 */ |
30 private OfflineContentAggregatorBridge(long nativeOfflineContentAggregatorBr idge) { | 35 private OfflineContentAggregatorBridge(long nativeOfflineContentAggregatorBr idge) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 return nativeGetItemById(mNativeOfflineContentAggregatorBridge, id.names pace, id.id); | 80 return nativeGetItemById(mNativeOfflineContentAggregatorBridge, id.names pace, id.id); |
76 } | 81 } |
77 | 82 |
78 @Override | 83 @Override |
79 public ArrayList<OfflineItem> getAllItems() { | 84 public ArrayList<OfflineItem> getAllItems() { |
80 if (mNativeOfflineContentAggregatorBridge == 0) return null; | 85 if (mNativeOfflineContentAggregatorBridge == 0) return null; |
81 return nativeGetAllItems(mNativeOfflineContentAggregatorBridge); | 86 return nativeGetAllItems(mNativeOfflineContentAggregatorBridge); |
82 } | 87 } |
83 | 88 |
84 @Override | 89 @Override |
85 public void addObserver(OfflineContentProvider.Observer observer) { | 90 public void getVisualsForItem(ContentId id, VisualsCallback callback) { |
86 mObservers.addObserver(observer); | 91 nativeGetVisualsForItem( |
92 mNativeOfflineContentAggregatorBridge, id.namespace, id.id, call back); | |
87 } | 93 } |
88 | 94 |
89 @Override | 95 @Override |
96 public void addObserver(final OfflineContentProvider.Observer observer) { | |
97 mObservers.addObserver(observer); | |
98 | |
99 if (areItemsAvailable()) { | |
nyquist
2017/04/13 05:08:24
I don't feel strongly, but we often early out inst
David Trainor- moved to gerrit
2017/04/13 07:20:23
Ah you're right. Yeah it's my old habits of tryin
| |
100 mHandler.post(new Runnable() { | |
101 @Override | |
102 public void run() { | |
103 notifyObserverOfItemsReady(observer); | |
104 } | |
105 }); | |
106 } | |
107 } | |
108 | |
109 @Override | |
90 public void removeObserver(OfflineContentProvider.Observer observer) { | 110 public void removeObserver(OfflineContentProvider.Observer observer) { |
91 mObservers.removeObserver(observer); | 111 mObservers.removeObserver(observer); |
92 } | 112 } |
93 | 113 |
114 private void notifyObserverOfItemsReady(Observer observer) { | |
115 if (!mObservers.hasObserver(observer)) return; | |
116 observer.onItemsAvailable(); | |
117 } | |
118 | |
94 // Methods called from C++ via JNI. | 119 // Methods called from C++ via JNI. |
95 @CalledByNative | 120 @CalledByNative |
96 private void onItemsAvailable() { | 121 private void onItemsAvailable() { |
97 mItemsAvailable = true; | 122 mItemsAvailable = true; |
98 | 123 |
99 for (Observer observer : mObservers) { | 124 for (Observer observer : mObservers) { |
100 observer.onItemsAvailable(); | 125 observer.onItemsAvailable(); |
101 } | 126 } |
102 } | 127 } |
103 | 128 |
(...skipping 15 matching lines...) Expand all Loading... | |
119 } | 144 } |
120 } | 145 } |
121 | 146 |
122 @CalledByNative | 147 @CalledByNative |
123 private void onItemUpdated(OfflineItem item) { | 148 private void onItemUpdated(OfflineItem item) { |
124 for (Observer observer : mObservers) { | 149 for (Observer observer : mObservers) { |
125 observer.onItemUpdated(item); | 150 observer.onItemUpdated(item); |
126 } | 151 } |
127 } | 152 } |
128 | 153 |
154 @CalledByNative | |
155 private static void onVisualsAvailable(OfflineContentProvider.VisualsCallbac k callback, | |
156 String nameSpace, String id, OfflineItemVisuals visuals) { | |
157 callback.onVisualsAvailable(new ContentId(nameSpace, id), visuals); | |
158 } | |
159 | |
129 /** | 160 /** |
130 * Called when the C++ OfflineContentAggregatorBridge is destroyed. This te ars down the Java | 161 * Called when the C++ OfflineContentAggregatorBridge is destroyed. This te ars down the Java |
131 * component of the JNI bridge so that this class, which may live due to oth er references, no | 162 * component of the JNI bridge so that this class, which may live due to oth er references, no |
132 * longer attempts to access the C++ side of the bridge. | 163 * longer attempts to access the C++ side of the bridge. |
133 */ | 164 */ |
134 @CalledByNative | 165 @CalledByNative |
135 private void onNativeDestroyed() { | 166 private void onNativeDestroyed() { |
136 mNativeOfflineContentAggregatorBridge = 0; | 167 mNativeOfflineContentAggregatorBridge = 0; |
137 } | 168 } |
138 | 169 |
(...skipping 17 matching lines...) Expand all Loading... | |
156 private native void nativeCancelDownload( | 187 private native void nativeCancelDownload( |
157 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); | 188 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); |
158 private native void nativePauseDownload( | 189 private native void nativePauseDownload( |
159 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); | 190 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); |
160 private native void nativeResumeDownload( | 191 private native void nativeResumeDownload( |
161 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); | 192 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); |
162 private native OfflineItem nativeGetItemById( | 193 private native OfflineItem nativeGetItemById( |
163 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); | 194 long nativeOfflineContentAggregatorBridge, String nameSpace, String id); |
164 private native ArrayList<OfflineItem> nativeGetAllItems( | 195 private native ArrayList<OfflineItem> nativeGetAllItems( |
165 long nativeOfflineContentAggregatorBridge); | 196 long nativeOfflineContentAggregatorBridge); |
197 private native void nativeGetVisualsForItem(long nativeOfflineContentAggrega torBridge, | |
198 String nameSpace, String id, OfflineContentProvider.VisualsCallback callback); | |
166 } | 199 } |
OLD | NEW |