| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.chrome.browser.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
| 6 | 6 |
| 7 import android.os.AsyncTask; | 7 import android.os.AsyncTask; |
| 8 | 8 |
| 9 import org.chromium.base.ObserverList; | 9 import org.chromium.base.ObserverList; |
| 10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.base.VisibleForTesting; | 11 import org.chromium.base.VisibleForTesting; |
| 12 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 13 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
| 14 import org.chromium.base.metrics.RecordHistogram; | 14 import org.chromium.base.metrics.RecordHistogram; |
| 15 import org.chromium.chrome.browser.profiles.Profile; | 15 import org.chromium.chrome.browser.profiles.Profile; |
| 16 import org.chromium.components.bookmarks.BookmarkId; | 16 import org.chromium.components.bookmarks.BookmarkId; |
| 17 import org.chromium.components.bookmarks.BookmarkType; | 17 import org.chromium.components.bookmarks.BookmarkType; |
| 18 import org.chromium.components.offlinepages.DeletePageResult; | 18 import org.chromium.components.offlinepages.DeletePageResult; |
| 19 import org.chromium.components.offlinepages.FeatureMode; | 19 import org.chromium.components.offlinepages.FeatureMode; |
| 20 import org.chromium.components.offlinepages.SavePageResult; | 20 import org.chromium.components.offlinepages.SavePageResult; |
| 21 import org.chromium.content_public.browser.WebContents; | 21 import org.chromium.content_public.browser.WebContents; |
| 22 | 22 |
| 23 import java.util.ArrayList; | 23 import java.util.ArrayList; |
| 24 import java.util.HashSet; |
| 24 import java.util.List; | 25 import java.util.List; |
| 26 import java.util.Set; |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * Access gate to C++ side offline pages functionalities. | 29 * Access gate to C++ side offline pages functionalities. |
| 28 */ | 30 */ |
| 29 @JNINamespace("offline_pages::android") | 31 @JNINamespace("offline_pages::android") |
| 30 public final class OfflinePageBridge { | 32 public final class OfflinePageBridge { |
| 33 public static final String BOOKMARK_NAMESPACE = "bookmark"; |
| 31 | 34 |
| 32 private long mNativeOfflinePageBridge; | 35 private long mNativeOfflinePageBridge; |
| 33 private boolean mIsNativeOfflinePageModelLoaded; | 36 private boolean mIsNativeOfflinePageModelLoaded; |
| 34 private final ObserverList<OfflinePageModelObserver> mObservers = | 37 private final ObserverList<OfflinePageModelObserver> mObservers = |
| 35 new ObserverList<OfflinePageModelObserver>(); | 38 new ObserverList<OfflinePageModelObserver>(); |
| 36 | 39 |
| 37 /** Mode of the offline pages feature */ | 40 /** Mode of the offline pages feature */ |
| 38 private static Integer sFeatureMode; | 41 private static Integer sFeatureMode; |
| 39 | 42 |
| 40 /** | 43 /** |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 public void offlinePageModelLoaded() {} | 81 public void offlinePageModelLoaded() {} |
| 79 | 82 |
| 80 /** | 83 /** |
| 81 * Called when the native side of offline pages is changed due to adding
, removing or | 84 * Called when the native side of offline pages is changed due to adding
, removing or |
| 82 * update an offline page. | 85 * update an offline page. |
| 83 */ | 86 */ |
| 84 public void offlinePageModelChanged() {} | 87 public void offlinePageModelChanged() {} |
| 85 | 88 |
| 86 /** | 89 /** |
| 87 * Called when an offline page is deleted. This can be called as a resul
t of | 90 * Called when an offline page is deleted. This can be called as a resul
t of |
| 91 * TODO(bburns): Switch to offline id/client id |
| 88 * #checkOfflinePageMetadata(). | 92 * #checkOfflinePageMetadata(). |
| 89 * @param bookmarkId A bookmark ID of the deleted offline page. | 93 * @param bookmarkId A bookmark ID of the deleted offline page. |
| 90 */ | 94 */ |
| 91 public void offlinePageDeleted(BookmarkId bookmarkId) {} | 95 public void offlinePageDeleted(BookmarkId id) {} |
| 92 } | 96 } |
| 93 | 97 |
| 94 private static long getTotalSize(List<OfflinePageItem> offlinePages) { | 98 private static long getTotalSize(List<OfflinePageItem> offlinePages) { |
| 95 long totalSize = 0; | 99 long totalSize = 0; |
| 96 for (OfflinePageItem offlinePage : offlinePages) { | 100 for (OfflinePageItem offlinePage : offlinePages) { |
| 97 totalSize += offlinePage.getFileSize(); | 101 totalSize += offlinePage.getFileSize(); |
| 98 } | 102 } |
| 99 return totalSize; | 103 return totalSize; |
| 100 } | 104 } |
| 101 | 105 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 * @return Gets all available offline pages. Requires that the model is alre
ady loaded. | 219 * @return Gets all available offline pages. Requires that the model is alre
ady loaded. |
| 216 */ | 220 */ |
| 217 public List<OfflinePageItem> getAllPages() { | 221 public List<OfflinePageItem> getAllPages() { |
| 218 assert mIsNativeOfflinePageModelLoaded; | 222 assert mIsNativeOfflinePageModelLoaded; |
| 219 List<OfflinePageItem> result = new ArrayList<OfflinePageItem>(); | 223 List<OfflinePageItem> result = new ArrayList<OfflinePageItem>(); |
| 220 nativeGetAllPages(mNativeOfflinePageBridge, result); | 224 nativeGetAllPages(mNativeOfflinePageBridge, result); |
| 221 return result; | 225 return result; |
| 222 } | 226 } |
| 223 | 227 |
| 224 /** | 228 /** |
| 229 * @return A list of all offline ids that match a particular |
| 230 * (namespace, client_id) |
| 231 */ |
| 232 public Set<Long> getOfflineIdsForClientId(String clientIdNamespace, String c
lientId) { |
| 233 assert mIsNativeOfflinePageModelLoaded; |
| 234 long[] offlineIds = nativeGetOfflineIdsForClientId( |
| 235 mNativeOfflinePageBridge, clientIdNamespace, clientId); |
| 236 Set<Long> result = new HashSet<>(offlineIds.length); |
| 237 for (long id : offlineIds) { |
| 238 result.add(id); |
| 239 } |
| 240 return result; |
| 241 } |
| 242 |
| 243 /** |
| 225 * Gets an offline page associated with a provided bookmark ID. | 244 * Gets an offline page associated with a provided bookmark ID. |
| 226 * | 245 * |
| 227 * @param bookmarkId Id of the bookmark associated with an offline page. | 246 * @param bookmarkId Id of the bookmark associated with an offline page. |
| 228 * @return An {@link OfflinePageItem} matching the bookmark Id or <code>null
</code> if none | 247 * @return An {@link OfflinePageItem} matching the bookmark Id or <code>null
</code> if none |
| 229 * exist. | 248 * exist. |
| 230 */ | 249 */ |
| 231 public OfflinePageItem getPageByBookmarkId(BookmarkId bookmarkId) { | 250 public OfflinePageItem getPageByBookmarkId(BookmarkId bookmarkId) { |
| 232 return nativeGetPageByBookmarkId(mNativeOfflinePageBridge, bookmarkId.ge
tId()); | 251 Set<Long> ids = |
| 252 getOfflineIdsForClientId(BOOKMARK_NAMESPACE, Long.toString(bookm
arkId.getId())); |
| 253 Long offlineId = ids.iterator().next(); |
| 254 return nativeGetPageByOfflineId(mNativeOfflinePageBridge, offlineId); |
| 233 } | 255 } |
| 234 | 256 |
| 235 /** | 257 /** |
| 236 * Gets an offline page associated with a provided online URL. | 258 * Gets an offline page associated with a provided online URL. |
| 237 * | 259 * |
| 238 * @param onlineURL URL of the page. | 260 * @param onlineURL URL of the page. |
| 239 * @return An {@link OfflinePageItem} matching the URL or <code>null</code>
if none exist. | 261 * @return An {@link OfflinePageItem} matching the URL or <code>null</code>
if none exist. |
| 240 */ | 262 */ |
| 241 public OfflinePageItem getPageByOnlineURL(String onlineURL) { | 263 public OfflinePageItem getPageByOnlineURL(String onlineURL) { |
| 242 return nativeGetPageByOnlineURL(mNativeOfflinePageBridge, onlineURL); | 264 return nativeGetPageByOnlineURL(mNativeOfflinePageBridge, onlineURL); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 269 // See http://crbug.com/566939 | 291 // See http://crbug.com/566939 |
| 270 if (savePageResult == SavePageResult.SUCCESS && isOfflinePageMod
elLoaded()) { | 292 if (savePageResult == SavePageResult.SUCCESS && isOfflinePageMod
elLoaded()) { |
| 271 long totalPageSizeAfter = getTotalSize(getAllPages()); | 293 long totalPageSizeAfter = getTotalSize(getAllPages()); |
| 272 recordStorageHistograms(0, totalPageSizeAfter); | 294 recordStorageHistograms(0, totalPageSizeAfter); |
| 273 } | 295 } |
| 274 callback.onSavePageDone(savePageResult, url); | 296 callback.onSavePageDone(savePageResult, url); |
| 275 } | 297 } |
| 276 }; | 298 }; |
| 277 recordFreeSpaceHistograms( | 299 recordFreeSpaceHistograms( |
| 278 "OfflinePages.SavePage.FreeSpacePercentage", "OfflinePages.SaveP
age.FreeSpaceMB"); | 300 "OfflinePages.SavePage.FreeSpacePercentage", "OfflinePages.SaveP
age.FreeSpaceMB"); |
| 279 nativeSavePage(mNativeOfflinePageBridge, callbackWrapper, webContents, b
ookmarkId.getId()); | 301 nativeSavePage(mNativeOfflinePageBridge, callbackWrapper, webContents, 0
, |
| 302 BOOKMARK_NAMESPACE, Long.toString(bookmarkId.getId())); |
| 280 } | 303 } |
| 281 | 304 |
| 282 /** | 305 /** |
| 283 * Marks that an offline page related to a specified bookmark has been acces
sed. | 306 * Marks that an offline page related to a specified bookmark has been acces
sed. |
| 284 * | 307 * |
| 285 * @param bookmarkId Bookmark ID for which the offline copy will be deleted. | 308 * @param bookmarkId Bookmark ID for which the offline copy will be deleted. |
| 286 */ | 309 */ |
| 287 public void markPageAccessed(BookmarkId bookmarkId) { | 310 public void markPageAccessed(BookmarkId bookmarkId) { |
| 288 assert mIsNativeOfflinePageModelLoaded; | 311 assert mIsNativeOfflinePageModelLoaded; |
| 289 nativeMarkPageAccessed(mNativeOfflinePageBridge, bookmarkId.getId()); | 312 Set<Long> ids = |
| 313 getOfflineIdsForClientId(BOOKMARK_NAMESPACE, Long.toString(bookm
arkId.getId())); |
| 314 Long offlineId = ids.iterator().next(); |
| 315 nativeMarkPageAccessed(mNativeOfflinePageBridge, offlineId); |
| 290 } | 316 } |
| 291 | 317 |
| 292 /** | 318 /** |
| 293 * Deletes an offline page related to a specified bookmark. | 319 * Deletes an offline page related to a specified bookmark. |
| 294 * | 320 * |
| 295 * @param bookmarkId Bookmark ID for which the offline copy will be deleted. | 321 * @param bookmarkId Bookmark ID for which the offline copy will be deleted. |
| 296 * @param callback Interface that contains a callback. | 322 * @param callback Interface that contains a callback. |
| 297 * @see DeletePageCallback | 323 * @see DeletePageCallback |
| 298 */ | 324 */ |
| 299 public void deletePage(final BookmarkId bookmarkId, DeletePageCallback callb
ack) { | 325 public void deletePage(final BookmarkId bookmarkId, DeletePageCallback callb
ack) { |
| 300 assert mIsNativeOfflinePageModelLoaded; | 326 assert mIsNativeOfflinePageModelLoaded; |
| 301 | 327 |
| 302 recordFreeSpaceHistograms("OfflinePages.DeletePage.FreeSpacePercentage", | 328 recordFreeSpaceHistograms("OfflinePages.DeletePage.FreeSpacePercentage", |
| 303 "OfflinePages.DeletePage.FreeSpaceMB"); | 329 "OfflinePages.DeletePage.FreeSpaceMB"); |
| 304 | 330 |
| 305 DeletePageCallback callbackWrapper = wrapCallbackWithHistogramReporting(
callback); | 331 DeletePageCallback callbackWrapper = wrapCallbackWithHistogramReporting(
callback); |
| 306 nativeDeletePage(mNativeOfflinePageBridge, callbackWrapper, bookmarkId.g
etId()); | 332 Set<Long> ids = |
| 333 getOfflineIdsForClientId(BOOKMARK_NAMESPACE, Long.toString(bookm
arkId.getId())); |
| 334 Long offlineId = ids.iterator().next(); |
| 335 nativeDeletePage(mNativeOfflinePageBridge, callbackWrapper, offlineId); |
| 307 } | 336 } |
| 308 | 337 |
| 309 /** | 338 /** |
| 310 * Deletes offline pages based on the list of provided bookamrk IDs. Calls t
he callback | 339 * Deletes offline pages based on the list of provided bookamrk IDs. Calls t
he callback |
| 311 * when operation is complete. Requires that the model is already loaded. | 340 * when operation is complete. Requires that the model is already loaded. |
| 312 * | 341 * |
| 313 * @param bookmarkIds A list of bookmark IDs for which the offline pages wil
l be deleted. | 342 * @param bookmarkIds A list of bookmark IDs for which the offline pages wil
l be deleted. |
| 314 * @param callback A callback that will be called once operation is complete
d. | 343 * @param callback A callback that will be called once operation is complete
d. |
| 315 */ | 344 */ |
| 316 public void deletePages(List<BookmarkId> bookmarkIds, DeletePageCallback cal
lback) { | 345 public void deletePages(List<BookmarkId> bookmarkIds, DeletePageCallback cal
lback) { |
| 317 assert mIsNativeOfflinePageModelLoaded; | 346 assert mIsNativeOfflinePageModelLoaded; |
| 318 long[] ids = new long[bookmarkIds.size()]; | 347 List<Long> idList = new ArrayList<>(bookmarkIds.size()); |
| 319 for (int i = 0; i < ids.length; i++) { | 348 for (int i = 0; i < bookmarkIds.size(); i++) { |
| 320 ids[i] = bookmarkIds.get(i).getId(); | 349 idList.addAll(getOfflineIdsForClientId( |
| 350 BOOKMARK_NAMESPACE, Long.toString(bookmarkIds.get(i).getId()
))); |
| 321 } | 351 } |
| 322 | 352 long[] ids = new long[idList.size()]; |
| 353 for (int i = 0; i < idList.size(); i++) { |
| 354 ids[i] = idList.get(i); |
| 355 } |
| 323 recordFreeSpaceHistograms("OfflinePages.DeletePage.FreeSpacePercentage", | 356 recordFreeSpaceHistograms("OfflinePages.DeletePage.FreeSpacePercentage", |
| 324 "OfflinePages.DeletePage.FreeSpaceMB"); | 357 "OfflinePages.DeletePage.FreeSpaceMB"); |
| 325 | 358 |
| 326 DeletePageCallback callbackWrapper = wrapCallbackWithHistogramReporting(
callback); | 359 DeletePageCallback callbackWrapper = wrapCallbackWithHistogramReporting(
callback); |
| 327 nativeDeletePages(mNativeOfflinePageBridge, callbackWrapper, ids); | 360 nativeDeletePages(mNativeOfflinePageBridge, callbackWrapper, ids); |
| 328 } | 361 } |
| 329 | 362 |
| 330 /** | 363 /** |
| 331 * Whether or not the underlying offline page model is loaded. | 364 * Whether or not the underlying offline page model is loaded. |
| 332 */ | 365 */ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 } | 433 } |
| 401 | 434 |
| 402 @CalledByNative | 435 @CalledByNative |
| 403 private void offlinePageModelChanged() { | 436 private void offlinePageModelChanged() { |
| 404 for (OfflinePageModelObserver observer : mObservers) { | 437 for (OfflinePageModelObserver observer : mObservers) { |
| 405 observer.offlinePageModelChanged(); | 438 observer.offlinePageModelChanged(); |
| 406 } | 439 } |
| 407 } | 440 } |
| 408 | 441 |
| 409 @CalledByNative | 442 @CalledByNative |
| 410 private void offlinePageDeleted(long bookmarkId) { | 443 private void offlinePageDeleted(long offlineId) { |
| 411 BookmarkId id = new BookmarkId(bookmarkId, BookmarkType.NORMAL); | 444 BookmarkId id = new BookmarkId(offlineId, BookmarkType.NORMAL); |
| 412 for (OfflinePageModelObserver observer : mObservers) { | 445 for (OfflinePageModelObserver observer : mObservers) { |
| 413 observer.offlinePageDeleted(id); | 446 observer.offlinePageDeleted(id); |
| 414 } | 447 } |
| 415 } | 448 } |
| 416 | 449 |
| 417 @CalledByNative | 450 @CalledByNative |
| 418 private static void createOfflinePageAndAddToList(List<OfflinePageItem> offl
inePagesList, | 451 private static void createOfflinePageAndAddToList(List<OfflinePageItem> offl
inePagesList, |
| 419 String url, long bookmarkId, String offlineUrl, long fileSize, long
creationTime, | 452 String url, long offlineId, String offlineUrl, long fileSize, long c
reationTime, |
| 420 int accessCount, long lastAccessTimeMs) { | 453 int accessCount, long lastAccessTimeMs) { |
| 421 offlinePagesList.add(createOfflinePageItem( | 454 offlinePagesList.add(createOfflinePageItem( |
| 422 url, bookmarkId, offlineUrl, fileSize, creationTime, accessCount
, | 455 url, offlineId, offlineUrl, fileSize, creationTime, accessCount,
lastAccessTimeMs)); |
| 423 lastAccessTimeMs)); | |
| 424 } | 456 } |
| 425 | 457 |
| 426 @CalledByNative | 458 @CalledByNative |
| 427 private static OfflinePageItem createOfflinePageItem(String url, long bookma
rkId, | 459 private static OfflinePageItem createOfflinePageItem(String url, long offlin
eId, |
| 428 String offlineUrl, long fileSize, long creationTime, int accessCount
, | 460 String offlineUrl, long fileSize, long creationTime, int accessCount
, |
| 429 long lastAccessTimeMs) { | 461 long lastAccessTimeMs) { |
| 430 return new OfflinePageItem( | 462 return new OfflinePageItem( |
| 431 url, bookmarkId, offlineUrl, fileSize, creationTime, accessCount
, lastAccessTimeMs); | 463 url, offlineId, offlineUrl, fileSize, creationTime, accessCount,
lastAccessTimeMs); |
| 432 } | 464 } |
| 433 | 465 |
| 434 private static native int nativeGetFeatureMode(); | 466 private static native int nativeGetFeatureMode(); |
| 435 private static native boolean nativeCanSavePage(String url); | 467 private static native boolean nativeCanSavePage(String url); |
| 436 | 468 |
| 437 private native long nativeInit(Profile profile); | 469 private native long nativeInit(Profile profile); |
| 438 private native void nativeDestroy(long nativeOfflinePageBridge); | 470 private native void nativeDestroy(long nativeOfflinePageBridge); |
| 439 private native void nativeGetAllPages( | 471 private native void nativeGetAllPages( |
| 440 long nativeOfflinePageBridge, List<OfflinePageItem> offlinePages); | 472 long nativeOfflinePageBridge, List<OfflinePageItem> offlinePages); |
| 441 private native OfflinePageItem nativeGetPageByBookmarkId( | 473 private native long[] nativeGetOfflineIdsForClientId( |
| 442 long nativeOfflinePageBridge, long bookmarkId); | 474 long nativeOfflinePageBridge, String clientNamespace, String clientI
d); |
| 475 private native OfflinePageItem nativeGetPageByOfflineId( |
| 476 long nativeOfflinePageBridge, long offlineId); |
| 443 private native OfflinePageItem nativeGetPageByOnlineURL( | 477 private native OfflinePageItem nativeGetPageByOnlineURL( |
| 444 long nativeOfflinePageBridge, String onlineURL); | 478 long nativeOfflinePageBridge, String onlineURL); |
| 445 private native void nativeSavePage(long nativeOfflinePageBridge, SavePageCal
lback callback, | 479 private native void nativeSavePage(long nativeOfflinePageBridge, SavePageCal
lback callback, |
| 446 WebContents webContents, long bookmarkId); | 480 WebContents webContents, long offlineId, String clientNamespace, Str
ing clientId); |
| 447 private native void nativeMarkPageAccessed(long nativeOfflinePageBridge, lon
g bookmarkId); | 481 private native void nativeMarkPageAccessed(long nativeOfflinePageBridge, lon
g offlineId); |
| 448 private native void nativeDeletePage(long nativeOfflinePageBridge, | 482 private native void nativeDeletePage( |
| 449 DeletePageCallback callback, long bookmarkId); | 483 long nativeOfflinePageBridge, DeletePageCallback callback, long offl
ineId); |
| 450 private native void nativeDeletePages( | 484 private native void nativeDeletePages( |
| 451 long nativeOfflinePageBridge, DeletePageCallback callback, long[] bo
okmarkIds); | 485 long nativeOfflinePageBridge, DeletePageCallback callback, long[] of
flineIds); |
| 452 private native void nativeGetPagesToCleanUp( | 486 private native void nativeGetPagesToCleanUp( |
| 453 long nativeOfflinePageBridge, List<OfflinePageItem> offlinePages); | 487 long nativeOfflinePageBridge, List<OfflinePageItem> offlinePages); |
| 454 private native void nativeCheckMetadataConsistency(long nativeOfflinePageBri
dge); | 488 private native void nativeCheckMetadataConsistency(long nativeOfflinePageBri
dge); |
| 455 private native String nativeGetOfflineUrlForOnlineUrl( | 489 private native String nativeGetOfflineUrlForOnlineUrl( |
| 456 long nativeOfflinePageBridge, String onlineUrl); | 490 long nativeOfflinePageBridge, String onlineUrl); |
| 457 private native boolean nativeIsOfflinePageUrl(long nativeOfflinePageBridge,
String offlineUrl); | 491 private native boolean nativeIsOfflinePageUrl(long nativeOfflinePageBridge,
String offlineUrl); |
| 458 } | 492 } |
| OLD | NEW |