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