Commit 3fce642e authored by Aiden Benner's avatar Aiden Benner Committed by Commit Bot

Refactor context#getApplicationContext calls from remoting

In preparation for enabling an errorprone check that would prohibit
context#getApplicationContext in favour of ContextUtils#getApplicationContext
see https://bugs.chromium.org/p/chromium/issues/detail?id=560466
for context.

Bug: 846456
Change-Id: I13c432d238876cdf8c075e28f74d49e86f8f00a8
Reviewed-on: https://chromium-review.googlesource.com/c/1340376Reviewed-by: default avatarLambros Lambrou <lambroslambrou@chromium.org>
Commit-Queue: Aiden Benner <abenner@google.com>
Cr-Commit-Position: refs/heads/master@{#608904}
parent 805a7dfe
...@@ -15,6 +15,8 @@ import android.database.Cursor; ...@@ -15,6 +15,8 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import org.chromium.base.ContextUtils;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -67,7 +69,7 @@ public class ChromotingDownloadManager { ...@@ -67,7 +69,7 @@ public class ChromotingDownloadManager {
if (needToBeDownloaded(i)) { if (needToBeDownloaded(i)) {
DownloadManager.Request request = DownloadManager.Request request =
new DownloadManager.Request(Uri.parse(mUris[i])); new DownloadManager.Request(Uri.parse(mUris[i]));
request.setDestinationInExternalFilesDir(mActivity.getApplicationContext(), request.setDestinationInExternalFilesDir(ContextUtils.getApplicationContext(),
Environment.DIRECTORY_DOWNLOADS, mNames[i]); Environment.DIRECTORY_DOWNLOADS, mNames[i]);
mUnfinishedDownloadIds.add(mDownloadManager.enqueue(request)); mUnfinishedDownloadIds.add(mDownloadManager.enqueue(request));
} }
......
...@@ -14,6 +14,7 @@ import android.net.Uri; ...@@ -14,6 +14,7 @@ import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.SecureRandomInitializer; import org.chromium.base.SecureRandomInitializer;
...@@ -87,7 +88,7 @@ public class ThirdPartyTokenFetcher { ...@@ -87,7 +88,7 @@ public class ThirdPartyTokenFetcher {
this.mCallback = callback; this.mCallback = callback;
this.mTokenUrlPatterns = tokenUrlPatterns; this.mTokenUrlPatterns = tokenUrlPatterns;
this.mRedirectUriScheme = context.getApplicationContext().getPackageName(); this.mRedirectUriScheme = ContextUtils.getApplicationContext().getPackageName();
// We don't follow the OAuth spec (http://tools.ietf.org/html/rfc6749#section-3.1.2) of the // We don't follow the OAuth spec (http://tools.ietf.org/html/rfc6749#section-3.1.2) of the
// redirect URI as it is possible for the other applications to intercept the redirect URI. // redirect URI as it is possible for the other applications to intercept the redirect URI.
...@@ -230,8 +231,7 @@ public class ThirdPartyTokenFetcher { ...@@ -230,8 +231,7 @@ public class ThirdPartyTokenFetcher {
public static void setEnabled(Activity context, boolean enabled) { public static void setEnabled(Activity context, boolean enabled) {
int enabledState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED int enabledState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
ComponentName component = new ComponentName( ComponentName component = new ComponentName(ContextUtils.getApplicationContext(),
context.getApplicationContext(),
ThirdPartyTokenFetcher.OAuthRedirectActivity.class); ThirdPartyTokenFetcher.OAuthRedirectActivity.class);
context.getPackageManager().setComponentEnabledSetting( context.getPackageManager().setComponentEnabledSetting(
component, component,
......
...@@ -24,9 +24,9 @@ public class JniInterface { ...@@ -24,9 +24,9 @@ public class JniInterface {
* code. Called on the UI thread. * code. Called on the UI thread.
* @param context The Application context. * @param context The Application context.
*/ */
@SuppressWarnings("NoContextGetApplicationContext")
public static void loadLibrary(Context context) { public static void loadLibrary(Context context) {
ContextUtils.initApplicationContext(context.getApplicationContext()); ContextUtils.initApplicationContext(context.getApplicationContext());
JniOAuthTokenGetter.setContext(context);
try { try {
System.loadLibrary(LIBRARY_NAME); System.loadLibrary(LIBRARY_NAME);
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
......
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
package org.chromium.chromoting.jni; package org.chromium.chromoting.jni;
import android.annotation.SuppressLint; import org.chromium.base.ContextUtils;
import android.content.Context;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
...@@ -25,18 +23,9 @@ public class JniOAuthTokenGetter { ...@@ -25,18 +23,9 @@ public class JniOAuthTokenGetter {
private static final String TAG = "Chromoting"; private static final String TAG = "Chromoting";
private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com/auth/chromoting"; private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com/auth/chromoting";
@SuppressLint("StaticFieldLeak")
private static Context sContext;
private static String sAccount; private static String sAccount;
private static String sLatestToken; private static String sLatestToken;
public static void setContext(Context context) {
Preconditions.notNull(context);
// Always store the application context so that we don't leak the activity context by
// accident.
sContext = context.getApplicationContext();
}
public static void setAccount(String account) { public static void setAccount(String account) {
Preconditions.notNull(account); Preconditions.notNull(account);
sAccount = account; sAccount = account;
...@@ -44,9 +33,10 @@ public class JniOAuthTokenGetter { ...@@ -44,9 +33,10 @@ public class JniOAuthTokenGetter {
@CalledByNative @CalledByNative
private static void fetchAuthToken(long callbackPtr) { private static void fetchAuthToken(long callbackPtr) {
Preconditions.notNull(sContext); Preconditions.notNull(ContextUtils.getApplicationContext());
Preconditions.notNull(sAccount); Preconditions.notNull(sAccount);
new OAuthTokenFetcher(sContext, sAccount, TOKEN_SCOPE, new Callback() { new OAuthTokenFetcher(ContextUtils.getApplicationContext(), sAccount, TOKEN_SCOPE,
new Callback() {
@Override @Override
public void onTokenFetched(String token) { public void onTokenFetched(String token) {
sLatestToken = token; sLatestToken = token;
...@@ -73,7 +63,8 @@ public class JniOAuthTokenGetter { ...@@ -73,7 +63,8 @@ public class JniOAuthTokenGetter {
} }
nativeResolveOAuthTokenCallback(callbackPtr, status, null, null); nativeResolveOAuthTokenCallback(callbackPtr, status, null, null);
} }
}).fetch(); })
.fetch();
} }
@CalledByNative @CalledByNative
...@@ -81,9 +72,10 @@ public class JniOAuthTokenGetter { ...@@ -81,9 +72,10 @@ public class JniOAuthTokenGetter {
if (sLatestToken == null || sLatestToken.isEmpty()) { if (sLatestToken == null || sLatestToken.isEmpty()) {
return; return;
} }
Preconditions.notNull(sContext); Preconditions.notNull(ContextUtils.getApplicationContext());
Preconditions.notNull(sAccount); Preconditions.notNull(sAccount);
new OAuthTokenFetcher(sContext, sAccount, TOKEN_SCOPE, new Callback() { new OAuthTokenFetcher(ContextUtils.getApplicationContext(), sAccount, TOKEN_SCOPE,
new Callback() {
@Override @Override
public void onTokenFetched(String token) { public void onTokenFetched(String token) {
sLatestToken = token; sLatestToken = token;
...@@ -93,7 +85,8 @@ public class JniOAuthTokenGetter { ...@@ -93,7 +85,8 @@ public class JniOAuthTokenGetter {
public void onError(Error error) { public void onError(Error error) {
Log.e(TAG, "Failed to clear token. Error: ", error); Log.e(TAG, "Failed to clear token. Error: ", error);
} }
}).clearAndFetch(sLatestToken); })
.clearAndFetch(sLatestToken);
} }
private static native void nativeResolveOAuthTokenCallback( private static native void nativeResolveOAuthTokenCallback(
......
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