Commit 3b51e82c authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android Webapps] Remove context function parameters from WebappLauncherActivity

This CL removes the application context parameter from several functions in
WebappLauncherActivity and WebappAuthenticator.

BUG=None

Change-Id: Ie4486cb4f9950a34e6b8349806cab35ebbb8f5a0
Reviewed-on: https://chromium-review.googlesource.com/c/1354128
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612742}
parent b54920b5
......@@ -168,8 +168,6 @@ public class ShortcutHelper {
protected Intent doInBackground() {
// Encoding {@link icon} as a string and computing the mac are expensive.
Context context = ContextUtils.getApplicationContext();
// Encode the icon as a base64 string (Launcher drops Bitmaps in the Intent).
String encodedIcon = encodeBitmapAsString(icon);
......@@ -179,7 +177,7 @@ public class ShortcutHelper {
sDelegate.getFullscreenAction(), url, nonEmptyScopeUrl, name, shortName,
encodedIcon, WEBAPP_SHORTCUT_VERSION, displayMode, orientation, themeColor,
backgroundColor, splashScreenUrl, iconUrl.isEmpty());
shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(context, url));
shortcutIntent.putExtra(EXTRA_MAC, getEncodedMac(url));
shortcutIntent.putExtra(EXTRA_SOURCE, source);
return shortcutIntent;
}
......@@ -590,11 +588,11 @@ public class ShortcutHelper {
/**
* @return String that can be used to verify that a WebappActivity is being started by Chrome.
*/
public static String getEncodedMac(Context context, String url) {
public static String getEncodedMac(String url) {
// The only reason we convert to a String here is because Android inexplicably eats a
// byte[] when adding the shortcut -- the Bundle received by the launched Activity even
// lacks the key for the extra.
byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
byte[] mac = WebappAuthenticator.getMacForUrl(url);
return Base64.encodeToString(mac, Base64.DEFAULT);
}
......
......@@ -11,6 +11,7 @@ import android.os.SystemClock;
import android.util.Log;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.SecureRandomInitializer;
import org.chromium.base.metrics.CachedMetrics.TimesHistogramSample;
import org.chromium.base.task.AsyncTask;
......@@ -61,13 +62,13 @@ public class WebappAuthenticator {
*
* @return true if the MAC is a valid MAC for the URL, false otherwise.
*/
public static boolean isUrlValid(Context context, String url, byte[] mac) {
public static boolean isUrlValid(String url, byte[] mac) {
byte[] goodMac = null;
// Temporarily allowing disk access while fixing. TODO: http://crbug.com/525785
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
long time = SystemClock.elapsedRealtime();
goodMac = getMacForUrl(context, url);
goodMac = getMacForUrl(url);
sWebappValidationTimes.record(SystemClock.elapsedRealtime() - time);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
......@@ -85,8 +86,8 @@ public class WebappAuthenticator {
*
* @return The bytes of a MAC for the URL, or null if a secure MAC was not available.
*/
public static byte[] getMacForUrl(Context context, String url) {
Mac mac = getMac(context);
public static byte[] getMacForUrl(String url) {
Mac mac = getMac();
if (mac == null) {
return null;
}
......@@ -169,9 +170,10 @@ public class WebappAuthenticator {
}
}
private static SecretKey getKey(Context context) {
private static SecretKey getKey() {
synchronized (sLock) {
if (sKey == null) {
Context context = ContextUtils.getApplicationContext();
SecretKey key = readKeyFromFile(context, MAC_KEY_BASENAME, MAC_ALGORITHM_NAME);
if (key != null) {
sKey = key;
......@@ -226,9 +228,9 @@ public class WebappAuthenticator {
/**
* @return A Mac, or null if it is not possible to instantiate one.
*/
private static Mac getMac(Context context) {
private static Mac getMac() {
try {
SecretKey key = getKey(context);
SecretKey key = getKey();
if (key == null) {
// getKey should have invoked triggerMacKeyGeneration, which should have set the
// random seed and generated a key from it. If not, there is a problem with the
......
......@@ -54,8 +54,7 @@ public class WebappDelegateFactory extends TabDelegateFactory {
intent.setPackage(mActivity.getPackageName());
mActivity.getWebappInfo().setWebappIntentExtras(intent);
intent.putExtra(
ShortcutHelper.EXTRA_MAC, ShortcutHelper.getEncodedMac(mActivity, startUrl));
intent.putExtra(ShortcutHelper.EXTRA_MAC, ShortcutHelper.getEncodedMac(startUrl));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
IntentUtils.safeStartActivity(ContextUtils.getApplicationContext(), intent);
}
......
......@@ -59,17 +59,16 @@ public class WebappLauncherActivity extends Activity {
super.onCreate(savedInstanceState);
long createTimestamp = SystemClock.elapsedRealtime();
Context appContext = getApplicationContext();
ChromeWebApkHost.init();
Intent intent = getIntent();
WebappInfo webappInfo = tryCreateWebappInfo(appContext, intent);
WebappInfo webappInfo = tryCreateWebappInfo(intent);
if (shouldRelaunchWebApk(intent, webappInfo)) {
relaunchWebApk(this, intent, webappInfo);
return;
}
if (shouldLaunchWebapp(appContext, intent, webappInfo)) {
if (shouldLaunchWebapp(intent, webappInfo)) {
launchWebapp(this, intent, webappInfo, createTimestamp);
return;
}
......@@ -77,8 +76,7 @@ public class WebappLauncherActivity extends Activity {
launchInTab(this, intent, webappInfo);
}
private static boolean shouldLaunchWebapp(
Context appContext, Intent intent, WebappInfo webappInfo) {
private static boolean shouldLaunchWebapp(Intent intent, WebappInfo webappInfo) {
// {@link WebApkInfo#create()} and {@link WebappInfo#create()} return null if the intent
// does not specify required values such as the uri.
if (webappInfo == null) return false;
......@@ -86,7 +84,7 @@ public class WebappLauncherActivity extends Activity {
String webappUrl = webappInfo.uri().toString();
String webappMac = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_MAC);
return (webappInfo.isForWebApk() || isValidMacForUrl(appContext, webappUrl, webappMac)
return (webappInfo.isForWebApk() || isValidMacForUrl(webappUrl, webappMac)
|| wasIntentFromChrome(intent));
}
......@@ -176,7 +174,7 @@ public class WebappLauncherActivity extends Activity {
/** Extracts start URL from source intent and launches URL in Chrome tab. */
private static void launchInTab(
Activity launchingActivity, Intent sourceIntent, WebappInfo webappInfo) {
Context appContext = launchingActivity.getApplicationContext();
Context appContext = ContextUtils.getApplicationContext();
String webappUrl = IntentUtils.safeGetStringExtra(sourceIntent, ShortcutHelper.EXTRA_URL);
int webappSource = (webappInfo == null) ? ShortcutSource.UNKNOWN : webappInfo.source();
......@@ -202,14 +200,13 @@ public class WebappLauncherActivity extends Activity {
* The MAC is used to prevent malicious apps from launching Chrome into a full screen
* Activity for phishing attacks (among other reasons).
*
* @param context
* @param url The URL for the web app.
* @param mac MAC to compare the URL against. See {@link WebappAuthenticator}.
* @return Whether the MAC is valid for the URL.
*/
private static boolean isValidMacForUrl(Context context, String url, String mac) {
private static boolean isValidMacForUrl(String url, String mac) {
return mac != null
&& WebappAuthenticator.isUrlValid(context, url, Base64.decode(mac, Base64.DEFAULT));
&& WebappAuthenticator.isUrlValid(url, Base64.decode(mac, Base64.DEFAULT));
}
private static boolean wasIntentFromChrome(Intent intent) {
......@@ -315,14 +312,15 @@ public class WebappLauncherActivity extends Activity {
}
/** Tries to create WebappInfo/WebApkInfo for the intent. */
private static WebappInfo tryCreateWebappInfo(Context appContext, Intent intent) {
private static WebappInfo tryCreateWebappInfo(Intent intent) {
// Builds WebApkInfo for the intent if the WebAPK package specified in the intent is a valid
// WebAPK and the URL specified in the intent can be fulfilled by the WebAPK.
String webApkPackage =
IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME);
String url = IntentUtils.safeGetStringExtra(intent, ShortcutHelper.EXTRA_URL);
if (!TextUtils.isEmpty(webApkPackage) && !TextUtils.isEmpty(url)
&& WebApkValidator.canWebApkHandleUrl(appContext, webApkPackage, url)) {
&& WebApkValidator.canWebApkHandleUrl(
ContextUtils.getApplicationContext(), webApkPackage, url)) {
return WebApkInfo.create(intent);
}
......
......@@ -4,7 +4,6 @@
package org.chromium.chrome.browser.webapps;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
......@@ -14,6 +13,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
......@@ -37,13 +37,13 @@ public class WebappAuthenticatorTest {
@SmallTest
@Feature({"Webapps"})
public void testAuthentication() {
Context context = InstrumentationRegistry.getTargetContext();
ContextUtils.initApplicationContextForTests(InstrumentationRegistry.getTargetContext());
String url = "http://www.example.org/hello.html";
byte[] mac = WebappAuthenticator.getMacForUrl(context, url);
byte[] mac = WebappAuthenticator.getMacForUrl(url);
Assert.assertNotNull(mac);
Assert.assertTrue(WebappAuthenticator.isUrlValid(context, url, mac));
Assert.assertFalse(WebappAuthenticator.isUrlValid(context, url + "?goats=true", mac));
Assert.assertTrue(WebappAuthenticator.isUrlValid(url, mac));
Assert.assertFalse(WebappAuthenticator.isUrlValid(url + "?goats=true", mac));
mac[4] += (byte) 1;
Assert.assertFalse(WebappAuthenticator.isUrlValid(context, url, mac));
Assert.assertFalse(WebappAuthenticator.isUrlValid(url, mac));
}
}
......@@ -83,8 +83,7 @@ public class WebappModeTest {
if (addMac) {
// Needed for security reasons. If the MAC is excluded, the URL of the webapp is opened
// in a browser window, instead.
String mac =
ShortcutHelper.getEncodedMac(InstrumentationRegistry.getTargetContext(), url);
String mac = ShortcutHelper.getEncodedMac(url);
intent.putExtra(ShortcutHelper.EXTRA_MAC, mac);
}
......
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