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