| 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; |
| 10 | 12 |
| 11 import java.util.ArrayList; | 13 import java.util.ArrayList; |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * A helper class responsible for exposing the C++ OfflineContentAggregator | 16 * A helper class responsible for exposing the C++ OfflineContentAggregator |
| 15 * (components/offline_items_collection/core/offline_content_aggregator.h) class
to Java. This | 17 * (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 | 18 * class is created and owned by it's C++ counterpart OfflineContentAggregatorBr
idge |
| 17 * (components/offline_items_collection/core/android/offline_content_aggregator_
bridge.h). | 19 * (components/offline_items_collection/core/android/offline_content_aggregator_
bridge.h). |
| 18 */ | 20 */ |
| 19 @JNINamespace("offline_items_collection::android") | 21 @JNINamespace("offline_items_collection::android") |
| 20 public class OfflineContentAggregatorBridge implements OfflineContentProvider { | 22 public class OfflineContentAggregatorBridge implements OfflineContentProvider { |
| 23 private final Handler mHandler = new Handler(); |
| 24 |
| 21 private long mNativeOfflineContentAggregatorBridge; | 25 private long mNativeOfflineContentAggregatorBridge; |
| 22 private ObserverList<OfflineContentProvider.Observer> mObservers; | 26 private ObserverList<OfflineContentProvider.Observer> mObservers; |
| 23 private boolean mItemsAvailable; | 27 private boolean mItemsAvailable; |
| 24 | 28 |
| 25 /** | 29 /** |
| 26 * A private constructor meant to be called by the C++ OfflineContentAggrega
torBridge. | 30 * A private constructor meant to be called by the C++ OfflineContentAggrega
torBridge. |
| 27 * @param nativeOfflineContentAggregatorBridge A C++ pointer to the | 31 * @param nativeOfflineContentAggregatorBridge A C++ pointer to the |
| 28 * OfflineContentAggregatorBridge. | 32 * OfflineContentAggregatorBridge. |
| 29 */ | 33 */ |
| 30 private OfflineContentAggregatorBridge(long nativeOfflineContentAggregatorBr
idge) { | 34 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); | 79 return nativeGetItemById(mNativeOfflineContentAggregatorBridge, id.names
pace, id.id); |
| 76 } | 80 } |
| 77 | 81 |
| 78 @Override | 82 @Override |
| 79 public ArrayList<OfflineItem> getAllItems() { | 83 public ArrayList<OfflineItem> getAllItems() { |
| 80 if (mNativeOfflineContentAggregatorBridge == 0) return null; | 84 if (mNativeOfflineContentAggregatorBridge == 0) return null; |
| 81 return nativeGetAllItems(mNativeOfflineContentAggregatorBridge); | 85 return nativeGetAllItems(mNativeOfflineContentAggregatorBridge); |
| 82 } | 86 } |
| 83 | 87 |
| 84 @Override | 88 @Override |
| 85 public void addObserver(OfflineContentProvider.Observer observer) { | 89 public void getVisualsForItem(ContentId id, VisualsCallback callback) { |
| 86 mObservers.addObserver(observer); | 90 nativeGetVisualsForItem( |
| 91 mNativeOfflineContentAggregatorBridge, id.namespace, id.id, call
back); |
| 87 } | 92 } |
| 88 | 93 |
| 89 @Override | 94 @Override |
| 95 public void addObserver(final OfflineContentProvider.Observer observer) { |
| 96 mObservers.addObserver(observer); |
| 97 if (!areItemsAvailable()) return; |
| 98 |
| 99 mHandler.post(new Runnable() { |
| 100 @Override |
| 101 public void run() { |
| 102 notifyObserverOfItemsReady(observer); |
| 103 } |
| 104 }); |
| 105 } |
| 106 |
| 107 @Override |
| 90 public void removeObserver(OfflineContentProvider.Observer observer) { | 108 public void removeObserver(OfflineContentProvider.Observer observer) { |
| 91 mObservers.removeObserver(observer); | 109 mObservers.removeObserver(observer); |
| 92 } | 110 } |
| 93 | 111 |
| 112 private void notifyObserverOfItemsReady(Observer observer) { |
| 113 if (!mObservers.hasObserver(observer)) return; |
| 114 observer.onItemsAvailable(); |
| 115 } |
| 116 |
| 94 // Methods called from C++ via JNI. | 117 // Methods called from C++ via JNI. |
| 95 @CalledByNative | 118 @CalledByNative |
| 96 private void onItemsAvailable() { | 119 private void onItemsAvailable() { |
| 97 mItemsAvailable = true; | 120 mItemsAvailable = true; |
| 98 | 121 |
| 99 for (Observer observer : mObservers) { | 122 for (Observer observer : mObservers) { |
| 100 observer.onItemsAvailable(); | 123 observer.onItemsAvailable(); |
| 101 } | 124 } |
| 102 } | 125 } |
| 103 | 126 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 119 } | 142 } |
| 120 } | 143 } |
| 121 | 144 |
| 122 @CalledByNative | 145 @CalledByNative |
| 123 private void onItemUpdated(OfflineItem item) { | 146 private void onItemUpdated(OfflineItem item) { |
| 124 for (Observer observer : mObservers) { | 147 for (Observer observer : mObservers) { |
| 125 observer.onItemUpdated(item); | 148 observer.onItemUpdated(item); |
| 126 } | 149 } |
| 127 } | 150 } |
| 128 | 151 |
| 152 @CalledByNative |
| 153 private static void onVisualsAvailable( |
| 154 VisualsCallback callback, String nameSpace, String id, OfflineItemVi
suals visuals) { |
| 155 callback.onVisualsAvailable(new ContentId(nameSpace, id), visuals); |
| 156 } |
| 157 |
| 129 /** | 158 /** |
| 130 * Called when the C++ OfflineContentAggregatorBridge is destroyed. This te
ars down the Java | 159 * 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 | 160 * 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. | 161 * longer attempts to access the C++ side of the bridge. |
| 133 */ | 162 */ |
| 134 @CalledByNative | 163 @CalledByNative |
| 135 private void onNativeDestroyed() { | 164 private void onNativeDestroyed() { |
| 136 mNativeOfflineContentAggregatorBridge = 0; | 165 mNativeOfflineContentAggregatorBridge = 0; |
| 137 } | 166 } |
| 138 | 167 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 156 private native void nativeCancelDownload( | 185 private native void nativeCancelDownload( |
| 157 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); | 186 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); |
| 158 private native void nativePauseDownload( | 187 private native void nativePauseDownload( |
| 159 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); | 188 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); |
| 160 private native void nativeResumeDownload( | 189 private native void nativeResumeDownload( |
| 161 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); | 190 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); |
| 162 private native OfflineItem nativeGetItemById( | 191 private native OfflineItem nativeGetItemById( |
| 163 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); | 192 long nativeOfflineContentAggregatorBridge, String nameSpace, String
id); |
| 164 private native ArrayList<OfflineItem> nativeGetAllItems( | 193 private native ArrayList<OfflineItem> nativeGetAllItems( |
| 165 long nativeOfflineContentAggregatorBridge); | 194 long nativeOfflineContentAggregatorBridge); |
| 195 private native void nativeGetVisualsForItem(long nativeOfflineContentAggrega
torBridge, |
| 196 String nameSpace, String id, VisualsCallback callback); |
| 166 } | 197 } |
| OLD | NEW |