Commit 855330b0 authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

Correctly apply limit of one custom button for non-trusted CCT intents

Bug: 810854
Change-Id: I90e5eb0c82e4d5495e760b631bb61e69eb6aaa43
Reviewed-on: https://chromium-review.googlesource.com/973521Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545033}
parent 406254d7
...@@ -154,11 +154,9 @@ public class CustomButtonParams { ...@@ -154,11 +154,9 @@ public class CustomButtonParams {
/** /**
* Parses a list of {@link CustomButtonParams} from the intent sent by clients. * Parses a list of {@link CustomButtonParams} from the intent sent by clients.
* @param intent The intent sent by the client. * @param intent The intent sent by the client.
* @param isTrustedIntent whether the intent is trusted.
* @return A list of parsed {@link CustomButtonParams}. Return an empty list if input is invalid * @return A list of parsed {@link CustomButtonParams}. Return an empty list if input is invalid
*/ */
public static List<CustomButtonParams> fromIntent( public static List<CustomButtonParams> fromIntent(Context context, Intent intent) {
Context context, Intent intent, boolean isTrustedIntent) {
List<CustomButtonParams> paramsList = new ArrayList<>(1); List<CustomButtonParams> paramsList = new ArrayList<>(1);
if (intent == null) return paramsList; if (intent == null) return paramsList;
......
...@@ -251,21 +251,25 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider { ...@@ -251,21 +251,25 @@ public class CustomTabIntentDataProvider extends BrowserSessionDataProvider {
* {@link #mBottombarButtons} and {@link #mToolbarButtons}. * {@link #mBottombarButtons} and {@link #mToolbarButtons}.
*/ */
private void retrieveCustomButtons(Intent intent, Context context) { private void retrieveCustomButtons(Intent intent, Context context) {
mCustomButtonParams = CustomButtonParams.fromIntent(context, intent, isTrustedIntent()); assert mCustomButtonParams == null;
if (mCustomButtonParams != null) { mCustomButtonParams = CustomButtonParams.fromIntent(context, intent);
for (CustomButtonParams params : mCustomButtonParams) { for (CustomButtonParams params : mCustomButtonParams) {
if (!params.showOnToolbar()) { if (!params.showOnToolbar()) {
mBottombarButtons.add(params); mBottombarButtons.add(params);
} else if (mToolbarButtons.size() < MAX_CUSTOM_TOOLBAR_ITEMS) { } else if (mToolbarButtons.size() < getMaxCustomToolbarItems()) {
mToolbarButtons.add(params); mToolbarButtons.add(params);
} else { } else {
Log.w(TAG, "Only %d items are allowed in the toolbar", Log.w(TAG, "Only %d items are allowed in the toolbar", getMaxCustomToolbarItems());
MAX_CUSTOM_TOOLBAR_ITEMS);
}
} }
} }
} }
private int getMaxCustomToolbarItems() {
if (!isTrustedIntent()) return 1;
return MAX_CUSTOM_TOOLBAR_ITEMS;
}
/** /**
* Processes the color passed from the client app and updates {@link #mToolbarColor}. * Processes the color passed from the client app and updates {@link #mToolbarColor}.
*/ */
......
...@@ -930,6 +930,11 @@ public class CustomTabActivityTest { ...@@ -930,6 +930,11 @@ public class CustomTabActivityTest {
Bitmap expectedIcon1 = createVectorDrawableBitmap(R.drawable.ic_content_copy_black, 48, 48); Bitmap expectedIcon1 = createVectorDrawableBitmap(R.drawable.ic_content_copy_black, 48, 48);
Bitmap expectedIcon2 = createVectorDrawableBitmap(R.drawable.ic_music_note_36dp, 48, 48); Bitmap expectedIcon2 = createVectorDrawableBitmap(R.drawable.ic_music_note_36dp, 48, 48);
Intent intent = createMinimalCustomTabIntent(); Intent intent = createMinimalCustomTabIntent();
// Mark the intent as trusted so it can show more than one action button.
IntentHandler.addTrustedIntentExtras(intent);
Assert.assertTrue(IntentHandler.isIntentChromeOrFirstParty(intent));
ArrayList<Bundle> toolbarItems = new ArrayList<>(2); ArrayList<Bundle> toolbarItems = new ArrayList<>(2);
final PendingIntent pi1 = PendingIntent.getBroadcast( final PendingIntent pi1 = PendingIntent.getBroadcast(
InstrumentationRegistry.getTargetContext(), 0, new Intent(), 0); InstrumentationRegistry.getTargetContext(), 0, new Intent(), 0);
...@@ -987,6 +992,41 @@ public class CustomTabActivityTest { ...@@ -987,6 +992,41 @@ public class CustomTabActivityTest {
Assert.assertEquals("Bestest testest", actionButton.getContentDescription()); Assert.assertEquals("Bestest testest", actionButton.getContentDescription());
} }
/**
* Test that additional action buttons are ignored for untrusted intents.
*/
@Test
@SmallTest
@Feature({"UiCatalogue"})
@RetryOnFailure
public void testMultipleActionButtons_untrusted()
throws InterruptedException, TimeoutException {
Bitmap expectedIcon1 = createVectorDrawableBitmap(R.drawable.ic_content_copy_black, 48, 48);
Bitmap expectedIcon2 = createVectorDrawableBitmap(R.drawable.ic_music_note_36dp, 48, 48);
Intent intent = createMinimalCustomTabIntent();
// By default, the intent should not be trusted.
Assert.assertFalse(IntentHandler.isIntentChromeOrFirstParty(intent));
ArrayList<Bundle> toolbarItems = new ArrayList<>(2);
final PendingIntent pi = PendingIntent.getBroadcast(
InstrumentationRegistry.getTargetContext(), 0, new Intent(), 0);
toolbarItems.add(makeToolbarItemBundle(expectedIcon1, "Shown", pi));
toolbarItems.add(makeToolbarItemBundle(expectedIcon2, "Not shown", pi));
intent.putParcelableArrayListExtra(CustomTabsIntent.EXTRA_TOOLBAR_ITEMS, toolbarItems);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
View toolbarView = mCustomTabActivityTestRule.getActivity().findViewById(R.id.toolbar);
Assert.assertTrue(
"A custom tab toolbar is never shown", toolbarView instanceof CustomTabToolbar);
CustomTabToolbar toolbar = (CustomTabToolbar) toolbarView;
final ImageButton actionButton = toolbar.getCustomActionButtonForTest(0);
Assert.assertNotNull("Action button not found", actionButton);
Assert.assertEquals("Shown", actionButton.getContentDescription());
Assert.assertNull(toolbar.getCustomActionButtonForTest(1));
}
/** /**
* Test the case that the action button should not be shown, given a bitmap with unacceptable * Test the case that the action button should not be shown, given a bitmap with unacceptable
* height/width ratio. * height/width ratio.
......
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