Commit 3c7b52b6 authored by Carlos Knippschild's avatar Carlos Knippschild Committed by Commit Bot

Allow sharing a loaded Offline Page via the Share... menu item

Also changes the refactors the creation of the ShareParams instance for
the offline page to accept a callback for the send the result and run
the needed IO operation in a background task (avoiding strict-mode
violations).

Bug: 758690
Change-Id: I57cd200961b851dc384ee11ca118a791244a3d7c
Reviewed-on: https://chromium-review.googlesource.com/923361
Commit-Queue: Carlos Knippschild <carlosk@chromium.org>
Reviewed-by: default avatarDmitry Titov <dimich@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537474}
parent b89470b0
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.offlinepages;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import org.chromium.base.ActivityState;
......@@ -345,44 +346,48 @@ public class OfflinePageUtils {
}
/**
* Share an offline copy of the current page.
* If possible, creates the ShareParams needed to share the current offline page loaded in the
* provided tab as a MHTML file.
*
* @param activity The activity used for sharing and file provider interaction.
* @param currentTab The current tab for which sharing is being done.
* @param currentTab The current tab from which the page is being shared.
* @param shareCallback The callback to be used to send the ShareParams. This will only be
* called if this function call returns true.
* @return true if the sharing of the page is possible and the callback will be invoked.
*/
public static ShareParams buildShareParams(final Activity activity, final Tab tab) {
if (tab == null) return null;
public static boolean maybeShareOfflinePage(
final Activity activity, Tab tab, final Callback<ShareParams> shareCallback) {
if (tab == null) return false;
final OfflinePageBridge offlinePageBridge =
getInstance().getOfflinePageBridge(tab.getProfile());
OfflinePageBridge offlinePageBridge = getInstance().getOfflinePageBridge(tab.getProfile());
if (offlinePageBridge == null) {
Log.e(TAG, "Unable to perform sharing on current tab.");
return null;
return false;
}
OfflinePageItem offlinePage = offlinePageBridge.getOfflinePage(tab.getWebContents());
// Bail if there is no offline page that can be shared.
if (offlinePage == null) return null;
// Only cached pages are supported right now.
// TODO(fgorski): Provide sharing support for user requested pages.
if (!isCachedPage(offlinePage)) return null;
final File offlineFile = new File(offlinePage.getFilePath());
final Uri contentUri = ChromeFileProvider.generateUri(activity, offlineFile);
return new ShareParams.Builder(activity, tab.getTitle(), tab.getUrl())
.setShareDirectly(false)
.setOfflineUri(contentUri)
.build();
}
// Bail if there is no offline page or sharing is not enabled.
if (offlinePage == null || !OfflinePageBridge.isPageSharingEnabled()) return false;
final String tabTitle = tab.getTitle();
final String tabUrl = tab.getUrl();
final File offlinePageFile = new File(offlinePage.getFilePath());
AsyncTask<Void, Void, Uri> task = new AsyncTask<Void, Void, Uri>() {
protected Uri doInBackground(Void... v) {
return ChromeFileProvider.generateUri(activity, offlinePageFile);
}
protected void onPostExecute(Uri uri) {
ShareParams shareParams = new ShareParams.Builder(activity, tabTitle, tabUrl)
.setShareDirectly(false)
.setOfflineUri(uri)
.build();
shareCallback.onResult(shareParams);
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
private static boolean isCachedPage(OfflinePageItem offlinePage) {
assert offlinePage != null;
String namespace = offlinePage.getClientId().getNamespace();
return namespace != OfflinePageBridge.DOWNLOAD_NAMESPACE
&& namespace != OfflinePageBridge.ASYNC_NAMESPACE
&& namespace != OfflinePageBridge.BROWSER_ACTIONS_NAMESPACE;
return true;
}
/**
......
......@@ -13,7 +13,6 @@ import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.offlinepages.OfflinePageBridge;
import org.chromium.chrome.browser.offlinepages.OfflinePageUtils;
import org.chromium.chrome.browser.physicalweb.PhysicalWebShareActivity;
import org.chromium.chrome.browser.printing.PrintShareActivity;
......@@ -150,14 +149,12 @@ public class ShareMenuActionHandler {
private void triggerShare(final Activity activity, final Tab currentTab,
final boolean shareDirectly, boolean isIncognito) {
boolean isOffline = OfflinePageUtils.isOfflinePage(currentTab);
RecordHistogram.recordBooleanHistogram("OfflinePages.SharedPageWasOffline", isOffline);
boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled();
if (canShareOfflinePage && isOffline) {
ShareParams params = OfflinePageUtils.buildShareParams(activity, currentTab);
if (params == null) return;
mDelegate.share(params);
boolean isOfflinePage = OfflinePageUtils.isOfflinePage(currentTab);
RecordHistogram.recordBooleanHistogram("OfflinePages.SharedPageWasOffline", isOfflinePage);
if (isOfflinePage
&& OfflinePageUtils.maybeShareOfflinePage(
activity, currentTab, (ShareParams p) -> mDelegate.share(p))) {
return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment