Commit 7db85418 authored by mgiuca's avatar mgiuca Committed by Commit bot

Web Share: Show a warning prompt when share is used from incognito.

This is the same dialog as shown when external links are opened from
incognito, informing the user that they are leaving incognito mode and
potentially sharing data with an external application.

BUG=645007

Review-Url: https://codereview.chromium.org/2354833002
Cr-Commit-Position: refs/heads/master@{#420564}
parent 945f1f0b
......@@ -363,17 +363,37 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
}
}
/**
* Shows an alert dialog prompting the user to leave incognito mode.
*
* @param activity The {@link Activity} to launch the dialog from.
* @param onAccept Will be called when the user chooses to leave incognito.
* @param onCancel Will be called when the user declines to leave incognito.
*/
public static void showLeaveIncognitoWarningDialog(Activity activity,
final OnClickListener onAccept, final OnCancelListener onCancel) {
new AlertDialog.Builder(activity, R.style.AlertDialogTheme)
.setTitle(R.string.external_app_leave_incognito_warning_title)
.setMessage(R.string.external_app_leave_incognito_warning)
.setPositiveButton(R.string.ok, onAccept)
.setNegativeButton(R.string.cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onCancel.onCancel(dialog);
}
})
.setOnCancelListener(onCancel)
.show();
}
@Override
public void startIncognitoIntent(final Intent intent, final String referrerUrl,
final String fallbackUrl, final Tab tab, final boolean needsToCloseTab) {
Context context = tab.getWindowAndroid().getContext().get();
if (!(context instanceof Activity)) return;
Activity activity = (Activity) context;
new AlertDialog.Builder(activity, R.style.AlertDialogTheme)
.setTitle(R.string.external_app_leave_incognito_warning_title)
.setMessage(R.string.external_app_leave_incognito_warning)
.setPositiveButton(R.string.ok, new OnClickListener() {
showLeaveIncognitoWarningDialog((Activity) context,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(intent);
......@@ -382,20 +402,13 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat
closeTab(tab);
}
}
})
.setNegativeButton(R.string.cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
loadIntent(intent, referrerUrl, fallbackUrl, tab, needsToCloseTab, true);
}
})
.setOnCancelListener(new OnCancelListener() {
},
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
loadIntent(intent, referrerUrl, fallbackUrl, tab, needsToCloseTab, true);
}
})
.show();
});
}
@Override
......
......@@ -6,9 +6,13 @@ package org.chromium.chrome.browser.webshare;
import android.app.Activity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.support.annotation.Nullable;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.chrome.browser.share.ShareHelper;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content_public.browser.WebContents;
......@@ -23,6 +27,7 @@ import org.chromium.webshare.mojom.ShareService;
*/
public class ShareServiceImpl implements ShareService {
private final Activity mActivity;
private final boolean mIsIncognito;
// These numbers are written to histograms. Keep in sync with WebShareMethod enum in
// histograms.xml, and don't reuse or renumber entries (except for the _COUNT entry).
......@@ -40,6 +45,7 @@ public class ShareServiceImpl implements ShareService {
public ShareServiceImpl(@Nullable WebContents webContents) {
mActivity = activityFromWebContents(webContents);
mIsIncognito = webContents.isIncognito();
}
@Override
......@@ -60,6 +66,28 @@ public class ShareServiceImpl implements ShareService {
return;
}
if (mIsIncognito) {
// In incognito mode, confirm with the user before sending intent externally.
showIncognitoWarningDialog(title, text, url, callback);
} else {
startShare(title, text, url, callback);
}
}
@Nullable
private static Activity activityFromWebContents(@Nullable WebContents webContents) {
if (webContents == null) return null;
ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webContents);
if (contentViewCore == null) return null;
WindowAndroid window = contentViewCore.getWindowAndroid();
if (window == null) return null;
return window.getActivity().get();
}
private void startShare(String title, String text, Url url, final ShareResponse callback) {
ShareHelper.TargetChosenCallback innerCallback = new ShareHelper.TargetChosenCallback() {
public void onTargetChosen(ComponentName chosenComponent) {
RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
......@@ -68,25 +96,33 @@ public class ShareServiceImpl implements ShareService {
}
public void onCancel() {
RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
WEBSHARE_OUTCOME_CANCELED, WEBSHARE_OUTCOME_COUNT);
callback.call("Share canceled");
cancelShare(callback);
}
};
ShareHelper.share(false, false, mActivity, title, text, url.url, null, null, innerCallback);
}
@Nullable
private static Activity activityFromWebContents(@Nullable WebContents webContents) {
if (webContents == null) return null;
ContentViewCore contentViewCore = ContentViewCore.fromWebContents(webContents);
if (contentViewCore == null) return null;
WindowAndroid window = contentViewCore.getWindowAndroid();
if (window == null) return null;
private static void cancelShare(ShareResponse callback) {
RecordHistogram.recordEnumeratedHistogram("WebShare.ShareOutcome",
WEBSHARE_OUTCOME_CANCELED, WEBSHARE_OUTCOME_COUNT);
callback.call("Share canceled");
}
return window.getActivity().get();
private void showIncognitoWarningDialog(final String title, final String text, final Url url,
final ShareResponse callback) {
ExternalNavigationDelegateImpl.showLeaveIncognitoWarningDialog(mActivity,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startShare(title, text, url, callback);
}
},
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cancelShare(callback);
}
});
}
}
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