| Index: chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java b/chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java
|
| index 1ac4ba92c082b6676dda3138c1e31b3fd97b0508..7d39bdf9a124d15ac2d0531aa86aee3d022d338a 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java
|
| @@ -20,6 +20,7 @@ import android.text.TextUtils;
|
| import android.util.Pair;
|
|
|
| import org.chromium.base.ApiCompatibilityUtils;
|
| +import org.chromium.base.ContextUtils;
|
| import org.chromium.base.Log;
|
| import org.chromium.base.VisibleForTesting;
|
| import org.chromium.base.metrics.RecordHistogram;
|
| @@ -224,7 +225,6 @@ public class IntentHandler {
|
|
|
| private final IntentHandlerDelegate mDelegate;
|
| private final String mPackageName;
|
| - private KeyguardManager mKeyguardManager;
|
|
|
| /**
|
| * Receiver for screen unlock broadcast.
|
| @@ -349,11 +349,10 @@ public class IntentHandler {
|
| /**
|
| * Handles an Intent after the ChromeTabbedActivity decides that it shouldn't ignore the
|
| * Intent.
|
| - * @param context Android Context.
|
| * @param intent Target intent.
|
| * @return Whether the Intent was successfully handled.
|
| */
|
| - boolean onNewIntent(Context context, Intent intent) {
|
| + boolean onNewIntent(Intent intent) {
|
| updateDeferredIntent(null);
|
|
|
| assert intentHasValidUrl(intent);
|
| @@ -368,7 +367,7 @@ public class IntentHandler {
|
| return handleWebSearchIntent(intent);
|
| }
|
|
|
| - String referrerUrl = getReferrerUrlIncludingExtraHeaders(intent, context);
|
| + String referrerUrl = getReferrerUrlIncludingExtraHeaders(intent);
|
| String extraHeaders = getExtraHeadersFromIntent(intent);
|
|
|
| // TODO(joth): Presumably this should check the action too.
|
| @@ -401,10 +400,9 @@ public class IntentHandler {
|
| * Extracts referrer URL string. The extra is used if we received it from a first party app or
|
| * if the referrer_extra is specified as android-app://package style URL.
|
| * @param intent The intent from which to extract the URL.
|
| - * @param context The activity that received the intent.
|
| * @return The URL string or null if none should be used.
|
| */
|
| - private static String getReferrerUrl(Intent intent, Context context) {
|
| + private static String getReferrerUrl(Intent intent) {
|
| Uri referrerExtra = getReferrer(intent);
|
| if (referrerExtra == null) return null;
|
| String referrerUrl = IntentHandler.getPendingReferrerUrl(
|
| @@ -413,7 +411,7 @@ public class IntentHandler {
|
| return referrerUrl;
|
| } else if (isValidReferrerHeader(referrerExtra.toString())) {
|
| return referrerExtra.toString();
|
| - } else if (IntentHandler.isIntentChromeOrFirstParty(intent, context)) {
|
| + } else if (IntentHandler.isIntentChromeOrFirstParty(intent)) {
|
| return referrerExtra.toString();
|
| }
|
| return null;
|
| @@ -425,11 +423,10 @@ public class IntentHandler {
|
| * The referrer extra takes priority over the "extra headers" one.
|
| *
|
| * @param intent The Intent containing the extras.
|
| - * @param context The application context.
|
| * @return The referrer, or null.
|
| */
|
| - public static String getReferrerUrlIncludingExtraHeaders(Intent intent, Context context) {
|
| - String referrerUrl = getReferrerUrl(intent, context);
|
| + public static String getReferrerUrlIncludingExtraHeaders(Intent intent) {
|
| + String referrerUrl = getReferrerUrl(intent);
|
| if (referrerUrl != null) return referrerUrl;
|
|
|
| Bundle bundleExtraHeaders = IntentUtils.safeGetBundleExtra(intent, Browser.EXTRA_HEADERS);
|
| @@ -449,8 +446,8 @@ public class IntentHandler {
|
| * @param params The {@link LoadUrlParams} to add referrer and headers.
|
| * @param intent The intent we use to parse the extras.
|
| */
|
| - public static void addReferrerAndHeaders(LoadUrlParams params, Intent intent, Context context) {
|
| - String referrer = getReferrerUrlIncludingExtraHeaders(intent, context);
|
| + public static void addReferrerAndHeaders(LoadUrlParams params, Intent intent) {
|
| + String referrer = getReferrerUrlIncludingExtraHeaders(intent);
|
| if (referrer != null) {
|
| params.setReferrer(new Referrer(referrer, Referrer.REFERRER_POLICY_DEFAULT));
|
| }
|
| @@ -529,8 +526,9 @@ public class IntentHandler {
|
| return true;
|
| }
|
|
|
| - private static PendingIntent getAuthenticationToken(Context appContext) {
|
| + private static PendingIntent getAuthenticationToken() {
|
| Intent fakeIntent = new Intent();
|
| + Context appContext = ContextUtils.getApplicationContext();
|
| fakeIntent.setComponent(getFakeComponentName(appContext.getPackageName()));
|
| return PendingIntent.getActivity(appContext, 0, fakeIntent, 0);
|
| }
|
| @@ -542,8 +540,8 @@ public class IntentHandler {
|
| * identify ourselves as a trusted sender. The method {@link #shouldIgnoreIntent} validates the
|
| * token.
|
| */
|
| - public static void startActivityForTrustedIntent(Intent intent, Context context) {
|
| - startActivityForTrustedIntentInternal(intent, context, null);
|
| + public static void startActivityForTrustedIntent(Intent intent) {
|
| + startActivityForTrustedIntentInternal(intent, null);
|
| }
|
|
|
| /**
|
| @@ -556,29 +554,27 @@ public class IntentHandler {
|
| * identify ourselves as a trusted sender. The method {@link #shouldIgnoreIntent} validates the
|
| * token.
|
| */
|
| - public static void startChromeLauncherActivityForTrustedIntent(Intent intent, Context context) {
|
| + public static void startChromeLauncherActivityForTrustedIntent(Intent intent) {
|
| // Specify the exact component that will handle creating a new tab. This allows specifying
|
| // URLs that are not exposed in the intent filters (i.e. chrome://).
|
| - startActivityForTrustedIntentInternal(intent, context, new ComponentName(
|
| - context.getPackageName(), TAB_ACTIVITY_COMPONENT_CLASS_NAME));
|
| + startActivityForTrustedIntentInternal(intent, TAB_ACTIVITY_COMPONENT_CLASS_NAME);
|
| }
|
|
|
| private static void startActivityForTrustedIntentInternal(
|
| - Intent intent, Context context, ComponentName componentName) {
|
| + Intent intent, String componentClassName) {
|
| + Context appContext = ContextUtils.getApplicationContext();
|
| // The caller might want to re-use the Intent, so we'll use a copy.
|
| Intent copiedIntent = new Intent(intent);
|
|
|
| - if (componentName != null) {
|
| + if (componentClassName != null) {
|
| assert copiedIntent.getComponent() == null;
|
| // Specify the exact component that will handle creating a new tab. This allows
|
| // specifying URLs that are not exposed in the intent filters (i.e. chrome://).
|
| - copiedIntent.setComponent(componentName);
|
| + copiedIntent.setComponent(
|
| + new ComponentName(appContext.getPackageName(), componentClassName));
|
| }
|
|
|
| - addTrustedIntentExtras(copiedIntent, context);
|
| -
|
| - // Make sure we use the application context.
|
| - Context appContext = context.getApplicationContext();
|
| + addTrustedIntentExtras(copiedIntent);
|
| appContext.startActivity(copiedIntent);
|
| }
|
|
|
| @@ -586,17 +582,17 @@ public class IntentHandler {
|
| * Sets TRUSTED_APPLICATION_CODE_EXTRA on the provided intent to identify it as coming from
|
| * a trusted source.
|
| */
|
| - public static void addTrustedIntentExtras(Intent intent, Context context) {
|
| - if (ExternalNavigationDelegateImpl.willChromeHandleIntent(context, intent, true)) {
|
| + public static void addTrustedIntentExtras(Intent intent) {
|
| + Context appContext = ContextUtils.getApplicationContext();
|
| + if (ExternalNavigationDelegateImpl.willChromeHandleIntent(appContext, intent, true)) {
|
| // The PendingIntent functions as an authentication token --- it could only have come
|
| // from us. Stash it in the real Intent as an extra. shouldIgnoreIntent will retrieve it
|
| // and check it with isIntentChromeInternal.
|
| - intent.putExtra(TRUSTED_APPLICATION_CODE_EXTRA,
|
| - getAuthenticationToken(context.getApplicationContext()));
|
| + intent.putExtra(TRUSTED_APPLICATION_CODE_EXTRA, getAuthenticationToken());
|
| // It is crucial that we never leak the authentication token to other packages, because
|
| // then the other package could be used to impersonate us/do things as us. Therefore,
|
| // scope the real Intent to our package.
|
| - intent.setPackage(context.getApplicationContext().getPackageName());
|
| + intent.setPackage(appContext.getPackageName());
|
| }
|
| }
|
|
|
| @@ -644,11 +640,10 @@ public class IntentHandler {
|
| /**
|
| * Returns true if the app should ignore a given intent.
|
| *
|
| - * @param context Android Context.
|
| * @param intent Intent to check.
|
| * @return true if the intent should be ignored.
|
| */
|
| - public boolean shouldIgnoreIntent(Context context, Intent intent) {
|
| + public boolean shouldIgnoreIntent(Intent intent) {
|
| // Although not documented to, many/most methods that retrieve values from an Intent may
|
| // throw. Because we can't control what packages might send to us, we should catch any
|
| // Throwable and then fail closed (safe). This is ugly, but resolves top crashers in the
|
| @@ -661,8 +656,8 @@ public class IntentHandler {
|
|
|
| // Determine if this intent came from a trustworthy source (either Chrome or Google
|
| // first party applications).
|
| - boolean isInternal = isIntentChromeOrFirstParty(intent, context);
|
| - boolean isFromChrome = wasIntentSenderChrome(intent, context);
|
| + boolean isInternal = isIntentChromeOrFirstParty(intent);
|
| + boolean isFromChrome = wasIntentSenderChrome(intent);
|
|
|
| // "Open new incognito tab" is currently limited to Chrome.
|
| //
|
| @@ -712,7 +707,7 @@ public class IntentHandler {
|
|
|
| // We must check for screen state at this point.
|
| // These might be slow.
|
| - boolean internalOrVisible = isInternal || isIntentUserVisible(context);
|
| + boolean internalOrVisible = isInternal || isIntentUserVisible();
|
| if (!internalOrVisible) {
|
| updateDeferredIntent(intent);
|
| return true;
|
| @@ -744,19 +739,17 @@ public class IntentHandler {
|
| intent, TRUSTED_APPLICATION_CODE_EXTRA);
|
| }
|
|
|
| - private static boolean isChromeToken(PendingIntent token, Context context) {
|
| + private static boolean isChromeToken(PendingIntent token) {
|
| // Fetch what should be a matching token.
|
| - Context appContext = context.getApplicationContext();
|
| - PendingIntent pending = getAuthenticationToken(appContext);
|
| + PendingIntent pending = getAuthenticationToken();
|
| return pending.equals(token);
|
| }
|
|
|
| /**
|
| * @param intent An Intent to be checked.
|
| - * @param context A context.
|
| * @return Whether an intent originates from Chrome.
|
| */
|
| - public static boolean wasIntentSenderChrome(Intent intent, Context context) {
|
| + public static boolean wasIntentSenderChrome(Intent intent) {
|
| if (intent == null) return false;
|
|
|
| PendingIntent token = fetchAuthenticationTokenFromIntent(intent);
|
| @@ -764,15 +757,14 @@ public class IntentHandler {
|
|
|
| // Do not ignore a valid URL Intent if the sender is Chrome. (If the PendingIntents are
|
| // equal, we know that the sender was us.)
|
| - return isChromeToken(token, context);
|
| + return isChromeToken(token);
|
| }
|
|
|
| /**
|
| * @param intent An Intent to be checked.
|
| - * @param context A context.
|
| * @return Whether an intent originates from Chrome or a first-party app.
|
| */
|
| - public static boolean isIntentChromeOrFirstParty(Intent intent, Context context) {
|
| + public static boolean isIntentChromeOrFirstParty(Intent intent) {
|
| if (intent == null) return false;
|
|
|
| PendingIntent token = fetchAuthenticationTokenFromIntent(intent);
|
| @@ -780,26 +772,26 @@ public class IntentHandler {
|
|
|
| // Do not ignore a valid URL Intent if the sender is Chrome. (If the PendingIntents are
|
| // equal, we know that the sender was us.)
|
| - if (isChromeToken(token, context)) {
|
| + if (isChromeToken(token)) {
|
| return true;
|
| }
|
| if (ExternalAuthUtils.getInstance().isGoogleSigned(
|
| - context, ApiCompatibilityUtils.getCreatorPackage(token))) {
|
| + ContextUtils.getApplicationContext(),
|
| + ApiCompatibilityUtils.getCreatorPackage(token))) {
|
| return true;
|
| }
|
| return false;
|
| }
|
|
|
| @VisibleForTesting
|
| - boolean isIntentUserVisible(Context context) {
|
| + boolean isIntentUserVisible() {
|
| // Only process Intents if the screen is on and the device is unlocked;
|
| // i.e. the user will see what is going on.
|
| - if (mKeyguardManager == null) {
|
| - mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
|
| - }
|
| - if (!ApiCompatibilityUtils.isInteractive(context)) return false;
|
| - return !ApiCompatibilityUtils.isDeviceProvisioned(context)
|
| - || !mKeyguardManager.inKeyguardRestrictedInputMode();
|
| + Context appContext = ContextUtils.getApplicationContext();
|
| + if (!ApiCompatibilityUtils.isInteractive(appContext)) return false;
|
| + if (!ApiCompatibilityUtils.isDeviceProvisioned(appContext)) return true;
|
| + return !((KeyguardManager) appContext.getSystemService(Context.KEYGUARD_SERVICE))
|
| + .inKeyguardRestrictedInputMode();
|
| }
|
|
|
| /*
|
| @@ -995,20 +987,18 @@ public class IntentHandler {
|
|
|
| /**
|
| * Some applications may request to load the URL with a particular transition type.
|
| - * @param context The application context.
|
| * @param intent Intent causing the URL load, may be null.
|
| * @param defaultTransition The transition to return if none specified in the intent.
|
| * @return The transition type to use for loading the URL.
|
| */
|
| - public static int getTransitionTypeFromIntent(Context context, Intent intent,
|
| - int defaultTransition) {
|
| + public static int getTransitionTypeFromIntent(Intent intent, int defaultTransition) {
|
| if (intent == null) return defaultTransition;
|
| int transitionType = IntentUtils.safeGetIntExtra(
|
| intent, IntentHandler.EXTRA_PAGE_TRANSITION_TYPE, PageTransition.LINK);
|
| if (transitionType == PageTransition.TYPED) {
|
| return transitionType;
|
| } else if (transitionType != PageTransition.LINK
|
| - && isIntentChromeOrFirstParty(intent, context)) {
|
| + && isIntentChromeOrFirstParty(intent)) {
|
| // 1st party applications may specify any transition type.
|
| return transitionType;
|
| }
|
|
|