Commit 70f315a4 authored by Tiger Oakes's avatar Tiger Oakes Committed by Commit Bot

Remove getApplicationContext calls from browserservices

Continuation of work in bug 646094.

Preparing to activate an errorprone check to flag
context#getApplicationContext calls. The goal is to prevent storing the
resulting context as a class property, and instead use ContextUtils.

This CL replaces two others:
https://chromium-review.googlesource.com/c/chromium/src/+/1095986
https://chromium-review.googlesource.com/c/chromium/src/+/1095999

Bug: 846456
Change-Id: Id7ed38c318d97b7370938d32677bcf73b85c3314
Reviewed-on: https://chromium-review.googlesource.com/1141933Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Tiger Oakes <tigero@google.com>
Cr-Commit-Position: refs/heads/master@{#576242}
parent 8fe77902
......@@ -185,7 +185,7 @@ public class LaunchIntentDispatcher implements IntentHandler.IntentHandlerDelega
// The notification settings cog on the flipped side of Notifications and in the Android
// Settings "App Notifications" view will open us with a specific category.
if (mIntent.hasCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)) {
NotificationPlatformBridge.launchNotificationPreferences(mActivity, mIntent);
NotificationPlatformBridge.launchNotificationPreferences(mIntent);
return Action.FINISH_ACTIVITY;
}
......
......@@ -9,6 +9,7 @@ import android.content.res.Resources;
import android.net.Uri;
import android.support.customtabs.trusted.TrustedWebActivityServiceConnectionManager;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.notifications.NotificationBuilderBase;
import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
......@@ -18,14 +19,13 @@ import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
*/
public class TrustedWebActivityClient {
private final TrustedWebActivityServiceConnectionManager mConnection;
private final Context mContext;
/**
* Creates a TrustedWebActivityService.
*/
public TrustedWebActivityClient(Context context) {
mConnection = new TrustedWebActivityServiceConnectionManager(context);
mContext = context.getApplicationContext();
public TrustedWebActivityClient() {
mConnection = new TrustedWebActivityServiceConnectionManager(
ContextUtils.getApplicationContext());
}
/**
......@@ -48,7 +48,7 @@ public class TrustedWebActivityClient {
*/
public void notifyNotification(Uri scope, String platformTag, int platformId,
NotificationBuilderBase builder) {
Resources res = mContext.getResources();
Resources res = ContextUtils.getApplicationContext().getResources();
String channelDisplayName = res.getString(R.string.notification_category_group_general);
mConnection.execute(scope, new Origin(scope).toString(), service -> {
......
......@@ -86,18 +86,27 @@ public class ChromeBrowserInitializer {
/**
* This class is an application specific object that orchestrates the app initialization.
* @param context The context to get the application context from.
* @return The singleton instance of {@link ChromeBrowserInitializer}.
*/
public static ChromeBrowserInitializer getInstance(Context context) {
public static ChromeBrowserInitializer getInstance() {
if (sChromeBrowserInitializer == null) {
sChromeBrowserInitializer = new ChromeBrowserInitializer(context);
sChromeBrowserInitializer = new ChromeBrowserInitializer();
}
return sChromeBrowserInitializer;
}
private ChromeBrowserInitializer(Context context) {
mApplication = (ChromeApplication) context.getApplicationContext();
/**
* This class is an application specific object that orchestrates the app initialization.
* @deprecated Use getInstance with no arguments instead.
* @param context The context to get the application context from.
* @return The singleton instance of {@link ChromeBrowserInitializer}.
*/
public static ChromeBrowserInitializer getInstance(Context context) {
return getInstance();
}
private ChromeBrowserInitializer() {
mApplication = (ChromeApplication) ContextUtils.getApplicationContext();
mHandler = new Handler(Looper.getMainLooper());
initLeakCanary();
}
......
......@@ -132,7 +132,7 @@ public class NotificationPlatformBridge {
mNotificationManager = new NotificationManagerProxyImpl(
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
}
mTwaClient = new TrustedWebActivityClient(context);
mTwaClient = new TrustedWebActivityClient();
}
/**
......@@ -237,17 +237,16 @@ public class NotificationPlatformBridge {
* from the gear button on a flipped notification, this launches the site specific preferences
* screen.
*
* @param context The context that received the intent.
* @param incomingIntent The received intent.
*/
public static void launchNotificationPreferences(Context context, Intent incomingIntent) {
public static void launchNotificationPreferences(Intent incomingIntent) {
// This method handles an intent fired by the Android system. There is no guarantee that the
// native library is loaded at this point. The native library is needed for the preferences
// activity, and it loads the library, but there are some native calls even before that
// activity is started: from RecordUserAction.record and (indirectly) from
// UrlFormatter.formatUrlForSecurityDisplay.
try {
ChromeBrowserInitializer.getInstance(context).handleSynchronousStartup();
ChromeBrowserInitializer.getInstance().handleSynchronousStartup();
} catch (ProcessInitException e) {
Log.e(TAG, "Failed to start browser process.", e);
// The library failed to initialize and nothing in the application can work, so kill
......@@ -258,7 +257,7 @@ public class NotificationPlatformBridge {
// Use the application context because it lives longer. When using the given context, it
// may be stopped before the preferences intent is handled.
Context applicationContext = context.getApplicationContext();
Context applicationContext = ContextUtils.getApplicationContext();
// If we can read an origin from the intent, use it to open the settings screen for that
// origin.
......
......@@ -116,7 +116,7 @@ public class TrustedWebActivityClientTest {
@Before
public void setUp() throws TimeoutException, RemoteException, InterruptedException {
mTargetContext = InstrumentationRegistry.getTargetContext();
mClient = new TrustedWebActivityClient(mTargetContext);
mClient = new TrustedWebActivityClient();
// TestTrustedWebActivityService is in the test support apk.
TrustedWebActivityClient.registerClient(mTargetContext, ORIGIN, TEST_SUPPORT_PACKAGE);
......
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