OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.graphics.Bitmap; | |
8 import android.util.LruCache; | |
9 import android.util.Pair; | |
10 | |
11 import org.chromium.base.CalledByNative; | |
12 import org.chromium.base.JNINamespace; | |
13 import org.chromium.base.ObserverList; | |
14 import org.chromium.chrome.browser.profiles.Profile; | |
15 import org.chromium.components.bookmarks.BookmarkId; | |
16 import org.chromium.content_public.browser.WebContents; | |
17 | |
18 import java.util.ArrayList; | |
19 import java.util.Arrays; | |
20 import java.util.List; | |
21 import java.util.Map; | |
22 | |
23 /** | |
24 * Access gate to C++ side enhanced bookmarks functionalities. | |
25 */ | |
26 @JNINamespace("enhanced_bookmarks::android") | |
27 public final class EnhancedBookmarksBridge { | |
28 private long mNativeEnhancedBookmarksBridge; | |
29 private final ObserverList<FiltersObserver> mFilterObservers = | |
30 new ObserverList<FiltersObserver>(); | |
31 private final ObserverList<SearchServiceObserver> mSearchObservers = | |
32 new ObserverList<SearchServiceObserver>(); | |
33 | |
34 private LruCache<String, Pair<String, Bitmap>> mSalientImageCache; | |
35 | |
36 /** | |
37 * Interface for getting result back from SalientImageForUrl function. | |
38 */ | |
39 public interface SalientImageCallback { | |
40 /** | |
41 * Callback method for fetching salient image. | |
42 * @param image Salient image. This can be null if the image cannot be f
ound. | |
43 * @param imageUrl Url of the image. Note this is not the same as the ur
l of the website | |
44 * containing the image. | |
45 */ | |
46 @CalledByNative("SalientImageCallback") | |
47 void onSalientImageReady(Bitmap image, String imageUrl); | |
48 } | |
49 | |
50 /** | |
51 * Interface to provide consumers notifications to changes in clusters | |
52 */ | |
53 public interface FiltersObserver { | |
54 /** | |
55 * Invoked when client detects that filters have been added/removed from
the server. | |
56 */ | |
57 void onFiltersChanged(); | |
58 } | |
59 | |
60 /** | |
61 * Interface to provide consumers notifications to changes in search service
results. | |
62 */ | |
63 public interface SearchServiceObserver { | |
64 /** | |
65 * Invoked when client detects that search results have been updated. Th
is callback is | |
66 * guaranteed to be called only once and only for the most recent query. | |
67 */ | |
68 void onSearchResultsReturned(); | |
69 } | |
70 | |
71 public EnhancedBookmarksBridge(Profile profile, int maxCacheSize) { | |
72 this(profile); | |
73 // Do not initialize LruCache if cache size is set to 0. | |
74 if (maxCacheSize != 0) { | |
75 mSalientImageCache = new LruCache<String, Pair<String, Bitmap>>(maxC
acheSize) { | |
76 @Override | |
77 protected int sizeOf(String key, Pair<String, Bitmap> urlImage)
{ | |
78 return urlImage.first.length() + urlImage.second.getByteCoun
t(); | |
79 } | |
80 }; | |
81 } | |
82 } | |
83 | |
84 public EnhancedBookmarksBridge(Profile profile) { | |
85 mNativeEnhancedBookmarksBridge = nativeInit(profile); | |
86 } | |
87 | |
88 public void destroy() { | |
89 assert mNativeEnhancedBookmarksBridge != 0; | |
90 nativeDestroy(mNativeEnhancedBookmarksBridge); | |
91 mNativeEnhancedBookmarksBridge = 0; | |
92 | |
93 if (mSalientImageCache != null) { | |
94 for (Map.Entry<String, Pair<String, Bitmap>> entry : | |
95 mSalientImageCache.snapshot().entrySet()) { | |
96 entry.getValue().second.recycle(); | |
97 } | |
98 mSalientImageCache.evictAll(); | |
99 } | |
100 } | |
101 | |
102 /** | |
103 * Adds a folder to the EnhancedBookmarkModel | |
104 * @param parent The parent of this folder | |
105 * @param index The position this folder should appear within the parent | |
106 * @param title The title of the bookmark | |
107 * @return The ID of the newly created folder. | |
108 */ | |
109 public BookmarkId addFolder(BookmarkId parent, int index, String title) { | |
110 return nativeAddFolder(mNativeEnhancedBookmarksBridge, parent, index, ti
tle); | |
111 } | |
112 | |
113 /** | |
114 * Adds a Bookmark to the EnhancedBookmarkModel | |
115 * @param parent The parent of this bookmark | |
116 * @param index The position this bookmark should appear within the parent | |
117 * @param title The title of the bookmark | |
118 * @param url URL of the bookmark | |
119 * @return The ID of the newly created bookmark | |
120 */ | |
121 public BookmarkId addBookmark(BookmarkId parent, int index, String title, St
ring url) { | |
122 return nativeAddBookmark(mNativeEnhancedBookmarksBridge, parent, index,
title, url); | |
123 } | |
124 | |
125 /** | |
126 * Moves a bookmark to another folder, and append it at the end of the list
of all children. | |
127 * @param bookmarkId The item to be be moved | |
128 * @param newParentId The new parent of the item | |
129 */ | |
130 public void moveBookmark(BookmarkId bookmarkId, BookmarkId newParentId) { | |
131 nativeMoveBookmark(mNativeEnhancedBookmarksBridge, bookmarkId, newParent
Id); | |
132 } | |
133 | |
134 /** | |
135 * Get descriptions of a given bookmark. | |
136 * @param id The id of the bookmark to look at. | |
137 * @return Description of the bookmark. If given a partner bookmark, this me
thod will return an | |
138 * empty list. | |
139 */ | |
140 public String getBookmarkDescription(BookmarkId id) { | |
141 return nativeGetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.g
etId(), | |
142 id.getType()); | |
143 } | |
144 | |
145 /** | |
146 * Sets the description of the given bookmark. | |
147 */ | |
148 public void setBookmarkDescription(BookmarkId id, String description) { | |
149 nativeSetBookmarkDescription(mNativeEnhancedBookmarksBridge, id.getId(),
id.getType(), | |
150 description); | |
151 } | |
152 | |
153 /** | |
154 * Registers a FiltersObserver to listen for filter change notifications. | |
155 * @param observer Observer to add | |
156 */ | |
157 public void addFiltersObserver(FiltersObserver observer) { | |
158 mFilterObservers.addObserver(observer); | |
159 } | |
160 | |
161 /** | |
162 * Unregisters a FiltersObserver from listening to filter change notificatio
ns. | |
163 * @param observer Observer to remove | |
164 */ | |
165 public void removeFiltersObserver(FiltersObserver observer) { | |
166 mFilterObservers.removeObserver(observer); | |
167 } | |
168 | |
169 /** | |
170 * Gets all the bookmark ids associated with a filter string. | |
171 * @param filter The filter string | |
172 * @return List of bookmark ids | |
173 */ | |
174 public List<BookmarkId> getBookmarksForFilter(String filter) { | |
175 List<BookmarkId> list = new ArrayList<BookmarkId>(); | |
176 nativeGetBookmarksForFilter(mNativeEnhancedBookmarksBridge, filter, list
); | |
177 return list; | |
178 } | |
179 | |
180 /** | |
181 * Sends request to search server for querying related bookmarks. | |
182 * @param query Keyword used to find related bookmarks. | |
183 */ | |
184 public void sendSearchRequest(String query) { | |
185 nativeSendSearchRequest(mNativeEnhancedBookmarksBridge, query); | |
186 } | |
187 | |
188 /** | |
189 * Get list of bookmarks as result of a search request that was sent before
in | |
190 * {@link EnhancedBookmarksBridge#sendSearchRequest(String)}. Normally this
function should be | |
191 * called after {@link SearchServiceObserver#onSearchResultsReturned()} | |
192 * @param query Keyword used to find related bookmarks. | |
193 * @return List of BookmarkIds that are related to query. It will be null if
the request is | |
194 * still on the fly, or empty list if there are no results for the q
uery. | |
195 */ | |
196 public List<BookmarkId> getSearchResultsForQuery(String query) { | |
197 return nativeGetSearchResults(mNativeEnhancedBookmarksBridge, query); | |
198 } | |
199 | |
200 /** | |
201 * Registers a SearchObserver that listens to search request updates. | |
202 * @param observer Observer to add | |
203 */ | |
204 public void addSearchObserver(SearchServiceObserver observer) { | |
205 mSearchObservers.addObserver(observer); | |
206 } | |
207 | |
208 /** | |
209 * Unregisters a SearchObserver that listens to search request updates. | |
210 * @param observer Observer to remove | |
211 */ | |
212 public void removeSearchObserver(SearchServiceObserver observer) { | |
213 mSearchObservers.removeObserver(observer); | |
214 } | |
215 | |
216 /** | |
217 * Request bookmark salient image for the given URL. Please refer to | |
218 * |BookmarkImageService::SalientImageForUrl|. | |
219 * @return True if this method is executed synchronously. False if | |
220 * {@link SalientImageCallback#onSalientImageReady(Bitmap, String)}
is called later | |
221 * (asynchronously). | |
222 */ | |
223 public boolean salientImageForUrl(final String url, final SalientImageCallba
ck callback) { | |
224 assert callback != null; | |
225 SalientImageCallback callbackWrapper = callback; | |
226 | |
227 if (mSalientImageCache != null) { | |
228 Pair<String, Bitmap> cached = mSalientImageCache.get(url); | |
229 if (cached != null) { | |
230 callback.onSalientImageReady(cached.second, cached.first); | |
231 return true; | |
232 } | |
233 | |
234 callbackWrapper = new SalientImageCallback() { | |
235 @Override | |
236 public void onSalientImageReady(Bitmap image, String imageUrl) { | |
237 if (image != null) { | |
238 mSalientImageCache.put(url, new Pair<String, Bitmap>(ima
geUrl, image)); | |
239 } | |
240 callback.onSalientImageReady(image, imageUrl); | |
241 } | |
242 }; | |
243 } | |
244 | |
245 nativeSalientImageForUrl(mNativeEnhancedBookmarksBridge, url, callbackWr
apper); | |
246 return false; | |
247 } | |
248 | |
249 /** | |
250 * Parses the web content of a tab, and stores salient images to local datab
ase. | |
251 * @param webContents Contents of the tab that the user is currently in. | |
252 */ | |
253 public void fetchImageForTab(WebContents webContents) { | |
254 nativeFetchImageForTab(mNativeEnhancedBookmarksBridge, webContents); | |
255 } | |
256 | |
257 /** | |
258 * Get all filters associated with the given bookmark. | |
259 * | |
260 * @param bookmark The bookmark to find filters for. | |
261 * @return Array of Strings, each representing a filter. If given a partner
bookmark, this | |
262 * method will return an empty array. | |
263 */ | |
264 public String[] getFiltersForBookmark(BookmarkId bookmark) { | |
265 return nativeGetFiltersForBookmark(mNativeEnhancedBookmarksBridge, bookm
ark.getId(), | |
266 bookmark.getType()); | |
267 } | |
268 | |
269 /** | |
270 * @return Current set of known auto-filters for bookmarks. | |
271 */ | |
272 public List<String> getFilters() { | |
273 List<String> list = | |
274 Arrays.asList(nativeGetFilters(mNativeEnhancedBookmarksBridge)); | |
275 return list; | |
276 } | |
277 | |
278 @CalledByNative | |
279 private void onFiltersChanged() { | |
280 for (FiltersObserver observer : mFilterObservers) { | |
281 observer.onFiltersChanged(); | |
282 } | |
283 } | |
284 | |
285 @CalledByNative | |
286 private void onSearchResultReturned() { | |
287 for (SearchServiceObserver observer : mSearchObservers) { | |
288 observer.onSearchResultsReturned(); | |
289 } | |
290 } | |
291 | |
292 @CalledByNative | |
293 private static List<BookmarkId> createBookmarkIdList() { | |
294 return new ArrayList<BookmarkId>(); | |
295 } | |
296 | |
297 @CalledByNative | |
298 private static void addToBookmarkIdList(List<BookmarkId> bookmarkIdList, lon
g id, int type) { | |
299 bookmarkIdList.add(new BookmarkId(id, type)); | |
300 } | |
301 | |
302 private native long nativeInit(Profile profile); | |
303 private native void nativeDestroy(long nativeEnhancedBookmarksBridge); | |
304 private native String nativeGetBookmarkDescription(long nativeEnhancedBookma
rksBridge, long id, | |
305 int type); | |
306 private native void nativeSetBookmarkDescription(long nativeEnhancedBookmark
sBridge, long id, | |
307 int type, String description); | |
308 private native void nativeGetBookmarksForFilter(long nativeEnhancedBookmarks
Bridge, | |
309 String filter, List<BookmarkId> list); | |
310 private native List<BookmarkId> nativeGetSearchResults(long nativeEnhancedBo
okmarksBridge, | |
311 String query); | |
312 private native String[] nativeGetFilters(long nativeEnhancedBookmarksBridge)
; | |
313 private native String[] nativeGetFiltersForBookmark(long nativeEnhancedBookm
arksBridge, long id, | |
314 int type); | |
315 private native BookmarkId nativeAddFolder(long nativeEnhancedBookmarksBridge
, BookmarkId parent, | |
316 int index, String title); | |
317 private native void nativeMoveBookmark(long nativeEnhancedBookmarksBridge, | |
318 BookmarkId bookmarkId, BookmarkId newParentId); | |
319 private native BookmarkId nativeAddBookmark(long nativeEnhancedBookmarksBrid
ge, | |
320 BookmarkId parent, int index, String title, String url); | |
321 private native void nativeSendSearchRequest(long nativeEnhancedBookmarksBrid
ge, String query); | |
322 private native void nativeSalientImageForUrl(long nativeEnhancedBookmarksBri
dge, | |
323 String url, SalientImageCallback callback); | |
324 private native void nativeFetchImageForTab(long nativeEnhancedBookmarksBridg
e, | |
325 WebContents webContents); | |
326 } | |
OLD | NEW |