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; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.offlinepages;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment; import android.os.Environment;
import org.chromium.base.ActivityState; import org.chromium.base.ActivityState;
...@@ -345,44 +346,48 @@ public class OfflinePageUtils { ...@@ -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 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) { public static boolean maybeShareOfflinePage(
if (tab == null) return null; final Activity activity, Tab tab, final Callback<ShareParams> shareCallback) {
if (tab == null) return false;
final OfflinePageBridge offlinePageBridge = OfflinePageBridge offlinePageBridge = getInstance().getOfflinePageBridge(tab.getProfile());
getInstance().getOfflinePageBridge(tab.getProfile());
if (offlinePageBridge == null) { if (offlinePageBridge == null) {
Log.e(TAG, "Unable to perform sharing on current tab."); Log.e(TAG, "Unable to perform sharing on current tab.");
return null; return false;
} }
OfflinePageItem offlinePage = offlinePageBridge.getOfflinePage(tab.getWebContents()); OfflinePageItem offlinePage = offlinePageBridge.getOfflinePage(tab.getWebContents());
// Bail if there is no offline page that can be shared. // Bail if there is no offline page or sharing is not enabled.
if (offlinePage == null) return null; if (offlinePage == null || !OfflinePageBridge.isPageSharingEnabled()) return false;
// Only cached pages are supported right now. final String tabTitle = tab.getTitle();
// TODO(fgorski): Provide sharing support for user requested pages. final String tabUrl = tab.getUrl();
if (!isCachedPage(offlinePage)) return null; final File offlinePageFile = new File(offlinePage.getFilePath());
AsyncTask<Void, Void, Uri> task = new AsyncTask<Void, Void, Uri>() {
final File offlineFile = new File(offlinePage.getFilePath()); protected Uri doInBackground(Void... v) {
final Uri contentUri = ChromeFileProvider.generateUri(activity, offlineFile); return ChromeFileProvider.generateUri(activity, offlinePageFile);
}
return new ShareParams.Builder(activity, tab.getTitle(), tab.getUrl()) protected void onPostExecute(Uri uri) {
.setShareDirectly(false) ShareParams shareParams = new ShareParams.Builder(activity, tabTitle, tabUrl)
.setOfflineUri(contentUri) .setShareDirectly(false)
.build(); .setOfflineUri(uri)
} .build();
shareCallback.onResult(shareParams);
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
private static boolean isCachedPage(OfflinePageItem offlinePage) { return true;
assert offlinePage != null;
String namespace = offlinePage.getClientId().getNamespace();
return namespace != OfflinePageBridge.DOWNLOAD_NAMESPACE
&& namespace != OfflinePageBridge.ASYNC_NAMESPACE
&& namespace != OfflinePageBridge.BROWSER_ACTIONS_NAMESPACE;
} }
/** /**
......
...@@ -13,7 +13,6 @@ import org.chromium.base.VisibleForTesting; ...@@ -13,7 +13,6 @@ import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.browser.UrlConstants; 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.offlinepages.OfflinePageUtils;
import org.chromium.chrome.browser.physicalweb.PhysicalWebShareActivity; import org.chromium.chrome.browser.physicalweb.PhysicalWebShareActivity;
import org.chromium.chrome.browser.printing.PrintShareActivity; import org.chromium.chrome.browser.printing.PrintShareActivity;
...@@ -150,14 +149,12 @@ public class ShareMenuActionHandler { ...@@ -150,14 +149,12 @@ public class ShareMenuActionHandler {
private void triggerShare(final Activity activity, final Tab currentTab, private void triggerShare(final Activity activity, final Tab currentTab,
final boolean shareDirectly, boolean isIncognito) { final boolean shareDirectly, boolean isIncognito) {
boolean isOffline = OfflinePageUtils.isOfflinePage(currentTab); boolean isOfflinePage = OfflinePageUtils.isOfflinePage(currentTab);
RecordHistogram.recordBooleanHistogram("OfflinePages.SharedPageWasOffline", isOffline); RecordHistogram.recordBooleanHistogram("OfflinePages.SharedPageWasOffline", isOfflinePage);
boolean canShareOfflinePage = OfflinePageBridge.isPageSharingEnabled(); if (isOfflinePage
if (canShareOfflinePage && isOffline) { && OfflinePageUtils.maybeShareOfflinePage(
ShareParams params = OfflinePageUtils.buildShareParams(activity, currentTab); activity, currentTab, (ShareParams p) -> mDelegate.share(p))) {
if (params == null) return;
mDelegate.share(params);
return; 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