org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl Java Examples

The following examples show how to use org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActivityDelegate.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether or not the Intent corresponds to an Activity that should be tracked.
 * @param isIncognito Whether or not the TabModel is managing incognito tabs.
 * @param intent Intent used to launch the Activity.
 * @return Whether or not to track the Activity.
 */
public boolean isValidActivity(boolean isIncognito, Intent intent) {
    if (intent == null) return false;
    String desiredClassName = isIncognito ? mIncognitoClass.getName() : mRegularClass.getName();
    String desiredLegacyClassName = isIncognito
            ? DocumentActivity.LEGACY_INCOGNITO_CLASS_NAME
            : DocumentActivity.LEGACY_CLASS_NAME;
    String className = null;
    if (intent.getComponent() == null) {
        ResolveInfo resolveInfo = ExternalNavigationDelegateImpl.resolveActivity(intent);
        if (resolveInfo != null) className = resolveInfo.activityInfo.name;
    } else {
        className = intent.getComponent().getClassName();
    }

    return TextUtils.equals(className, desiredClassName)
            || TextUtils.equals(className, desiredLegacyClassName);
}
 
Example #2
Source File: DownloadManagerService.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Launch the intent for a given download item.
 * TODO(qinmin): Move this to DownloadManagerDelegate.
 *
 * @param downloadInfo Info about the downloaded item.
 * @param downloadId   ID of the download item in DownloadManager.
 */
protected void openDownloadedContent(final DownloadInfo downloadInfo, final long downloadId) {
    final boolean isSupportedMimeType = isSupportedMimeType(downloadInfo.getMimeType());
    new AsyncTask<Void, Void, Intent>() {
        @Override
        public Intent doInBackground(Void... params) {
            return getLaunchIntentFromDownloadId(
                    mContext, downloadInfo.getFilePath(), downloadId, isSupportedMimeType);
        }

        @Override
        protected void onPostExecute(Intent intent) {
            if (intent == null) return;

            Context context = ContextUtils.getApplicationContext();
            if (ExternalNavigationDelegateImpl.resolveIntent(context, intent, true)) {
                DownloadUtils.fireOpenIntentForDownload(context, intent);
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #3
Source File: ActivityDelegate.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether or not the Intent corresponds to an Activity that should be tracked.
 * @param isIncognito Whether or not the TabModel is managing incognito tabs.
 * @param intent Intent used to launch the Activity.
 * @return Whether or not to track the Activity.
 */
public boolean isValidActivity(boolean isIncognito, Intent intent) {
    if (intent == null) return false;
    String desiredClassName = isIncognito ? mIncognitoClass.getName() : mRegularClass.getName();
    String desiredLegacyClassName = isIncognito
            ? DocumentActivity.LEGACY_INCOGNITO_CLASS_NAME
            : DocumentActivity.LEGACY_CLASS_NAME;
    String className = null;
    if (intent.getComponent() == null) {
        ResolveInfo resolveInfo = ExternalNavigationDelegateImpl.resolveActivity(intent);
        if (resolveInfo != null) className = resolveInfo.activityInfo.name;
    } else {
        className = intent.getComponent().getClassName();
    }

    return TextUtils.equals(className, desiredClassName)
            || TextUtils.equals(className, desiredLegacyClassName);
}
 
Example #4
Source File: ActivityDelegate.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether or not the Intent corresponds to an Activity that should be tracked.
 * @param isIncognito Whether or not the TabModel is managing incognito tabs.
 * @param intent Intent used to launch the Activity.
 * @return Whether or not to track the Activity.
 */
public boolean isValidActivity(boolean isIncognito, Intent intent) {
    if (intent == null) return false;
    String desiredClassName = isIncognito ? mIncognitoClass.getName() : mRegularClass.getName();
    String desiredLegacyClassName = isIncognito
            ? DocumentActivity.LEGACY_INCOGNITO_CLASS_NAME
            : DocumentActivity.LEGACY_CLASS_NAME;
    String className = null;
    if (intent.getComponent() == null) {
        ResolveInfo resolveInfo = ExternalNavigationDelegateImpl.resolveActivity(intent);
        if (resolveInfo != null) className = resolveInfo.activityInfo.name;
    } else {
        className = intent.getComponent().getClassName();
    }

    return TextUtils.equals(className, desiredClassName)
            || TextUtils.equals(className, desiredLegacyClassName);
}
 
Example #5
Source File: IntentHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Sets TRUSTED_APPLICATION_CODE_EXTRA on the provided intent to identify it as coming from
 * a trusted source.
 */
public static void addTrustedIntentExtras(Intent intent) {
    if (ExternalNavigationDelegateImpl.willChromeHandleIntent(intent, true)) {
        // 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(ContextUtils.getApplicationContext().getPackageName());
        // 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());
    }
}
 
Example #6
Source File: IntentHandler.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * 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)) {
        // 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()));
        // 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());
    }
}
 
Example #7
Source File: CustomTabDelegateFactory.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public InterceptNavigationDelegateImpl createInterceptNavigationDelegate(Tab tab) {
    if (mIsOpenedByChrome) {
        mNavigationDelegate = new ExternalNavigationDelegateImpl(tab);
    } else {
        mNavigationDelegate = new CustomTabNavigationDelegate(tab, tab.getAppAssociatedWith());
    }
    mNavigationHandler = new ExternalNavigationHandler(mNavigationDelegate);
    return new InterceptNavigationDelegateImpl(mNavigationHandler, tab);
}
 
Example #8
Source File: IntentHandler.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * 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)) {
        // 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()));
        // 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());
    }
}
 
Example #9
Source File: DownloadManagerService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Launch the intent for a given download item, or Download Home if that's not possible.
 * TODO(qinmin): Move this to DownloadManagerDelegate.
 *
 * @param context             Context to use.
 * @param filePath            Path to the downloaded item.
 * @param isSupportedMimeType MIME type of the downloaded item.
 * @param isOffTheRecord      Whether the download was for a off the record profile.
 * @param downloadGuid        GUID of the download item in DownloadManager.
 * @param downloadId          ID of the download item in DownloadManager.
 */
protected static void openDownloadedContent(final Context context, final String filePath,
        final boolean isSupportedMimeType, final boolean isOffTheRecord,
        final String downloadGuid, final long downloadId) {
    new AsyncTask<Void, Void, Intent>() {
        @Override
        public Intent doInBackground(Void... params) {
            return getLaunchIntentFromDownloadId(
                    context, filePath, downloadId, isSupportedMimeType);
        }

        @Override
        protected void onPostExecute(Intent intent) {
            boolean didLaunchIntent = intent != null
                    && ExternalNavigationDelegateImpl.resolveIntent(intent, true)
                    && DownloadUtils.fireOpenIntentForDownload(context, intent);

            if (!didLaunchIntent) {
                openDownloadsPage(context);
                return;
            }

            if (didLaunchIntent && hasDownloadManagerService()) {
                DownloadManagerService.getDownloadManagerService().updateLastAccessTime(
                        downloadGuid, isOffTheRecord);
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
 
Example #10
Source File: CustomTabDelegateFactory.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public InterceptNavigationDelegateImpl createInterceptNavigationDelegate(Tab tab) {
    if (mIsOpenedByChrome) {
        mNavigationDelegate = new ExternalNavigationDelegateImpl(tab);
    } else {
        mNavigationDelegate = new CustomTabNavigationDelegate(tab, tab.getAppAssociatedWith());
    }
    mNavigationHandler = new ExternalNavigationHandler(mNavigationDelegate);
    return new InterceptNavigationDelegateImpl(mNavigationHandler, tab);
}
 
Example #11
Source File: InstantAppsHandler.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
private boolean handleIncomingIntentInternal(
        Context context, Intent intent, boolean isCustomTabsIntent, long startTime) {
    boolean isEnabled = isEnabled(context);
    if (!isEnabled || (isCustomTabsIntent && !IntentUtils.safeGetBooleanExtra(
            intent, CUSTOM_APPS_INSTANT_APP_EXTRA, false))) {
        Log.i(TAG, "Not handling with Instant Apps. Enabled? " + isEnabled);
        return false;
    }

    if (IntentUtils.safeGetBooleanExtra(intent, DO_NOT_LAUNCH_EXTRA, false)) {
        maybeRecordFallbackStats(intent);
        Log.i(TAG, "Not handling with Instant Apps (DO_NOT_LAUNCH_EXTRA)");
        return false;
    }

    if (IntentUtils.safeGetBooleanExtra(
            intent, IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, false)
            || IntentUtils.safeHasExtra(intent, ShortcutHelper.EXTRA_SOURCE)
            || isIntentFromChrome(context, intent)
            || (IntentHandler.getUrlFromIntent(intent) == null)) {
        Log.i(TAG, "Not handling with Instant Apps (other)");
        return false;
    }

    // Used to search for the intent handlers. Needs null component to return correct results.
    Intent intentCopy = new Intent(intent);
    intentCopy.setComponent(null);
    Intent selector = intentCopy.getSelector();
    if (selector != null) selector.setComponent(null);

    if (!(isCustomTabsIntent || isChromeDefaultHandler(context))
            || ExternalNavigationDelegateImpl.isPackageSpecializedHandler(
                    context, null, intentCopy)) {
        // Chrome is not the default browser or a specialized handler exists.
        Log.i(TAG, "Not handling with Instant Apps because Chrome is not default or "
                + "there's a specialized handler");
        return false;
    }

    Intent callbackIntent = new Intent(intent);
    callbackIntent.putExtra(DO_NOT_LAUNCH_EXTRA, true);
    callbackIntent.putExtra(INSTANT_APP_START_TIME_EXTRA, startTime);

    return tryLaunchingInstantApp(context, intent, isCustomTabsIntent, callbackIntent);
}
 
Example #12
Source File: InstantAppsHandler.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private boolean handleIncomingIntentInternal(
        Context context, Intent intent, boolean isCustomTabsIntent, long startTime,
        boolean isRedirect) {
    if (!isRedirect && !isCustomTabsIntent && BuildInfo.isAtLeastO()) {
        Log.i(TAG, "Disabled for Android O+");
        return false;
    }

    if (isCustomTabsIntent && !IntentUtils.safeGetBooleanExtra(
            intent, CUSTOM_APPS_INSTANT_APP_EXTRA, false)) {
        Log.i(TAG, "Not handling with Instant Apps (missing CUSTOM_APPS_INSTANT_APP_EXTRA)");
        return false;
    }

    if (IntentUtils.safeGetBooleanExtra(intent, DO_NOT_LAUNCH_EXTRA, false)
            || (BuildInfo.isAtLeastO() && (intent.getFlags() & FLAG_DO_NOT_LAUNCH) != 0)) {
        maybeRecordFallbackStats(intent);
        Log.i(TAG, "Not handling with Instant Apps (DO_NOT_LAUNCH_EXTRA)");
        return false;
    }

    if (IntentUtils.safeGetBooleanExtra(
            intent, IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, false)
            || IntentUtils.safeHasExtra(intent, ShortcutHelper.EXTRA_SOURCE)
            || isIntentFromChrome(context, intent)
            || (IntentHandler.getUrlFromIntent(intent) == null)) {
        Log.i(TAG, "Not handling with Instant Apps (other)");
        return false;
    }

    // Used to search for the intent handlers. Needs null component to return correct results.
    Intent intentCopy = new Intent(intent);
    intentCopy.setComponent(null);
    Intent selector = intentCopy.getSelector();
    if (selector != null) selector.setComponent(null);

    if (!(isCustomTabsIntent || isChromeDefaultHandler(context))
            || ExternalNavigationDelegateImpl.isPackageSpecializedHandler(null, intentCopy)) {
        // Chrome is not the default browser or a specialized handler exists.
        Log.i(TAG, "Not handling with Instant Apps because Chrome is not default or "
                + "there's a specialized handler");
        return false;
    }

    Intent callbackIntent = new Intent(intent);
    callbackIntent.putExtra(DO_NOT_LAUNCH_EXTRA, true);
    callbackIntent.putExtra(INSTANT_APP_START_TIME_EXTRA, startTime);

    return tryLaunchingInstantApp(context, intent, isCustomTabsIntent, callbackIntent);
}
 
Example #13
Source File: CustomTabActivity.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the URL currently being displayed in the Custom Tab in the regular browser.
 * @param forceReparenting Whether tab reparenting should be forced for testing.
 *
 * @return Whether or not the tab was sent over successfully.
 */
boolean openCurrentUrlInBrowser(boolean forceReparenting) {
    Tab tab = getActivityTab();
    if (tab == null) return false;

    String url = tab.getUrl();
    if (DomDistillerUrlUtils.isDistilledPage(url)) {
        url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
    }
    if (TextUtils.isEmpty(url)) url = getUrlToLoad();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, false);

    boolean willChromeHandleIntent = getIntentDataProvider().isOpenedByChrome();
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
    try {
        willChromeHandleIntent |= ExternalNavigationDelegateImpl
                .willChromeHandleIntent(intent, true);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    Bundle startActivityOptions = ActivityOptionsCompat.makeCustomAnimation(
            this, R.anim.abc_fade_in, R.anim.abc_fade_out).toBundle();
    if (willChromeHandleIntent || forceReparenting) {
        Runnable finalizeCallback = new Runnable() {
            @Override
            public void run() {
                finishAndClose(true);
            }
        };

        mMainTab = null;
        // mHasCreatedTabEarly == true => mMainTab != null in the rest of the code.
        mHasCreatedTabEarly = false;
        CustomTabsConnection.getInstance(getApplication()).resetPostMessageHandlerForSession(
                mSession, null);
        tab.detachAndStartReparenting(intent, startActivityOptions, finalizeCallback);
    } else {
        // Temporarily allowing disk access while fixing. TODO: http://crbug.com/581860
        StrictMode.allowThreadDiskWrites();
        try {
            if (mIntentDataProvider.isInfoPage()) {
                IntentHandler.startChromeLauncherActivityForTrustedIntent(intent);
            } else {
                startActivity(intent, startActivityOptions);
            }
        } finally {
            StrictMode.setThreadPolicy(oldPolicy);
        }
    }
    return true;
}
 
Example #14
Source File: CustomTabDelegateFactory.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * @return The {@link CustomTabNavigationDelegate} in this tab. For test purpose only.
 */
@VisibleForTesting
ExternalNavigationDelegateImpl getExternalNavigationDelegate() {
    return mNavigationDelegate;
}
 
Example #15
Source File: CustomTabActivity.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the URL currently being displayed in the Custom Tab in the regular browser.
 * @param forceReparenting Whether tab reparenting should be forced for testing.
 *
 * @return Whether or not the tab was sent over successfully.
 */
boolean openCurrentUrlInBrowser(boolean forceReparenting) {
    Tab tab = getActivityTab();
    if (tab == null) return false;

    String url = tab.getUrl();
    if (DomDistillerUrlUtils.isDistilledPage(url)) {
        url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
    }
    if (TextUtils.isEmpty(url)) url = getUrlToLoad();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, false);

    boolean willChromeHandleIntent = getIntentDataProvider().isOpenedByChrome();
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    StrictMode.allowThreadDiskWrites();
    try {
        willChromeHandleIntent |= ExternalNavigationDelegateImpl
                .willChromeHandleIntent(this, intent, true);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    Bundle startActivityOptions = ActivityOptionsCompat.makeCustomAnimation(
            this, R.anim.abc_fade_in, R.anim.abc_fade_out).toBundle();
    if (willChromeHandleIntent || forceReparenting) {
        Runnable finalizeCallback = new Runnable() {
            @Override
            public void run() {
                finishAndClose(true);
            }
        };

        mMainTab = null;
        tab.detachAndStartReparenting(intent, startActivityOptions, finalizeCallback);
    } else {
        // Temporarily allowing disk access while fixing. TODO: http://crbug.com/581860
        StrictMode.allowThreadDiskReads();
        StrictMode.allowThreadDiskWrites();
        try {
            startActivity(intent, startActivityOptions);
        } finally {
            StrictMode.setThreadPolicy(oldPolicy);
        }
    }
    return true;
}
 
Example #16
Source File: CustomTabDelegateFactory.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
/**
 * @return The {@link CustomTabNavigationDelegate} in this tab. For test purpose only.
 */
@VisibleForTesting
ExternalNavigationDelegateImpl getExternalNavigationDelegate() {
    return mNavigationDelegate;
}
 
Example #17
Source File: CustomTabActivity.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * Opens the URL currently being displayed in the Custom Tab in the regular browser.
 * @param forceReparenting Whether tab reparenting should be forced for testing.
 * @param stayInChrome     Whether the user stays in Chrome after the tab is reparented.
 * @return Whether or not the tab was sent over successfully.
 */
boolean openCurrentUrlInBrowser(boolean forceReparenting, boolean stayInChrome) {
    Tab tab = getActivityTab();
    if (tab == null) return false;

    String url = tab.getUrl();
    if (DomDistillerUrlUtils.isDistilledPage(url)) {
        url = DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url);
    }
    if (TextUtils.isEmpty(url)) url = getUrlToLoad();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(ChromeLauncherActivity.EXTRA_IS_ALLOWED_TO_RETURN_TO_PARENT, false);
    if (ChromeFeatureList.isEnabled("ReadItLaterInMenu")) {
        // In this trial both "open in chrome" and "read it later" should target Chrome.
        intent.setPackage(getPackageName());
    }

    boolean willChromeHandleIntent = getIntentDataProvider().isOpenedByChrome();
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    StrictMode.allowThreadDiskWrites();
    try {
        willChromeHandleIntent |= ExternalNavigationDelegateImpl
                .willChromeHandleIntent(this, intent, true);
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }

    Bundle startActivityOptions = ActivityOptionsCompat.makeCustomAnimation(
            this, R.anim.abc_fade_in, R.anim.abc_fade_out).toBundle();
    if (willChromeHandleIntent || forceReparenting) {
        Runnable finalizeCallback = new Runnable() {
            @Override
            public void run() {
                finishAndClose();
            }
        };

        mMainTab = null;
        tab.detachAndStartReparenting(intent, startActivityOptions, finalizeCallback,
                stayInChrome);
    } else {
        // Temporarily allowing disk access while fixing. TODO: http://crbug.com/581860
        StrictMode.allowThreadDiskReads();
        StrictMode.allowThreadDiskWrites();
        try {
            startActivity(intent, startActivityOptions);
        } finally {
            StrictMode.setThreadPolicy(oldPolicy);
        }
    }
    return true;
}