Chromium Code Reviews| 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.content.Context; | 7 import android.content.Context; |
| 8 import android.content.Intent; | 8 import android.content.Intent; |
| 9 import android.content.IntentFilter; | 9 import android.content.IntentFilter; |
| 10 import android.os.BatteryManager; | 10 import android.os.BatteryManager; |
| 11 import android.os.Environment; | 11 import android.os.Environment; |
| 12 | 12 |
| 13 import org.chromium.base.Callback; | |
| 13 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
| 14 import org.chromium.base.metrics.RecordHistogram; | 15 import org.chromium.base.metrics.RecordHistogram; |
| 15 import org.chromium.base.metrics.RecordUserAction; | 16 import org.chromium.base.metrics.RecordUserAction; |
| 16 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
| 17 import org.chromium.chrome.browser.ChromeActivity; | 18 import org.chromium.chrome.browser.ChromeActivity; |
| 18 import org.chromium.chrome.browser.snackbar.Snackbar; | 19 import org.chromium.chrome.browser.snackbar.Snackbar; |
| 19 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 20 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 20 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | 21 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| 21 import org.chromium.chrome.browser.tab.Tab; | 22 import org.chromium.chrome.browser.tab.Tab; |
| 22 import org.chromium.chrome.browser.tabmodel.TabModelSelector; | 23 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 23 import org.chromium.components.bookmarks.BookmarkId; | 24 import org.chromium.components.bookmarks.BookmarkId; |
| 25 import org.chromium.components.offlinepages.DeletePageResult; | |
| 24 import org.chromium.content_public.browser.LoadUrlParams; | 26 import org.chromium.content_public.browser.LoadUrlParams; |
| 25 import org.chromium.content_public.browser.WebContents; | 27 import org.chromium.content_public.browser.WebContents; |
| 26 import org.chromium.net.ConnectionType; | 28 import org.chromium.net.ConnectionType; |
| 27 import org.chromium.net.NetworkChangeNotifier; | 29 import org.chromium.net.NetworkChangeNotifier; |
| 28 import org.chromium.ui.base.PageTransition; | 30 import org.chromium.ui.base.PageTransition; |
| 29 | 31 |
| 32 import java.util.ArrayList; | |
| 33 import java.util.List; | |
| 30 import java.util.concurrent.TimeUnit; | 34 import java.util.concurrent.TimeUnit; |
| 31 | 35 |
| 32 /** | 36 /** |
| 33 * A class holding static util functions for offline pages. | 37 * A class holding static util functions for offline pages. |
| 34 */ | 38 */ |
| 35 public class OfflinePageUtils { | 39 public class OfflinePageUtils { |
| 36 private static final String TAG = "OfflinePageUtils"; | 40 private static final String TAG = "OfflinePageUtils"; |
| 37 /** Background task tag to differentiate from other task types */ | 41 /** Background task tag to differentiate from other task types */ |
| 38 public static final String TASK_TAG = "OfflinePageUtils"; | 42 public static final String TASK_TAG = "OfflinePageUtils"; |
| 39 | 43 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 public static void saveBookmarkOffline(BookmarkId bookmarkId, Tab tab) { | 76 public static void saveBookmarkOffline(BookmarkId bookmarkId, Tab tab) { |
| 73 // If bookmark ID is missing there is nothing to save here. | 77 // If bookmark ID is missing there is nothing to save here. |
| 74 if (bookmarkId == null) return; | 78 if (bookmarkId == null) return; |
| 75 | 79 |
| 76 // Making sure the feature is enabled. | 80 // Making sure the feature is enabled. |
| 77 if (!OfflinePageBridge.isOfflineBookmarksEnabled()) return; | 81 if (!OfflinePageBridge.isOfflineBookmarksEnabled()) return; |
| 78 | 82 |
| 79 // Making sure tab is worth keeping. | 83 // Making sure tab is worth keeping. |
| 80 if (shouldSkipSavingTabOffline(tab)) return; | 84 if (shouldSkipSavingTabOffline(tab)) return; |
| 81 | 85 |
| 82 OfflinePageBridge offlinePageBridge = OfflinePageBridge.getForProfile(ta b.getProfile()); | 86 final OfflinePageBridge offlinePageBridge = |
| 87 OfflinePageBridge.getForProfile(tab.getProfile()); | |
| 83 if (offlinePageBridge == null) return; | 88 if (offlinePageBridge == null) return; |
| 84 | 89 |
| 85 WebContents webContents = tab.getWebContents(); | 90 final WebContents webContents = tab.getWebContents(); |
| 86 ClientId clientId = ClientId.createClientIdForBookmarkId(bookmarkId); | 91 final ClientId clientId = ClientId.createClientIdForBookmarkId(bookmarkI d); |
| 92 final String url = webContents.getUrl(); | |
| 87 | 93 |
| 88 // TODO(fgorski): Ensure that request is queued if the model is not load ed. | 94 offlinePageBridge.getPagesByOnlineUrl(url, new Callback<List<OfflinePage Item>>() { |
|
fgorski
2016/06/15 20:19:01
this solution does not cover the case where you fa
romax
2016/06/16 01:24:26
I think we should have some logic here but I'm not
| |
| 89 offlinePageBridge.savePage(webContents, clientId, new OfflinePageBridge. SavePageCallback() { | |
| 90 @Override | 95 @Override |
| 91 public void onSavePageDone(int savePageResult, String url, long offl ineId) { | 96 public void onResult(List<OfflinePageItem> items) { |
| 92 // TODO(fgorski): Decide if we need to do anything with result. | 97 List<ClientId> pages_to_delete = new ArrayList<ClientId>(); |
| 93 // Perhaps some UMA reporting, but that can really happen somepl ace else. | 98 for (OfflinePageItem item : items) { |
| 99 if (item.getUrl().equals(url) | |
| 100 && item.getClientId().getNamespace().equals(clientId .getNamespace())) { | |
| 101 pages_to_delete.add(item.getClientId()); | |
| 102 } | |
| 103 } | |
| 104 offlinePageBridge.deletePagesByClientId(pages_to_delete, new Cal lback<Integer>() { | |
| 105 @Override | |
| 106 public void onResult(Integer deletePageResult) { | |
| 107 if (deletePageResult != DeletePageResult.SUCCESS | |
| 108 && deletePageResult != DeletePageResult.NOT_FOUN D) { | |
| 109 return; | |
| 110 } | |
| 111 offlinePageBridge.savePage( | |
| 112 webContents, clientId, new OfflinePageBridge.Sav ePageCallback() { | |
| 113 @Override | |
| 114 public void onSavePageDone( | |
| 115 int savePageResult, String url, long offlineId) { | |
| 116 // TODO(fgorski): Decide if we need to d o anything with | |
| 117 // result. | |
| 118 // Perhaps some UMA reporting, but that can really happen | |
| 119 // someplace else. | |
| 120 } | |
| 121 }); | |
| 122 } | |
| 123 }); | |
| 94 } | 124 } |
| 95 }); | 125 }); |
| 96 } | 126 } |
| 97 | 127 |
| 98 /** | 128 /** |
| 99 * Indicates whether we should skip saving the given tab as an offline page. | 129 * Indicates whether we should skip saving the given tab as an offline page. |
| 100 * A tab shouldn't be saved offline if it shows an error page or a sad tab p age. | 130 * A tab shouldn't be saved offline if it shows an error page or a sad tab p age. |
| 101 */ | 131 */ |
| 102 private static boolean shouldSkipSavingTabOffline(Tab tab) { | 132 private static boolean shouldSkipSavingTabOffline(Tab tab) { |
| 103 WebContents webContents = tab.getWebContents(); | 133 WebContents webContents = tab.getWebContents(); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 private static int batteryPercentage(Intent batteryStatus) { | 279 private static int batteryPercentage(Intent batteryStatus) { |
| 250 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | 280 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); |
| 251 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); | 281 int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); |
| 252 if (scale == 0) return 0; | 282 if (scale == 0) return 0; |
| 253 | 283 |
| 254 int percentage = Math.round(100 * level / (float) scale); | 284 int percentage = Math.round(100 * level / (float) scale); |
| 255 Log.d(TAG, "Battery Percentage is " + percentage); | 285 Log.d(TAG, "Battery Percentage is " + percentage); |
| 256 return percentage; | 286 return percentage; |
| 257 } | 287 } |
| 258 } | 288 } |
| OLD | NEW |