Commit 38d5cb21 authored by cco3's avatar cco3 Committed by Commit bot

Remove Context params from GeolocationHeader

Methods of GeolocationHeader typically take a context, but the
context is merely used to get the package name and start an Activity.
We can instead use ContextUtils.getApplicationContext().

Review-Url: https://codereview.chromium.org/2619093002
Cr-Commit-Position: refs/heads/master@{#443019}
parent 3208d218
......@@ -92,8 +92,7 @@ public class ChromeActionModeCallback implements ActionMode.Callback {
if (TextUtils.isEmpty(query)) return;
String url = TemplateUrlService.getInstance().getUrlForSearchQuery(query);
String headers =
GeolocationHeader.getGeoHeader(mContext.getApplicationContext(), url, mTab);
String headers = GeolocationHeader.getGeoHeader(url, mTab);
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setVerbatimHeaders(headers);
......
......@@ -1030,13 +1030,13 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener,
if (hasFocus && currentTab != null && !currentTab.isIncognito()) {
if (mNativeInitialized
&& TemplateUrlService.getInstance().isDefaultSearchEngineGoogle()) {
GeolocationHeader.primeLocationForGeoHeader(getContext());
GeolocationHeader.primeLocationForGeoHeader();
} else {
mDeferredNativeRunnables.add(new Runnable() {
@Override
public void run() {
if (TemplateUrlService.getInstance().isDefaultSearchEngineGoogle()) {
GeolocationHeader.primeLocationForGeoHeader(getContext());
GeolocationHeader.primeLocationForGeoHeader();
}
}
});
......@@ -2192,8 +2192,7 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener,
// Loads the |url| in the current ContentView and gives focus to the ContentView.
if (currentTab != null && !url.isEmpty()) {
LoadUrlParams loadUrlParams = new LoadUrlParams(url);
loadUrlParams.setVerbatimHeaders(
GeolocationHeader.getGeoHeader(getContext(), url, currentTab));
loadUrlParams.setVerbatimHeaders(GeolocationHeader.getGeoHeader(url, currentTab));
loadUrlParams.setTransitionType(transition | PageTransition.FROM_ADDRESS_BAR);
currentTab.loadUrl(loadUrlParams);
......
......@@ -189,33 +189,29 @@ public class GeolocationHeader {
/**
* Requests a location refresh so that a valid location will be available for constructing
* an X-Geo header in the near future (i.e. within 5 minutes).
*
* @param context The Context used to get the device location.
*/
public static void primeLocationForGeoHeader(Context context) {
if (!hasGeolocationPermission(context)) return;
public static void primeLocationForGeoHeader() {
if (!hasGeolocationPermission()) return;
if (sFirstLocationTime == Long.MAX_VALUE) {
sFirstLocationTime = SystemClock.elapsedRealtime();
}
GeolocationTracker.refreshLastKnownLocation(context, REFRESH_LOCATION_AGE);
GeolocationTracker.refreshLastKnownLocation(
ContextUtils.getApplicationContext(), REFRESH_LOCATION_AGE);
}
/**
* Returns whether the X-Geo header is allowed to be sent for the current URL.
*
* @param context The Context used to get the device location.
* @param url The URL of the request with which this header will be sent.
* @param isIncognito Whether the request will happen in an incognito tab.
*/
public static boolean isGeoHeaderEnabledForUrl(Context context, String url,
boolean isIncognito) {
return geoHeaderStateForUrl(context, url, isIncognito, false) == HEADER_ENABLED;
public static boolean isGeoHeaderEnabledForUrl(String url, boolean isIncognito) {
return geoHeaderStateForUrl(url, isIncognito, false) == HEADER_ENABLED;
}
@HeaderState
private static int geoHeaderStateForUrl(
Context context, String url, boolean isIncognito, boolean recordUma) {
private static int geoHeaderStateForUrl(String url, boolean isIncognito, boolean recordUma) {
// Only send X-Geo in normal mode.
if (isIncognito) return INCOGNITO;
......@@ -225,7 +221,7 @@ public class GeolocationHeader {
Uri uri = Uri.parse(url);
if (!HTTPS_SCHEME.equals(uri.getScheme())) return NOT_HTTPS;
if (!hasGeolocationPermission(context)) {
if (!hasGeolocationPermission()) {
if (recordUma) recordHistogram(UMA_LOCATION_DISABLED_FOR_CHROME_APP);
return LOCATION_PERMISSION_BLOCKED;
}
......@@ -248,20 +244,20 @@ public class GeolocationHeader {
*
* Returns null otherwise.
*
* @param context The Context used to get the device location.
* @param url The URL of the request with which this header will be sent.
* @param tab The Tab currently being accessed.
* @return The X-Geo header string or null.
*/
public static String getGeoHeader(Context context, String url, Tab tab) {
public static String getGeoHeader(String url, Tab tab) {
boolean isIncognito = tab.isIncognito();
boolean locationAttached = true;
Location location = null;
long locationAge = Long.MAX_VALUE;
@HeaderState int headerState = geoHeaderStateForUrl(context, url, isIncognito, true);
@HeaderState int headerState = geoHeaderStateForUrl(url, isIncognito, true);
if (headerState == HEADER_ENABLED) {
// Only send X-Geo header if there's a fresh location available.
location = GeolocationTracker.getLastKnownLocation(context);
location =
GeolocationTracker.getLastKnownLocation(ContextUtils.getApplicationContext());
if (location == null) {
recordHistogram(UMA_LOCATION_NOT_AVAILABLE);
locationAttached = false;
......@@ -276,8 +272,8 @@ public class GeolocationHeader {
locationAttached = false;
}
@LocationSource int locationSource = getLocationSource(context);
@Permission int appPermission = getGeolocationPermission(context, tab);
@LocationSource int locationSource = getLocationSource();
@Permission int appPermission = getGeolocationPermission(tab);
@Permission int domainPermission = getDomainPermission(url, isIncognito);
// Record the permission state with a histogram.
......@@ -322,16 +318,11 @@ public class GeolocationHeader {
}
@CalledByNative
public static boolean hasGeolocationPermission() {
Context context = ContextUtils.getApplicationContext();
return hasGeolocationPermission(context);
}
static boolean hasGeolocationPermission(Context context) {
static boolean hasGeolocationPermission() {
int pid = Process.myPid();
int uid = Process.myUid();
if (ApiCompatibilityUtils.checkPermission(
context, Manifest.permission.ACCESS_COARSE_LOCATION, pid, uid)
if (ApiCompatibilityUtils.checkPermission(ContextUtils.getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION, pid, uid)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
......@@ -340,8 +331,8 @@ public class GeolocationHeader {
// incorrectly requires FINE_LOCATION permission (it should only require COARSE_LOCATION
// permission). http://crbug.com/580733
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
&& ApiCompatibilityUtils.checkPermission(
context, Manifest.permission.ACCESS_FINE_LOCATION, pid, uid)
&& ApiCompatibilityUtils.checkPermission(ContextUtils.getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION, pid, uid)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
......@@ -354,8 +345,8 @@ public class GeolocationHeader {
* This permission can be either granted, blocked or prompt.
*/
@Permission
static int getGeolocationPermission(Context context, Tab tab) {
if (hasGeolocationPermission(context)) return PERMISSION_GRANTED;
static int getGeolocationPermission(Tab tab) {
if (hasGeolocationPermission()) return PERMISSION_GRANTED;
return tab.getWindowAndroid().canRequestPermission(
Manifest.permission.ACCESS_COARSE_LOCATION)
? PERMISSION_PROMPT
......@@ -402,9 +393,9 @@ public class GeolocationHeader {
/** Returns the location source. */
@LocationSource
private static int getLocationSource(Context context) {
LocationManager locationManager =
(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
private static int getLocationSource() {
LocationManager locationManager = (LocationManager) ContextUtils.getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getProviders(true);
boolean hasNetworkProvider = false;
boolean hasGpsProvider = false;
......
......@@ -106,7 +106,7 @@ public class GeolocationSnackbarController implements SnackbarController {
// Don't show the snackbar if Chrome doesn't have location permission since X-Geo won't be
// sent unless the user explicitly grants this permission.
if (!GeolocationHeader.hasGeolocationPermission(context)) return true;
if (!GeolocationHeader.hasGeolocationPermission()) return true;
// Don't show the snackbar if Google isn't the default search engine since X-Geo won't be
// sent unless the user explicitly sets Google as their default search engine and sees that
......
......@@ -360,8 +360,7 @@ public class SearchEngineAdapter extends BaseAdapter implements LoadListener, On
ContentSetting locationPermission = locationSettings.getContentSetting();
if (locationPermission == ContentSetting.ASK) {
// Handle the case where the geoHeader being sent when no permission has been specified.
if (checkGeoHeader && GeolocationHeader.isGeoHeaderEnabledForUrl(
mContext, url, false)) {
if (checkGeoHeader && GeolocationHeader.isGeoHeaderEnabledForUrl(url, false)) {
locationPermission = ContentSetting.ALLOW;
}
}
......
......@@ -488,7 +488,7 @@ public class SingleWebsitePreferences extends PreferenceFragment
private boolean hasXGeoLocationPermission(Context context) {
String searchUrl = TemplateUrlService.getInstance().getUrlForSearchQuery("foo");
return mSite.getAddress().matches(searchUrl)
&& GeolocationHeader.isGeoHeaderEnabledForUrl(context, searchUrl, false);
&& GeolocationHeader.isGeoHeaderEnabledForUrl(searchUrl, false);
}
/**
......
......@@ -10,7 +10,6 @@ import android.os.Build;
import android.os.SystemClock;
import android.support.test.filters.SmallTest;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.test.util.Feature;
......@@ -86,7 +85,7 @@ public class GeolocationHeaderTest extends ChromeActivityTestCaseBase<ChromeActi
GeolocationInfo infoHttp = new GeolocationInfo("http://www.google.de", null, false);
infoHttps.setContentSetting(httpsPermission);
infoHttp.setContentSetting(httpPermission);
String header = GeolocationHeader.getGeoHeader(ContextUtils.getApplicationContext(),
String header = GeolocationHeader.getGeoHeader(
"https://www.google.de/search?q=kartoffelsalat",
getActivity().getActivityTab());
assertHeaderState(header, shouldBeNull);
......@@ -100,8 +99,8 @@ public class GeolocationHeaderTest extends ChromeActivityTestCaseBase<ChromeActi
@Override
public void run() {
setMockLocation(latitute, longitude, time);
String header = GeolocationHeader.getGeoHeader(ContextUtils.getApplicationContext(),
SEARCH_URL_1, getActivity().getActivityTab());
String header = GeolocationHeader.getGeoHeader(SEARCH_URL_1,
getActivity().getActivityTab());
assertHeaderState(header, shouldBeNull);
}
});
......@@ -134,8 +133,7 @@ public class GeolocationHeaderTest extends ChromeActivityTestCaseBase<ChromeActi
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertNull(GeolocationHeader.getGeoHeader(
ContextUtils.getApplicationContext(), url, tab));
assertNull(GeolocationHeader.getGeoHeader(url, tab));
}
});
} catch (InterruptedException e) {
......@@ -149,8 +147,7 @@ public class GeolocationHeaderTest extends ChromeActivityTestCaseBase<ChromeActi
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertNotNull(GeolocationHeader.getGeoHeader(
ContextUtils.getApplicationContext(), url, tab));
assertNotNull(GeolocationHeader.getGeoHeader(url, tab));
}
});
} catch (InterruptedException e) {
......
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